refactor: improve trigger characters for path completion and handle wiki links in emoji completion

This commit is contained in:
purocean
2024-06-19 02:04:36 +08:00
parent 774e59e229
commit 8acc67fc51
2 changed files with 18 additions and 6 deletions
+13 -6
View File
@@ -62,7 +62,7 @@ interface CompletionContext {
}
class CompletionProvider implements Monaco.languages.CompletionItemProvider {
triggerCharacters = ['/', '#', '[']
triggerCharacters = ['/', ':', '#', '[']
private readonly monaco: typeof Monaco
private readonly ctx: Ctx
@@ -263,17 +263,24 @@ class CompletionProvider implements Monaco.languages.CompletionItemProvider {
}
private async * providePathSuggestions (position: Monaco.Position, context: CompletionContext): AsyncIterable<Monaco.languages.CompletionItem> {
const valueBeforeLastSlash = context.linkPrefix.substring(0, context.linkPrefix.lastIndexOf('/') + 1) // keep the last slash
let idx = context.linkPrefix.lastIndexOf('/')
if (context.kind === CompletionContextKind.WikiLink) {
idx = Math.max(context.linkPrefix.lastIndexOf(':'), idx)
}
const valueBeforeLastSlash = context.linkPrefix.substring(0, idx + 1) // keep the last slash
const currentFile = this.ctx.store.state.currentFile
if (!currentFile) {
return
}
const parentDir = this.ctx.utils.path.resolve(
this.ctx.utils.path.dirname(currentFile.path),
valueBeforeLastSlash || '.'
)
const basePath = this.ctx.utils.path.dirname(currentFile.path)
const parentDir = context.kind === CompletionContextKind.WikiLink
? this.ctx.utils.path.resolve(basePath, valueBeforeLastSlash.replace(/:/g, '/') || '.')
: this.ctx.utils.path.resolve(basePath, valueBeforeLastSlash || '.')
const pathSegmentStart = position.delta(0, valueBeforeLastSlash.length - context.linkPrefix.length)
const insertRange = new this.monaco.Range(
+5
View File
@@ -25,6 +25,11 @@ class EmojiCompletionProvider implements Monaco.languages.CompletionItemProvider
const cursor = position.column - 1
const linePrefixText = line.slice(0, cursor)
// Check if the cursor is in a wiki link
if (linePrefixText.lastIndexOf('[[') > linePrefixText.lastIndexOf(']]')) {
return { suggestions: [] }
}
const match = linePrefixText.match(/:[a-zA-Z0-9]*$/)
if (!match || linePrefixText.charAt(linePrefixText.length - match[0].length - 1) === ':') {
return { suggestions: [] }