提交 57f48553 编写于 作者: R Rob Lourens

Fix #80667

上级 d3780038
......@@ -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;
......
......@@ -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);
......@@ -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((<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', () => {
......@@ -102,4 +106,4 @@ suite('TextSearchResult', () => {
// assert.deepEqual(result.range, range);
// assertPreviewRangeText('bar\nfoo', result);
// });
});
\ No newline at end of file
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册