(
doc: CodeMirror.Doc,
completionItem: CompletionItem,
additionalUtf8ByteOffset: number,
apiKey: string,
createDisposables: () => IDisposable[]
)
| 187 | } |
| 188 | |
| 189 | renderCompletion( |
| 190 | doc: CodeMirror.Doc, |
| 191 | completionItem: CompletionItem, |
| 192 | additionalUtf8ByteOffset: number, |
| 193 | apiKey: string, |
| 194 | createDisposables: () => IDisposable[] |
| 195 | ): void { |
| 196 | this.clearCompletion('about to render new completions'); |
| 197 | const startOffsetUtf8Bytes = |
| 198 | Number(completionItem.range?.startOffset ?? 0) - additionalUtf8ByteOffset; |
| 199 | const endOffsetUtf8Bytes = |
| 200 | Number(completionItem.range?.endOffset ?? 0) - additionalUtf8ByteOffset; |
| 201 | const currentCompletion: typeof this.currentCompletion = { |
| 202 | completionItem, |
| 203 | lineWidgets: [], |
| 204 | textMarkers: [], |
| 205 | disposables: createDisposables(), |
| 206 | doc, |
| 207 | start: doc.posFromIndex(numUtf8BytesToNumCodeUnits(doc.getValue(), startOffsetUtf8Bytes)), |
| 208 | end: doc.posFromIndex(numUtf8BytesToNumCodeUnits(doc.getValue(), endOffsetUtf8Bytes)), |
| 209 | apiKey, |
| 210 | docState: doc.getValue(), |
| 211 | }; |
| 212 | const cursor = doc.getCursor(); |
| 213 | let createdInlineAtCursor = false; |
| 214 | completionItem.completionParts.forEach((part) => { |
| 215 | if (part.type === CompletionPartType.INLINE) { |
| 216 | const bookmarkElement = document.createElement('span'); |
| 217 | bookmarkElement.classList.add('codeium-ghost'); |
| 218 | bookmarkElement.innerText = part.text; |
| 219 | const partOffsetBytes = Number(part.offset) - additionalUtf8ByteOffset; |
| 220 | const partOffset = numUtf8BytesToNumCodeUnits(doc.getValue(), partOffsetBytes); |
| 221 | const pos = doc.posFromIndex(partOffset); |
| 222 | const bookmarkWidget = doc.setBookmark(pos, { |
| 223 | widget: bookmarkElement, |
| 224 | insertLeft: true, |
| 225 | // We need all widgets to have handleMouseEvents true for the glitches |
| 226 | // where the completion doesn't disappear. |
| 227 | handleMouseEvents: true, |
| 228 | }); |
| 229 | currentCompletion.textMarkers.push({ |
| 230 | marker: bookmarkWidget, |
| 231 | pos, |
| 232 | spanElement: bookmarkElement, |
| 233 | }); |
| 234 | if (pos.line === cursor.line && pos.ch === cursor.ch) { |
| 235 | createdInlineAtCursor = true; |
| 236 | } |
| 237 | } else if (part.type === CompletionPartType.BLOCK) { |
| 238 | // We use CodeMirror's LineWidget feature to render the block ghost text element. |
| 239 | const lineElement = document.createElement('div'); |
| 240 | lineElement.classList.add('codeium-ghost'); |
| 241 | part.text.split('\n').forEach((line) => { |
| 242 | const preElement = document.createElement('pre'); |
| 243 | preElement.classList.add('CodeMirror-line', 'codeium-ghost-line'); |
| 244 | if (line === '') { |
| 245 | line = ' '; |
| 246 | } |
no test coverage detected