Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch external pdf files for automatic reload #1928

Merged
merged 2 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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