-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathsearchView.ts
More file actions
159 lines (136 loc) Β· 5.45 KB
/
searchView.ts
File metadata and controls
159 lines (136 loc) Β· 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import path from 'path';
import vscode from "vscode";
import { VscodeTools } from "../Tools";
import { DefaultOpenMode, SearchHit, SearchHitLine, SearchResults, WithPath } from "../../typings";
export function initializeSearchView(context: vscode.ExtensionContext) {
const searchView = new SearchView();
const searchViewViewer = vscode.window.createTreeView(
`searchView`, {
treeDataProvider: searchView,
showCollapseAll: true,
canSelectMany: false
});
context.subscriptions.push(
searchViewViewer,
vscode.commands.registerCommand(`code-for-ibmi.refreshSearchView`, async () => searchView.refresh()),
vscode.commands.registerCommand(`code-for-ibmi.closeSearchView`, async () => vscode.commands.executeCommand(`setContext`, `code-for-ibmi:searchViewVisible`, false)),
vscode.commands.registerCommand(`code-for-ibmi.collapseSearchView`, async () => searchView.collapse()),
vscode.commands.registerCommand(`code-for-ibmi.setSearchResults`, async (searchResults: SearchResults) => {
if (searchResults.hits.some(hit => hit.lines.length)) {
searchViewViewer.message = vscode.l10n.t(`{0} file(s) contain(s) '{1}'`, searchResults.hits.length, searchResults.term);
}
else {
searchViewViewer.message = vscode.l10n.t(`{0} file(s) named '{1}'`, searchResults.hits.length, searchResults.term);
}
searchView.setResults(searchResults);
})
)
}
class SearchView implements vscode.TreeDataProvider<vscode.TreeItem> {
private _results: SearchResults = { term: "", hits: [] };
private _onDidChangeTreeData: vscode.EventEmitter<vscode.TreeItem | undefined | null | void> = new vscode.EventEmitter<vscode.TreeItem | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<vscode.TreeItem | undefined | null | void> = this._onDidChangeTreeData.event;
setViewVisible(visible: boolean) {
vscode.commands.executeCommand(`setContext`, `code-for-ibmi:searchViewVisible`, visible);
}
setResults(results: SearchResults) {
this._results = results;
this.refresh();
this.setViewVisible(true);
vscode.commands.executeCommand(`searchView.focus`)
}
refresh() {
this._onDidChangeTreeData.fire();
}
getTreeItem(element: vscode.TreeItem) {
return element;
}
collapse() {
vscode.commands.executeCommand(`workbench.actions.treeView.searchView.collapseAll`);
}
async getChildren(hitSource: HitSource): Promise<vscode.TreeItem[]> {
if (!hitSource) {
return this._results.hits.map(hit => new HitSource(this._results.term, hit));
} else {
return hitSource.getChildren();
}
}
}
class HitSource extends vscode.TreeItem implements WithPath {
private readonly _readonly?: boolean;
readonly path: string;
constructor(readonly term: string, readonly result: SearchHit) {
const hits = result.lines.length;
super(computeSearchHitLabel(term, result), hits ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.None);
this.contextValue = `hitSource`;
this.iconPath = vscode.ThemeIcon.File;
this.path = result.path;
this._readonly = result.readonly;
this.tooltip = result.file ? VscodeTools.ifsFileToToolTip(this.path, result.file) :
result.member? VscodeTools.memberToToolTip(this.path, result.member) :
result.path;
if (hits) {
this.description = `${hits} hit${hits === 1 ? `` : `s`}`;
}
else {
this.description = result.path;
this.command = {
command: `code-for-ibmi.openWithDefaultMode`,
title: `Open`,
arguments: [this, this._readonly ? "browse" as DefaultOpenMode : undefined]
};
}
}
async getChildren(): Promise<LineHit[]> {
return this.result.lines.map(line => new LineHit(this.term, this.path, line, this._readonly));
}
}
class LineHit extends vscode.TreeItem {
constructor(readonly term: string, readonly path: string, line: SearchHitLine, readonly?: boolean) {
const highlights: [number, number][] = [];
const upperContent = line.content.trim().toUpperCase();
const upperTerm = term.toUpperCase();
let index = 0;
// Calculate the highlights
let position;
if (term.length > 0) {
const positionLine = line.number - 1;
while (index >= 0) {
index = upperContent.indexOf(upperTerm, index);
if (index >= 0) {
highlights.push([index, index + term.length]);
if (!position) {
const offset = index + (line.content.length - line.content.trimStart().length);
position = new vscode.Range(positionLine, offset, positionLine, offset + term.length)
}
index += term.length;
}
}
}
super({
label: line.content.trim(),
highlights
});
this.contextValue = `lineHit`;
this.collapsibleState = vscode.TreeItemCollapsibleState.None;
this.description = `line ${line.number}`;
this.command = {
command: `code-for-ibmi.openWithDefaultMode`,
title: `Open`,
arguments: [this, readonly ? "browse" as DefaultOpenMode : undefined, position]
};
}
}
function computeSearchHitLabel(term: string, result: SearchHit) {
const label = result.label || path.posix.basename(result.path);
if (result.lines.length) {
return label;
}
else {
const position = label.toLocaleLowerCase().lastIndexOf(term.toLocaleLowerCase());
return {
label,
highlights: position > -1 ? [[position, term.length + position]] : undefined
} as vscode.TreeItemLabel;
}
}