Remove ISearchIndex object

This commit is contained in:
Daniel Imms
2018-12-26 16:03:31 -08:00
parent e0d535e0da
commit 1a1f7e984f
3 changed files with 41 additions and 49 deletions
+2 -5
View File
@@ -32,11 +32,8 @@ export interface ISearchOptions {
incremental?: boolean; incremental?: boolean;
} }
export interface ISearchIndex { export interface ISearchResult {
term: string;
col: number; col: number;
row: number; row: number;
} }
export interface ISearchResult extends ISearchIndex {
term: string;
}
+22 -27
View File
@@ -3,7 +3,7 @@
* @license MIT * @license MIT
*/ */
import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces';
const NON_WORD_CHARACTERS = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; const NON_WORD_CHARACTERS = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?';
const LINES_CACHE_TIME_TO_LIVE = 15 * 1000; // 15 secs const LINES_CACHE_TIME_TO_LIVE = 15 * 1000; // 15 secs
@@ -56,12 +56,12 @@ export class SearchHelper implements ISearchHelper {
this._initLinesCache(); this._initLinesCache();
// Search startRow // Search startRow
result = this._findInLine(term, { row: startRow, col: startCol }, searchOptions); result = this._findInLine(term, startRow, startCol, searchOptions);
// Search from startRow + 1 to end // Search from startRow + 1 to end
if (!result) { if (!result) {
for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) {
result = this._findInLine(term, { row: y, col: 0 }, searchOptions); result = this._findInLine(term, y, 0, searchOptions);
if (result) { if (result) {
break; break;
} }
@@ -72,7 +72,7 @@ export class SearchHelper implements ISearchHelper {
// case startCol > 0) // case startCol > 0)
if (!result) { if (!result) {
for (let y = 0; y <= startRow; y++) { for (let y = 0; y <= startRow; y++) {
result = this._findInLine(term, {row: y, col: 0}, searchOptions); result = this._findInLine(term, y, 0, searchOptions);
if (result) { if (result) {
break; break;
} }
@@ -114,15 +114,12 @@ export class SearchHelper implements ISearchHelper {
this._initLinesCache(); this._initLinesCache();
// Search startRow // Search startRow
result = this._findInLine(term, { row: startRow, col: startCol }, searchOptions, isReverseSearch); result = this._findInLine(term, startRow, startCol, searchOptions, isReverseSearch);
// Search from startRow - 1 to top // Search from startRow - 1 to top
if (!result) { if (!result) {
for (let y = startRow - 1; y >= 0; y--) { for (let y = startRow - 1; y >= 0; y--) {
result = this._findInLine(term, { result = this._findInLine(term, y, this._terminal._core.buffer.lines.get(y).length, searchOptions, isReverseSearch);
row: y,
col: this._terminal._core.buffer.lines.get(y).length
}, searchOptions, isReverseSearch);
if (result) { if (result) {
break; break;
} }
@@ -134,10 +131,7 @@ export class SearchHelper implements ISearchHelper {
if (!result) { if (!result) {
const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1; const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1;
for (let y = searchFrom; y >= startRow; y--) { for (let y = searchFrom; y >= startRow; y--) {
result = this._findInLine(term, { result = this._findInLine(term, y, this._terminal._core.buffer.lines.get(y).length, searchOptions, isReverseSearch);
row: y,
col: this._terminal._core.buffer.lines.get(y).length
}, searchOptions, isReverseSearch);
if (result) { if (result) {
break; break;
} }
@@ -187,20 +181,21 @@ export class SearchHelper implements ISearchHelper {
* started on an earlier line then it is skipped since it will be properly searched when the terminal line that the * started on an earlier line then it is skipped since it will be properly searched when the terminal line that the
* text starts on is searched. * text starts on is searched.
* @param term The search term. * @param term The search term.
* @param y The line to search. * @param row The line to start the search from.
* @param col The column to start the search from.
* @param searchOptions Search options. * @param searchOptions Search options.
* @return The search result if it was found. * @return The search result if it was found.
*/ */
protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}, isReverseSearch: boolean = false): ISearchResult { protected _findInLine(term: string, row: number, col: number, searchOptions: ISearchOptions = {}, isReverseSearch: boolean = false): ISearchResult {
if (this._terminal._core.buffer.lines.get(searchIndex.row).isWrapped) { if (this._terminal._core.buffer.lines.get(row).isWrapped) {
return; return;
} }
let stringLine = this._linesCache ? this._linesCache[searchIndex.row] : void 0; let stringLine = this._linesCache ? this._linesCache[row] : void 0;
if (stringLine === void 0) { if (stringLine === void 0) {
stringLine = this.translateBufferLineToStringWithWrap(searchIndex.row, true); stringLine = this.translateBufferLineToStringWithWrap(row, true);
if (this._linesCache) { if (this._linesCache) {
this._linesCache[searchIndex.row] = stringLine; this._linesCache[row] = stringLine;
} }
} }
@@ -212,37 +207,37 @@ export class SearchHelper implements ISearchHelper {
const searchRegex = RegExp(searchTerm, 'g'); const searchRegex = RegExp(searchTerm, 'g');
let foundTerm: RegExpExecArray; let foundTerm: RegExpExecArray;
if (isReverseSearch) { if (isReverseSearch) {
while (foundTerm = searchRegex.exec(searchStringLine.slice(0, searchIndex.col))) { while (foundTerm = searchRegex.exec(searchStringLine.slice(0, col))) {
resultIndex = searchRegex.lastIndex - foundTerm[0].length; resultIndex = searchRegex.lastIndex - foundTerm[0].length;
term = foundTerm[0]; term = foundTerm[0];
searchRegex.lastIndex -= (term.length - 1); searchRegex.lastIndex -= (term.length - 1);
} }
} else { } else {
foundTerm = searchRegex.exec(searchStringLine.slice(searchIndex.col)); foundTerm = searchRegex.exec(searchStringLine.slice(col));
if (foundTerm && foundTerm[0].length > 0) { if (foundTerm && foundTerm[0].length > 0) {
resultIndex = searchIndex.col + (searchRegex.lastIndex - foundTerm[0].length); resultIndex = col + (searchRegex.lastIndex - foundTerm[0].length);
term = foundTerm[0]; term = foundTerm[0];
} }
} }
} else { } else {
if (isReverseSearch) { if (isReverseSearch) {
resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); resultIndex = searchStringLine.lastIndexOf(searchTerm, col - searchTerm.length);
} else { } else {
resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); resultIndex = searchStringLine.indexOf(searchTerm, col);
} }
} }
if (resultIndex >= 0) { if (resultIndex >= 0) {
// Adjust the row number and search index if needed since a "line" of text can span multiple rows // Adjust the row number and search index if needed since a "line" of text can span multiple rows
if (resultIndex >= this._terminal.cols) { if (resultIndex >= this._terminal.cols) {
searchIndex.row += Math.floor(resultIndex / this._terminal.cols); row += Math.floor(resultIndex / this._terminal.cols);
resultIndex = resultIndex % this._terminal.cols; resultIndex = resultIndex % this._terminal.cols;
} }
if (searchOptions.wholeWord && !this._isWholeWord(resultIndex, searchStringLine, term)) { if (searchOptions.wholeWord && !this._isWholeWord(resultIndex, searchStringLine, term)) {
return; return;
} }
const line = this._terminal._core.buffer.lines.get(searchIndex.row); const line = this._terminal._core.buffer.lines.get(row);
for (let i = 0; i < resultIndex; i++) { for (let i = 0; i < resultIndex; i++) {
const charData = line.get(i); const charData = line.get(i);
@@ -261,7 +256,7 @@ export class SearchHelper implements ISearchHelper {
return { return {
term, term,
col: resultIndex, col: resultIndex,
row: searchIndex.row row
}; };
} }
} }
+17 -17
View File
@@ -7,7 +7,7 @@ declare var require: any;
import { assert, expect } from 'chai'; import { assert, expect } from 'chai';
import * as search from './search'; import * as search from './search';
import { SearchHelper } from './SearchHelper'; import { SearchHelper } from './SearchHelper';
import { ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; import { ISearchOptions, ISearchResult } from './Interfaces';
class MockTerminalPlain {} class MockTerminalPlain {}
@@ -30,10 +30,10 @@ class MockTerminal {
class TestSearchHelper extends SearchHelper { class TestSearchHelper extends SearchHelper {
public findInLine(term: string, rowNumber: number, searchOptions?: ISearchOptions): ISearchResult { public findInLine(term: string, rowNumber: number, searchOptions?: ISearchOptions): ISearchResult {
return this._findInLine(term, {row: rowNumber, col: 0}, searchOptions); return this._findInLine(term, rowNumber, 0, searchOptions);
} }
public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions, isReverseSearch?: boolean): ISearchResult { public findFromIndex(term: string, row: number, col: number, searchOptions?: ISearchOptions, isReverseSearch?: boolean): ISearchResult {
return this._findInLine(term, searchIndex, searchOptions, isReverseSearch); return this._findInLine(term, row, col, searchOptions, isReverseSearch);
} }
} }
@@ -258,11 +258,11 @@ describe('search addon', () => {
wholeWord: false, wholeWord: false,
caseSensitive: false caseSensitive: false
}; };
const find0 = term.searchHelper.findFromIndex('hello', {row: 0, col: 0}, searchOptions); const find0 = term.searchHelper.findFromIndex('hello', 0, 0, searchOptions);
const find1 = term.searchHelper.findFromIndex('hello', {row: 0, col: find0.col + find0.term.length}, searchOptions); const find1 = term.searchHelper.findFromIndex('hello', 0, find0.col + find0.term.length, searchOptions);
const find2 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: 0}, searchOptions); const find2 = term.searchHelper.findFromIndex('aaaa', 1, 0, searchOptions);
const find3 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: find2.col + find2.term.length}, searchOptions); const find3 = term.searchHelper.findFromIndex('aaaa', 1, find2.col + find2.term.length, searchOptions);
const find4 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: find3.col + find3.term.length}, searchOptions); const find4 = term.searchHelper.findFromIndex('aaaa', 1, find3.col + find3.term.length, searchOptions);
expect(find0).eql({col: 0, row: 0, term: 'hello'}); expect(find0).eql({col: 0, row: 0, term: 'hello'});
expect(find1).eql({col: 9, row: 0, term: 'hello'}); expect(find1).eql({col: 9, row: 0, term: 'hello'});
expect(find2).eql({col: 0, row: 1, term: 'aaaa'}); expect(find2).eql({col: 0, row: 1, term: 'aaaa'});
@@ -280,10 +280,10 @@ describe('search addon', () => {
caseSensitive: false caseSensitive: false
}; };
const isReverseSearch = true; const isReverseSearch = true;
const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions, isReverseSearch); const find0 = term.searchHelper.findFromIndex('is', 0, 16, searchOptions, isReverseSearch);
const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions, isReverseSearch); const find1 = term.searchHelper.findFromIndex('is', 0, find0.col, searchOptions, isReverseSearch);
const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions, isReverseSearch); const find2 = term.searchHelper.findFromIndex('it', 0, 16, searchOptions, isReverseSearch);
const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions, isReverseSearch); const find3 = term.searchHelper.findFromIndex('it', 0, find2.col, searchOptions, isReverseSearch);
expect(find0).eql({col: 14, row: 0, term: 'is'}); expect(find0).eql({col: 14, row: 0, term: 'is'});
expect(find1).eql({col: 3, row: 0, term: 'is'}); expect(find1).eql({col: 3, row: 0, term: 'is'});
expect(find2).eql({col: 11, row: 0, term: 'it'}); expect(find2).eql({col: 11, row: 0, term: 'it'});
@@ -300,10 +300,10 @@ describe('search addon', () => {
caseSensitive: true caseSensitive: true
}; };
const isReverseSearch = true; const isReverseSearch = true;
const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions, isReverseSearch); const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, 16, searchOptions, isReverseSearch);
const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions, isReverseSearch); const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, find0.col, searchOptions, isReverseSearch);
const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions, isReverseSearch); const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, find1.col, searchOptions, isReverseSearch);
const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions, isReverseSearch); const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, find2.col, searchOptions, isReverseSearch);
expect(find0).eql({col: 13, row: 0, term: 'ABC'}); expect(find0).eql({col: 13, row: 0, term: 'ABC'});
expect(find1).eql({col: 10, row: 0, term: 'ABC'}); expect(find1).eql({col: 10, row: 0, term: 'ABC'});
expect(find2).eql({col: 3, row: 0, term: 'ABC'}); expect(find2).eql({col: 3, row: 0, term: 'ABC'});