Skip to content

Commit 08b6afb

Browse files
authored
Merge pull request #927 from krassowski/fix-box-selection
Fix conflict with block/rectangular selections on Alt + click
2 parents 14d8c09 + d18d140 commit 08b6afb

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,40 @@ export class CMJumpToDefinition extends CodeMirrorIntegration {
7171
}
7272

7373
register() {
74-
this.editor_handlers.set(
75-
'mousedown',
76-
this._jumpToDefinitionOrRefernce.bind(this)
77-
);
74+
this.editor_handlers.set('mousedown', this._jumpOnMouseUp.bind(this));
7875
super.register();
7976
}
8077

78+
private _jumpOnMouseUp(
79+
virtualEditor: CodeMirrorVirtualEditor,
80+
event: MouseEvent
81+
) {
82+
// For Alt + click we need to wait for mouse up to enable users to create
83+
// rectangular selections with Alt + drag.
84+
if (this.modifierKey === 'Alt') {
85+
document.body.addEventListener(
86+
'mouseup',
87+
(mouseUpEvent: MouseEvent) => {
88+
if (mouseUpEvent.target !== event.target) {
89+
// Cursor moved, possibly block selection was attempted, see:
90+
// https://github.com/jupyter-lsp/jupyterlab-lsp/issues/823
91+
return;
92+
}
93+
return this._jumpToDefinitionOrRefernce(virtualEditor, event);
94+
},
95+
{
96+
once: true
97+
}
98+
);
99+
} else {
100+
// For Ctrl + click we need to act on mouse down to prevent
101+
// adding multiple cursors if jump were to occur.
102+
return this._jumpToDefinitionOrRefernce(virtualEditor, event);
103+
}
104+
}
105+
81106
private _jumpToDefinitionOrRefernce(
82-
virtual_editor: CodeMirrorVirtualEditor,
107+
virtualEditor: CodeMirrorVirtualEditor,
83108
event: MouseEvent
84109
) {
85110
const { button } = event;
@@ -88,24 +113,24 @@ export class CMJumpToDefinition extends CodeMirrorIntegration {
88113
if (!shouldJump) {
89114
return;
90115
}
91-
let root_position = this.position_from_mouse(event);
92-
if (root_position == null) {
116+
let rootPosition = this.position_from_mouse(event);
117+
if (rootPosition == null) {
93118
this.console.warn(
94119
'Could not retrieve root position from mouse event to jump to definition/reference'
95120
);
96121
return;
97122
}
98-
let document = virtual_editor.document_at_root_position(root_position);
99-
let virtual_position =
100-
virtual_editor.root_position_to_virtual_position(root_position);
123+
let document = virtualEditor.document_at_root_position(rootPosition);
124+
let virtualPosition =
125+
virtualEditor.root_position_to_virtual_position(rootPosition);
101126

102127
const positionParams: lsp.TextDocumentPositionParams = {
103128
textDocument: {
104129
uri: document.document_info.uri
105130
},
106131
position: {
107-
line: virtual_position.line,
108-
character: virtual_position.ch
132+
line: virtualPosition.line,
133+
character: virtualPosition.ch
109134
}
110135
};
111136

@@ -239,19 +264,19 @@ export class CMJumpToDefinition extends CodeMirrorIntegration {
239264

240265
let { uri, range } = targetInfo;
241266

242-
let virtual_position = PositionConverter.lsp_to_cm(
267+
let virtualPosition = PositionConverter.lsp_to_cm(
243268
range.start
244269
) as IVirtualPosition;
245270

246271
if (uris_equal(uri, positionParams.textDocument.uri)) {
247-
let editor_index = this.adapter.get_editor_index_at(virtual_position);
272+
let editor_index = this.adapter.get_editor_index_at(virtualPosition);
248273
// if in current file, transform from the position within virtual document to the editor position:
249274
let editor_position =
250-
this.virtual_editor.transform_virtual_to_editor(virtual_position);
275+
this.virtual_editor.transform_virtual_to_editor(virtualPosition);
251276
if (editor_position === null) {
252277
this.console.warn(
253278
'Could not jump: conversion from virtual position to editor position failed',
254-
virtual_position
279+
virtualPosition
255280
);
256281
return JumpResult.PositioningFailure;
257282
}
@@ -281,7 +306,7 @@ export class CMJumpToDefinition extends CodeMirrorIntegration {
281306
return JumpResult.AssumeSuccess;
282307
} else {
283308
// otherwise there is no virtual document and we expect the returned position to be source position:
284-
let source_position_ce = PositionConverter.cm_to_ce(virtual_position);
309+
let source_position_ce = PositionConverter.cm_to_ce(virtualPosition);
285310
this.console.log(`Jumping to external file: ${uri}`);
286311
this.console.log('Jump target (source location):', source_position_ce);
287312

@@ -344,7 +369,7 @@ class JumperLabIntegration implements IFeatureLabIntegration {
344369
this.jumpers = new Map();
345370

346371
if (fileEditorTracker !== null) {
347-
fileEditorTracker.widgetAdded.connect((sender, widget) => {
372+
fileEditorTracker.widgetAdded.connect((_, widget) => {
348373
let fileEditor = widget.content;
349374

350375
if (fileEditor.editor instanceof CodeMirrorEditor) {
@@ -354,7 +379,7 @@ class JumperLabIntegration implements IFeatureLabIntegration {
354379
});
355380
}
356381

357-
notebookTracker.widgetAdded.connect(async (sender, widget) => {
382+
notebookTracker.widgetAdded.connect(async (_, widget) => {
358383
// NOTE: assuming that the default cells content factory produces CodeMirror editors(!)
359384
let jumper = new NotebookJumper(widget, documentManager);
360385
this.jumpers.set(widget.id, jumper);
@@ -432,7 +457,7 @@ const COMMANDS = (trans: TranslationBundle): IFeatureCommand[] => [
432457
},
433458
{
434459
id: 'jump-back',
435-
execute: async ({ connection, virtual_position, document, features }) => {
460+
execute: async ({ features }) => {
436461
const jump_feature = features.get(FEATURE_ID) as CMJumpToDefinition;
437462
jump_feature.jumper.global_jump_back();
438463
},

0 commit comments

Comments
 (0)