Skip to content

Commit 56fbe0c

Browse files
committed
Fix sporadic misplacement of hover tooltips
1 parent 4da6697 commit 56fbe0c

File tree

1 file changed

+35
-19
lines changed
  • packages/jupyterlab-lsp/src/features

1 file changed

+35
-19
lines changed

packages/jupyterlab-lsp/src/features/hover.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ interface IResponseData {
4646
ce_editor: CodeEditor.IEditor;
4747
}
4848

49+
interface IOnHoverResponse {
50+
hover: lsProtocol.Hover;
51+
position: IVirtualPosition;
52+
}
53+
4954
/**
5055
* Check whether mouse is close to given element (within a specified number of pixels)
5156
* @param what target element
@@ -127,12 +132,10 @@ export class HoverCM extends CodeMirrorIntegration {
127132
private virtual_position: IVirtualPosition;
128133
protected cache: ResponseCache;
129134

130-
private debounced_get_hover: Throttler<
131-
Promise<lsProtocol.Hover | undefined | null>
132-
>;
135+
private debounced_get_hover: Throttler<Promise<IOnHoverResponse | null>>;
133136
private tooltip: FreeTooltip;
134137
private _previousHoverRequest: Promise<
135-
Promise<lsProtocol.Hover | undefined | null>
138+
Promise<IOnHoverResponse | null>
136139
> | null = null;
137140

138141
constructor(options: IEditorIntegrationOptions) {
@@ -251,13 +254,10 @@ export class HoverCM extends CodeMirrorIntegration {
251254
}
252255

253256
protected create_throttler() {
254-
return new Throttler<Promise<lsProtocol.Hover | undefined | null>>(
255-
this.on_hover,
256-
{
257-
limit: this.settings.composite.throttlerDelay,
258-
edge: 'trailing'
259-
}
260-
);
257+
return new Throttler<Promise<IOnHoverResponse | null>>(this.on_hover, {
258+
limit: this.settings.composite.throttlerDelay,
259+
edge: 'trailing'
260+
});
261261
}
262262

263263
afterChange(change: IEditorChange, root_position: IRootPosition) {
@@ -268,17 +268,20 @@ export class HoverCM extends CodeMirrorIntegration {
268268
this.remove_range_highlight();
269269
}
270270

271-
protected on_hover = async () => {
271+
protected on_hover = async (
272+
position: IVirtualPosition
273+
): Promise<IOnHoverResponse | null> => {
272274
if (
273275
!(
274276
this.connection.isReady &&
275277
this.connection.serverCapabilities?.hoverProvider
276278
)
277279
) {
278-
return;
280+
return null;
279281
}
280-
let position = this.virtual_position;
281-
return await this.connection.clientRequests['textDocument/hover'].request({
282+
const hover = await this.connection.clientRequests[
283+
'textDocument/hover'
284+
].request({
282285
textDocument: {
283286
// this might be wrong - should not it be using the specific virtual document?
284287
uri: this.virtual_document.document_info.uri
@@ -288,6 +291,15 @@ export class HoverCM extends CodeMirrorIntegration {
288291
character: position.ch
289292
}
290293
});
294+
295+
if (hover == null) {
296+
return null;
297+
}
298+
299+
return {
300+
hover: hover,
301+
position: position
302+
};
291303
};
292304

293305
protected static get_markup_for_hover(
@@ -442,28 +454,32 @@ export class HoverCM extends CodeMirrorIntegration {
442454
let response_data = this.restore_from_cache(document, virtual_position);
443455

444456
if (response_data == null) {
445-
const promise = this.debounced_get_hover.invoke();
457+
const promise = this.debounced_get_hover.invoke(virtual_position);
446458
this._previousHoverRequest = promise;
447459
let response = await promise;
448460
if (this._previousHoverRequest === promise) {
449461
this._previousHoverRequest = null;
450462
}
451-
if (response && this.is_useful_response(response)) {
463+
if (
464+
response &&
465+
is_equal(response.position, virtual_position) &&
466+
this.is_useful_response(response.hover)
467+
) {
452468
let ce_editor =
453469
this.virtual_editor.get_editor_at_root_position(root_position);
454470
let cm_editor =
455471
this.virtual_editor.ce_editor_to_cm_editor.get(ce_editor)!;
456472

457473
let editor_range = this.get_editor_range(
458-
response,
474+
response.hover,
459475
root_position,
460476
token,
461477
cm_editor
462478
);
463479

464480
response_data = {
465481
response: this.add_range_if_needed(
466-
response,
482+
response.hover,
467483
editor_range,
468484
ce_editor
469485
),

0 commit comments

Comments
 (0)