From 450d6b12fa03fe99805848b57e64b7113a7a9396 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 9 Jan 2018 16:47:26 +0100 Subject: [PATCH] Don't add fully-contained selections above/below This is slower than it needs to be and creates behavioral problems when selections get merged in some cases. Signed-off-by: Nathan Sobo --- spec/text-editor-spec.js | 26 ++++++++++++++++++++++++++ src/selection.js | 16 ++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/spec/text-editor-spec.js b/spec/text-editor-spec.js index 328b7e8c4..a9aa80cd1 100644 --- a/spec/text-editor-spec.js +++ b/spec/text-editor-spec.js @@ -2376,6 +2376,19 @@ describe('TextEditor', () => { ]) }) }) + + it('does not create a new selection if it would be fully contained within another selection', () => { + editor.setText('abc\ndef\nghi\njkl\nmno') + editor.setCursorBufferPosition([0, 1]) + + let addedSelectionCount = 0 + editor.onDidAddSelection(() => { addedSelectionCount++ }) + + editor.addSelectionBelow() + editor.addSelectionBelow() + editor.addSelectionBelow() + expect(addedSelectionCount).toBe(3) + }) }) describe('.addSelectionAbove()', () => { @@ -2498,6 +2511,19 @@ describe('TextEditor', () => { ]) }) }) + + it('does not create a new selection if it would be fully contained within another selection', () => { + editor.setText('abc\ndef\nghi\njkl\nmno') + editor.setCursorBufferPosition([4, 1]) + + let addedSelectionCount = 0 + editor.onDidAddSelection(() => { addedSelectionCount++ }) + + editor.addSelectionAbove() + editor.addSelectionAbove() + editor.addSelectionAbove() + expect(addedSelectionCount).toBe(3) + }) }) describe('.splitSelectionsIntoLines()', () => { diff --git a/src/selection.js b/src/selection.js index a15f6dcbd..2c64fa126 100644 --- a/src/selection.js +++ b/src/selection.js @@ -832,8 +832,12 @@ class Selection { if (clippedRange.isEmpty()) continue } - const selection = this.editor.addSelectionForScreenRange(clippedRange) - selection.setGoalScreenRange(range) + const containingSelections = this.editor.selectionsMarkerLayer.findMarkers({containsScreenRange: clippedRange}) + if (containingSelections.length === 0) { + const selection = this.editor.addSelectionForScreenRange(clippedRange) + selection.setGoalScreenRange(range) + } + break } } @@ -854,8 +858,12 @@ class Selection { if (clippedRange.isEmpty()) continue } - const selection = this.editor.addSelectionForScreenRange(clippedRange) - selection.setGoalScreenRange(range) + const containingSelections = this.editor.selectionsMarkerLayer.findMarkers({containsScreenRange: clippedRange}) + if (containingSelections.length === 0) { + const selection = this.editor.addSelectionForScreenRange(clippedRange) + selection.setGoalScreenRange(range) + } + break } }