From 43936ac6116533964c38f4c7217c5a91b63fb68c Mon Sep 17 00:00:00 2001 From: svenefftinge Date: Mon, 14 May 2018 13:03:36 +0000 Subject: [PATCH] [search] Only show parts of line with match Allow multiple comma separated includes and excludes. Auto prefix with **. Signed-off-by: svenefftinge --- .../search-in-workspace-result-tree-widget.ts | 17 ++++++++++------- .../src/browser/search-in-workspace-widget.ts | 17 +++++++++++------ .../src/common/search-in-workspace-interface.ts | 4 ++-- .../node/ripgrep-search-in-workspace-server.ts | 8 ++++++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.ts b/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.ts index 9a2864d79638c..76182e8d80376 100644 --- a/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.ts +++ b/packages/search-in-workspace/src/browser/search-in-workspace-result-tree-widget.ts @@ -145,12 +145,12 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget { this.cancelIndicator = new CancellationTokenSource(); const token = this.cancelIndicator.token; const searchId = await this.searchService.search(searchTerm, { - onResult: async (searchId: number, result: SearchInWorkspaceResult) => { - if (token.isCancellationRequested) { + onResult: async (aSearchId: number, result: SearchInWorkspaceResult) => { + if (token.isCancellationRequested || aSearchId !== searchId) { return; } const { name, path } = this.filenameAndPath(result.file); - const resultElement = this.resultTree.get(result.file); + let resultElement = this.resultTree.get(result.file); if (resultElement) { const resultLine = this.createResultLineNode(result, resultElement); @@ -159,7 +159,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget { const children: SearchInWorkspaceResultLineNode[] = []; const icon = await this.labelProvider.getIcon(new URI(result.file)); if (CompositeTreeNode.is(this.model.root)) { - const resultElement: SearchInWorkspaceResultNode = { + resultElement = { selected: false, name, path, @@ -182,7 +182,9 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget { this.refreshModelChildren(); } }, searchOptions); - token.onCancellationRequested(() => this.searchService.cancel(searchId)); + token.onCancellationRequested(() => { + this.searchService.cancel(searchId); + }); } protected refreshModelChildren() { @@ -346,9 +348,10 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget { } protected renderResultLineNode(node: SearchInWorkspaceResultLineNode): h.Child { - const start = h.span(node.lineText.substr(0, node.character - 1)); + const prefix = node.character > 26 ? '... ' : ''; + const start = h.span(prefix + node.lineText.substr(0, node.character - 1).substr(-25)); const match = this.renderMatchLinePart(node); - const end = h.span(node.lineText.substr(node.character - 1 + node.length)); + const end = h.span(node.lineText.substr(node.character - 1 + node.length, 75)); return h.div( { className: `resultLine noWrapInfo ${node.selected ? 'selected' : ''}`, diff --git a/packages/search-in-workspace/src/browser/search-in-workspace-widget.ts b/packages/search-in-workspace/src/browser/search-in-workspace-widget.ts index 0790a0760c18d..bf1133e12eeaa 100644 --- a/packages/search-in-workspace/src/browser/search-in-workspace-widget.ts +++ b/packages/search-in-workspace/src/browser/search-in-workspace-widget.ts @@ -81,8 +81,8 @@ export class SearchInWorkspaceWidget extends BaseWidget implements StatefulWidge matchWholeWord: false, useRegExp: false, includeIgnored: false, - include: "", - exclude: "", + include: [], + exclude: [], maxResults: 500 }; this.resultTreeWidget.onChange(r => { @@ -159,8 +159,8 @@ export class SearchInWorkspaceWidget extends BaseWidget implements StatefulWidge protected clear = () => { this.searchTerm = ""; this.replaceTerm = ""; - this.searchInWorkspaceOptions.include = ""; - this.searchInWorkspaceOptions.exclude = ""; + this.searchInWorkspaceOptions.include = []; + this.searchInWorkspaceOptions.exclude = []; this.includeIgnoredState.enabled = false; this.matchCaseState.enabled = false; this.wholeWordState.enabled = false; @@ -333,20 +333,25 @@ export class SearchInWorkspaceWidget extends BaseWidget implements StatefulWidge protected renderGlobField(kind: "include" | "exclude"): h.Child { const label = h.div({ className: "label" }, "files to " + kind); + const currentValue = this.searchInWorkspaceOptions[kind]; const input = h.input({ type: "text", - value: this.searchInWorkspaceOptions[kind], + value: currentValue && currentValue.join(', ') || '', id: kind + "-glob-field", onkeyup: e => { if (e.target) { if (Key.ENTER.keyCode === e.keyCode) { this.resultTreeWidget.search(this.searchTerm, this.searchInWorkspaceOptions); } else { - this.searchInWorkspaceOptions[kind] = (e.target as HTMLInputElement).value; + this.searchInWorkspaceOptions[kind] = this.splitOnComma((e.target as HTMLInputElement).value); } } } }); return h.div({ className: "glob-field" }, label, input); } + + protected splitOnComma(patterns: string): string[] { + return patterns.split(',').map(s => s.trim()); + } } diff --git a/packages/search-in-workspace/src/common/search-in-workspace-interface.ts b/packages/search-in-workspace/src/common/search-in-workspace-interface.ts index d0ced50cca3c5..e19f3cbed84c5 100644 --- a/packages/search-in-workspace/src/common/search-in-workspace-interface.ts +++ b/packages/search-in-workspace/src/common/search-in-workspace-interface.ts @@ -31,11 +31,11 @@ export interface SearchInWorkspaceOptions { /** * Glob pattern for matching files and directories to include the search. */ - include?: string; + include?: string[]; /** * Glob pattern for matching files and directories to exclude the search. */ - exclude?: string + exclude?: string[] } export interface SearchInWorkspaceResult { diff --git a/packages/search-in-workspace/src/node/ripgrep-search-in-workspace-server.ts b/packages/search-in-workspace/src/node/ripgrep-search-in-workspace-server.ts index 46ccee139b324..65f4e6672af57 100644 --- a/packages/search-in-workspace/src/node/ripgrep-search-in-workspace-server.ts +++ b/packages/search-in-workspace/src/node/ripgrep-search-in-workspace-server.ts @@ -76,10 +76,14 @@ export class RipgrepSearchInWorkspaceServer implements SearchInWorkspaceServer { const args = this.getArgs(opts); const globs = []; if (opts && opts.include) { - globs.push("--glob='" + opts.include + "'"); + for (const include of opts.include) { + globs.push("--glob='**/" + include + "'"); + } } if (opts && opts.exclude) { - globs.push("--glob='!" + opts.exclude + "'"); + for (const exclude of opts.exclude) { + globs.push("--glob='!**/" + exclude + "'"); + } } const processOptions: RawProcessOptions = { command: rgPath,