-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
265 lines (241 loc) · 9.29 KB
/
extension.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const path = require("path")
const vscode = require("vscode")
const eol = require("os").EOL
const cp = require("child_process")
exports.activate = () => {
vscode.languages.registerWorkspaceSymbolProvider({
provideWorkspaceSymbols: provideWorkspaceSymbols,
resolveWorkspaceSymbol: resolveSymbolLocation
})
vscode.commands.registerCommand("ctagsymbols.regenerateTags", regenerateAllTags)
vscode.workspace.onDidSaveTextDocument(autoRegenerateTags)
}
exports.deactivate = () => {}
const groupBy = (xs, f) =>
xs.reduce(function(groups, x) {
const key = f(x)
groups[key] = groups[key] || []
groups[key].push(x);
return groups;
}, {})
const uniqueEntries = entries => {
const groups = groupBy(entries, e => `${e.location.uri.fsPath}:${e.name}`)
return Object.values(groups).map(group => group[0])
}
const resolveNumber = symbolInfo => {
const line = symbolInfo.target-1
const pos = new vscode.Position(line, 0)
return new vscode.Location(symbolInfo.location.uri, pos)
}
// Updates symbolInfo with the location of the first occurrence of its target pattern.
const resolveLineNumberForPattern = async symbolInfo => {
const file = await vscode.workspace.fs.readFile(symbolInfo.location.uri)
const fileContent = file.toString()
const index = fileContent.indexOf(symbolInfo.target)
if(index < 0) {
return symbolInfo.location
} else {
const line = fileContent.substring(0, index).split(eol).length-1
const pos = new vscode.Position(line, 0)
return new vscode.Location(symbolInfo.location.uri, pos)
}
}
// Does the given cache differ from the given file?
const needsUpdate = async (cache, tagsFile) => {
if(!cache || cache.forFile.fsPath != tagsFile.fsPath) {
console.log("Cache needs update: pointed to a new file.")
return true
}
try {
const stat = await vscode.workspace.fs.stat(tagsFile)
if(stat.mtime > cache.timestamp) {
console.log("Cache needs update: out of date.")
return true
}
} catch (e) {
// If the file doesn't exist, we only need to recache it if it previously *did* exist.
return cache.entries.length != 0
}
return false
}
const tagLineRegex = /([^\t]+)\t([^\t]+)\t(.*)/
// Builds an in-memory representation of tagsFile.
const buildSymbolCache = async (tagsFile, rootDir, hideDuplicateTags) => {
try {
const data = (await vscode.workspace.fs.readFile(tagsFile)).toString()
const entries = data.split(eol).reduce(parseSymbol.bind(null, rootDir), [])
console.log(`Loaded tags from ${tagsFile.fsPath}`)
const filteredEntries = hideDuplicateTags
? uniqueEntries(entries)
: entries
return new SymbolCache(tagsFile, filteredEntries)
} catch (e) {
console.log(`Unable to read tags from '${tagsFile.fsPath}'; providing no symbols.`)
return new SymbolCache(tagsFile, [])
}
}
// Parses line into a SymbolInformation and appends it to the end of the entries array.
const parseSymbol = (rootDir, entries, line) => {
if(!line.startsWith("!_TAG_")) {
const parts = line.match(tagLineRegex)
if(parts && parts.length == 4) {
const file = path.isAbsolute(parts[2])
? parts[2]
: path.join(rootDir, parts[2])
const loc = new vscode.Location(vscode.Uri.file(file), null)
const entry = new vscode.SymbolInformation(parts[1], vscode.SymbolKind.Constant, "", loc)
entry.target = toTargetAddress(parts[3])
entries.push(entry)
}
}
return entries
}
// Parses the given string into a target address.
// A target address is either a line number, or a pattern representing the text on the target line.
const toTargetAddress = s => {
let matches
try {
switch(s[0]) {
case '/':
matches = s.match(/^\/\^(.+)\$\/\s*;?/)
return matches ? matches[1] : s
case '?':
matches = s.match(/^\?(.+)\?\s*;?/)
return matches ? matches[1] : s
default:
return Number(s)
}
} catch (e) {
console.warn(`Invalid regular expression: '${s}'`)
}
}
// Merges entries in the given list of symbol caches into a single list of entries,
// sorted by symbol name.
const mergeCacheEntries = symbolCaches => {
let mergedSymbolCache = symbolCaches.flatMap(c => c.entries)
mergedSymbolCache.sort((a, b) => {
if(a.name < b.name) {
return -1
}
if(a.name > b.name) {
return 1
}
return 0
})
return mergedSymbolCache
}
// Returns a new in-memory representation of the given tags file, if the old one is out of date.
const rebuildIfNecessary = async (cache, folder, tagsFileName, hideDuplicateTags) => {
const tagsFile = vscode.Uri.file(path.join(folder.uri.fsPath, tagsFileName))
if(await needsUpdate(cache, tagsFile)) {
return await buildSymbolCache(tagsFile, folder.uri.fsPath, hideDuplicateTags)
}
}
class SymbolCache {
constructor(forFile, entries) {
this.forFile = forFile
this.timestamp = Date.now()
this.entries = entries
}
}
class MergedSymbolCache {
constructor() {
this.symbolCaches = []
this.entries = []
}
// Ensures that this symbol cache is coherent with the tags files backing it.
async ensureCoherency(tagsFileName, hideDuplicateTags, folders) {
const tasks = folders.map(async (folder, ix) =>
await rebuildIfNecessary(this.symbolCaches[ix], folder, tagsFileName, hideDuplicateTags)
)
const anyUpdated = (await Promise.all(tasks)).reduce((anyUpdated, cache, ix) => {
if(cache) {
this.symbolCaches[ix] = cache
return true
}
return anyUpdated
}, false)
if(anyUpdated) {
this.entries = mergeCacheEntries(this.symbolCaches)
}
}
}
const mergedSymbolCache = new MergedSymbolCache()
const provideWorkspaceSymbols = async query => {
// Read settings
const config = vscode.workspace.getConfiguration("ctagsymbols")
const minQueryLength = config.get("minQueryLength")
if(query.length < minQueryLength) {
return []
}
const folders = vscode.workspace.workspaceFolders
const tagsFileName = config.get("tagsFileName")
const hideDuplicateTags = config.get("hideDuplicateTags")
const maxNumberOfSymbols = config.get("maxNumberOfSymbols")
// Ensure we've got the latest tags
await mergedSymbolCache.ensureCoherency(tagsFileName, hideDuplicateTags, folders)
// Query the cached tag list
const queryRegex = new RegExp(query, "i")
const filteredEntries = mergedSymbolCache.entries
.filter(entry => entry.name.match(queryRegex))
return maxNumberOfSymbols
? filteredEntries.slice(0, maxNumberOfSymbols)
: filteredEntries
}
// Updates symbolInfo with the location of the symbol.
const resolveSymbolLocation = async symbolInfo => {
if(symbolInfo.location.range) {
return symbolInfo
}
if(typeof symbolInfo.target === "number") {
symbolInfo.location = resolveNumber(symbolInfo)
} else {
symbolInfo.location = await resolveLineNumberForPattern(symbolInfo)
}
delete symbolInfo.target
return symbolInfo
}
// Regenerate tags files for all workspaces.
const regenerateAllTags = () => {
const config = vscode.workspace.getConfiguration("ctagsymbols")
const tagsFile = config.get("tagsFileName")
const commandTemplate = config.get("regenerateCommand")
vscode.workspace.workspaceFolders.forEach(folder =>
regenerateTags(folder.uri.fsPath, tagsFile, commandTemplate)
)
}
const wsFolderRegex = /\$\{workspaceFolder\}/
const tagsPathRegex = /\$\{tagsFile\}/
const fillInPaths = (template, folder, tagsPath) =>
template.replace(wsFolderRegex, folder).replace(tagsPathRegex, tagsPath)
// Regenerate tags file for the given workspace folder and tags file, using the
// given command template.
const regenerateTags = (folder, tagsFile, commandTemplate) => {
const tagsPath = path.join(folder, tagsFile)
console.log(`Regenerating ${tagsPath}...`)
const command = fillInPaths(commandTemplate, folder, tagsPath)
cp.exec(command, err => {
if(err) {
console.error(`Unable to regenerate ${tagsPath}:\n${err.message}`)
const message = commandTemplate.startsWith("ctags")
? "Unable to regenerate tags. Have you installed ctags?"
: "Unable to regenerate tags. Check your \"Regenerate CTags\" command settings."
vscode.window.showErrorMessage(message)
}
})
}
// Regenerate tags for the workspace folder in which the given document resides,
// if this features is enabled.
const autoRegenerateTags = textDocument => {
const config = vscode.workspace.getConfiguration("ctagsymbols")
if(config.get("regenerateOnSave")) {
const tagsFile = config.get("tagsFileName")
const commandTemplate = config.get("regenerateCommand")
const workspace = vscode.workspace.workspaceFolders.find(ws =>
textDocument.fileName.startsWith(ws.uri.fsPath)
)
if(workspace) {
regenerateTags(workspace.uri.fsPath, tagsFile, commandTemplate)
}
}
}