Skip to content

Commit 11426bb

Browse files
authored
Merge pull request #1662 from alexr00/alexr00/searchWrapped
Added support for searching accross wrapped lines
2 parents 12e3840 + 5e004b2 commit 11426bb

File tree

3 files changed

+123
-51
lines changed

3 files changed

+123
-51
lines changed

demo/client.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
/// <reference path="../typings/xterm.d.ts"/>
99

1010
import { Terminal } from '../lib/public/Terminal';
11-
import * as attach from '../build/addons/attach/attach';
12-
import * as fit from '../build/addons/fit/fit';
13-
import * as fullscreen from '../build/addons/fullscreen/fullscreen';
14-
import * as search from '../build/addons/search/search';
15-
import * as webLinks from '../build/addons/webLinks/webLinks';
16-
import * as winptyCompat from '../build/addons/winptyCompat/winptyCompat';
11+
import * as attach from '../lib/addons/attach/attach';
12+
import * as fit from '../lib/addons/fit/fit';
13+
import * as fullscreen from '../lib/addons/fullscreen/fullscreen';
14+
import * as search from '../lib/addons/search/search';
15+
import * as webLinks from '../lib/addons/webLinks/webLinks';
16+
import * as winptyCompat from '../lib/addons/winptyCompat/winptyCompat';
1717

1818
// Pulling in the module's types relies on the <reference> above, it's looks a
1919
// little weird here as we're importing "this" module

src/addons/search/SearchHelper.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,41 @@ export class SearchHelper implements ISearchHelper {
100100
}
101101

102102
/**
103-
* Searches a line for a search term.
104-
* @param term The search term.
103+
* Searches a line for a search term. Takes the provided terminal line and searches the text line, which may contain
104+
* subsequent terminal lines if the text is wrapped. If the provided line number is part of a wrapped text line that
105+
* started on an earlier line then it is skipped since it will be properly searched when the terminal line that the
106+
* text starts on is searched.
107+
* @param term Tne search term.
105108
* @param y The line to search.
106109
* @param searchOptions Search options.
107110
* @return The search result if it was found.
108111
*/
109112
protected _findInLine(term: string, y: number, searchOptions: ISearchOptions = {}): ISearchResult {
110-
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
113+
if (this._terminal._core.buffer.lines.get(y).isWrapped) {
114+
return;
115+
}
116+
const lowerStringLine = this.translateBufferLineToStringWithWrap(y, true).toLowerCase();
111117
const lowerTerm = term.toLowerCase();
112118
let searchIndex = -1;
113119
if (searchOptions.regex) {
114120
const searchRegex = RegExp(lowerTerm, 'g');
115121
const foundTerm = searchRegex.exec(lowerStringLine);
116-
if (foundTerm) {
122+
if (foundTerm && foundTerm[0].length > 0) {
117123
searchIndex = searchRegex.lastIndex - foundTerm[0].length;
118124
term = foundTerm[0];
119125
}
120126
} else {
121127
searchIndex = lowerStringLine.indexOf(lowerTerm);
122128
}
129+
123130
if (searchIndex >= 0) {
131+
// Adjust the row number and search index if needed since a "line" of text can span multiple rows
132+
if (searchIndex >= this._terminal.cols) {
133+
y += Math.floor(searchIndex / this._terminal.cols);
134+
searchIndex = searchIndex % this._terminal.cols;
135+
}
124136
const line = this._terminal._core.buffer.lines.get(y);
137+
125138
for (let i = 0; i < searchIndex; i++) {
126139
const charData = line.get(i);
127140
// Adjust the searchIndex to normalize emoji into single chars
@@ -144,6 +157,28 @@ export class SearchHelper implements ISearchHelper {
144157
}
145158
}
146159

160+
/**
161+
* Translates a buffer line to a string, including subsequent lines if they are wraps.
162+
* Wide characters will count as two columns in the resulting string. This
163+
* function is useful for getting the actual text underneath the raw selection
164+
* position.
165+
* @param line The line being translated.
166+
* @param trimRight Whether to trim whitespace to the right.
167+
*/
168+
public translateBufferLineToStringWithWrap(lineIndex: number, trimRight: boolean): string {
169+
let lineString = '';
170+
let lineWrapsToNext: boolean;
171+
172+
do {
173+
lineString += this._terminal._core.buffer.translateBufferLineToString(lineIndex, true);
174+
lineIndex++;
175+
const nextLine = this._terminal._core.buffer.lines.get(lineIndex);
176+
lineWrapsToNext = nextLine ? this._terminal._core.buffer.lines.get(lineIndex).isWrapped : false;
177+
} while (lineWrapsToNext);
178+
179+
return lineString;
180+
}
181+
147182
/**
148183
* Selects and scrolls to a result.
149184
* @param result The result to select.

src/addons/search/search.test.ts

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class MockTerminalPlain {}
1414
class MockTerminal {
1515
private _core: any;
1616
public searchHelper: TestSearchHelper;
17+
public cols: number;
1718
constructor(options: any) {
1819
this._core = new (require('../../../lib/Terminal').Terminal)(options);
1920
this.searchHelper = new TestSearchHelper(this as any);
21+
this.cols = options.cols;
2022
}
2123
get core(): any {
2224
return this._core;
@@ -32,53 +34,88 @@ class TestSearchHelper extends SearchHelper {
3234
}
3335
}
3436

35-
describe('search addon', function(): void {
37+
describe('search addon', () => {
3638
describe('apply', () => {
3739
it('should register findNext and findPrevious', () => {
3840
search.apply(<any>MockTerminalPlain);
3941
assert.equal(typeof (<any>MockTerminalPlain).prototype.findNext, 'function');
4042
assert.equal(typeof (<any>MockTerminalPlain).prototype.findPrevious, 'function');
4143
});
4244
});
43-
it('Searchhelper - should find correct position', function(): void {
44-
search.apply(<any>MockTerminal);
45-
const term = new MockTerminal({cols: 20, rows: 3});
46-
term.core.write('Hello World\r\ntest\n123....hello');
47-
term.pushWriteData();
48-
const hello0 = term.searchHelper.findInLine('Hello', 0);
49-
const hello1 = term.searchHelper.findInLine('Hello', 1);
50-
const hello2 = term.searchHelper.findInLine('Hello', 2);
51-
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
52-
expect(hello1).eql(undefined);
53-
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
54-
});
55-
it('should respect search regex', function(): void {
56-
search.apply(<any>MockTerminal);
57-
const term = new MockTerminal({cols: 10, rows: 4});
58-
term.core.write('abcdefghijklmnopqrstuvwxyz\r\n~/dev ');
59-
/*
60-
abcdefghij
61-
klmnopqrst
62-
uvwxyz
63-
~/dev
64-
*/
65-
term.pushWriteData();
66-
const searchOptions = {
67-
regex: true,
68-
wholeWord: false,
69-
caseSensitive: false
70-
};
71-
const hello0 = term.searchHelper.findInLine('dee*', 0, searchOptions);
72-
term.searchHelper.findInLine('jkk*', 0, searchOptions);
73-
term.searchHelper.findInLine('mnn*', 1, searchOptions);
74-
const tilda0 = term.searchHelper.findInLine('^~', 3, searchOptions);
75-
const tilda1 = term.searchHelper.findInLine('^[~]', 3, searchOptions);
76-
const tilda2 = term.searchHelper.findInLine('^\\~', 3, searchOptions);
77-
expect(hello0).eql({col: 3, row: 0, term: 'de'});
78-
// TODO: uncomment this test when line wrap search is checked in expect(hello1).eql({col: 9, row: 0, term: 'jk'});
79-
// TODO: uncomment this test when line wrap search is checked in expect(hello2).eql(undefined);
80-
expect(tilda0).eql({col: 0, row: 3, term: '~'});
81-
expect(tilda1).eql({col: 0, row: 3, term: '~'});
82-
expect(tilda2).eql({col: 0, row: 3, term: '~'});
45+
describe('find', () => {
46+
it('Searchhelper - should find correct position', () => {
47+
search.apply(<any>MockTerminal);
48+
const term = new MockTerminal({cols: 20, rows: 3});
49+
term.core.write('Hello World\r\ntest\n123....hello');
50+
term.pushWriteData();
51+
const hello0 = term.searchHelper.findInLine('Hello', 0);
52+
const hello1 = term.searchHelper.findInLine('Hello', 1);
53+
const hello2 = term.searchHelper.findInLine('Hello', 2);
54+
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
55+
expect(hello1).eql(undefined);
56+
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
57+
});
58+
it('should find search term accross line wrap', () => {
59+
search.apply(<any>MockTerminal);
60+
const term = new MockTerminal({cols: 10, rows: 5});
61+
term.core.write('texttextHellotext\r\n');
62+
term.core.write('texttexttextHellotext');
63+
term.pushWriteData();
64+
/*
65+
texttextHe
66+
llotext
67+
texttextte
68+
xtHellotex
69+
t
70+
*/
71+
72+
const hello0 = (term.searchHelper as any)._findInLine('Hello', 0);
73+
const hello1 = (term.searchHelper as any)._findInLine('Hello', 1);
74+
const hello2 = (term.searchHelper as any)._findInLine('Hello', 2);
75+
const hello3 = (term.searchHelper as any)._findInLine('Hello', 3);
76+
const llo = (term.searchHelper as any)._findInLine('llo', 1);
77+
expect(hello0).eql({col: 8, row: 0, term: 'Hello'});
78+
expect(hello1).eql(undefined);
79+
expect(hello2).eql({col: 2, row: 3, term: 'Hello'});
80+
expect(hello3).eql(undefined);
81+
expect(llo).eql(undefined);
82+
});
83+
it('should respect search regex', () => {
84+
search.apply(<any>MockTerminal);
85+
const term = new MockTerminal({cols: 10, rows: 4});
86+
term.core.write('abcdefghijklmnopqrstuvwxyz\r\n~/dev ');
87+
/*
88+
abcdefghij
89+
klmnopqrst
90+
uvwxyz
91+
~/dev
92+
*/
93+
term.pushWriteData();
94+
const searchOptions = {
95+
regex: true,
96+
wholeWord: false,
97+
caseSensitive: false
98+
};
99+
const hello0 = term.searchHelper.findInLine('dee*', 0, searchOptions);
100+
const hello1 = term.searchHelper.findInLine('jkk*', 0, searchOptions);
101+
const hello2 = term.searchHelper.findInLine('mnn*', 1, searchOptions);
102+
const tilda0 = term.searchHelper.findInLine('^~', 3, searchOptions);
103+
const tilda1 = term.searchHelper.findInLine('^[~]', 3, searchOptions);
104+
const tilda2 = term.searchHelper.findInLine('^\\~', 3, searchOptions);
105+
expect(hello0).eql({col: 3, row: 0, term: 'de'});
106+
expect(hello1).eql({col: 9, row: 0, term: 'jk'});
107+
expect(hello2).eql(undefined);
108+
expect(tilda0).eql({col: 0, row: 3, term: '~'});
109+
expect(tilda1).eql({col: 0, row: 3, term: '~'});
110+
expect(tilda2).eql({col: 0, row: 3, term: '~'});
111+
});
112+
it('should not select empty lines', () => {
113+
search.apply(<any>MockTerminal);
114+
const term = new MockTerminal({cols: 20, rows: 3});
115+
term.core.write(' ');
116+
term.pushWriteData();
117+
const line = term.searchHelper.findInLine('^.*$', 0, { regex: true });
118+
expect(line).eql(undefined);
119+
});
83120
});
84121
});

0 commit comments

Comments
 (0)