Skip to content

Commit

Permalink
Merge pull request #1928 from James-Yu/1919-watch-pdf-files
Browse files Browse the repository at this point in the history
Watch external pdf files for automatic reload
  • Loading branch information
jlelong committed Feb 3, 2020
2 parents 62f3415 + aa5e6cc commit 1d783ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/components/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ export class Manager {

private extension: Extension
private fileWatcher?: chokidar.FSWatcher
private pdfWatcher?: chokidar.FSWatcher
private bibWatcher?: chokidar.FSWatcher
private filesWatched: string[] = []
private pdfsWatched: string[] = []
private bibsWatched: string[] = []
private watcherOptions: chokidar.WatchOptions

Expand All @@ -47,8 +49,10 @@ export class Manager {
useFsEvents: false,
usePolling,
interval,
binaryInterval: Math.max(interval, 1000)
binaryInterval: Math.max(interval, 1000),
awaitWriteFinish: true
}
this.initiatePdfWatcher()
}

/* Returns the output directory developed according to the input tex path
Expand Down Expand Up @@ -717,6 +721,37 @@ export class Manager {
}
}

private initiatePdfWatcher() {
if (this.pdfWatcher !== undefined) {
return
}
this.extension.logger.addLogMessage('Creating file watcher for .pdf files.')
this.pdfWatcher = chokidar.watch([], this.watcherOptions)
this.pdfWatcher.on('change', (file: string) => this.onWatchedPdfChanged(file))
this.pdfWatcher.on('unlink', (file: string) => this.onWatchedPdfDeleted(file))
}

private onWatchedPdfChanged(file: string) {
this.extension.logger.addLogMessage(`PDF file watcher - responding to change in ${file}`)
this.extension.viewer.refreshExistingViewer()
}

onWatchedPdfDeleted(file: string) {
this.extension.logger.addLogMessage(`PDF file watcher: ${file} deleted.`)
if (this.pdfWatcher) {
this.pdfWatcher.unwatch(file)
}
this.pdfsWatched.splice(this.pdfsWatched.indexOf(file), 1)
}

watchPdfFile(pdfPath: string) {
if (this.pdfWatcher && !this.pdfsWatched.includes(pdfPath)) {
this.extension.logger.addLogMessage(`Adding .pdf file ${pdfPath} to pdf file watcher.`)
this.pdfWatcher.add(pdfPath)
this.pdfsWatched.push(pdfPath)
}
}

private buildOnFileChanged(file: string, bibChanged: boolean = false) {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
if (configuration.get('latex.autoBuild.run') as string !== 'onFileChange') {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export function activate(context: vscode.ExtensionContext) {
}

if (e.languageId === 'pdf') {
extension.manager.watchPdfFile(e.uri.fsPath)
vscode.commands.executeCommand('workbench.action.closeActiveEditor').then(() => {
extension.commander.pdf(e.uri)
})
Expand Down

0 comments on commit 1d783ba

Please sign in to comment.