cocでLSP互換の補完を実装する
例えばcoc-copilotでこんな感じにする
code:typescript
import {
CancellationToken,
CompletionContext,
CompletionItem,
CompletionItemProvider,
ExtensionContext,
languages,
Position,
TextDocument,
} from 'coc.nvim';
export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(
languages.registerCompletionItemProvider('copilot', 'Copilot', null, new CopilotCompletionItemProvider())
);
}
class CopilotCompletionItemProvider implements CompletionItemProvider {
async provideCompletionItems(
document: TextDocument,
position: Position,
token: CancellationToken,
context: CompletionContext
) {
const completionItems: CompletionItem[] = [];
// const buffer = (await workspace.document).buffer;
// const copilot = (await buffer.getVar('_copilot')) as Copilot | null;
// if (copilot?.suggestions == null) return completionItems;
completionItems.push({
label: 'sample',
// ---- add more property! ----
});
return completionItems;
}
// 補完候補選択時に item をゴニョゴニョしたい、もしくはする必要がある場合
resolveCompletionItem(item: CompletionItem, token: CancellationToken) {
return item;
}
}