Skip to content

Commit

Permalink
Merge branch 'hotfix/0.7.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeboster committed Mar 28, 2021
2 parents 6f8dfa3 + b7e68b6 commit 3eff1f0
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 47 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.7.3
- Use layoutReady event to scan file on load
- Avoid showing redundant suggestions (input is equal)
- Fix duplicated suggestions
- Select automatically suggestion if there is only one
- Partially revert sort order to show shorter words first
- Improve settings tab (order and settings name)

# 0.7.2
- Fix JapaneseTokenizer

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-autocomplete-plugin",
"name": "Autocomplete",
"version": "0.7.2",
"version": "0.7.3",
"minAppVersion": "0.10.0",
"description": "This plugin provides a text autocomplete feature to enhance typing speed.",
"author": "Yeboster",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autocomplete-obsidian",
"version": "0.7.2",
"version": "0.7.3",
"description": "An Obsidian plugin to provide text autocomplete.",
"main": "index.js",
"author": "Yeboster <yeboster@gmail.com>",
Expand Down
17 changes: 11 additions & 6 deletions src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class Autocomplete {

public scanFile(file: TFile, strategy: TokenizeStrategy = 'default') {
const providers = this.providers
file?.vault?.read(file).then((content: string) => {
file.vault?.read(file).then((content: string) => {
// TODO: Make it async
providers.forEach((provider) => {
if (provider instanceof FlowProvider)
Expand All @@ -161,12 +161,17 @@ export class Autocomplete {
(acc, provider) => acc.concat(provider.matchWith(completionWord || '')),
[]
)
if (this.suggestions.length === 1) {
// Suggest automatically
this.selected.index = 0
this.selectSuggestion(editor)
} else {
editor.addKeyMap(this.keyMaps)

editor.addKeyMap(this.keyMaps)

this.view = generateView(this.suggestions, this.selected.index)
this.addClickListener(this.view, editor)
appendWidget(editor, this.view)
this.view = generateView(this.suggestions, this.selected.index)
this.addClickListener(this.view, editor)
appendWidget(editor, this.view)
}
}

private keyMaps = {
Expand Down
22 changes: 18 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export default class AutocompletePlugin extends Plugin {
const settings = this.settings
if (this.settings.flowProvider) this.statusBar.addStatusBar()
if (settings.flowProviderScanCurrent) {
// Passing autocomplete as context
this.app.workspace.on('file-open', this.onFileOpened, this)
const file = this.app.workspace.getActiveFile()
this.autocomplete.scanFile(file, settings.flowProviderTokenizeStrategy)

if (this.app.workspace.layoutReady) this.onLayoutReady()
this.app.workspace.on('layout-ready', this.onLayoutReady, this)
}

this.registerCodeMirror((editor) => {
Expand All @@ -85,6 +85,7 @@ export default class AutocompletePlugin extends Plugin {
const workspace = this.app.workspace
// Always remove to avoid any kind problem
workspace.off('file-open', this.onFileOpened)
workspace.off('layout-ready', this.onLayoutReady)

this.statusBar.removeStatusBar()

Expand Down Expand Up @@ -134,8 +135,21 @@ export default class AutocompletePlugin extends Plugin {
this.autocomplete.updateViewIn(editor, event)
}

private onLayoutReady() {
const file = this.app.workspace.getActiveFile()
if (file)
this.autocomplete.scanFile(
file,
this.settings.flowProviderTokenizeStrategy
)
}

private onFileOpened(file: TFile) {
this.autocomplete.scanFile(file)
if (file)
this.autocomplete.scanFile(
file,
this.settings.flowProviderTokenizeStrategy
)
}

private getValidEditorFor(
Expand Down
18 changes: 15 additions & 3 deletions src/providers/flow/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ export abstract class Tokenizer {
cursorIndex: number,
options: TokenizerOptions = { normalize: false }
): string | null {
let wordStartIndex = this.lastWordStartPos(text, cursorIndex, options)
const { normalized, updatedCursor } = options.normalize
? this.normalizedLine(text, cursorIndex)
: { normalized: text, updatedCursor: cursorIndex }

if (options.normalize)
// Already normalized
options.normalize = false

let wordStartIndex = this.lastWordStartPos(
normalized,
updatedCursor,
options
)
let word: string | null = null
if (wordStartIndex !== cursorIndex)
word = text.slice(wordStartIndex, cursorIndex)
if (wordStartIndex !== updatedCursor)
word = text.slice(wordStartIndex, updatedCursor)

return word
}
Expand Down
15 changes: 11 additions & 4 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ export abstract class Provider {
// Otherwise, uses case-insensitive logic
const suggestions = this.completions
.filter((suggestion) =>
inputHasUpperCase
? suggestion.includes(input)
: suggestion.toLowerCase().includes(inputLowered)
suggestion != input
? inputHasUpperCase
? suggestion.includes(input)
: suggestion.toLowerCase().includes(inputLowered)
: false
)
.sort((a, b) => a.localeCompare(b))
.sort(
(a, b) =>
Number(b.toLowerCase().startsWith(inputLowered)) -
Number(a.toLowerCase().startsWith(inputLowered))
)
.sort((a,b) => a.localeCompare(b))
.map((suggestion) => {
return { category: this.category, value: suggestion }
})
Expand Down
52 changes: 25 additions & 27 deletions src/settings/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,8 @@ export class AutocompleteSettingsTab extends PluginSettingTab {
})
)

// TODO: Improve UI reactivity when parent setting is disabled
new Setting(containerEl)
.setName('Flow Provider: Scan current file')
.setDesc(
'Provides current file text suggestions. Be aware of performance issues with large files.'
)
.addToggle((cb) => {
const settings = this.plugin.settings
cb.setValue(
settings.flowProvider && settings.flowProviderScanCurrent
).onChange((value) => {
if (settings.flowProvider) {
this.plugin.settings.flowProviderScanCurrent = value
this.plugin.saveData(this.plugin.settings)
this.plugin.refresh()
} else if (value) {
// Cannot enable plugin
cb.setValue(false)
new Notice('Cannot activate because flow provider is not enabled.')
}
})
})

new Setting(containerEl)
.setName('Flow Provider: Scan current file strategy')
.setName('Flow Provider: Scan strategy')
.setDesc('Choose the default scan strategy')
.addDropdown((cb) => {
// Add options
Expand All @@ -109,12 +86,33 @@ export class AutocompleteSettingsTab extends PluginSettingTab {
this.plugin.saveData(this.plugin.settings)
this.plugin.refresh()
} else {
new Notice(
'Cannot change because flow provider is not enabled.'
)
new Notice('Cannot change because flow provider is not enabled.')
}
}
)
})

// TODO: Improve UI reactivity when parent setting is disabled
new Setting(containerEl)
.setName('Flow Provider: Scan current file')
.setDesc(
'Provides current file text suggestions. Be aware of performance issues with large files.'
)
.addToggle((cb) => {
const settings = this.plugin.settings
cb.setValue(
settings.flowProvider && settings.flowProviderScanCurrent
).onChange((value) => {
if (settings.flowProvider) {
this.plugin.settings.flowProviderScanCurrent = value
this.plugin.saveData(this.plugin.settings)
this.plugin.refresh()
} else if (value) {
// Cannot enable plugin
cb.setValue(false)
new Notice('Cannot activate because flow provider is not enabled.')
}
})
})
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"0.6.2": "0.10.0",
"0.7.0": "0.10.0",
"0.7.1": "0.10.0",
"0.7.2": "0.10.0"
"0.7.2": "0.10.0",
"0.7.3": "0.10.0"
}

0 comments on commit 3eff1f0

Please sign in to comment.