generated from obsidianmd/obsidian-sample-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve to show suggestions more than 20 times faster (#33)
- Loading branch information
1 parent
083a45b
commit f6e047d
Showing
6 changed files
with
195 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { App, MarkdownView } from "obsidian"; | ||
import { groupBy, uniq } from "../util/collection-helper"; | ||
import { Word, WordsByFirstLetter } from "./suggester"; | ||
import { Tokenizer } from "../tokenizer/tokenizer"; | ||
|
||
export class CurrentFileWordProvider { | ||
private words: Word[] = []; | ||
wordsByFirstLetter: WordsByFirstLetter; | ||
|
||
constructor(private app: App, private tokenizer: Tokenizer) {} | ||
|
||
async refreshWords(): Promise<void> { | ||
this.clearWords(); | ||
|
||
if (!this.app.workspace.getActiveViewOfType(MarkdownView)) { | ||
return; | ||
} | ||
|
||
const file = this.app.workspace.getActiveFile(); | ||
if (!file) { | ||
return; | ||
} | ||
|
||
const content = await this.app.vault.cachedRead(file); | ||
this.words = uniq(this.tokenizer.tokenize(content)).map((x) => ({ | ||
value: x, | ||
})); | ||
this.wordsByFirstLetter = groupBy(this.words, (x) => x.value.charAt(0)); | ||
} | ||
|
||
clearWords(): void { | ||
this.words = []; | ||
this.wordsByFirstLetter = {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { App } from "obsidian"; | ||
import { pushWord, Word, WordsByFirstLetter } from "./suggester"; | ||
import { AppHelper } from "../app-helper"; | ||
|
||
export class InternalLinkWordProvider { | ||
private words: Word[] = []; | ||
wordsByFirstLetter: WordsByFirstLetter; | ||
|
||
constructor(private app: App, private appHelper: AppHelper) {} | ||
|
||
refreshWords(): void { | ||
this.clearWords(); | ||
|
||
const resolvedInternalLinkWords = this.app.vault | ||
.getMarkdownFiles() | ||
.map((x) => ({ | ||
value: `[[${x.basename}]]`, | ||
aliases: [x.basename, ...this.appHelper.getAliases(x)], | ||
description: x.path, | ||
})); | ||
|
||
const unresolvedInternalLinkWords = this.appHelper | ||
.searchPhantomLinks() | ||
.map((x) => ({ | ||
value: `[[${x}]]`, | ||
aliases: [x], | ||
description: "Not created yet", | ||
})); | ||
|
||
this.words = [...resolvedInternalLinkWords, ...unresolvedInternalLinkWords]; | ||
for (const word of this.words) { | ||
// 2 because `[[..` | ||
pushWord(this.wordsByFirstLetter, word.value.charAt(2), word); | ||
word.aliases?.forEach((a) => | ||
pushWord(this.wordsByFirstLetter, a.charAt(2), word) | ||
); | ||
} | ||
} | ||
|
||
clearWords(): void { | ||
this.words = []; | ||
this.wordsByFirstLetter = {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.