(
textModels: CodeMirror.Doc[],
currentTextModel: CodeMirror.Doc,
editorOptions: EditorOptions,
relativePath: string | undefined,
createDisposables: (() => IDisposable[]) | undefined
)
| 107 | } |
| 108 | |
| 109 | async triggerCompletion( |
| 110 | textModels: CodeMirror.Doc[], |
| 111 | currentTextModel: CodeMirror.Doc, |
| 112 | editorOptions: EditorOptions, |
| 113 | relativePath: string | undefined, |
| 114 | createDisposables: (() => IDisposable[]) | undefined |
| 115 | ): Promise<void> { |
| 116 | const apiKey = await this.client.apiKeyPoller.apiKey; |
| 117 | if (apiKey === undefined) { |
| 118 | return; |
| 119 | } |
| 120 | const cursor = currentTextModel.getCursor(); |
| 121 | const { text, utf8ByteOffset, additionalUtf8ByteOffset } = computeTextAndOffsetsForCodeMirror( |
| 122 | textModels, |
| 123 | currentTextModel |
| 124 | ); |
| 125 | const numUtf8Bytes = additionalUtf8ByteOffset + utf8ByteOffset; |
| 126 | const request = new GetCompletionsRequest({ |
| 127 | metadata: this.client.getMetadata(this.ideInfo, apiKey), |
| 128 | document: { |
| 129 | text, |
| 130 | editorLanguage: editorLanguage(currentTextModel), |
| 131 | language: language(currentTextModel, relativePath), |
| 132 | cursorOffset: BigInt(numUtf8Bytes), |
| 133 | lineEnding: '\n', |
| 134 | // We could use the regular path which could have a drive: prefix, but |
| 135 | // this is probably unusual. |
| 136 | relativePath, |
| 137 | }, |
| 138 | editorOptions, |
| 139 | }); |
| 140 | const response = await this.client.getCompletions(request); |
| 141 | if (response === undefined) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // No more await allowed below this point, given that we've checked for |
| 146 | // abort, so this must be the latest debounced request. |
| 147 | this.clearCompletion( |
| 148 | "about to replace completions if the cursor hasn't moved and we got completions" |
| 149 | ); |
| 150 | const newCursor = currentTextModel.getCursor(); |
| 151 | if (newCursor.ch !== cursor.ch || newCursor.line !== cursor.line) { |
| 152 | // TODO(prem): Is this check necessary? |
| 153 | return; |
| 154 | } |
| 155 | if (response.completionItems.length === 0) { |
| 156 | return; |
| 157 | } |
| 158 | const completionItem = response.completionItems[0]; |
| 159 | this.renderCompletion( |
| 160 | currentTextModel, |
| 161 | completionItem, |
| 162 | additionalUtf8ByteOffset, |
| 163 | apiKey, |
| 164 | createDisposables ? createDisposables : () => [] |
| 165 | ); |
| 166 | } |
no test coverage detected