diff --git a/src/compile/build.ts b/src/compile/build.ts index 6556d7fdd..13785d907 100644 --- a/src/compile/build.ts +++ b/src/compile/build.ts @@ -33,8 +33,8 @@ export class Builder { private readonly MAX_PRINT_LINE = '10000' constructor() { - lw.cacher.src.onChange(filePath => this.buildOnFileChanged(filePath)) - lw.cacher.bib.onChange(filePath => this.buildOnFileChanged(filePath, true)) + lw.watcher.src.onChange(filePath => this.buildOnFileChanged(filePath)) + lw.watcher.bib.onChange(filePath => this.buildOnFileChanged(filePath, true)) // Check if pdflatex is available, and is MikTeX distro try { const pdflatexVersion = cp.execSync('pdflatex --version') diff --git a/src/completion/completer/citation.ts b/src/completion/completer/citation.ts index 90070bd48..1997e2511 100644 --- a/src/completion/completer/citation.ts +++ b/src/completion/completer/citation.ts @@ -133,9 +133,9 @@ export class Citation implements IProvider { private readonly bibEntries = new Map() constructor() { - lw.cacher.bib.onCreate(filePath => this.parseBibFile(filePath)) - lw.cacher.bib.onChange(filePath => this.parseBibFile(filePath)) - lw.cacher.bib.onDelete(filePath => this.removeEntriesInFile(filePath)) + lw.watcher.bib.onCreate(filePath => this.parseBibFile(filePath)) + lw.watcher.bib.onChange(filePath => this.parseBibFile(filePath)) + lw.watcher.bib.onDelete(filePath => this.removeEntriesInFile(filePath)) } provideFrom(_result: RegExpMatchArray, args: IProviderArgs) { diff --git a/src/core/cache.ts b/src/core/cache.ts index fb5ec578b..39979cedb 100644 --- a/src/core/cache.ts +++ b/src/core/cache.ts @@ -12,7 +12,6 @@ import type { ICompletionItem } from '../completion/latex' import { InputFileRegExp } from '../utils/inputfilepath' import * as CacherUtils from './cacherlib/cacherutils' import * as PathUtils from './cacherlib/pathutils' -import { Watcher } from './cacherlib/watcher' import { getLogger } from '../utils/logging/logger' import { parser } from '../parse/parser' import { performance } from 'perf_hooks' @@ -59,19 +58,16 @@ export interface Cache { export class Cacher { private readonly caches: {[filePath: string]: Cache} = {} - readonly src: Watcher = new Watcher() - readonly pdf: Watcher = new Watcher('.pdf') - readonly bib: Watcher = new Watcher('.bib') private caching = 0 private promises: {[filePath: string]: Promise} = {} constructor() { - this.src.onChange((filePath: string) => { + lw.watcher.src.onChange((filePath: string) => { if (CacherUtils.canCache(filePath)) { void this.refreshCache(filePath) } }) - this.src.onDelete((filePath: string) => { + lw.watcher.src.onDelete((filePath: string) => { if (filePath in this.caches) { delete this.caches[filePath] logger.log(`Removed ${filePath} .`) @@ -84,9 +80,9 @@ export class Cacher { logger.log(`Ignored ${filePath} .`) return } - if (!this.src.has(filePath)) { + if (!lw.watcher.src.has(filePath)) { logger.log(`Adding ${filePath} .`) - this.src.add(filePath) + lw.watcher.src.add(filePath) } } @@ -127,9 +123,9 @@ export class Cacher { } reset() { - this.src.reset() - this.bib.reset() - this.pdf.reset() + lw.watcher.src.reset() + lw.watcher.bib.reset() + lw.watcher.pdf.reset() Object.keys(this.caches).forEach(filePath => delete this.caches[filePath]) } @@ -204,7 +200,7 @@ export class Cacher { }) logger.log(`Input ${result.path} from ${cache.filePath} .`) - if (this.src.has(result.path)) { + if (lw.watcher.src.has(result.path)) { continue } this.add(result.path) @@ -232,7 +228,7 @@ export class Cacher { logger.log(`External document ${externalPath} from ${cache.filePath} .` + (result[1] ? ` Prefix is ${result[1]}`: '')) - if (this.src.has(externalPath)) { + if (lw.watcher.src.has(externalPath)) { continue } this.add(externalPath) @@ -270,8 +266,8 @@ export class Cacher { for (const bibPath of bibPaths) { cache.bibfiles.add(bibPath) logger.log(`Bib ${bibPath} from ${cache.filePath} .`) - if (!this.bib.has(bibPath)) { - this.bib.add(bibPath) + if (!lw.watcher.bib.has(bibPath)) { + lw.watcher.bib.add(bibPath) } } } @@ -306,7 +302,7 @@ export class Cacher { !fs.existsSync(inputFile)) { continue } - if (inputFile === filePath || this.src.has(inputFile)) { + 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. @@ -330,7 +326,7 @@ export class Cacher { } else { logger.log(`Cache not finished on ${filePath} when parsing fls.`) } - } else if (!this.src.has(inputFile)) { + } else if (!lw.watcher.src.has(inputFile)) { // Watch non-tex files. this.add(inputFile) } @@ -364,8 +360,8 @@ export class Cacher { this.get(lw.manager.rootFile)?.bibfiles.add(bibPath) logger.log(`Found .bib ${bibPath} from .aux ${filePath} .`) } - if (!this.bib.has(bibPath)) { - this.bib.add(bibPath) + if (!lw.watcher.bib.has(bibPath)) { + lw.watcher.bib.add(bibPath) } } } diff --git a/src/core/root-file.ts b/src/core/root-file.ts index 1b5858cc3..e658e012f 100644 --- a/src/core/root-file.ts +++ b/src/core/root-file.ts @@ -39,7 +39,7 @@ export class Manager { constructor() { this.registerSetEnvVar() - lw.cacher.src.onDelete(filePath => { + lw.watcher.src.onDelete(filePath => { if (filePath === this.rootFile) { this.rootFile = undefined void this.findRoot() @@ -304,7 +304,7 @@ export class Manager { // We also clean the completions from the old project lw.completer.input.reset() lw.dupLabelDetector.reset() - lw.cacher.src.reset() + lw.watcher.src.reset() lw.cacher.add(rootFile) void lw.cacher.refreshCache(rootFile).then(async () => { // We need to parse the fls to discover file dependencies when defined by TeX macro diff --git a/src/core/cacherlib/watcher.ts b/src/core/watcher.ts similarity index 93% rename from src/core/cacherlib/watcher.ts rename to src/core/watcher.ts index b440b8df5..92954eed4 100644 --- a/src/core/cacherlib/watcher.ts +++ b/src/core/watcher.ts @@ -1,21 +1,21 @@ import * as vscode from 'vscode' import * as fs from 'fs' import * as path from 'path' -import { lw } from '../../lw' -import * as eventbus from '../event-bus' -import { getLogger } from '../../utils/logging/logger' -import { isBinary } from '../root-file' +import { lw } from '../lw' +import * as eventbus from './event-bus' +import { getLogger } from '../utils/logging/logger' +import { isBinary } from './root-file' const logger = getLogger('Cacher', 'Watcher') -export class Watcher { +class Watcher { private readonly watchers: {[folder: string]: {watcher: vscode.FileSystemWatcher, files: Set}} = {} private readonly onCreateHandlers: Set<(filePath: string) => void> = new Set() private readonly onChangeHandlers: Set<(filePath: string) => void> = new Set() private readonly onDeleteHandlers: Set<(filePath: string) => void> = new Set() private readonly polling: {[filePath: string]: {time: number, size: number}} = {} - constructor(private readonly fileExt: string = '.*') {} + constructor(private readonly fileExt: '.*' | '.bib' | '.pdf' = '.*') {} onCreate(handler: (filePath: string) => void) { this.onCreateHandlers.add(handler) @@ -130,3 +130,9 @@ export class Watcher { logger.log('Reset.') } } + +export const watcher = { + src: new Watcher(), + pdf: new Watcher('.pdf'), + bib: new Watcher('.bib') +} diff --git a/src/lw.ts b/src/lw.ts index 5c028b90f..39b8ace8c 100644 --- a/src/lw.ts +++ b/src/lw.ts @@ -1,4 +1,6 @@ import * as vscode from 'vscode' +import type { watcher } from './core/watcher' + import type { Builder } from './compile/build' import type { Cacher } from './core/cache' import type { Cleaner } from './extras/cleaner' @@ -29,6 +31,7 @@ import type * as commands from './core/commands' export const lw = { extensionContext: Object.create(null) as vscode.ExtensionContext, extensionRoot: '', + watcher: {} as typeof watcher, eventBus: Object.create(null) as EventBus, configuration: Object.create(null) as Configuration, lwfs: Object.create(null) as LwFileSystem, diff --git a/src/main.ts b/src/main.ts index a4897fe75..a22b7f902 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,10 @@ import * as vscode from 'vscode' import * as path from 'path' import { lw, registerDisposable } from './lw' + +import { watcher } from './core/watcher' +lw.watcher = watcher + import { pdfViewerHookProvider, pdfViewerPanelSerializer } from './preview/viewer' import { MathPreviewPanelSerializer } from './extras/math-preview-panel' import { BibtexCompleter } from './completion/bibtex' diff --git a/src/preview/viewer.ts b/src/preview/viewer.ts index b9f346e54..f4ba1d57a 100644 --- a/src/preview/viewer.ts +++ b/src/preview/viewer.ts @@ -23,7 +23,7 @@ export { pdfViewerPanelSerializer } from './viewerlib/pdfviewerpanel' export class Viewer { constructor() { - lw.cacher.pdf.onChange(pdfPath => { + lw.watcher.pdf.onChange(pdfPath => { if (lw.builder.isOutputPDF(pdfPath)) { this.refreshExistingViewer(pdfPath) } @@ -128,7 +128,7 @@ export class Viewer { } const pdfFileUri = vscode.Uri.file(pdfFile) viewerManager.createClientSet(pdfFileUri) - lw.cacher.pdf.add(pdfFileUri.fsPath) + lw.watcher.pdf.add(pdfFileUri.fsPath) try { logger.log(`Serving PDF file at ${url}`) await vscode.env.openExternal(vscode.Uri.parse(url, true)) diff --git a/src/preview/viewerlib/pdfviewermanager.ts b/src/preview/viewerlib/pdfviewermanager.ts index 11ce00bb3..23c0b8d89 100644 --- a/src/preview/viewerlib/pdfviewermanager.ts +++ b/src/preview/viewerlib/pdfviewermanager.ts @@ -50,7 +50,7 @@ class PdfViewerManager { initiatePdfViewerPanel(pdfPanel: PdfViewerPanel): PdfViewerPanel | undefined { const pdfFileUri = pdfPanel.pdfFileUri - lw.cacher.pdf.add(pdfFileUri.fsPath) + lw.watcher.pdf.add(pdfFileUri.fsPath) this.createClientSet(pdfFileUri) const panelSet = this.getPanelSet(pdfFileUri) if (!panelSet) {