Skip to content

Commit 8ae254b

Browse files
committed
Solve cache collisions, clean up state when removing range highlight
1 parent 35e0530 commit 8ae254b

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

atest/05_Features/Hover.robot

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Hover works in foreign code (javascript)
2626
Element Should Contain ${HOVER_BOX} function js_add(a: any, b: any): any
2727
Page Should Contain Element ${HOVER_BOX} code.language-typescript
2828
# also for multiple cells of the same document
29+
Enter Cell Editor 3
2930
Hover Over Math
3031
Element Should Contain ${HOVER_BOX} const Math: Math
3132

@@ -38,9 +39,10 @@ Hover Over
3839
Trigger Tooltip
3940
[Arguments] ${sel}
4041
Wait Until Keyword Succeeds 10 x 0.1 s Mouse Over ${sel}
42+
Click Element ${sel}
4143
Wait Until Page Contains Element ${HOVER_SIGNAL}
42-
Press Keys None CTRL
43-
Wait Until Keyword Succeeds 20x 0.5s Page Should Contain Element ${HOVER_BOX}
44+
Press Keys ${sel} CTRL
45+
Wait Until Keyword Succeeds 10x 0.1s Page Should Contain Element ${HOVER_BOX}
4446

4547
Setup Hover Test
4648
Setup Notebook Python Hover.ipynb

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getModifierState, uris_equal } from '../utils';
1+
import { getModifierState } from '../utils';
22
import { IRootPosition, is_equal, IVirtualPosition } from '../positioning';
33
import * as lsProtocol from 'vscode-languageserver-protocol';
44
import * as CodeMirror from 'codemirror';
@@ -23,6 +23,7 @@ import hoverSvg from '../../style/icons/hover.svg';
2323
import { IEditorChange } from '../virtual/editor';
2424
import { CodeEditor } from '@jupyterlab/codeeditor';
2525
import { PositionConverter } from '../converter';
26+
import { VirtualDocument } from '../virtual/document';
2627

2728
export const hoverIcon = new LabIcon({
2829
name: 'lsp:hover',
@@ -31,7 +32,7 @@ export const hoverIcon = new LabIcon({
3132

3233
interface IResponseData {
3334
response: lsProtocol.Hover;
34-
uri: string;
35+
document: VirtualDocument;
3536
editor_range: IEditorRange;
3637
ce_editor: CodeEditor.IEditor;
3738
}
@@ -91,14 +92,12 @@ export class HoverCM extends CodeMirrorIntegration {
9192
}
9293

9394
protected restore_from_cache(
94-
root_position: IRootPosition
95+
root_position: IRootPosition,
96+
document: VirtualDocument,
97+
virtual_position: IVirtualPosition
9598
): IResponseData | null {
96-
const virtual_position = this.virtual_editor.root_position_to_virtual_position(
97-
root_position
98-
);
99-
const document_uri = this.virtual_document.document_info.uri;
10099
const matching_items = this.cache.data.filter(cache_item => {
101-
if (!uris_equal(cache_item.uri, document_uri)) {
100+
if (cache_item.document !== document) {
102101
return false;
103102
}
104103
let range = cache_item.response.range;
@@ -123,7 +122,17 @@ export class HoverCM extends CodeMirrorIntegration {
123122
getModifierState(event, this.modifierKey) &&
124123
typeof this.last_hover_character !== 'undefined'
125124
) {
126-
let response_data = this.restore_from_cache(this.last_hover_character);
125+
const document = this.virtual_editor.document_at_root_position(
126+
this.last_hover_character
127+
);
128+
const virtual_position = this.virtual_editor.root_position_to_virtual_position(
129+
this.last_hover_character
130+
);
131+
let response_data = this.restore_from_cache(
132+
this.last_hover_character,
133+
document,
134+
virtual_position
135+
);
127136
if (response_data == null) {
128137
return;
129138
}
@@ -157,6 +166,7 @@ export class HoverCM extends CodeMirrorIntegration {
157166
protected on_hover = async () => {
158167
return await this.connection.getHoverTooltip(
159168
this.virtual_position,
169+
// this might be wrong - should not it be using the specific virtual document?
160170
this.virtual_document.document_info,
161171
false
162172
);
@@ -281,7 +291,11 @@ export class HoverCM extends CodeMirrorIntegration {
281291
this.virtual_position = virtual_position;
282292
this.last_hover_character = root_position;
283293

284-
let response_data = this.restore_from_cache(root_position);
294+
let response_data = this.restore_from_cache(
295+
root_position,
296+
document,
297+
virtual_position
298+
);
285299

286300
if (response_data == null) {
287301
let response = await this.debounced_get_hover.invoke();
@@ -306,7 +320,7 @@ export class HoverCM extends CodeMirrorIntegration {
306320
editor_range,
307321
ce_editor
308322
),
309-
uri: this.virtual_document.document_info.uri,
323+
document: document,
310324
editor_range: editor_range,
311325
ce_editor: ce_editor
312326
};
@@ -341,6 +355,8 @@ export class HoverCM extends CodeMirrorIntegration {
341355
if (this.hover_marker) {
342356
this.hover_marker.clear();
343357
this.hover_marker = null;
358+
this.last_hover_response = undefined;
359+
this.last_hover_character = undefined;
344360
}
345361
};
346362

0 commit comments

Comments
 (0)