5
5
import com .codeshell .intellij .settings .CodeShellSettings ;
6
6
import com .codeshell .intellij .utils .CodeGenHintRenderer ;
7
7
import com .codeshell .intellij .utils .CodeShellIcons ;
8
+ import com .codeshell .intellij .utils .CodeShellUtils ;
8
9
import com .codeshell .intellij .utils .EditorUtils ;
9
10
import com .intellij .openapi .application .ApplicationManager ;
10
- import com .intellij .openapi .command .WriteCommandAction ;
11
11
import com .intellij .openapi .editor .*;
12
12
import com .intellij .openapi .editor .event .*;
13
13
import com .intellij .openapi .editor .impl .EditorComponentImpl ;
@@ -42,7 +42,7 @@ public class CodeShellWidget extends EditorBasedWidget
42
42
43
43
public static final Key <String []> SHELL_CODER_CODE_SUGGESTION = new Key <>("CodeShell Code Suggestion" );
44
44
public static final Key <Integer > SHELL_CODER_POSITION = new Key <>("CodeShell Position" );
45
-
45
+ public static boolean enableSuggestion = false ;
46
46
protected CodeShellWidget (@ NotNull Project project ) {
47
47
super (project );
48
48
}
@@ -181,6 +181,7 @@ public void caretRemoved(@NotNull CaretEvent event) {
181
181
182
182
@ Override
183
183
public void afterDocumentChange (@ NotNull Document document ) {
184
+ enableSuggestion = true ;
184
185
if (ApplicationManager .getApplication ().isDispatchThread ()) {
185
186
EditorFactory .getInstance ().editors (document )
186
187
.filter (this ::isFocusedEditor )
@@ -190,7 +191,7 @@ public void afterDocumentChange(@NotNull Document document) {
190
191
}
191
192
192
193
private void updateInlayHints (Editor focusedEditor ) {
193
- if (Objects .isNull (focusedEditor )) {
194
+ if (Objects .isNull (focusedEditor ) || ! EditorUtils . isMainEditor ( focusedEditor ) ) {
194
195
return ;
195
196
}
196
197
VirtualFile file = FileDocumentManager .getInstance ().getFile (focusedEditor .getDocument ());
@@ -206,8 +207,8 @@ private void updateInlayHints(Editor focusedEditor) {
206
207
file .putUserData (SHELL_CODER_POSITION , focusedEditor .getCaretModel ().getOffset ());
207
208
208
209
InlayModel inlayModel = focusedEditor .getInlayModel ();
209
- inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
210
- inlayModel .getBlockElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
210
+ inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (CodeShellUtils ::disposeInlayHints );
211
+ inlayModel .getBlockElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (CodeShellUtils ::disposeInlayHints );
211
212
}
212
213
return ;
213
214
}
@@ -229,8 +230,9 @@ private void updateInlayHints(Editor focusedEditor) {
229
230
}
230
231
if (inlineHint .startsWith (modifiedText )) {
231
232
inlineHint = inlineHint .substring (modifiedText .length ());
233
+ enableSuggestion = false ;
232
234
if (inlineHint .length () > 0 ) {
233
- inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
235
+ inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (CodeShellUtils ::disposeInlayHints );
234
236
inlayModel .addInlineElement (currentPosition , true , new CodeGenHintRenderer (inlineHint ));
235
237
existingHints [0 ] = inlineHint ;
236
238
@@ -239,7 +241,7 @@ private void updateInlayHints(Editor focusedEditor) {
239
241
return ;
240
242
} else if (existingHints .length > 1 ) {
241
243
existingHints = Arrays .copyOfRange (existingHints , 1 , existingHints .length );
242
- addCodeSuggestion (focusedEditor , file , currentPosition , existingHints );
244
+ CodeShellUtils . addCodeSuggestion (focusedEditor , file , currentPosition , existingHints );
243
245
return ;
244
246
} else {
245
247
file .putUserData (SHELL_CODER_CODE_SUGGESTION , null );
@@ -248,47 +250,18 @@ private void updateInlayHints(Editor focusedEditor) {
248
250
}
249
251
}
250
252
251
- inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
252
- inlayModel .getBlockElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
253
+ inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (CodeShellUtils ::disposeInlayHints );
254
+ inlayModel .getBlockElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (CodeShellUtils ::disposeInlayHints );
253
255
254
256
file .putUserData (SHELL_CODER_POSITION , currentPosition );
255
-
257
+ if (!enableSuggestion || currentPosition < lastPosition ){
258
+ enableSuggestion = false ;
259
+ return ;
260
+ }
256
261
CodeShellCompleteService codeShell = ApplicationManager .getApplication ().getService (CodeShellCompleteService .class );
257
262
CharSequence editorContents = focusedEditor .getDocument ().getCharsSequence ();
258
263
CompletableFuture <String []> future = CompletableFuture .supplyAsync (() -> codeShell .getCodeCompletionHints (editorContents , currentPosition ));
259
- future .thenAccept (hintList -> this .addCodeSuggestion (focusedEditor , file , currentPosition , hintList ));
260
- }
261
-
262
- private void disposeInlayHints (Inlay <?> inlay ) {
263
- if (inlay .getRenderer () instanceof CodeGenHintRenderer ) {
264
- inlay .dispose ();
265
- }
266
- }
267
-
268
- private void addCodeSuggestion (Editor focusedEditor , VirtualFile file , int suggestionPosition , String [] hintList ) {
269
- WriteCommandAction .runWriteCommandAction (focusedEditor .getProject (), () -> {
270
- if (suggestionPosition != focusedEditor .getCaretModel ().getOffset ()) {
271
- return ;
272
- }
273
- if (Objects .nonNull (focusedEditor .getSelectionModel ().getSelectedText ())) {
274
- return ;
275
- }
276
-
277
- file .putUserData (SHELL_CODER_CODE_SUGGESTION , hintList );
278
- file .putUserData (SHELL_CODER_POSITION , suggestionPosition );
279
-
280
- InlayModel inlayModel = focusedEditor .getInlayModel ();
281
- inlayModel .getInlineElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
282
- inlayModel .getBlockElementsInRange (0 , focusedEditor .getDocument ().getTextLength ()).forEach (this ::disposeInlayHints );
283
- if (Objects .nonNull (hintList ) && hintList .length > 0 ) {
284
- if (!hintList [0 ].trim ().isEmpty ()) {
285
- inlayModel .addInlineElement (suggestionPosition , true , new CodeGenHintRenderer (hintList [0 ]));
286
- }
287
- for (int i = 1 ; i < hintList .length ; i ++) {
288
- inlayModel .addBlockElement (suggestionPosition , false , false , 0 , new CodeGenHintRenderer (hintList [i ]));
289
- }
290
- }
291
- });
264
+ future .thenAccept (hintList -> CodeShellUtils .addCodeSuggestion (focusedEditor , file , currentPosition , hintList ));
292
265
}
293
266
294
267
}
0 commit comments