1
1
import {
2
+ CompletionConnector ,
2
3
CompletionHandler ,
3
4
ContextConnector ,
4
- KernelConnector ,
5
- CompletionConnector
5
+ KernelConnector
6
6
} from '@jupyterlab/completer' ;
7
7
import { CodeEditor } from '@jupyterlab/codeeditor' ;
8
8
import { JSONArray , JSONObject } from '@lumino/coreutils' ;
9
- import { CompletionItemKind , CompletionTriggerKind } from '../../lsp' ;
9
+ import {
10
+ AdditionalCompletionTriggerKinds ,
11
+ CompletionItemKind ,
12
+ CompletionTriggerKind ,
13
+ ExtendedCompletionTriggerKind
14
+ } from '../../lsp' ;
10
15
import * as lsProtocol from 'vscode-languageserver-types' ;
11
16
import { VirtualDocument } from '../../virtual/document' ;
12
17
import { IVirtualEditor } from '../../virtual/editor' ;
@@ -17,7 +22,6 @@ import {
17
22
} from '../../positioning' ;
18
23
import { LSPConnection } from '../../connection' ;
19
24
import { Session } from '@jupyterlab/services' ;
20
- import ICompletionItemsResponseType = CompletionHandler . ICompletionItemsResponseType ;
21
25
22
26
import { CodeCompletion as LSPCompletionSettings } from '../../_completion' ;
23
27
import { FeatureSettings } from '../../feature' ;
@@ -27,6 +31,7 @@ import {
27
31
KernelKind
28
32
} from '@krassowski/completion-theme/lib/types' ;
29
33
import { LabIcon } from '@jupyterlab/ui-components' ;
34
+ import ICompletionItemsResponseType = CompletionHandler . ICompletionItemsResponseType ;
30
35
31
36
/**
32
37
* A LSP connector for completion handlers.
@@ -44,7 +49,7 @@ export class LSPConnector
44
49
responseType = ICompletionItemsResponseType ;
45
50
46
51
virtual_editor : IVirtualEditor < CodeEditor . IEditor > ;
47
- trigger_kind : CompletionTriggerKind ;
52
+ trigger_kind : ExtendedCompletionTriggerKind ;
48
53
49
54
protected get suppress_auto_invoke_in ( ) : string [ ] {
50
55
return this . options . settings . composite . suppressInvokeIn ;
@@ -156,6 +161,17 @@ export class LSPConnector
156
161
cursor_in_root
157
162
) ;
158
163
164
+ const lsp_promise = this . fetch_lsp (
165
+ token ,
166
+ typed_character ,
167
+ virtual_start ,
168
+ virtual_end ,
169
+ virtual_cursor ,
170
+ document ,
171
+ position_in_token
172
+ ) ;
173
+ let promise : Promise < CompletionHandler . ICompletionItemsReply > = null ;
174
+
159
175
try {
160
176
if ( this . _kernel_connector && this . _has_kernel ) {
161
177
// TODO: this would be awesome if we could connect to rpy2 for R suggestions in Python,
@@ -165,41 +181,34 @@ export class LSPConnector
165
181
const kernelLanguage = await this . _kernel_language ( ) ;
166
182
167
183
if ( document . language === kernelLanguage ) {
168
- return Promise . all ( [
184
+ promise = Promise . all ( [
169
185
this . _kernel_connector . fetch ( request ) ,
170
- this . fetch_lsp (
171
- token ,
172
- typed_character ,
173
- virtual_start ,
174
- virtual_end ,
175
- virtual_cursor ,
176
- document ,
177
- position_in_token
178
- )
186
+ lsp_promise
179
187
] ) . then ( ( [ kernel , lsp ] ) =>
180
188
this . merge_replies ( this . transform_reply ( kernel ) , lsp , this . _editor )
181
189
) ;
182
190
}
183
191
}
184
-
185
- return this . fetch_lsp (
186
- token ,
187
- typed_character ,
188
- virtual_start ,
189
- virtual_end ,
190
- virtual_cursor ,
191
- document ,
192
- position_in_token
193
- ) . catch ( e => {
194
- console . warn ( 'LSP: hint failed' , e ) ;
195
- return this . fallback_connector
196
- . fetch ( request )
197
- . then ( this . transform_reply ) ;
198
- } ) ;
192
+ if ( ! promise ) {
193
+ promise = lsp_promise . catch ( e => {
194
+ console . warn ( 'LSP: hint failed' , e ) ;
195
+ return this . fallback_connector
196
+ . fetch ( request )
197
+ . then ( this . transform_reply ) ;
198
+ } ) ;
199
+ }
199
200
} catch ( e ) {
200
201
console . warn ( 'LSP: kernel completions failed' , e ) ;
201
- return this . fallback_connector . fetch ( request ) . then ( this . transform_reply ) ;
202
+ promise = this . fallback_connector
203
+ . fetch ( request )
204
+ . then ( this . transform_reply ) ;
202
205
}
206
+
207
+ return promise . then ( reply => {
208
+ reply = this . suppress_if_needed ( reply ) ;
209
+ this . trigger_kind = CompletionTriggerKind . Invoked ;
210
+ return reply ;
211
+ } ) ;
203
212
}
204
213
205
214
async fetch_lsp (
@@ -216,6 +225,11 @@ export class LSPConnector
216
225
console . log ( '[LSP][Completer] Fetching and Transforming' ) ;
217
226
console . log ( '[LSP][Completer] Token:' , token , start , end ) ;
218
227
228
+ const trigger_kind =
229
+ this . trigger_kind == AdditionalCompletionTriggerKinds . AutoInvoked
230
+ ? CompletionTriggerKind . Invoked
231
+ : this . trigger_kind ;
232
+
219
233
let lspCompletionItems = ( ( await connection . getCompletion (
220
234
cursor ,
221
235
{
@@ -226,7 +240,7 @@ export class LSPConnector
226
240
document . document_info ,
227
241
false ,
228
242
typed_character ,
229
- this . trigger_kind
243
+ trigger_kind
230
244
) ) || [ ] ) as lsProtocol . CompletionItem [ ] ;
231
245
232
246
let prefix = token . value . slice ( 0 , position_in_token + 1 ) ;
@@ -398,6 +412,19 @@ export class LSPConnector
398
412
save ( id : CompletionHandler . IRequest , value : void ) : Promise < any > {
399
413
return Promise . resolve ( undefined ) ;
400
414
}
415
+
416
+ private suppress_if_needed ( reply : CompletionHandler . ICompletionItemsReply ) {
417
+ if ( this . trigger_kind == AdditionalCompletionTriggerKinds . AutoInvoked ) {
418
+ if ( reply . start == reply . end ) {
419
+ return {
420
+ start : reply . start ,
421
+ end : reply . end ,
422
+ items : [ ]
423
+ } ;
424
+ }
425
+ }
426
+ return reply ;
427
+ }
401
428
}
402
429
403
430
/**
0 commit comments