Make search option props optional, test optional typings
This commit is contained in:
@@ -22,7 +22,13 @@ export interface ISearchHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ISearchOptions {
|
export interface ISearchOptions {
|
||||||
regex: boolean;
|
regex?: boolean;
|
||||||
wholeWord: boolean;
|
wholeWord?: boolean;
|
||||||
caseSensitive: boolean;
|
caseSensitive?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ISearchResult {
|
||||||
|
term: string;
|
||||||
|
col: number;
|
||||||
|
row: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,7 @@
|
|||||||
* @license MIT
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ISearchHelper, ISearchAddonTerminal, ISearchOptions } from './Interfaces';
|
import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces';
|
||||||
|
|
||||||
interface ISearchResult {
|
|
||||||
term: string;
|
|
||||||
col: number;
|
|
||||||
row: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that knows how to search the terminal and how to display the results.
|
* A class that knows how to search the terminal and how to display the results.
|
||||||
@@ -28,7 +22,7 @@ export class SearchHelper implements ISearchHelper {
|
|||||||
* @param searchOptions Search options.
|
* @param searchOptions Search options.
|
||||||
* @return Whether a result was found.
|
* @return Whether a result was found.
|
||||||
*/
|
*/
|
||||||
public findNext(term: string, searchOptions: ISearchOptions): boolean {
|
public findNext(term: string, searchOptions?: ISearchOptions): boolean {
|
||||||
if (!term || term.length === 0) {
|
if (!term || term.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -70,7 +64,7 @@ export class SearchHelper implements ISearchHelper {
|
|||||||
* @param searchOptions Search options.
|
* @param searchOptions Search options.
|
||||||
* @return Whether a result was found.
|
* @return Whether a result was found.
|
||||||
*/
|
*/
|
||||||
public findPrevious(term: string, searchOptions: ISearchOptions): boolean {
|
public findPrevious(term: string, searchOptions?: ISearchOptions): boolean {
|
||||||
if (!term || term.length === 0) {
|
if (!term || term.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -112,7 +106,7 @@ export class SearchHelper implements ISearchHelper {
|
|||||||
* @param searchOptions Search options.
|
* @param searchOptions Search options.
|
||||||
* @return The search result if it was found.
|
* @return The search result if it was found.
|
||||||
*/
|
*/
|
||||||
private _findInLine(term: string, y: number, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): ISearchResult {
|
protected _findInLine(term: string, y: number, searchOptions: ISearchOptions = {}): ISearchResult {
|
||||||
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
|
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
|
||||||
const lowerTerm = term.toLowerCase();
|
const lowerTerm = term.toLowerCase();
|
||||||
let searchIndex = -1;
|
let searchIndex = -1;
|
||||||
|
|||||||
@@ -6,17 +6,17 @@
|
|||||||
import { assert, expect } from 'chai';
|
import { assert, expect } from 'chai';
|
||||||
import * as search from './search';
|
import * as search from './search';
|
||||||
import { SearchHelper } from './SearchHelper';
|
import { SearchHelper } from './SearchHelper';
|
||||||
import { ISearchHelper } from './Interfaces';
|
import { ISearchOptions, ISearchResult } from './Interfaces';
|
||||||
|
|
||||||
|
|
||||||
class MockTerminalPlain {}
|
class MockTerminalPlain {}
|
||||||
|
|
||||||
class MockTerminal {
|
class MockTerminal {
|
||||||
private _core: any;
|
private _core: any;
|
||||||
public searchHelper: ISearchHelper;
|
public searchHelper: TestSearchHelper;
|
||||||
constructor(options: any) {
|
constructor(options: any) {
|
||||||
this._core = new (require('../../../lib/Terminal').Terminal)(options);
|
this._core = new (require('../../../lib/Terminal').Terminal)(options);
|
||||||
this.searchHelper = new SearchHelper(this as any);
|
this.searchHelper = new TestSearchHelper(this as any);
|
||||||
}
|
}
|
||||||
get core(): any {
|
get core(): any {
|
||||||
return this._core;
|
return this._core;
|
||||||
@@ -26,6 +26,12 @@ class MockTerminal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TestSearchHelper extends SearchHelper {
|
||||||
|
public findInLine(term: string, y: number, searchOptions?: ISearchOptions): ISearchResult {
|
||||||
|
return this._findInLine(term, y, searchOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('search addon', function(): void {
|
describe('search addon', function(): void {
|
||||||
describe('apply', () => {
|
describe('apply', () => {
|
||||||
it('should register findNext and findPrevious', () => {
|
it('should register findNext and findPrevious', () => {
|
||||||
@@ -39,9 +45,9 @@ describe('search addon', function(): void {
|
|||||||
const term = new MockTerminal({cols: 20, rows: 3});
|
const term = new MockTerminal({cols: 20, rows: 3});
|
||||||
term.core.write('Hello World\r\ntest\n123....hello');
|
term.core.write('Hello World\r\ntest\n123....hello');
|
||||||
term.pushWriteData();
|
term.pushWriteData();
|
||||||
const hello0 = (term.searchHelper as any)._findInLine('Hello', 0);
|
const hello0 = term.searchHelper.findInLine('Hello', 0);
|
||||||
const hello1 = (term.searchHelper as any)._findInLine('Hello', 1);
|
const hello1 = term.searchHelper.findInLine('Hello', 1);
|
||||||
const hello2 = (term.searchHelper as any)._findInLine('Hello', 2);
|
const hello2 = term.searchHelper.findInLine('Hello', 2);
|
||||||
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
|
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
|
||||||
expect(hello1).eql(undefined);
|
expect(hello1).eql(undefined);
|
||||||
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
|
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
|
||||||
@@ -62,12 +68,12 @@ describe('search addon', function(): void {
|
|||||||
wholeWord: false,
|
wholeWord: false,
|
||||||
caseSensitive: false
|
caseSensitive: false
|
||||||
};
|
};
|
||||||
const hello0 = (term.searchHelper as any)._findInLine('dee*', 0, searchOptions);
|
const hello0 = term.searchHelper.findInLine('dee*', 0, searchOptions);
|
||||||
const hello1 = (term.searchHelper as any)._findInLine('jkk*', 0, searchOptions);
|
term.searchHelper.findInLine('jkk*', 0, searchOptions);
|
||||||
const hello2 = (term.searchHelper as any)._findInLine('mnn*', 1, searchOptions);
|
term.searchHelper.findInLine('mnn*', 1, searchOptions);
|
||||||
const tilda0 = (term.searchHelper as any)._findInLine('^~', 3, searchOptions);
|
const tilda0 = term.searchHelper.findInLine('^~', 3, searchOptions);
|
||||||
const tilda1 = (term.searchHelper as any)._findInLine('^[~]', 3, searchOptions);
|
const tilda1 = term.searchHelper.findInLine('^[~]', 3, searchOptions);
|
||||||
const tilda2 = (term.searchHelper as any)._findInLine('^\\~', 3, searchOptions);
|
const tilda2 = term.searchHelper.findInLine('^\\~', 3, searchOptions);
|
||||||
expect(hello0).eql({col: 3, row: 0, term: 'de'});
|
expect(hello0).eql({col: 3, row: 0, term: 'de'});
|
||||||
// TODO: uncomment this test when line wrap search is checked in expect(hello1).eql({col: 9, row: 0, term: 'jk'});
|
// TODO: uncomment this test when line wrap search is checked in expect(hello1).eql({col: 9, row: 0, term: 'jk'});
|
||||||
// TODO: uncomment this test when line wrap search is checked in expect(hello2).eql(undefined);
|
// TODO: uncomment this test when line wrap search is checked in expect(hello2).eql(undefined);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { ISearchAddonTerminal, ISearchOptions } from './Interfaces';
|
|||||||
* @param searchOptions Search options
|
* @param searchOptions Search options
|
||||||
* @return Whether a result was found.
|
* @return Whether a result was found.
|
||||||
*/
|
*/
|
||||||
export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean {
|
export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {}): boolean {
|
||||||
const addonTerminal = <ISearchAddonTerminal>terminal;
|
const addonTerminal = <ISearchAddonTerminal>terminal;
|
||||||
if (!addonTerminal.__searchHelper) {
|
if (!addonTerminal.__searchHelper) {
|
||||||
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
|
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
|
||||||
@@ -29,7 +29,7 @@ export function findNext(terminal: Terminal, term: string, searchOptions: ISearc
|
|||||||
* @param searchOptions Search options
|
* @param searchOptions Search options
|
||||||
* @return Whether a result was found.
|
* @return Whether a result was found.
|
||||||
*/
|
*/
|
||||||
export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean {
|
export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions): boolean {
|
||||||
const addonTerminal = <ISearchAddonTerminal>terminal;
|
const addonTerminal = <ISearchAddonTerminal>terminal;
|
||||||
if (!addonTerminal.__searchHelper) {
|
if (!addonTerminal.__searchHelper) {
|
||||||
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
|
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
|
||||||
|
|||||||
Reference in New Issue
Block a user