llloret / sonic-pi-vscode-editor

Use Sonic Pi from VS Code
Other
68 stars 15 forks source link

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

Open GavinRay97 opened 4 years ago

GavinRay97 commented 4 years ago

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");
    }
});
llloret commented 4 years ago

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 commented 4 years ago

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.

llloret commented 4 years ago

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