Skip to content

Commit

Permalink
Fixes #33621: Added check for intersection to SelectionHighlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 committed May 7, 2018
1 parent 9624859 commit 8f0d370
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/vs/editor/common/core/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ export class Range {
return true;
}

/**
* Test if the two ranges are intersecting. If the ranges are touching it returns true.
*/
public static areIntersecting(a: IRange, b: IRange): boolean {
// Check if `a` is before `b`
if (a.endLineNumber < b.startLineNumber || (a.endLineNumber === b.startLineNumber && a.endColumn <= b.startColumn)) {
return false;
}

// Check if `b` is before `a`
if (b.endLineNumber < a.startLineNumber || (b.endLineNumber === a.startLineNumber && b.endColumn <= a.startColumn)) {
return false;
}

// These ranges must intersect
return true;
}

/**
* A function that compares ranges, useful for sorting ranges
* It will first compare ranges on the startPosition and then on the endPosition
Expand Down
4 changes: 3 additions & 1 deletion src/vs/editor/contrib/multicursor/multicursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,9 @@ export class SelectionHighlighter extends Disposable implements IEditorContribut
const cmp = Range.compareRangesUsingStarts(match, selections[j]);
if (cmp < 0) {
// match is before sel
matches.push(match);
if (!Range.areIntersecting(match, selections[j])) {
matches.push(match);
}
i++;
} else if (cmp > 0) {
// sel is before match
Expand Down
10 changes: 10 additions & 0 deletions src/vs/editor/test/common/core/range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,14 @@ suite('Editor Core - Range', () => {
assert.equal(new Range(2, 2, 5, 10).containsRange(new Range(2, 3, 5, 9)), true);
assert.equal(new Range(2, 2, 5, 10).containsRange(new Range(3, 100, 4, 100)), true);
});

test('areIntersecting', () => {
assert.equal(Range.areIntersecting(new Range(2, 2, 3, 2), new Range(4, 2, 5, 2)), false);
assert.equal(Range.areIntersecting(new Range(4, 2, 5, 2), new Range(2, 2, 3, 2)), false);
assert.equal(Range.areIntersecting(new Range(4, 2, 5, 2), new Range(5, 2, 6, 2)), false);
assert.equal(Range.areIntersecting(new Range(5, 2, 6, 2), new Range(4, 2, 5, 2)), false);
assert.equal(Range.areIntersecting(new Range(2, 2, 2, 7), new Range(2, 4, 2, 6)), true);
assert.equal(Range.areIntersecting(new Range(2, 2, 2, 7), new Range(2, 4, 2, 9)), true);
assert.equal(Range.areIntersecting(new Range(2, 4, 2, 9), new Range(2, 2, 2, 7)), true);
});
});
4 changes: 4 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ declare namespace monaco {
* Test if the two ranges are touching in any way.
*/
static areIntersectingOrTouching(a: IRange, b: IRange): boolean;
/**
* Test if the two ranges are intersecting. If the ranges are touching it returns true.
*/
static areIntersecting(a: IRange, b: IRange): boolean;
/**
* A function that compares ranges, useful for sorting ranges
* It will first compare ranges on the startPosition and then on the endPosition
Expand Down

0 comments on commit 8f0d370

Please sign in to comment.