Responded to feedback and fixed the returned found term

This commit is contained in:
Alex Ross
2018-09-07 15:29:51 -07:00
parent 03e54cce7d
commit e5465841f8
5 changed files with 64 additions and 36 deletions
+14 -14
View File
@@ -3,7 +3,7 @@
* @license MIT
*/
import { ISearchHelper, ISearchAddonTerminal } from './Interfaces';
import { ISearchHelper, ISearchAddonTerminal, ISearchOptions } from './Interfaces';
interface ISearchResult {
term: string;
@@ -19,17 +19,16 @@ export class SearchHelper implements ISearchHelper {
// TODO: Search for multiple instances on 1 line
// TODO: Don't use the actual selection, instead use a "find selection" so multiple instances can be highlighted
// TODO: Highlight other instances in the viewport
// TODO: Support regex, case sensitivity, etc.
}
/**
* Find the next instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing.
* @param term Tne search term.
* @param regex Should use regular expressions
* @param searchOptions Search options.
* @return Whether a result was found.
*/
public findNext(term: string, regex: boolean = false): boolean {
public findNext(term: string, searchOptions: ISearchOptions): boolean {
if (!term || term.length === 0) {
return false;
}
@@ -44,7 +43,7 @@ export class SearchHelper implements ISearchHelper {
// Search from ydisp + 1 to end
for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) {
result = this._findInLine(term, y, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
@@ -53,7 +52,7 @@ export class SearchHelper implements ISearchHelper {
// Search from the top to the current ydisp
if (!result) {
for (let y = 0; y < startRow; y++) {
result = this._findInLine(term, y, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
@@ -68,10 +67,10 @@ export class SearchHelper implements ISearchHelper {
* Find the previous instance of the term, then scroll to and select it. If it
* doesn't exist, do nothing.
* @param term Tne search term.
* @param regex Should use regular expressions
* @param searchOptions Search options.
* @return Whether a result was found.
*/
public findPrevious(term: string, regex: boolean = false): boolean {
public findPrevious(term: string, searchOptions: ISearchOptions): boolean {
if (!term || term.length === 0) {
return false;
}
@@ -86,7 +85,7 @@ export class SearchHelper implements ISearchHelper {
// Search from ydisp + 1 to end
for (let y = startRow - 1; y >= 0; y--) {
result = this._findInLine(term, y, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
@@ -95,7 +94,7 @@ export class SearchHelper implements ISearchHelper {
// Search from the top to the current ydisp
if (!result) {
for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) {
result = this._findInLine(term, y, regex);
result = this._findInLine(term, y, searchOptions);
if (result) {
break;
}
@@ -108,20 +107,21 @@ export class SearchHelper implements ISearchHelper {
/**
* Searches a line for a search term.
* @param term Tne search term.
* @param term The search term.
* @param y The line to search.
* @param regex Should use regular expressions
* @param searchOptions Search options.
* @return The search result if it was found.
*/
private _findInLine(term: string, y: number, regex: boolean): ISearchResult {
private _findInLine(term: string, y: number, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): ISearchResult {
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();
let searchIndex = -1;
if (regex) {
if (searchOptions.regex) {
const searchRegex = RegExp(lowerTerm, 'g');
const foundTerm = searchRegex.exec(lowerStringLine);
if (foundTerm) {
searchIndex = searchRegex.lastIndex - foundTerm[0].length;
term = foundTerm[0];
}
} else {
searchIndex = lowerStringLine.indexOf(lowerTerm);