Skip to content

Commit 324a59f

Browse files
committed
Add highlights test and fix #431
1 parent fcdb20a commit 324a59f

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

atest/05_Features/Highlights.robot

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
*** Settings ***
2+
Suite Setup Setup Suite For Screenshots highlights
3+
Test Setup Setup Highlights Test
4+
Test Teardown Clean Up After Working With File Highlights.ipynb
5+
Force Tags feature:highlights
6+
Resource ../Keywords.robot
7+
8+
*** Test Cases ***
9+
# cursor is symbolized by pipe (|), for example when
10+
# it is at the end of line, after `1` in `test = 1`
11+
# it is presented as: `test = 1|`
12+
Highlights work at the start of a token
13+
Enter Cell Editor 1 line=1
14+
Press Keys None END # cursor to the end of first line (`test = 1|`)
15+
Should Not Highlight Any Tokens
16+
Press Keys None HOME # cursor before the token (`|test = 1`)
17+
Should Highlight Token test
18+
Should Not Highlight Token gist
19+
20+
Highlights work at the end of a token
21+
Enter Cell Editor 1 line=1
22+
Press Keys None END # cursor to the end of first line (`test = 1|`)
23+
Press Keys None DOWN # cursor to the end of the token in second line (`test`)
24+
Should Highlight Token test
25+
Should Not Highlight Token gist
26+
27+
Highlights are changed when moving cursor between cells
28+
[Documentation] GH431
29+
Enter Cell Editor 1 line=2
30+
Press Keys None END # cursor after the token in second line (`test|`)
31+
Should Highlight Token test
32+
Should Not Highlight Token gist
33+
Press Keys None DOWN # cursor to next cell, which is empty
34+
Should Not Highlight Any Tokens
35+
Press Keys None DOWN # cursor to third cell (`|gist = 1`)
36+
Should Highlight Token gist
37+
Press Keys None DOWN # cursor to third cell, second line (`|test `)
38+
Should Highlight Token test
39+
40+
Highlights are added after typing
41+
Enter Cell Editor 1 line=2
42+
Should Highlight Token test
43+
Press Keys None a
44+
Should Highlight Token testa
45+
46+
*** Keywords ***
47+
Should Not Highlight Any Tokens
48+
Page Should Not Contain css:.cm-lsp-highlight
49+
50+
Should Highlight Token
51+
[Arguments] ${token} ${timeout}=10s
52+
${token_element} Set Variable xpath://span[contains(@class, 'cm-lsp-highlight')][contains(text(), '${token}')]
53+
Wait Until Page Contains Element ${token_element} timeout=${timeout}
54+
55+
Should Not Highlight Token
56+
[Arguments] ${token} ${timeout}=10s
57+
${token_element} Set Variable xpath://span[contains(@class, 'cm-lsp-highlight')][contains(text(), '${token}')]
58+
Wait Until Page Does Not Contain Element ${token_element} timeout=${timeout}
59+
60+
Setup Highlights Test
61+
Setup Notebook Python Highlights.ipynb

atest/examples/Highlights.ipynb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"test = 1\n",
10+
"test"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": []
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"gist = 1\n",
27+
"test\n",
28+
"gist\n",
29+
"test"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.7.5"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 4
54+
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export class HighlightsCM extends CodeMirrorIntegration {
6565
this.debounced_get_highlight = this.create_debouncer();
6666
});
6767
this.editor_handlers.set('cursorActivity', this.onCursorActivity);
68+
this.editor_handlers.set('blur', this.onCursorActivity);
69+
this.editor_handlers.set('focus', this.onCursorActivity);
6870
super.register();
6971
}
7072

@@ -136,9 +138,17 @@ export class HighlightsCM extends CodeMirrorIntegration {
136138

137139
const token = this.virtual_editor.get_token_at(root_position);
138140

139-
// if token has not changed, no need to update highlight
140-
if (this.last_token && token.value === this.last_token.value) {
141-
console.log('LSP: not requesting highlights (token did not change)');
141+
// if token has not changed, no need to update highlight, unless it is an empty token
142+
// which would indicate that the cursor is at the first character
143+
if (
144+
this.last_token &&
145+
token.value === this.last_token.value &&
146+
token.value !== ''
147+
) {
148+
console.log(
149+
'LSP: not requesting highlights (token did not change)',
150+
token
151+
);
142152
return;
143153
}
144154

0 commit comments

Comments
 (0)