-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathcache.ts
662 lines (620 loc) · 24.1 KB
/
cache.ts
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import * as vscode from 'vscode'
import * as fs from 'fs'
import * as path from 'path'
import os from 'os'
import micromatch from 'micromatch'
import { performance } from 'perf_hooks'
import { lw } from '../lw'
import type { FileCache } from '../types'
import * as utils from '../utils/utils'
import { InputFileRegExp } from '../utils/inputfilepath'
const logger = lw.log('Cacher')
const caches: Map<string, FileCache> = new Map()
const promises: Map<string, Promise<void>> = new Map()
export const cache = {
add,
get,
paths,
promises,
getIncludedTeX,
getIncludedBib,
getFlsChildren,
wait,
reset,
refreshCache,
refreshCacheAggressive,
loadFlsFile
}
lw.watcher.src.onChange((filePath: string) => {
if (canCache(filePath)) {
void refreshCache(filePath)
}
})
lw.watcher.src.onDelete((filePath: string) => {
if (get(filePath) === undefined) {
caches.delete(filePath)
logger.log(`Removed ${filePath} .`)
}
})
lw.onDispose({ dispose: () => reset() })
/**
* Checks if a file path can be cached based on its extension and exclusion
* criteria.
*
* @param {string} filePath - The file path to be checked.
* @returns {boolean} - True if the file can be cached, false otherwise.
*/
function canCache(filePath: string): boolean {
return lw.file.hasTeXExt(path.extname(filePath)) && !filePath.includes('expl3-code.tex')
}
/**
* Checks if a file path is excluded based on user-defined globs in
* 'latex.watch.files.ignore'.
*
* @param {string} filePath - The file path to be checked.
* @returns {boolean} - True if the file is excluded, false otherwise.
*/
function isExcluded(filePath: string): boolean {
const globsToIgnore = vscode.workspace.getConfiguration('latex-workshop').get('latex.watch.files.ignore') as string[]
const format = (str: string): string => (os.platform() === 'win32' ? str.replace(/\\/g, '/') : str)
return micromatch.some(filePath, globsToIgnore, { format })
}
/**
* Adds a file path to the watcher if it is not excluded.
*
* @param {string} filePath - The file path to be added.
*/
function add(filePath: string) {
if (isExcluded(filePath)) {
logger.log(`Ignored ${filePath} .`)
return
}
if (!lw.watcher.src.has(filePath)) {
logger.log(`Adding ${filePath} .`)
lw.watcher.src.add(filePath)
}
}
/**
* Retrieves the cache for a specific file path.
*
* @param {string} filePath - The file path to retrieve the cache for.
* @returns {FileCache | undefined} - The cache for the specified file path, or
* undefined if not found.
*/
function get(filePath: string): FileCache | undefined {
return caches.get(filePath)
}
/**
* Retrieves an array of all cached file paths.
*
* @returns {string[]} - An array of cached file paths.
*/
function paths(): string[] {
return Array.from(caches.keys())
}
/**
* Waits for a file to be cached, refreshing if necessary.
*
* The function waits for the specified file to be either cached or a promise to
* be created, with a maximum wait time determined by the 'seconds' parameter.
* If the file is not cached or no promise is created within the specified time,
* it forcefully refreshes the cache for the file and returns the corresponding
* promise.
*
* @param {string} filePath - The file path to wait for.
* @param {number} seconds - The maximum wait time in seconds.
* @returns {Promise<Promise<void> | undefined>} - A promise resolving when the file is
* cached, or undefined if an error occurs.
*/
async function wait(filePath: string, seconds: number = 2): Promise<Promise<void> | undefined> {
let waited = 0
while (promises.get(filePath) === undefined && get(filePath) === undefined) {
// Just open vscode, has not cached, wait for a bit?
await new Promise(resolve => setTimeout(resolve, 100))
waited++
if (waited >= seconds * 10) {
// Waited for two seconds before starting cache. Really?
logger.log(`Error loading cache: ${filePath} . Forcing.`)
await refreshCache(filePath)
break
}
}
return promises.get(filePath)
}
/**
* Resets the watchers and clears all caches.
*/
function reset() {
lw.watcher.src.reset()
lw.watcher.bib.reset()
// lw.watcher.pdf.reset()
Object.keys(caches).forEach(filePath => caches.delete(filePath))
}
let cachingFilesCount = 0
/**
* Refreshes the cache for a specific file path.
*
* The function refreshes the cache for the specified file path. If the file is
* excluded or cannot be cached, it skips the refresh. After the cache is
* refreshed, it updates the Abstract Syntax Tree (AST) and various elements in
* the file cache.
*
* The function also utilizes the 'cachingFilesCount' variable, which is a count
* of the number of files currently being cached. This count is used to
* determine when all files have been successfully cached. Once the caching
* process for a file is completed, it decrements the count and checks if it was
* the last file being cached. If so, it triggers a reconstruction of the
* structure viewer. This ensures that the structure viewer is updated only
* after all file caches have been refreshed.
*
* @param {string} filePath - The file path to refresh the cache for.
* @param {string} rootPath - The root path for resolving relative paths.
* @returns {Promise<Promise<void> | undefined>} - A promise resolving when the cache is
* refreshed, or undefined if the file is excluded or cannot be cached.
*/
async function refreshCache(filePath: string, rootPath?: string): Promise<Promise<void> | undefined> {
if (isExcluded(filePath)) {
logger.log(`Ignored ${filePath} .`)
return
}
if (!canCache(filePath)) {
return
}
logger.log(`Caching ${filePath} .`)
cachingFilesCount++
const openEditor: vscode.TextDocument | undefined = vscode.workspace.textDocuments.find(
document => document.fileName === path.normalize(filePath))
const content = openEditor?.isDirty ? openEditor.getText() : (lw.file.read(filePath) ?? '')
const fileCache: FileCache = {
filePath,
content,
contentTrimmed: utils.stripCommentsAndVerbatim(content),
elements: {},
children: [],
bibfiles: new Set(),
external: {}}
caches.set(filePath, fileCache)
rootPath = rootPath || lw.root.file.path
updateChildren(fileCache, rootPath)
promises.set(
filePath,
updateAST(fileCache).then(() => {
updateElements(fileCache)
}).finally(() => {
lw.lint.label.check()
cachingFilesCount--
promises.delete(filePath)
lw.event.fire(lw.event.FileParsed, filePath)
if (cachingFilesCount === 0) {
void lw.outline.reconstruct()
}
})
)
return promises.get(filePath)
}
let updateCompleter: NodeJS.Timeout
/**
* Refreshes the cache aggressively based on user-defined settings.
*
* The function checks if aggressive cache updating is enabled in the user's
* configuration. If enabled, it schedules a delayed refresh of the cache for
* the specified file path. If the refresh is already scheduled, it cancels the
* existing timeout and schedules a new one. This helps prevent excessive cache
* refreshing during rapid file changes.
*
* @param {string} filePath - The file path to refresh the cache for.
*/
function refreshCacheAggressive(filePath: string) {
if (get(filePath) === undefined) {
return
}
const configuration = vscode.workspace.getConfiguration('latex-workshop')
if (configuration.get('intellisense.update.aggressive.enabled')) {
if (updateCompleter) {
clearTimeout(updateCompleter)
}
updateCompleter = setTimeout(async () => {
await refreshCache(filePath, lw.root.file.path)
await loadFlsFile(lw.root.file.path || filePath)
}, configuration.get('intellisense.update.delay', 1000))
}
}
/**
* Updates the Abstract Syntax Tree (AST) for a given file cache using parser.
*
* @param {FileCache} fileCache - The file cache to update the AST for.
*/
async function updateAST(fileCache: FileCache) {
logger.log(`Parse LaTeX AST: ${fileCache.filePath} .`)
fileCache.ast = await lw.parser.parse.tex(fileCache.content)
logger.log(`Parsed LaTeX AST: ${fileCache.filePath} .`)
}
/**
* Updates the children of a file cache based on input files and external
* documents.
*
* @param {FileCache} fileCache - The file cache to update the children for.
* @param {string} rootPath - The root path for resolving relative paths.
*/
function updateChildren(fileCache: FileCache, rootPath: string | undefined) {
rootPath = rootPath || fileCache.filePath
updateChildrenInput(fileCache, rootPath)
updateChildrenXr(fileCache, rootPath)
logger.log(`Updated inputs of ${fileCache.filePath} .`)
}
/**
* Parses input files from the content of a file cache and updates the children
* array.
*
* The function uses a regular expression to find input files in the trimmed
* content of the specified file cache. It adds each identified input file to
* the children array, and if the file is not already being watched, it adds it
* to the watcher and triggers a refresh of its cache.
*
* @param {FileCache} fileCache - The file cache to update the input children for.
* @param {string} rootPath - The root path for resolving relative paths.
*/
function updateChildrenInput(fileCache: FileCache, rootPath: string) {
const inputFileRegExp = new InputFileRegExp()
while (true) {
const result = inputFileRegExp.exec(fileCache.contentTrimmed, fileCache.filePath, rootPath)
if (!result) {
break
}
if (!fs.existsSync(result.path) || path.relative(result.path, rootPath) === '') {
continue
}
fileCache.children.push({
index: result.match.index,
filePath: result.path
})
logger.log(`Input ${result.path} from ${fileCache.filePath} .`)
if (lw.watcher.src.has(result.path)) {
continue
}
add(result.path)
void refreshCache(result.path, rootPath)
}
}
/**
* Parses external document references from the content of a file cache and
* updates the children array.
*
* The function uses a regular expression to find external document references
* in the trimmed content of the specified file cache. It resolves the paths of
* external documents and adds them to the children array. If an external
* document is not already being watched, it adds it to the watcher and triggers
* a refresh of its cache.
*
* @param {FileCache} fileCache - The file cache to update the external document
* children for.
* @param {string} rootPath - The root path for resolving relative paths.
*/
function updateChildrenXr(fileCache: FileCache, rootPath: string) {
const externalDocRegExp = /\\externaldocument(?:\[(.*?)\])?\{(.*?)\}/g
while (true) {
const result = externalDocRegExp.exec(fileCache.contentTrimmed)
if (!result) {
break
}
const texDirs = vscode.workspace.getConfiguration('latex-workshop').get('latex.texDirs') as string[]
const externalPath = utils.resolveFile([path.dirname(fileCache.filePath), path.dirname(rootPath), ...texDirs], result[2])
if (!externalPath || !fs.existsSync(externalPath) || path.relative(externalPath, rootPath) === '') {
logger.log(`Failed resolving external ${result[2]} . Tried ${externalPath} ` +
(externalPath && path.relative(externalPath, rootPath) === '' ? ', which is root.' : '.'))
continue
}
const rootCache = get(rootPath)
if (rootCache !== undefined) {
rootCache.external[externalPath] = result[1] || ''
logger.log(`External document ${externalPath} from ${fileCache.filePath} .` + (result[1] ? ` Prefix is ${result[1]}`: ''))
}
if (lw.watcher.src.has(externalPath)) {
continue
}
add(externalPath)
void refreshCache(externalPath, externalPath)
}
}
/**
* Updates various elements in the file cache after parsing the LaTeX Abstract
* Syntax Tree (AST).
*
* The function updates elements in the specified file cache based on the parsed
* LaTeX AST. It includes updating citations, packages, references, glossaries,
* environments, commands, and input graphics paths. Additionally, it updates
* the bibliography files referenced in the file content and logs the time taken
* to complete the update.
*
* @param {FileCache} fileCache - The file cache to update the elements for.
*/
function updateElements(fileCache: FileCache) {
const start = performance.now()
lw.completion.citation.parse(fileCache)
// Package parsing must be before command and environment.
lw.completion.usepackage.parse(fileCache)
lw.completion.reference.parse(fileCache)
lw.completion.glossary.parse(fileCache)
lw.completion.environment.parse(fileCache)
lw.completion.macro.parse(fileCache)
lw.completion.subsuperscript.parse(fileCache)
lw.completion.input.parseGraphicsPath(fileCache)
updateBibfiles(fileCache)
const elapsed = performance.now() - start
logger.log(`Updated elements in ${elapsed.toFixed(2)} ms: ${fileCache.filePath} .`)
}
/**
* Updates bibliography files in the file cache based on the content of the
* LaTeX file.
*
* The function uses regular expressions to find bibliography file references in
* the content of the specified file cache. It extracts the paths of the
* bibliography files and adds them to the bibliography files set in the cache.
* If a bibliography file is not already being watched, it adds it to the
* bibliography watcher.
*
* @param {FileCache} fileCache - The file cache to update the bibliography files
* for.
*/
function updateBibfiles(fileCache: FileCache) {
const bibReg = /(?:\\(?:bibliography|addbibresource)(?:\[[^[\]{}]*\])?){(?:\\subfix{)?([\s\S]+?)(?:\})?}|(?:\\putbib)\[(.+?)\]/gm
while (true) {
const result = bibReg.exec(fileCache.contentTrimmed)
if (!result) {
break
}
const bibs = (result[1] ? result[1] : result[2]).split(',').map(bib => bib.trim())
for (const bib of bibs) {
const bibPaths = lw.file.getBibPath(bib, path.dirname(fileCache.filePath))
for (const bibPath of bibPaths) {
if (isExcluded(bibPath)) {
continue
}
fileCache.bibfiles.add(bibPath)
logger.log(`Bib ${bibPath} from ${fileCache.filePath} .`)
if (!lw.watcher.bib.has(bibPath)) {
lw.watcher.bib.add(bibPath)
}
}
}
}
}
/**
* Parses the content of a `.fls` file attached to the given `filePath` and
* updates caches accordingly.
*
* The function parses the content of a `.fls` file associated with the
* specified `filePath`. It identifies input files and output files, updates the
* cache's children, and checks for `.aux` files to parse for possible `.bib`
* files. This function is typically called after a successful build to look for
* the root file and compute the cachedContent tree.
*
* @param {string} filePath - The path of a LaTeX file.
*/
async function loadFlsFile(filePath: string) {
const flsPath = lw.file.getFlsPath(filePath)
if (flsPath === undefined) {
return
}
logger.log(`Parsing .fls ${flsPath} .`)
const rootDir = path.dirname(filePath)
const outDir = lw.file.getOutDir(filePath)
const ioFiles = parseFlsContent(fs.readFileSync(flsPath).toString(), rootDir)
for (const inputFile of ioFiles.input) {
// Drop files that are also listed as OUTPUT or should be ignored
if (ioFiles.output.includes(inputFile) ||
isExcluded(inputFile) ||
!fs.existsSync(inputFile)) {
continue
}
if (inputFile === filePath || lw.watcher.src.has(inputFile)) {
// Drop the current rootFile often listed as INPUT
// Drop any file that is already watched as it is handled by
// onWatchedFileChange.
continue
}
const inputExt = path.extname(inputFile)
if (inputExt === '.tex') {
if (get(filePath) === undefined) {
logger.log(`Cache not finished on ${filePath} when parsing fls, try re-cache.`)
await refreshCache(filePath)
}
// It might be possible that `filePath` is excluded from caching.
const fileCache = get(filePath)
if (fileCache !== undefined) {
// Parse tex files as imported subfiles.
fileCache.children.push({
index: Number.MAX_VALUE,
filePath: inputFile
})
add(inputFile)
logger.log(`Found ${inputFile} from .fls ${flsPath} , caching.`)
void refreshCache(inputFile, filePath)
} else {
logger.log(`Cache not finished on ${filePath} when parsing fls.`)
}
} else if (!lw.watcher.src.has(inputFile) && !['.aux', '.out'].includes(inputExt)) {
// Watch non-tex files. aux and out are excluded because they are auto-generated during the building process
add(inputFile)
}
}
for (const outputFile of ioFiles.output) {
if (path.extname(outputFile) === '.aux' && fs.existsSync(outputFile)) {
logger.log(`Found .aux ${filePath} from .fls ${flsPath} , parsing.`)
parseAuxFile(outputFile, path.dirname(outputFile).replace(outDir, rootDir))
logger.log(`Parsed .aux ${filePath} .`)
}
}
logger.log(`Parsed .fls ${flsPath} .`)
}
/**
* Parses the content of a `.fls` file and extracts input and output files.
*
* The function uses a regular expression to match lines in the `.fls` file
* indicating input and output files. It then resolves the paths of these files
* relative to the root directory and returns an object with arrays of input and
* output files.
*
* @param {string} content - The content of the `.fls` file.
* @param {string} rootDir - The root directory for resolving relative paths.
* @returns {{input: string[], output: string[]}} - An object containing arrays
* of input and output files.
*/
function parseFlsContent(content: string, rootDir: string): {input: string[], output: string[]} {
const inputFiles: Set<string> = new Set()
const outputFiles: Set<string> = new Set()
const regex = /^(?:(INPUT)\s*(.*))|(?:(OUTPUT)\s*(.*))$/gm
// regex groups
// #1: an INPUT entry --> #2 input file path
// #3: an OUTPUT entry --> #4: output file path
while (true) {
const result = regex.exec(content)
if (!result) {
break
}
if (result[1]) {
const inputFilePath = path.resolve(rootDir, result[2])
if (inputFilePath) {
inputFiles.add(inputFilePath)
}
} else if (result[3]) {
const outputFilePath = path.resolve(rootDir, result[4])
if (outputFilePath) {
outputFiles.add(outputFilePath)
}
}
}
return {input: Array.from(inputFiles), output: Array.from(outputFiles)}
}
/**
* Parses a `.aux` file to extract bibliography file references and updates the
* caches.
*
* The function reads the content of the specified `.aux` file and uses a
* regular expression to find bibliography file references. It then updates the
* cache with the discovered bibliography files.
*
* @param {string} filePath - The path of the `.aux` file.
* @param {string} srcDir - The source directory for resolving relative paths.
*/
function parseAuxFile(filePath: string, srcDir: string) {
const content = fs.readFileSync(filePath).toString()
const regex = /^\\bibdata{(.*)}$/gm
while (true) {
const result = regex.exec(content)
if (!result) {
return
}
const bibs = (result[1] ? result[1] : result[2]).split(',').map((bib) => { return bib.trim() })
for (const bib of bibs) {
const bibPaths = lw.file.getBibPath(bib, srcDir)
for (const bibPath of bibPaths) {
if (isExcluded(bibPath)) {
continue
}
if (lw.root.file.path && !get(lw.root.file.path)?.bibfiles.has(bibPath)) {
get(lw.root.file.path)?.bibfiles.add(bibPath)
logger.log(`Found .bib ${bibPath} from .aux ${filePath} .`)
}
if (!lw.watcher.bib.has(bibPath)) {
lw.watcher.bib.add(bibPath)
}
}
}
}
}
/**
* Returns an array of included bibliography files in the specified LaTeX file.
*
* The function recursively traverses the included LaTeX files starting from the
* specified file path (or the root file if undefined) and collects the
* bibliography files. It avoids duplicates and returns an array of unique
* included bibliography files.
*
* @param {string | undefined} filePath - The path of the LaTeX file. If
* undefined, traces from the root file.
* @param {string[]} includedBib - An array to store the included bibliography
* files (default: []).
* @returns {string[]} - An array of included bibliography files.
*/
function getIncludedBib(filePath?: string, includedBib: string[] = []): string[] {
filePath = filePath ?? lw.root.file.path
if (filePath === undefined) {
return []
}
const fileCache = get(filePath)
if (fileCache === undefined) {
return []
}
const checkedTeX = [ filePath ]
includedBib.push(...fileCache.bibfiles)
for (const child of fileCache.children) {
if (checkedTeX.includes(child.filePath)) {
// Already parsed
continue
}
getIncludedBib(child.filePath, includedBib)
}
// Make sure to return an array with unique entries
return Array.from(new Set(includedBib))
}
/**
* Returns an array of included LaTeX files in the specified LaTeX file.
*
* The function recursively traverses the included LaTeX files starting from the
* specified file path (or the root file if undefined) and collects the LaTeX
* files. It avoids duplicates and returns an array of unique included LaTeX
* files. The 'cachedOnly' parameter controls whether to include only cached
* files or all included files.
*
* @param {string | undefined} filePath - The path of the LaTeX file. If
* undefined, traces from the root file.
* @param {string[]} includedTeX - An array to store the included LaTeX files
* (default: []).
* @param {boolean} cachedOnly - Indicates whether to include only cached files
* (default: true).
* @returns {string[]} - An array of included LaTeX files.
*/
function getIncludedTeX(filePath?: string, includedTeX: string[] = [], cachedOnly: boolean = true): string[] {
filePath = filePath ?? lw.root.file.path
if (filePath === undefined) {
return []
}
const fileCache = get(filePath)
if (cachedOnly && fileCache === undefined) {
return []
}
includedTeX.push(filePath)
if (fileCache === undefined) {
return []
}
for (const child of fileCache.children) {
if (includedTeX.includes(child.filePath)) {
// Already included
continue
}
getIncludedTeX(child.filePath, includedTeX, cachedOnly)
}
return includedTeX
}
/**
* Returns an array of input files from the `.fls` file associated with the
* specified LaTeX file.
*
* @param {string} texFile - The path of the LaTeX file.
* @returns {string[]} - An array of input files from the `.fls` file.
*
* The function reads the content of the `.fls` file associated with the
* specified LaTeX file, parses the input files, and returns an array of
* included input files. It is used to identify the dependencies of a LaTeX file
* after a successful build.
*/
function getFlsChildren(texFile: string): string[] {
const flsPath = lw.file.getFlsPath(texFile)
if (flsPath === undefined) {
return []
}
const rootDir = path.dirname(texFile)
const ioFiles = parseFlsContent(fs.readFileSync(flsPath).toString(), rootDir)
return ioFiles.input
}