Skip to content

Commit

Permalink
remove reverseSearch from ISearchOptions
Browse files Browse the repository at this point in the history
add isReverseSearch argument to findInLine
modify unit tests
  • Loading branch information
noamyogev84 committed Dec 7, 2018
1 parent 38adfcd commit 278d696
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/addons/search/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 6 additions & 9 deletions src/addons/search/SearchHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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];
Expand All @@ -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);
Expand Down
28 changes: 14 additions & 14 deletions src/addons/search/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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'});
Expand All @@ -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'});
Expand Down

0 comments on commit 278d696

Please sign in to comment.