Skip to content

Commit

Permalink
Fix #80667
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Sep 15, 2019
1 parent d378003 commit 57f4855
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/vs/workbench/services/search/common/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ export class TextSearchMatch implements ITextSearchMatch {
constructor(text: string, range: ISearchRange | ISearchRange[], previewOptions?: ITextSearchPreviewOptions) {
this.ranges = range;

if (previewOptions && previewOptions.matchLines === 1 && (!Array.isArray(range) || range.length === 1)) {
// Trim preview if this is one match and a single-line match with a preview requested.
// Otherwise send the full text, like for replace or for showing multiple previews.
// TODO this is fishy.
if (previewOptions && previewOptions.matchLines === 1 && (!Array.isArray(range) || range.length === 1) && isSingleLineRange(range)) {
const oneRange = Array.isArray(range) ? range[0] : range;

// 1 line preview requested
Expand All @@ -273,7 +276,6 @@ export class TextSearchMatch implements ITextSearchMatch {
} else {
const firstMatchLine = Array.isArray(range) ? range[0].startLineNumber : range.startLineNumber;

// n line, no preview requested, or multiple matches in the preview
this.preview = {
text,
matches: mapArrayOrNot(range, r => new SearchRange(r.startLineNumber - firstMatchLine, r.startColumn, r.endLineNumber - firstMatchLine, r.endColumn))
Expand All @@ -282,6 +284,12 @@ export class TextSearchMatch implements ITextSearchMatch {
}
}

function isSingleLineRange(range: ISearchRange | ISearchRange[]): boolean {
return Array.isArray(range) ?
range[0].startLineNumber === range[0].endLineNumber :
range.startLineNumber === range.endLineNumber;
}

export class SearchRange implements ISearchRange {
startLineNumber: number;
startColumn: number;
Expand Down
26 changes: 15 additions & 11 deletions src/vs/workbench/services/search/test/common/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ suite('TextSearchResult', () => {
charsPerLine: 100
};

function assertPreviewRangeText(text: string, result: TextSearchMatch): void {
function assertOneLinePreviewRangeText(text: string, result: TextSearchMatch): void {
assert.equal(
result.preview.text.substring((<SearchRange>result.preview.matches).startColumn, (<SearchRange>result.preview.matches).endColumn),
text);
Expand All @@ -22,49 +22,49 @@ suite('TextSearchResult', () => {
const range = new OneLineRange(5, 0, 0);
const result = new TextSearchMatch('', range);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('', result);
assertOneLinePreviewRangeText('', result);
});

test('empty with preview options', () => {
const range = new OneLineRange(5, 0, 0);
const result = new TextSearchMatch('', range, previewOptions1);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('', result);
assertOneLinePreviewRangeText('', result);
});

test('short without preview options', () => {
const range = new OneLineRange(5, 4, 7);
const result = new TextSearchMatch('foo bar', range);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('bar', result);
assertOneLinePreviewRangeText('bar', result);
});

test('short with preview options', () => {
const range = new OneLineRange(5, 4, 7);
const result = new TextSearchMatch('foo bar', range, previewOptions1);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('bar', result);
assertOneLinePreviewRangeText('bar', result);
});

test('leading', () => {
const range = new OneLineRange(5, 25, 28);
const result = new TextSearchMatch('long text very long text foo', range, previewOptions1);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('foo', result);
assertOneLinePreviewRangeText('foo', result);
});

test('trailing', () => {
const range = new OneLineRange(5, 0, 3);
const result = new TextSearchMatch('foo long text very long text long text very long text long text very long text long text very long text long text very long text', range, previewOptions1);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('foo', result);
assertOneLinePreviewRangeText('foo', result);
});

test('middle', () => {
const range = new OneLineRange(5, 30, 33);
const result = new TextSearchMatch('long text very long text long foo text very long text long text very long text long text very long text long text very long text', range, previewOptions1);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('foo', result);
assertOneLinePreviewRangeText('foo', result);
});

test('truncating match', () => {
Expand All @@ -76,7 +76,7 @@ suite('TextSearchResult', () => {
const range = new OneLineRange(0, 4, 7);
const result = new TextSearchMatch('foo bar', range, previewOptions);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('b', result);
assertOneLinePreviewRangeText('b', result);
});

test('one line of multiline match', () => {
Expand All @@ -88,7 +88,11 @@ suite('TextSearchResult', () => {
const range = new SearchRange(5, 4, 6, 3);
const result = new TextSearchMatch('foo bar\nfoo bar', range, previewOptions);
assert.deepEqual(result.ranges, range);
assertPreviewRangeText('bar', result);
assert.equal(result.preview.text, 'foo bar\nfoo bar');
assert.equal((<SearchRange>result.preview.matches).startLineNumber, 0);
assert.equal((<SearchRange>result.preview.matches).startColumn, 4);
assert.equal((<SearchRange>result.preview.matches).endLineNumber, 1);
assert.equal((<SearchRange>result.preview.matches).endColumn, 3);
});

// test('all lines of multiline match', () => {
Expand All @@ -102,4 +106,4 @@ suite('TextSearchResult', () => {
// assert.deepEqual(result.range, range);
// assertPreviewRangeText('bar\nfoo', result);
// });
});
});

0 comments on commit 57f4855

Please sign in to comment.