Skip to content

Commit 69e094a

Browse files
committed
Fix diagnostics scrolling/jumping issue
1 parent 8009a58 commit 69e094a

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class DiagnosticsPanel {
231231
console.warn('LPS: diagnostics row not found for jump execute()');
232232
return;
233233
}
234-
this.widget.content.jumpTo(row);
234+
return this.widget.content.jumpTo(row);
235235
},
236236
label: this.trans.__('Jump to location'),
237237
icon: jumpToIcon

packages/jupyterlab-lsp/src/features/diagnostics/feature.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
5757
protected settings: IFeatureSettings<LSPDiagnosticsSettings>;
5858
protected console = new BrowserConsole().scope('Diagnostics');
5959
private _responseReceived: PromiseDelegate<void> = new PromiseDelegate();
60+
private _firstResponseReceived: PromiseDelegate<void> = new PromiseDelegate();
6061
private _diagnosticsDatabases = new WeakMap<
6162
WidgetLSPAdapter<any>,
6263
DiagnosticsDatabase
@@ -96,6 +97,7 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
9697
return value;
9798
}
9899
});
100+
99101
const connectionManager = options.connectionManager;
100102
// https://github.com/jupyterlab/jupyterlab/issues/14783
101103
options.shell.currentChanged.connect(shell => {
@@ -143,14 +145,30 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
143145
// few seconds to trigger timeout). Because `response.version` is
144146
// optional it would require further testing.
145147

146-
await adapter.updateFinished;
147-
await this._responseReceived.promise;
148+
if (view.state.field(this._invalidationCounter) == 0) {
149+
// If we are displaying the editor for the first time,
150+
// e.g. after scrolling down in windowed notebook,
151+
// do not wait for next update, show what we already know.
152+
153+
// TODO: this still fails when scrolling down fast and then
154+
// scrolling up to the skipped cells because the state invlidation
155+
// counter kicks in but diagnostics does not get rendered yet before
156+
// we leave..
157+
await this._firstResponseReceived.promise;
158+
} else {
159+
await adapter.updateFinished;
160+
await this._responseReceived.promise;
161+
}
148162

149163
const database = this.getDiagnosticsDB(adapter);
150164

151165
for (const editorDiagnostics of database.values()) {
152166
for (const editorDiagnostic of editorDiagnostics) {
153-
if (editorDiagnostic.editor.editor !== view) {
167+
const editor = editorDiagnostic.editorAccessor.getEditor() as
168+
| CodeMirrorEditor
169+
| undefined;
170+
171+
if (editor?.editor !== view) {
154172
continue;
155173
}
156174
const diagnostic = editorDiagnostic.diagnostic;
@@ -165,7 +183,7 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
165183
editorDiagnostic.range.end
166184
);
167185

168-
const from = editorDiagnostic.editor.getOffsetAt(
186+
const from = editor.getOffsetAt(
169187
start.line >= lines
170188
? {
171189
line: Math.min(start.line, lines),
@@ -174,7 +192,7 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
174192
: start
175193
);
176194
// TODO: this is wrong; there is however an issue if this is not applied
177-
const to = editorDiagnostic.editor.getOffsetAt(
195+
const to = editor.getOffsetAt(
178196
end.line >= lines
179197
? {
180198
line: Math.min(end.line, lines),
@@ -451,7 +469,6 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
451469
}
452470

453471
const editorAccessor = document.getEditorAtVirtualLine(start);
454-
const editor = editorAccessor.getEditor()!;
455472

456473
const startInEditor = document.transformVirtualToEditor(start);
457474
let endInEditor: IEditorPosition | null;
@@ -483,7 +500,7 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
483500
for (let diagnostic of diagnostics) {
484501
diagnosticsList.push({
485502
diagnostic,
486-
editor: editor as CodeMirrorEditor,
503+
editorAccessor: editorAccessor,
487504
range: {
488505
start: startInEditor,
489506
end: endInEditor
@@ -497,10 +514,12 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
497514

498515
const previousList = diagnosticsDB.get(document);
499516
const editorsWhichHadDiagnostics = new Set(
500-
previousList?.map(d => d.editor)
517+
previousList?.map(d => d.editorAccessor.getEditor())
501518
);
502519

503-
const editorsWithDiagnostics = new Set(diagnosticsList?.map(d => d.editor));
520+
const editorsWithDiagnostics = new Set(
521+
diagnosticsList?.map(d => d.editorAccessor.getEditor())
522+
);
504523
diagnosticsDB.set(document, diagnosticsList);
505524

506525
// Refresh editors with diagnostics; this is needed because linter's
@@ -548,6 +567,7 @@ export class DiagnosticsFeature extends Feature implements IDiagnosticsFeature {
548567
this.setDiagnostics(response, document, adapter);
549568
const done = new Promise<void>(resolve => {
550569
setTimeout(() => {
570+
this._firstResponseReceived.resolve();
551571
this._responseReceived.resolve();
552572
this._responseReceived = new PromiseDelegate();
553573
resolve();

packages/jupyterlab-lsp/src/features/diagnostics/listing.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export class DiagnosticsListing extends VDomRenderer<DiagnosticsListing.Model> {
286286
let cellNumber: number | null = null;
287287
if (adapter.hasMultipleEditors) {
288288
const cellIndex = adapter.editors.findIndex(
289-
value => value.ceEditor.getEditor() == diagnosticData.editor
289+
value => value.ceEditor == diagnosticData.editorAccessor
290290
);
291291
cellNumber = cellIndex + 1;
292292
}
@@ -330,7 +330,7 @@ export class DiagnosticsListing extends VDomRenderer<DiagnosticsListing.Model> {
330330
key={row.key}
331331
data-key={row.key}
332332
onClick={() => {
333-
this.jumpTo(row);
333+
return this.jumpTo(row);
334334
}}
335335
>
336336
{cells}
@@ -360,12 +360,10 @@ export class DiagnosticsListing extends VDomRenderer<DiagnosticsListing.Model> {
360360
return this._diagnostics.get(key);
361361
}
362362

363-
jumpTo(row: IDiagnosticsRow) {
364-
const cmEditor = row.data.editor;
365-
cmEditor.setCursorPosition(
366-
PositionConverter.cm_to_ce(row.data.range.start)
367-
);
368-
cmEditor.focus();
363+
async jumpTo(row: IDiagnosticsRow): Promise<void> {
364+
const editor = await row.data.editorAccessor.reveal();
365+
editor.setCursorPosition(PositionConverter.cm_to_ce(row.data.range.start));
366+
editor.focus();
369367
}
370368
}
371369

packages/jupyterlab-lsp/src/features/diagnostics/tokens.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { CodeMirrorEditor } from '@jupyterlab/codemirror';
2-
import { IEditorPosition, WidgetLSPAdapter } from '@jupyterlab/lsp';
1+
import { IEditorPosition, WidgetLSPAdapter, Document } from '@jupyterlab/lsp';
32
import { Token } from '@lumino/coreutils';
43
import * as lsProtocol from 'vscode-languageserver-protocol';
54

@@ -11,7 +10,7 @@ import { PLUGIN_ID } from '../../tokens';
1110
*/
1211
export interface IEditorDiagnostic {
1312
diagnostic: lsProtocol.Diagnostic;
14-
editor: CodeMirrorEditor;
13+
editorAccessor: Document.IEditor;
1514
range: {
1615
start: IEditorPosition;
1716
end: IEditorPosition;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function guessFailureReason(
170170
let start = diagnostic.range.start;
171171
if (adapter.hasMultipleEditors) {
172172
let editorIndex = adapter.editors.findIndex(
173-
e => e.ceEditor.getEditor() === diagnostic.editor
173+
e => e.ceEditor === diagnostic.editorAccessor
174174
);
175175
let cellNumber = editorIndex === -1 ? '(?)' : editorIndex + 1;
176176
return trans.__(

0 commit comments

Comments
 (0)