From 278d696709b799c20481107ebf2a6ff53ae0764b Mon Sep 17 00:00:00 2001 From: Noam Date: Fri, 7 Dec 2018 22:56:57 +0200 Subject: [PATCH] remove reverseSearch from ISearchOptions add isReverseSearch argument to findInLine modify unit tests --- src/addons/search/Interfaces.ts | 2 +- src/addons/search/SearchHelper.ts | 15 ++++++--------- src/addons/search/search.test.ts | 28 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index e03b70c4c4..e15224d170 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -25,13 +25,13 @@ export interface ISearchOptions { regex?: boolean; wholeWord?: boolean; caseSensitive?: boolean; - reverseSearch?: boolean; } export interface ISearchIndex { col: number; row: number; } + export interface ISearchResult extends ISearchIndex { term: string; } diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index dd0559917e..5052a07d37 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -5,7 +5,6 @@ import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; const nonWordCharacters = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; - /** * A class that knows how to search the terminal and how to display the results. */ @@ -74,9 +73,7 @@ export class SearchHelper implements ISearchHelper { if (!term || term.length === 0) { return false; } - - searchOptions.reverseSearch = true; - + const isReverseSearch = true; let result: ISearchResult; let startRow = this._terminal._core.buffer.ydisp; let startCol: number = this._terminal._core.buffer.lines.get(startRow).length; @@ -91,7 +88,7 @@ export class SearchHelper implements ISearchHelper { // Search from ydisp + 1 to end for (let y = startRow; y >= 0; y--) { - result = this._findInLine(term, {row: y, col: startCol}, searchOptions); + result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); if (result) { break; } @@ -103,7 +100,7 @@ export class SearchHelper implements ISearchHelper { const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1; for (let y = searchFrom; y > startRow; y--) { startCol = this._terminal._core.buffer.lines.get(y).length; - result = this._findInLine(term, {row: y, col: startCol}, searchOptions); + result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); if (result) { break; } @@ -135,7 +132,7 @@ export class SearchHelper implements ISearchHelper { * @param searchOptions Search options. * @return The search result if it was found. */ - protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}): ISearchResult { + protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}, isReverseSearch: boolean = false): ISearchResult { if (this._terminal._core.buffer.lines.get(searchIndex.row).isWrapped) { return; } @@ -148,7 +145,7 @@ export class SearchHelper implements ISearchHelper { if (searchOptions.regex) { const searchRegex = RegExp(searchTerm, 'g'); let foundTerm: RegExpExecArray; - if (searchOptions.reverseSearch) { + if (isReverseSearch) { while (foundTerm = searchRegex.exec(searchStringLine.slice(0, searchIndex.col))) { resultIndex = searchRegex.lastIndex - foundTerm[0].length; term = foundTerm[0]; @@ -162,7 +159,7 @@ export class SearchHelper implements ISearchHelper { } } } else { - if (searchOptions.reverseSearch) { + if (isReverseSearch) { resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); } else { resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index be9ec4796c..0a38f755e0 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -32,8 +32,8 @@ class TestSearchHelper extends SearchHelper { public findInLine(term: string, rowNumber: number, searchOptions?: ISearchOptions): ISearchResult { return this._findInLine(term, {row: rowNumber, col: 0}, searchOptions); } - public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions): ISearchResult { - return this._findInLine(term, searchIndex, searchOptions); + public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions, isReverseSearch?: boolean): ISearchResult { + return this._findInLine(term, searchIndex, searchOptions, isReverseSearch); } } @@ -279,13 +279,13 @@ describe('search addon', () => { const searchOptions = { regex: false, wholeWord: false, - caseSensitive: false, - reverseSearch: true + caseSensitive: false }; - const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions); - const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions); - const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions); - const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions); + const isReverseSearch = true; + const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions, isReverseSearch); + const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions, isReverseSearch); + const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions, isReverseSearch); + const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions, isReverseSearch); expect(find0).eql({col: 14, row: 0, term: 'is'}); expect(find1).eql({col: 3, row: 0, term: 'is'}); expect(find2).eql({col: 11, row: 0, term: 'it'}); @@ -299,13 +299,13 @@ describe('search addon', () => { const searchOptions = { regex: true, wholeWord: false, - caseSensitive: true, - reverseSearch: true + caseSensitive: true }; - const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions); - const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions); - const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions); - const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions); + const isReverseSearch = true; + const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions, isReverseSearch); + const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions, isReverseSearch); + const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions, isReverseSearch); + const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions, isReverseSearch); expect(find0).eql({col: 13, row: 0, term: 'ABC'}); expect(find1).eql({col: 10, row: 0, term: 'ABC'}); expect(find2).eql({col: 3, row: 0, term: 'ABC'});