( monacoSite: MonacoSite, completionItem: CompletionItem, document: monaco.editor.ITextModel, additionalUtf8ByteOffset: number, apiKey: string, editor?: monaco.editor.ICodeEditor )
| 111 | } |
| 112 | |
| 113 | function createInlineCompletionItem( |
| 114 | monacoSite: MonacoSite, |
| 115 | completionItem: CompletionItem, |
| 116 | document: monaco.editor.ITextModel, |
| 117 | additionalUtf8ByteOffset: number, |
| 118 | apiKey: string, |
| 119 | editor?: monaco.editor.ICodeEditor |
| 120 | ): monaco.languages.InlineCompletion | undefined { |
| 121 | if (!completionItem.completion || !completionItem.range) { |
| 122 | return undefined; |
| 123 | } |
| 124 | |
| 125 | // Create and return inlineCompletionItem. |
| 126 | const { value: text, utf16Offset } = getValueAndStartOffset(monacoSite, document); |
| 127 | const startPosition = document.getPositionAt( |
| 128 | utf16Offset + |
| 129 | numUtf8BytesToNumCodeUnits( |
| 130 | text, |
| 131 | Number(completionItem.range.startOffset) - additionalUtf8ByteOffset |
| 132 | ) |
| 133 | ); |
| 134 | const endPosition = document.getPositionAt( |
| 135 | utf16Offset + |
| 136 | numUtf8BytesToNumCodeUnits( |
| 137 | text, |
| 138 | Number(completionItem.range.endOffset) - additionalUtf8ByteOffset |
| 139 | ) |
| 140 | ); |
| 141 | const range = new MonacoRange(startPosition, endPosition); |
| 142 | let completionText = completionItem.completion.text; |
| 143 | let callback: (() => void) | undefined = undefined; |
| 144 | if (editor && completionItem.suffix && completionItem.suffix.text.length > 0) { |
| 145 | // Add suffix to the completion text. |
| 146 | completionText += completionItem.suffix.text; |
| 147 | // Create callback to move cursor after accept. |
| 148 | // Note that this is a hack to get around Monaco's API limitations. |
| 149 | // There's no need to convert to code units since we only use simple characters. |
| 150 | const deltaCursorOffset = Number(completionItem.suffix.deltaCursorOffset); |
| 151 | callback = () => { |
| 152 | const selection = editor.getSelection(); |
| 153 | if (selection === null) { |
| 154 | console.warn('Unexpected, no selection'); |
| 155 | return; |
| 156 | } |
| 157 | const newPosition = document.getPositionAt( |
| 158 | document.getOffsetAt(selection.getPosition()) + deltaCursorOffset |
| 159 | ); |
| 160 | editor.setSelection(new MonacoRange(newPosition, newPosition)); |
| 161 | editor._commandService.executeCommand('editor.action.inlineSuggest.trigger'); |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | const inlineCompletionItem: monaco.languages.InlineCompletion = { |
| 166 | insertText: completionText, |
| 167 | text: completionText, |
| 168 | range, |
| 169 | command: { |
| 170 | id: 'codeium.acceptCompletion', |
no test coverage detected