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

How do you auto reload pdf after compiling? #355

Open
xiebruce opened this issue Dec 5, 2021 · 28 comments
Open

How do you auto reload pdf after compiling? #355

xiebruce opened this issue Dec 5, 2021 · 28 comments

Comments

@xiebruce
Copy link

xiebruce commented Dec 5, 2021

I was able to compile the test.ly by pressing command+s on my Mac and it generate a test.pdf file, I open this pdf file in a split window on vscode.
image

As you known, we need to repeat this procedure: write code→save(compile)→preview, but I can't do this, every time I save(compile) the test.ly file, the test.pdf file not auto reloading, I have to close it and reopen to see the change.

How can I solve this problem?

vslilypond

v1.7.3

vscode

Version: 1.62.3 (Universal)
Commit: ccbaa2d27e38e5afa3e5c21c1c7bef4657064247
Date: 2021-11-17T07:59:13.865Z (2 wks ago)
Electron: 13.5.2
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Darwin x64 20.6.0

macOS

Big Sur 11.5.1 (20G80)
@Apostolique
Copy link

Did you get the LilyPond PDF Preview extension for vscode?

@xiebruce
Copy link
Author

xiebruce commented Dec 5, 2021

Did you get the LilyPond PDF Preview extension for vscode?

No, I didn't, because I think it's included in VSLilyPond, see here and here

image

image

@Apostolique
Copy link

But were you using it?

@xiebruce
Copy link
Author

xiebruce commented Dec 5, 2021

But were you using it?

I think yes
image

And I found "LilyPond PDF Preview" has been installed automatically
image

when I double click on the pdf file that generated by lilypond in vscode, it's opened by the LilyPond PDF Preview plugin
reopen by pdf lilypond previewer

@lhl2617
Copy link
Owner

lhl2617 commented Dec 5, 2021

This is strange, on my Mac with 11.6 Big Sur, VSLilyPond 1.60 I can’t reproduce this issue

@xiebruce
Copy link
Author

xiebruce commented Dec 6, 2021

This is strange, on my Mac with 11.6 Big Sur, VSLilyPond 1.60 I can’t reproduce this issue

Can you try the latest version of vsccode(Version: 1.62.3 (Universal)) and VSLilyPond(v1.7.3)? maybe you can reproduce this issue.

Here is my demo, if it is as expected, the pdf will auto reload after I save(compile) the test.ly right? But it didn't!
vslilypond-pdf-not-auto-reloading

@Apostolique
Copy link

Do the other features work like clicking notes to go from pdf to code and from code to pdf?

@xiebruce
Copy link
Author

xiebruce commented Dec 6, 2021

Do the other features work like clicking notes to go from pdf to code and from code to pdf?

Pdf to code: Yes, it works
Code to pdf: No, do I need to hold any key before clicking? Actually I tried hold option, cmd, shift, none of these works.

click-pdf-to-code_optimized

@Apostolique
Copy link

I forgot that was something from the command pallet (Ctrl + Shift + P): LilyPond PDF Preview: Go to PDF location from Cursor.

If I open the vscode Output panel and select VSLilyPond: PDF Preview, when I save a LilyPond file I see this log:

[LOG]: [WEBVIEW]: reload
[LOG]: [WEBVIEW]: pagesinit
[LOG]: [WEBVIEW]: allPagesLoaded
[LOG]: Clearing links for f:\Compositions\Sample01.pdf
[LOG]: Links cleared for f:\Compositions\Sample01.pdf
[LOG]: [WEBVIEW]: Sent clear-links
[LOG]: [WEBVIEW]: Finished handling textedits
[LOG]: [WEBVIEW]: Received link-register-ready
[LOG]: [WEBVIEW]: Finished registering links

@xiebruce
Copy link
Author

xiebruce commented Dec 6, 2021

I forgot that was something from the command pallet (Ctrl + Shift + P): LilyPond PDF Preview: Go to PDF location from Cursor.

If I open the vscode Output panel and select VSLilyPond: PDF Preview, when I save a LilyPond file I see this log:

[LOG]: [WEBVIEW]: reload
[LOG]: [WEBVIEW]: pagesinit
[LOG]: [WEBVIEW]: allPagesLoaded
[LOG]: Clearing links for f:\Compositions\Sample01.pdf
[LOG]: Links cleared for f:\Compositions\Sample01.pdf
[LOG]: [WEBVIEW]: Sent clear-links
[LOG]: [WEBVIEW]: Finished handling textedits
[LOG]: [WEBVIEW]: Received link-register-ready
[LOG]: [WEBVIEW]: Finished registering links

There's no LilyPond PDF Preview: Go to PDF location from Cursor option on my vscode
go-to-pdf_optimized

I tried to uninstall and reinstall it, but I can't
image

@Apostolique
Copy link

You got my instructions backward. It's not VSLilyPond: Go to PDF... It's LilyPond PDF Preview: Go to PDF location from Cursor.

Also look in your Output for VSLilyPond: PDF Preview:
image

@xiebruce
Copy link
Author

xiebruce commented Dec 6, 2021

You got my instructions backward. It's not VSLilyPond: Go to PDF... It's LilyPond PDF Preview: Go to PDF location from Cursor.

Also look in your Output for VSLilyPond: PDF Preview: image

Sorry, I found it, but I can't see any log(including reload log) in the VSLilyPond: PDF Preview console
no-reload-log

And I've tried uninstall all lilypond relative extensions and reinstall vslilypond, still the same, no reloading operations can be found.

@xiebruce
Copy link
Author

xiebruce commented Dec 8, 2021

@Apostolique @lhl2617 Hi, according to the info I provided above, do you guys know what's happening on my vscode or vslilypond ?

@Apostolique
Copy link

Apostolique commented Dec 11, 2021

I'm not sure but since it doesn't work you can find any PDF reader that has auto reload for now.

I use SumatraPDF but that's for Windows. I'm sure you can find a similar one for mac.

@xiebruce
Copy link
Author

I'm not sure but since it doesn't work you can find any PDF reader that has auto reload for now.

I use SumatraPDF but that's for Windows. I'm sure you can find a similar one for mac.

Yep, the build-in App "Preview.app" on Mac can do this, but I have to press command+tab(same as alt+tab on Win) to see the change.

@lhl2617
Copy link
Owner

lhl2617 commented Dec 13, 2021

I'm unable to repro your bug, can you try opening a dev console in VSCode (https://stackoverflow.com/questions/30765782/what-is-the-use-of-the-developer-tools-in-vs-code) and see if anything is wrong there?

@xiebruce
Copy link
Author

@lhl2617

Uncaught (in promise) ReferenceError: Cannot access 'd' before initialization
    at pdf.viewer.js:43

image

@Dr-ZeeD
Copy link

Dr-ZeeD commented Feb 5, 2022

I seem to be having the same issue. It had worked for me on Big Sur before. After a completely clean install of VS Code and VSLilypond, the auto-refresh of the PDF preview window (which really uses the LilyPond PDF Preview that comes with VSLilyPond) does not work anymore.

@Dr-ZeeD
Copy link

Dr-ZeeD commented Feb 5, 2022

Interestingly, I found a situation that results in the PDF within VSCode to refresh:

  1. Open a lilypond file and its preview within VSCode. Also, open a Preview (the app) window of the PDF.
  2. Modify the lilypond file and compile it. Right now, neither the VSCode preview nor the Preview should reflect the changes.
  3. Click on the Preview window. It should refresh and show the changes. After a brief period, the VSCode preview will also refresh and show the changes.

@xiebruce
Copy link
Author

xiebruce commented Feb 6, 2022

Interestingly, I found a situation that results in the PDF within VSCode to refresh:

  1. Open a lilypond file and its preview within VSCode. Also, open a Preview (the app) window of the PDF.
  2. Modify the lilypond file and compile it. Right now, neither the VSCode preview nor the Preview should reflect the changes.
  3. Click on the Preview window. It should refresh and show the changes. After a brief period, the VSCode preview will also refresh and show the changes.

I made a test in your condition, it turns out that the we should switch to another app then switch back to vscode after saving the code, only by doing this, the PDF inside vscode(opened by LilyPond PDF Preview) can reload. This has nothing to do with the macOS app "Preview.app".

@Dr-ZeeD
Copy link

Dr-ZeeD commented Feb 6, 2022

I've just tried it with "not Preview.app". Does not work. So far I have only gotten it to work by clicking on Preview.app with the same file.

@Tobertet
Copy link

I have the same problem, and I am using Linux.

Whenever I save, I can see the logs in the Output panel and the PDF is being generated. However, the PDF Preview is not refreshing the output.

@snowskeleton
Copy link

I'm having the same problem.

macOS v12.4 Monterey
VSCode v1.69.1 (Universal)
VSLilyPond v1.7.3
LilyPond PDF Preview v0.2.8 (installed with VSLilyPond)

Switching to Preview.app and back updates the PDF both in Preview.app and in LilyPond PDF Viewer. Switching to an app other than Preview.app doesn't seem to trigger the reload. I also tried opening the PDF (after saving/compiling) with QuickLook (pressing Space bar on macOS in the Finder). QuickLook showed me the updated PDF, but neither Preview.app nor LilyPond PDF Viewer updated (until focusing Preview.app, of course).

@snowskeleton
Copy link

Update: I found that LilyPond PDF Viewer will automatically update if it's let sit long enough. In my experience, it's usually less than a minute, but not consistent. I can not consistently replicate this.

@clojj
Copy link

clojj commented Oct 9, 2022

Hi @lhl2617 ..thanks for this fantastic plugin !

Locally debugging the PDF Preview Extension, I found that (on Linux) compiling with lilypond will generate a new PDF file (with the same name).
Therefore when I created a listener for creation event like this:

        this._watcher.onDidCreate((e) => {
          if (e.toString() === this.resource.toString()) {
            this.reload()
          }
        })

here
https://github.com/lhl2617/VSLilyPond-PDF-preview/blob/master/src/extension/pdf-preview.ts#L62
..automatical reload works fine !

So this yields the somewhat weird question, if all OS's lilypond command treat PDF compilation the same or different (?)

If you want I can create a PR and try to cover all events

@zzzeek
Copy link

zzzeek commented Mar 28, 2023

hi folks, I have this bug. I also don't have ready ability to hack on the plugin itself.

so to work around this, I open my console, select "terminal", and on my Fedora linux machine (things may have to be different on OSX) I run this while loop to refresh the pdf each time it gets recreated, which seems to wake up the pdf viewer:

while [ 1 ]; do sleep 1; find . -name "*.pdf" -mmin -0.02  -exec sh -c "echo 'detected changes on {}'; touch {}; sleep 1 " \;   ; done;

That will run every second, and it will: 1. run a find for .pdf files that were modified less than .02 minutes ago 2. do a touch 3. the sleep another second per "touch", so that it doesn't keep touching the file over and over again.

I organize that into a script refresh.sh and just have it wherever my .ly files are.

(noting I just tried lilypond for the first time about an hour ago)

@leiyi-0612
Copy link

Hi @lhl2617 ..thanks for this fantastic plugin !

Locally debugging the PDF Preview Extension, I found that (on Linux) compiling with lilypond will generate a new PDF file (with the same name). Therefore when I created a listener for creation event like this:

        this._watcher.onDidCreate((e) => {
          if (e.toString() === this.resource.toString()) {
            this.reload()
          }
        })

here https://github.com/lhl2617/VSLilyPond-PDF-preview/blob/master/src/extension/pdf-preview.ts#L62 ..automatical reload works fine !

So this yields the somewhat weird question, if all OS's lilypond command treat PDF compilation the same or different (?)

If you want I can create a PR and try to cover all events

Thank you for offering the solution, I did this on my Mac and it worked!
image

@Noelind
Copy link

Noelind commented Oct 22, 2024

First of all thank you @lhl2617 for making this possible in the first place! This is my first time venturing in Lilypond territory and I figured it'd be good fit for me given my familiarity with VSCode and TeX. And since I've set this up, I can't even think of going back to GUI based sheet editors.

I realize this is an old issue but I wanted to share a fix that worked for me regarding the auto-reload of the .pdf file. Note that I'm doing this on a MacOS Sonoma 14.5 machine. Shouldn't be too different though on other systems. @clojj and @leiyi-0612 comments were immensely helpful for fixing this 🙏. (I'm pretty much repeating @leiyi-0612 instructions step by step)

  1. Open pdf-preview.js located at /Users/{username}/.vscode/extensions/lhl2617.lilypond-pdf-preview-0.2.8/out/extension/pdf-preview.js
  2. Find statement const watcher = this._register(vscode.workspace.createFileSystemWatcher(resource.fsPath));
  3. Paste these lines below that statement and save.
        this._register(watcher.onDidCreate((e) => {
          if (e.toString() === this.resource.toString()) {
            this.reload()
          }
        }));
  1. Reload VSCode and edit/save a .ly file.

Extra thoughts: Not an expert on this but my guess as to why onDidChange isn't working is because the .pdf file is being built from scratch everytime a .ly file is saved. Meaning there's a brief period of time where the file doesn't exist and onDidChange can't locate it(??)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants