@@ -22,6 +22,9 @@ import { ILSPCompletionThemeManager } from '@krassowski/completion-theme/lib/typ
22
22
import { LSPCompletionRenderer } from './renderer' ;
23
23
import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
24
24
25
+ const DOC_PANEL_SELECTOR = '.jp-Completer-docpanel' ;
26
+ const DOC_PANEL_PLACEHOLDER_CLASS = 'lsp-completer-placeholder' ;
27
+
25
28
export class CompletionCM extends CodeMirrorIntegration {
26
29
private _completionCharacters : string [ ] ;
27
30
@@ -112,11 +115,29 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
112
115
renderer : LSPCompletionRenderer ,
113
116
item : LazyCompletionItem
114
117
) {
118
+ if ( ! item . supportsResolution ( ) ) {
119
+ return ;
120
+ }
121
+
115
122
if ( item . needsResolution ( ) ) {
123
+ this . set_doc_panel_placeholder ( true ) ;
116
124
item . fetchDocumentation ( ) ;
117
125
} else if ( item . isResolved ( ) ) {
118
126
this . refresh_doc_panel ( item ) ;
119
127
}
128
+
129
+ // also fetch completion for the previous and the next item to prevent jitter
130
+ const index = this . current_index ;
131
+ const items = this . current_items ;
132
+
133
+ if ( index - 1 >= 0 ) {
134
+ const previous = items [ index - 1 ] as LazyCompletionItem ;
135
+ previous ?. self ?. fetchDocumentation ( ) ;
136
+ }
137
+ if ( index + 1 < items . length ) {
138
+ const next = items [ index + 1 ] as LazyCompletionItem ;
139
+ next ?. self ?. fetchDocumentation ( ) ;
140
+ }
120
141
}
121
142
122
143
private swap_adapter (
@@ -197,27 +218,39 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
197
218
this . current_completion_handler . connector = this . current_completion_connector ;
198
219
}
199
220
200
- refresh_doc_panel ( item : LazyCompletionItem ) {
221
+ private get current_items ( ) {
201
222
// TODO upstream: make completer public?
202
223
let completer = this . current_completion_handler . completer ;
203
224
204
225
// TODO upstream: allow to get completionItems() without markup
205
226
// (note: not trivial as _markup() does filtering too)
206
- const items = completer . model . completionItems ( ) ;
227
+ return completer . model . completionItems ( ) ;
228
+ }
229
+
230
+ private get current_index ( ) {
231
+ let completer = this . current_completion_handler . completer ;
207
232
208
233
// TODO upstream: add getActiveItem() to Completer
209
234
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
210
235
// @ts -ignore
211
- const index = completer . _activeIndex ;
212
- const active : CompletionHandler . ICompletionItem = items [ index ] ;
236
+ return completer . _activeIndex ;
237
+ }
238
+
239
+ refresh_doc_panel ( item : LazyCompletionItem ) {
240
+ let completer = this . current_completion_handler . completer ;
241
+
242
+ const active : CompletionHandler . ICompletionItem = this . current_items [
243
+ this . current_index
244
+ ] ;
213
245
214
246
if ( active . insertText != item . insertText ) {
215
247
return ;
216
248
}
217
249
218
- if ( item . documentation ) {
219
- let docPanel = completer . node . querySelector ( '.jp-Completer-docpanel' ) ;
250
+ const docPanel = completer . node . querySelector ( DOC_PANEL_SELECTOR ) ;
251
+ docPanel . classList . remove ( DOC_PANEL_PLACEHOLDER_CLASS ) ;
220
252
253
+ if ( item . documentation ) {
221
254
// remove all children
222
255
docPanel . textContent = '' ;
223
256
// TODO upstream: renderer should take care of the documentation rendering
@@ -227,6 +260,20 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
227
260
docPanel . appendChild ( node ) ;
228
261
229
262
docPanel . setAttribute ( 'style' , '' ) ;
263
+ } else {
264
+ docPanel . setAttribute ( 'style' , 'none' ) ;
265
+ }
266
+ }
267
+
268
+ set_doc_panel_placeholder ( enabled : boolean ) {
269
+ let completer = this . current_completion_handler . completer ;
270
+ const docPanel = completer . node . querySelector ( DOC_PANEL_SELECTOR ) ;
271
+ if ( enabled ) {
272
+ docPanel . setAttribute ( 'style' , '' ) ;
273
+ docPanel . classList . add ( DOC_PANEL_PLACEHOLDER_CLASS ) ;
274
+ } else {
275
+ docPanel . setAttribute ( 'style' , 'none' ) ;
276
+ docPanel . classList . remove ( DOC_PANEL_PLACEHOLDER_CLASS ) ;
230
277
}
231
278
}
232
279
0 commit comments