Skip to content

Commit

Permalink
[search] Only show parts of line with match
Browse files Browse the repository at this point in the history
Allow multiple comma separated includes and excludes.
Auto prefix with **.

Signed-off-by: svenefftinge <sven.efftinge@typefox.io>
  • Loading branch information
svenefftinge committed May 14, 2018
1 parent 6eec2e2 commit 43936ac
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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() {
Expand Down Expand Up @@ -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' : ''}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 43936ac

Please sign in to comment.