|
| 1 | +import { monaco } from "react-monaco-editor"; |
| 2 | + |
| 3 | +class MonacoRange { |
| 4 | + startLineNumber: number; |
| 5 | + startColumn: number; |
| 6 | + endLineNumber: number; |
| 7 | + endColumn: number; |
| 8 | + |
| 9 | + constructor(start: monaco.IPosition, end: monaco.IPosition) { |
| 10 | + this.startLineNumber = start.lineNumber; |
| 11 | + this.startColumn = start.column; |
| 12 | + this.endLineNumber = end.lineNumber; |
| 13 | + this.endColumn = end.column; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function text2CompletionItems( |
| 18 | + text: string, |
| 19 | + position: monaco.Position |
| 20 | +): monaco.languages.InlineCompletion { |
| 21 | + const startPosition = { lineNumber: position.lineNumber, column: 1 }; |
| 22 | + const endPosition = position; |
| 23 | + console.log("startPosition", startPosition, text); |
| 24 | + const range = new MonacoRange(startPosition, endPosition); |
| 25 | + const inlineCompletionItem: monaco.languages.InlineCompletion = { |
| 26 | + insertText: text, |
| 27 | + range, |
| 28 | + command: { |
| 29 | + id: "codeium.acceptCompletion", |
| 30 | + title: "Accept Completion", |
| 31 | + arguments: [ |
| 32 | + "apiKey", |
| 33 | + "completionItem.completion.completionId", |
| 34 | + undefined, |
| 35 | + ], |
| 36 | + }, |
| 37 | + }; |
| 38 | + return inlineCompletionItem; |
| 39 | +} |
| 40 | + |
| 41 | +export class MonacoCompletionProvider |
| 42 | + implements monaco.languages.InlineCompletionsProvider |
| 43 | +{ |
| 44 | + client: null; |
| 45 | + items: monaco.languages.InlineCompletion[] = []; |
| 46 | + defaultItems: string[] = []; |
| 47 | + constructor() { |
| 48 | + console.log("MonacoCompletionProvider constructor"); |
| 49 | + this.client = null; |
| 50 | + this.defaultItems = ["hello world\nhello world2"]; |
| 51 | + } |
| 52 | + async provideInlineCompletions( |
| 53 | + model: monaco.editor.ITextModel, |
| 54 | + position: monaco.Position, |
| 55 | + context: monaco.languages.InlineCompletionContext, |
| 56 | + token: monaco.CancellationToken |
| 57 | + ): Promise<monaco.languages.InlineCompletions | undefined> { |
| 58 | + return { |
| 59 | + items: this.defaultItems.map((item) => |
| 60 | + text2CompletionItems(item, position) |
| 61 | + ), |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + handleItemDidShow(): void { |
| 66 | + // Do nothing. |
| 67 | + } |
| 68 | + |
| 69 | + freeInlineCompletions(): void { |
| 70 | + // Do nothing. |
| 71 | + } |
| 72 | +} |
0 commit comments