remove reverseSearch from ISearchOptions

add isReverseSearch argument to findInLine
modify unit tests
This commit is contained in:
Noam
2018-12-07 22:56:57 +02:00
parent 38adfcd81c
commit 278d696709
3 changed files with 21 additions and 24 deletions
+1 -1
View File
@@ -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;
}
+6 -9
View File
@@ -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);
+14 -14
View File
@@ -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'});