Skip to content

Commit 84b4db2

Browse files
committed
Erratum to #206: add more tests, fix virtual_shift usage
1 parent 0309429 commit 84b4db2

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

packages/jupyterlab-lsp/src/virtual/document.spec.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,27 @@ describe('VirtualDocument', () => {
8787
let cm_editor_for_cell_2 = {} as CodeMirror.Editor;
8888
let cm_editor_for_cell_3 = {} as CodeMirror.Editor;
8989
let cm_editor_for_cell_4 = {} as CodeMirror.Editor;
90+
// first block
9091
document.append_code_block(
9192
'test line in Python 1\n%R 1st test line in R line magic 1',
9293
cm_editor_for_cell_1
9394
);
95+
// second block
9496
document.append_code_block(
9597
'test line in Python 2\n%R 1st test line in R line magic 2',
9698
cm_editor_for_cell_2
9799
);
100+
// third block
101+
document.append_code_block(
102+
'test line in Python 3\n%R -i imported_variable 1st test line in R line magic 3',
103+
cm_editor_for_cell_2
104+
);
105+
// fourth block
98106
document.append_code_block(
99107
'%%R\n1st test line in R cell magic 1',
100108
cm_editor_for_cell_3
101109
);
110+
// fifth block
102111
document.append_code_block(
103112
'%%R -i imported_variable\n1st test line in R cell magic 2',
104113
cm_editor_for_cell_4
@@ -135,11 +144,15 @@ describe('VirtualDocument', () => {
135144
expect(foreign_document.value).to.equal(
136145
'1st test line in R line magic 1\n\n\n' +
137146
'1st test line in R line magic 2\n\n\n' +
147+
'imported_variable <- data.frame(); 1st test line in R line magic 3\n\n\n' +
148+
// 23456789012345678901234567890123456 - 's' is 36th
138149
'1st test line in R cell magic 1\n\n\n' +
139150
'imported_variable <- data.frame(); 1st test line in R cell magic 2\n'
151+
// 0123456789012345678901234567890123456 - 's' is 36th
140152
);
141153

142-
// The second (R) line in the first block ("s" in "1st", "1st" in "1st test line in R line magic")
154+
// The first R line (in source); second in the first block;
155+
// targeting "s" in "1st", "1st" in "1st test line in R line magic" (first virtual line == line 0)
143156
let virtual_r_1_1 = {
144157
line: 0,
145158
ch: 1
@@ -157,13 +170,41 @@ describe('VirtualDocument', () => {
157170
expect(editor_position.line).to.equal(1);
158171
expect(editor_position.ch).to.equal(4);
159172

160-
// The second (R) line in the second block
173+
// The second R line (in source), second in the second block
174+
// targeting 1 in "1st test line in R line magic 2" (4th virtual line == line 3)
161175
editor_position = foreign_document.transform_virtual_to_editor({
162176
line: 3,
163177
ch: 0
164178
} as IVirtualPosition);
179+
// 0th editor line is 'test line in Python 2\n'
165180
expect(editor_position.line).to.equal(1);
181+
// 1st editor lines is '%R 1st test line in R line magic 2'
182+
// 0123 - 3rd character
166183
expect(editor_position.ch).to.equal(3);
184+
185+
// The third R line (in source), second in the third block;
186+
// targeting "s" in "1st" in "1st test line in R line magic 3" (7th virtual line == line 6)
187+
editor_position = foreign_document.transform_virtual_to_editor({
188+
line: 6,
189+
ch: 36
190+
} as IVirtualPosition);
191+
// 0th editor line is 'test line in Python 3\n'
192+
expect(editor_position.line).to.equal(1);
193+
// 1st editor line is '%R -i imported_variable 1st test line in R line magic 3'
194+
// 01234567890123456789012345 - 25th character
195+
expect(editor_position.ch).to.equal(25);
196+
197+
// The fifth R line (in source), second in the fifth block;
198+
// targeting "s" in "1st" in "1st test line in R cell magic 2" (13th virtual lines == line 12)
199+
editor_position = foreign_document.transform_virtual_to_editor({
200+
line: 12,
201+
ch: 36
202+
} as IVirtualPosition);
203+
// 0th editor line is '%%R -i imported_variable\n'
204+
expect(editor_position.line).to.equal(1);
205+
// 1st editor line is '1st test line in R cell magic 2'
206+
// 01
207+
expect(editor_position.ch).to.equal(1);
167208
});
168209
});
169210
});

packages/jupyterlab-lsp/src/virtual/document.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,9 @@ export class VirtualDocument {
573573
this.source_lines.set(this.last_source_line + i, {
574574
editor_line: i,
575575
editor_shift: {
576-
line: editor_shift.line,
577-
column: i === 0 ? editor_shift.column : 0
576+
line: editor_shift.line - (virtual_shift?.line || 0),
577+
column:
578+
i === 0 ? editor_shift.column - (virtual_shift?.column || 0) : 0
578579
},
579580
// TODO: move those to a new abstraction layer (DocumentBlock class)
580581
editor: cm_editor,
@@ -683,7 +684,7 @@ export class VirtualDocument {
683684
let editor_line = source_line.editor_line;
684685
let editor_shift = source_line.editor_shift;
685686
return {
686-
// only shift column in the first line
687+
// only shift column in the line beginning the virtual document (first list of the editor in cell magics, but might be any line of editor in line magics!)
687688
ch: pos.ch + (editor_line === 0 ? editor_shift.column : 0),
688689
line: editor_line + editor_shift.line
689690
// TODO or:

0 commit comments

Comments
 (0)