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

Add Live-Reload on save command, add some helper functions to clean up #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

GavinRay97
Copy link

This commands adds a new toggle which hooks workspace.onDidSaveTextDocument() to automatically update the loop in Sonic Pi when the buffer is saved. Also two smaller commands are added, tryGetFirstRubyDocument() and runTextEditorCode() which led to a bit better code re-use and less clutter in extension.ts.

Hope someone else finds use in the Live-reload feature 😃

vscode.commands.registerTextEditorCommand('sonicpieditor.livereload', (textEditor) => {
    liveReload = !liveReload;
    // If enabling
    if (liveReload) {
        // Initially run the code
        runTextEditorCode(main, textEditor);
        // Then set up the on-save subscription
        onSaveSubscription = vscode.workspace.onDidSaveTextDocument((doc) => {
            if (doc.languageId === 'ruby') { main.runCode(doc.getText()); }
        });
        // Display notifications
        vscode.window.setStatusBarMessage("Sonic Pi [Live-Reload]");
        vscode.window.showInformationMessage("Sonic Pi Live-Reload Enabled");
    }
    // If disabling
    else {
        // Dispose of the on-save subscription
        onSaveSubscription.dispose();
        // Display notifications
        vscode.window.showInformationMessage("Sonic Pi Live-Reload Disabled");
        vscode.window.setStatusBarMessage("Sonic Pi server started");
    }
});

@llloret
Copy link
Owner

llloret commented Jun 14, 2020

Hi, @GavinRay97 , thanks a lot for your contribution. I think this can be very useful, and really like it.

I think we should consider having this as a setting, instead of as a command toggle that gets forgotten on exit. What do you think? I would intuitively expect this kind of behaviour to be controlled by a setting, not a command toggle. But perhaps you have a different use case in mind?

And then, later, perhaps we can also add another setting to have the opposite: that every time that people run the code, it gets saved.

What do you think?

Also, thank you for the refactoring!

@GavinRay97
Copy link
Author

I think that could be a solid user experience too 👍

Admittedly I was trying to hack out a quick first-implementation (and the toggle made it easier to test) but I agree that if you're using this feature, you probably just want it as the default behavior.

@llloret
Copy link
Owner

llloret commented Jun 16, 2020

So, would you mind adding the code to use this as a setting? Then, we can merge it.
Thank you!

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

Successfully merging this pull request may close these issues.

None yet

2 participants