From 57f485539ce2aa53c9f60e60bfdb47ae93f5d5f1 Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Sun, 15 Sep 2019 14:24:06 -0700 Subject: [PATCH] Fix #80667 --- .../services/search/common/search.ts | 12 +++++++-- .../search/test/common/search.test.ts | 26 +++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/vs/workbench/services/search/common/search.ts b/src/vs/workbench/services/search/common/search.ts index f66e10720cbe5..9c27a27edd032 100644 --- a/src/vs/workbench/services/search/common/search.ts +++ b/src/vs/workbench/services/search/common/search.ts @@ -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 @@ -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)) @@ -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; diff --git a/src/vs/workbench/services/search/test/common/search.test.ts b/src/vs/workbench/services/search/test/common/search.test.ts index 4e32035be54d7..94088dcb3418e 100644 --- a/src/vs/workbench/services/search/test/common/search.test.ts +++ b/src/vs/workbench/services/search/test/common/search.test.ts @@ -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((result.preview.matches).startColumn, (result.preview.matches).endColumn), text); @@ -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', () => { @@ -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', () => { @@ -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((result.preview.matches).startLineNumber, 0); + assert.equal((result.preview.matches).startColumn, 4); + assert.equal((result.preview.matches).endLineNumber, 1); + assert.equal((result.preview.matches).endColumn, 3); }); // test('all lines of multiline match', () => { @@ -102,4 +106,4 @@ suite('TextSearchResult', () => { // assert.deepEqual(result.range, range); // assertPreviewRangeText('bar\nfoo', result); // }); -}); \ No newline at end of file +});