Skip to content
Open
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/**
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"title": "Run selected text",
"category": "Sonic Pi"
},
{
"command": "sonicpieditor.livereload",
"title": "Toggle Live-Reload (Run on Save)",
"category": "Sonic Pi"
},
{
"command": "sonicpieditor.stop",
"title": "Stop",
Expand Down
101 changes: 68 additions & 33 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,41 @@
// It would be great if someone can check if it works in Linux, and provide a PR if it doesn't.

import * as vscode from 'vscode';

import { Main } from './main';

type Maybe<T> = T | null;

/**
* Attempts to find the first open Ruby document, returns null if not found
*/
function tryGetFirstRubyDocument(): Maybe<vscode.TextDocument> {
let textEditors = vscode.window.visibleTextEditors;
let rubyEditors = textEditors.filter((editor) => {
return editor.document.languageId === 'ruby';
});
if (!rubyEditors.length){
vscode.window.showWarningMessage('No open Ruby editors were found, attempting to run Sonic Pi code will have no effect. Please open a Ruby file and try again.');
return null;
}
return rubyEditors[0].document;
}

/**
* Runs the code from a TextEditor document
*
* @param main the instance of the Main class, used for context to keep this function out of the module body scope
* @param textEditor an instance of a TextEditor from a registerTextEditorCommand callback
*/
function runTextEditorCode(main: Main, textEditor: vscode.TextEditor) {
let doc = textEditor.document;
if (doc.languageId !== 'ruby') {
let maybeRubyDoc = tryGetFirstRubyDocument();
if (maybeRubyDoc) { main.runCode(maybeRubyDoc.getText()); }
} else {
main.runCode(doc.getText());
}
}

export function activate(context: vscode.ExtensionContext) {
console.log('Ruby detected. Sonic Pi editor extension active!');

Expand All @@ -53,45 +85,21 @@ export function activate(context: vscode.ExtensionContext) {
});

disposable = vscode.commands.registerTextEditorCommand('sonicpieditor.run', (textEditor) => {
let doc = textEditor.document;
// If the focus is on something that is not ruby (i.e. something on the output pane),
// run the first found open ruby editor instead
if (doc.languageId !== 'ruby'){
let textEditors = vscode.window.visibleTextEditors;
let rubyEditors = textEditors.filter((editor) => {
return editor.document.languageId === 'ruby';
});

// TODO: if no ruby editors, show a warning to indicate that this will not have effect
if (!rubyEditors.length){
return;
}
doc = rubyEditors[0].document;
}
let code = doc.getText();
main.runCode(code);
runTextEditorCode(main, textEditor);
});


disposable = vscode.commands.registerTextEditorCommand('sonicpieditor.runselected', (textEditor) => {
let doc = textEditor.document;
// If the focus is on something that is not ruby (i.e. something on the output pane),
// run the first found open ruby editor instead
if (doc.languageId !== 'ruby'){
let textEditors = vscode.window.visibleTextEditors;
let rubyEditors = textEditors.filter((editor) => {
return editor.document.languageId === 'ruby';
});

// TODO: if no ruby editors, show a warning to indicate that this will not have effect
if (!rubyEditors.length){
return;
}
doc = rubyEditors[0].document;
let doc = textEditor.document;
if (doc.languageId !== 'ruby') {
let maybeRubyDoc = tryGetFirstRubyDocument();
if (maybeRubyDoc) { doc = maybeRubyDoc; }
}
// run the first found open ruby editor instead
let code = doc.getText(textEditor.selection);
if (!code){
let runFileWhenRunSelectedIsEmpty = vscode.workspace.getConfiguration('sonicpieditor').runFileWhenRunSelectedIsEmpty;
if (!code) {
let runFileWhenRunSelectedIsEmpty = vscode.workspace.getConfiguration('sonicpieditor').runFileWhenRunSelectedIsEmpty;
if (!runFileWhenRunSelectedIsEmpty){
vscode.window.showWarningMessage('You tried to Run selected code with no code selected.' +
'Do you want to run the whole file when this happens?', 'Yes, once', 'Yes, always', 'No, never').then(
Expand Down Expand Up @@ -147,6 +155,33 @@ export function activate(context: vscode.ExtensionContext) {
}
});

let liveReload = false;
let onSaveSubscription: vscode.Disposable;

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

context.subscriptions.push(disposable);
}

Expand Down