|
| 1 | +import { Languages } from '@blockception/ide-shared'; |
| 2 | +import { |
| 3 | + CancellationToken, |
| 4 | + Connection, |
| 5 | + InlineCompletionItem, |
| 6 | + InlineCompletionParams, |
| 7 | + ProposedFeatures, |
| 8 | + WorkDoneProgressReporter, |
| 9 | +} from 'vscode-languageserver'; |
| 10 | +import { Context } from '../context/context'; |
| 11 | +import { ExtensionContext } from '../extension'; |
| 12 | +import { IExtendedLogger } from '../logger/logger'; |
| 13 | +import { getCurrentWord } from '../references/function'; |
| 14 | +import { BaseService } from '../services/base'; |
| 15 | +import { CapabilityBuilder } from '../services/capabilities'; |
| 16 | +import { IService } from '../services/service'; |
| 17 | +import { createBuilder } from '../completion/builder/builder'; |
| 18 | +import { CompletionContext } from '../completion/context'; |
| 19 | +import { onCompletionRequest } from '../completion/on-request'; |
| 20 | + |
| 21 | +/** The maximum number of inline suggestions to offer at once. */ |
| 22 | +const maxItems = 5; |
| 23 | + |
| 24 | +export class InlineCompletionService extends BaseService implements IService { |
| 25 | + readonly name: string = 'inline-completion'; |
| 26 | + |
| 27 | + constructor(logger: IExtendedLogger, extension: ExtensionContext) { |
| 28 | + super(logger.withPrefix('[inline-completion]'), extension); |
| 29 | + } |
| 30 | + |
| 31 | + onInitialize(capabilities: CapabilityBuilder): void { |
| 32 | + capabilities.set('inlineCompletionProvider', {}); |
| 33 | + } |
| 34 | + |
| 35 | + setupHandlers(connection: Connection): void { |
| 36 | + // `inlineCompletion` is a proposed feature, enabled through `ProposedFeatures.all`, |
| 37 | + // but it is not part of the base `Connection` type. |
| 38 | + const languages = (connection as ProposedFeatures.Connection).languages; |
| 39 | + this.addDisposable(languages.inlineCompletion.on(this.onInlineCompletion.bind(this))); |
| 40 | + } |
| 41 | + |
| 42 | + private onInlineCompletion( |
| 43 | + params: InlineCompletionParams, |
| 44 | + token: CancellationToken, |
| 45 | + workDoneProgress: WorkDoneProgressReporter, |
| 46 | + ): InlineCompletionItem[] { |
| 47 | + const document = this.extension.documents.get(params.textDocument.uri); |
| 48 | + if (!document) return []; |
| 49 | + |
| 50 | + // Inline ghost text is most useful for mcfunction; richer languages are |
| 51 | + // already served well by the regular completion list. |
| 52 | + if (document.languageId !== Languages.McFunctionIdentifier) return []; |
| 53 | + |
| 54 | + const cursor = document.offsetAt(params.position); |
| 55 | + const word = getCurrentWord(document, cursor); |
| 56 | + |
| 57 | + // Only the portion of the word that has been typed before the cursor. |
| 58 | + const prefix = word.text.slice(0, cursor - word.offset); |
| 59 | + if (prefix === '') return []; |
| 60 | + |
| 61 | + const context = Context.create<CompletionContext>( |
| 62 | + this.extension, |
| 63 | + { |
| 64 | + document, |
| 65 | + token, |
| 66 | + workDoneProgress, |
| 67 | + cursor, |
| 68 | + builder: createBuilder(token, workDoneProgress), |
| 69 | + textDocument: params.textDocument, |
| 70 | + position: params.position, |
| 71 | + }, |
| 72 | + { logger: this.logger }, |
| 73 | + ); |
| 74 | + |
| 75 | + onCompletionRequest(context); |
| 76 | + |
| 77 | + const range = { |
| 78 | + start: document.positionAt(word.offset), |
| 79 | + end: params.position, |
| 80 | + }; |
| 81 | + |
| 82 | + const lowerPrefix = prefix.toLowerCase(); |
| 83 | + const seen = new Set<string>(); |
| 84 | + const out: InlineCompletionItem[] = []; |
| 85 | + |
| 86 | + for (const item of context.builder.getItems()) { |
| 87 | + const insert = typeof item.insertText === 'string' ? item.insertText : item.label; |
| 88 | + if (insert.length <= prefix.length) continue; |
| 89 | + if (!insert.toLowerCase().startsWith(lowerPrefix)) continue; |
| 90 | + if (seen.has(insert)) continue; |
| 91 | + |
| 92 | + seen.add(insert); |
| 93 | + out.push({ insertText: insert, filterText: insert, range }); |
| 94 | + |
| 95 | + if (out.length >= maxItems) break; |
| 96 | + } |
| 97 | + |
| 98 | + return out; |
| 99 | + } |
| 100 | +} |
0 commit comments