|
1 | 1 | import { CompletionContext, completionStatus } from "@codemirror/autocomplete"; |
2 | | -import { ChangeSet, Transaction } from "@codemirror/state"; |
3 | 2 | import { EditorView, type ViewUpdate } from "@codemirror/view"; |
4 | | -import { completionsToChangeSpec, getCodeiumCompletions } from "./codeium.js"; |
5 | | -import { |
6 | | - acceptSuggestion, |
7 | | - addSuggestions, |
8 | | - clearSuggestion, |
9 | | -} from "./effects.js"; |
| 3 | +import { acceptSuggestion, clearSuggestion } from "./effects.js"; |
10 | 4 | import { completionDecoration } from "./completionDecoration.js"; |
11 | | -import { copilotEvent, copilotIgnore } from "./annotations.js"; |
12 | | -import { codeiumConfig, codeiumOtherDocumentsConfig } from "./config.js"; |
| 5 | +import { copilotEvent } from "./annotations.js"; |
| 6 | +import { codeiumConfig } from "./config.js"; |
| 7 | +import { requestCompletion } from "./requestCompletion.js"; |
13 | 8 |
|
14 | 9 | /** |
15 | 10 | * To request a completion, the document needs to have been |
@@ -57,7 +52,7 @@ export function completionRequester() { |
57 | 52 |
|
58 | 53 | return EditorView.updateListener.of((update: ViewUpdate) => { |
59 | 54 | const config = update.view.state.facet(codeiumConfig); |
60 | | - const { override } = update.view.state.facet(codeiumOtherDocumentsConfig); |
| 55 | + if (!config.alwaysOn) return; |
61 | 56 |
|
62 | 57 | if (!shouldRequestCompletion(update)) return; |
63 | 58 |
|
@@ -85,76 +80,14 @@ export function completionRequester() { |
85 | 80 | return; |
86 | 81 | } |
87 | 82 |
|
88 | | - const source = state.doc.toString(); |
89 | | - |
90 | 83 | // Set a new timeout to request completion |
91 | 84 | timeout = setTimeout(async () => { |
92 | 85 | // Check if the position has changed |
93 | 86 | if (pos !== lastPos) return; |
94 | 87 |
|
95 | | - const otherDocuments = await override(); |
96 | | - |
97 | | - // Request completion from the server |
98 | | - try { |
99 | | - const completionResult = await getCodeiumCompletions({ |
100 | | - text: source, |
101 | | - cursorOffset: pos, |
102 | | - config, |
103 | | - otherDocuments, |
104 | | - }); |
105 | | - |
106 | | - if ( |
107 | | - !completionResult || |
108 | | - completionResult.completionItems.length === 0 |
109 | | - ) { |
110 | | - return; |
111 | | - } |
112 | | - |
113 | | - // Check if the position is still the same. If |
114 | | - // it has changed, ignore the code that we just |
115 | | - // got from the API and don't show anything. |
116 | | - if ( |
117 | | - !( |
118 | | - pos === lastPos && |
119 | | - completionStatus(update.view.state) !== "active" && |
120 | | - update.view.hasFocus |
121 | | - ) |
122 | | - ) { |
123 | | - return; |
124 | | - } |
125 | | - |
126 | | - // Dispatch an effect to add the suggestion |
127 | | - // If the completion starts before the end of the line, |
128 | | - // check the end of the line with the end of the completion |
129 | | - const changeSpecs = completionsToChangeSpec(completionResult); |
130 | | - |
131 | | - const index = 0; |
132 | | - const firstSpec = changeSpecs.at(index); |
133 | | - if (!firstSpec) return; |
134 | | - const insertChangeSet = ChangeSet.of(firstSpec, state.doc.length); |
135 | | - const reverseChangeSet = insertChangeSet.invert(state.doc); |
136 | | - |
137 | | - update.view.dispatch({ |
138 | | - changes: insertChangeSet, |
139 | | - effects: addSuggestions.of({ |
140 | | - index, |
141 | | - reverseChangeSet, |
142 | | - changeSpecs, |
143 | | - }), |
144 | | - annotations: [ |
145 | | - copilotIgnore.of(null), |
146 | | - copilotEvent.of(null), |
147 | | - Transaction.addToHistory.of(false), |
148 | | - ], |
149 | | - }); |
150 | | - } catch (error) { |
151 | | - console.warn("copilot completion failed", error); |
152 | | - // Javascript wait for 500ms for some reason is necessary here. |
153 | | - // TODO - FIGURE OUT WHY THIS RESOLVES THE BUG |
154 | | - |
155 | | - await new Promise((resolve) => setTimeout(resolve, 300)); |
156 | | - } |
| 88 | + await requestCompletion(update.view, lastPos); |
157 | 89 | }, config.timeout); |
| 90 | + |
158 | 91 | // Update the last position |
159 | 92 | lastPos = pos; |
160 | 93 | }); |
|
0 commit comments