Skip to content

Commit

Permalink
Added search support for 'match whole word'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 committed Sep 7, 2018
1 parent 3a310f1 commit dab6225
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions demo/index.html
Expand Up @@ -16,6 +16,7 @@ <h3>Actions</h3>
<p>
<label>Find next <input id="find-next"/></label>
<label>Find previous <input id="find-previous"/></label>
<label>Whole word<input type="checkbox" id="whole-word"/></label>
</p>
</div>
<div>
Expand Down
4 changes: 2 additions & 2 deletions demo/main.js
Expand Up @@ -83,13 +83,13 @@ function createTerminal() {
addDomListener(actionElements.findNext, 'keypress', function (e) {
if (e.key === "Enter") {
e.preventDefault();
term.findNext(actionElements.findNext.value);
term.findNext(actionElements.findNext.value, document.getElementById('whole-word').checked);
}
});
addDomListener(actionElements.findPrevious, 'keypress', function (e) {
if (e.key === "Enter") {
e.preventDefault();
term.findPrevious(actionElements.findPrevious.value);
term.findPrevious(actionElements.findPrevious.value, document.getElementById('whole-word').checked);
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/addons/search/Interfaces.ts
Expand Up @@ -17,6 +17,6 @@ export interface ISearchAddonTerminal extends Terminal {
}

export interface ISearchHelper {
findNext(term: string): boolean;
findPrevious(term: string): boolean;
findNext(term: string, wholeWord: boolean): boolean;
findPrevious(term: string, wholeWord: boolean): boolean;
}
30 changes: 23 additions & 7 deletions src/addons/search/SearchHelper.ts
Expand Up @@ -4,6 +4,7 @@
*/

import { ISearchHelper, ISearchAddonTerminal } from './Interfaces';
const nonWordCharacters = ' ~!@#$%^&*()_+`-=[]{}|\;:"\',./<>?';

interface ISearchResult {
term: string;
Expand All @@ -28,7 +29,7 @@ export class SearchHelper implements ISearchHelper {
* @param term Tne search term.
* @return Whether a result was found.
*/
public findNext(term: string): boolean {
public findNext(term: string, wholeWord: boolean = false): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -43,7 +44,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);
result = this._findInLine(term, y, wholeWord);
if (result) {
break;
}
Expand All @@ -52,7 +53,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);
result = this._findInLine(term, y, wholeWord);
if (result) {
break;
}
Expand All @@ -69,7 +70,7 @@ export class SearchHelper implements ISearchHelper {
* @param term Tne search term.
* @return Whether a result was found.
*/
public findPrevious(term: string): boolean {
public findPrevious(term: string, wholeWord: boolean = false): boolean {
if (!term || term.length === 0) {
return false;
}
Expand All @@ -84,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);
result = this._findInLine(term, y, wholeWord);
if (result) {
break;
}
Expand All @@ -93,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);
result = this._findInLine(term, y, wholeWord);
if (result) {
break;
}
Expand All @@ -104,17 +105,32 @@ export class SearchHelper implements ISearchHelper {
return this._selectResult(result);
}

/**
* A found substring is a whole word if it doesn't have an alphanumeric character directly adjacent to it.
* @param searchIndex starting indext of the potential whole word substring
* @param line entire string in which the potential whole word was found
* @param term the substring that starts at searchIndex
*/
private _isWholeWord(searchIndex: number, line: string, term: string): boolean {
return (((searchIndex === 0) || (nonWordCharacters.indexOf(line[searchIndex - 1]) !== -1)) &&
(((searchIndex + term.length) === line.length) || (nonWordCharacters.indexOf(line[searchIndex + term.length]) !== -1)));
}

/**
* Searches a line for a search term.
* @param term Tne search term.
* @param y The line to search.
* @return The search result if it was found.
*/
private _findInLine(term: string, y: number): ISearchResult {
private _findInLine(term: string, y: number, wholeWord: boolean): ISearchResult {
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();
let searchIndex = lowerStringLine.indexOf(lowerTerm);
if (searchIndex >= 0) {
if (wholeWord && !this._isWholeWord(searchIndex, lowerStringLine, term)) {
return;
}

const line = this._terminal._core.buffer.lines.get(y);
for (let i = 0; i < searchIndex; i++) {
const charData = line.get(i);
Expand Down
16 changes: 16 additions & 0 deletions src/addons/search/search.test.ts
Expand Up @@ -46,4 +46,20 @@ describe('search addon', function(): void {
expect(hello1).eql(undefined);
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
});
it('should respect whole-word search option', function(): void {
search.apply(<any>MockTerminal);
const term = new MockTerminal({cols: 20, rows: 5});
term.core.write('Hello World\r\nWorld Hello\r\nWorldHelloWorld\r\nHelloWorld\r\nWorldHello');
term.pushWriteData();
const hello0 = (term.searchHelper as any)._findInLine('Hello', 0, true);
const hello1 = (term.searchHelper as any)._findInLine('Hello', 1, true);
const hello2 = (term.searchHelper as any)._findInLine('Hello', 2, true);
const hello3 = (term.searchHelper as any)._findInLine('Hello', 3, true);
const hello4 = (term.searchHelper as any)._findInLine('Hello', 4, true);
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
expect(hello1).eql({col: 6, row: 1, term: 'Hello'});
expect(hello2).eql(undefined);
expect(hello3).eql(undefined);
expect(hello4).eql(undefined);
});
});
16 changes: 8 additions & 8 deletions src/addons/search/search.ts
Expand Up @@ -13,12 +13,12 @@ import { ISearchAddonTerminal } from './Interfaces';
* @param term Tne search term.
* @return Whether a result was found.
*/
export function findNext(terminal: Terminal, term: string): boolean {
export function findNext(terminal: Terminal, term: string, wholeWord: boolean): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findNext(term);
return addonTerminal.__searchHelper.findNext(term, wholeWord);
}

/**
Expand All @@ -27,20 +27,20 @@ export function findNext(terminal: Terminal, term: string): boolean {
* @param term Tne search term.
* @return Whether a result was found.
*/
export function findPrevious(terminal: Terminal, term: string): boolean {
export function findPrevious(terminal: Terminal, term: string, wholeWord: boolean): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
}
return addonTerminal.__searchHelper.findPrevious(term);
return addonTerminal.__searchHelper.findPrevious(term, wholeWord);
}

export function apply(terminalConstructor: typeof Terminal): void {
(<any>terminalConstructor.prototype).findNext = function(term: string): boolean {
return findNext(this, term);
(<any>terminalConstructor.prototype).findNext = function(term: string, wholeWord: boolean): boolean {
return findNext(this, term, wholeWord);
};

(<any>terminalConstructor.prototype).findPrevious = function(term: string): boolean {
return findPrevious(this, term);
(<any>terminalConstructor.prototype).findPrevious = function(term: string, wholeWord: boolean): boolean {
return findPrevious(this, term, wholeWord);
};
}

0 comments on commit dab6225

Please sign in to comment.