sonic-pi-vscode-editor icon indicating copy to clipboard operation
sonic-pi-vscode-editor copied to clipboard

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

Open GavinRay97 opened this issue 5 years ago • 3 comments

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 :smiley:

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");
    }
});

GavinRay97 avatar Jun 13 '20 01:06 GavinRay97

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!

llloret avatar Jun 14 '20 16:06 llloret

I think that could be a solid user experience too :+1:

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.

GavinRay97 avatar Jun 14 '20 20:06 GavinRay97

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

llloret avatar Jun 16 '20 19:06 llloret