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 VSCode Formatting Ability #22

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"onCommand:extension.csoundPlayActiveDocument",
"onCommand:extension.csoundKillCsoundProcess",
"onCommand:extension.csoundEvalOrc",
"onCommand:extension.csoundEvalSco"
"onCommand:extension.csoundEvalSco",
"onLanguage:csound-csd"
],
"main": "./dist/extension.js",
"browser": "./dist/web/extension.js",
Expand Down Expand Up @@ -138,6 +139,12 @@
"key": "ctrl+enter",
"mac": "cmd+enter",
"when": "editorTextFocus"
},
{
"command": "extension.csoundFormat",
"key": "ctrl+shift+i",
"mac": "cmd+shift+i",
"when": "editorFocus"
}
],
"snippets": [
Expand Down
7 changes: 7 additions & 0 deletions src/csoundCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,11 @@ export async function evalSco(textEditor: vscode.TextEditor) {
const { text, from , to} = getScoEvalText(document, selection);
socket.send("$" + text, port, address);
flash(textEditor, new vscode.Range(from, to));
}

export class GoDocumentFormatter implements vscode.DocumentFormattingEditProvider {
public provideDocumentFormattingEdits(document: vscode.TextDocument):
Thenable<vscode.TextEdit[]> {
return Promise.resolve([]);
}
}
37 changes: 37 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as vscode from 'vscode';

import * as commands from './csoundCommands';

const fullRange = (doc: vscode.TextDocument) => doc.validateRange(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(Number.MAX_VALUE, Number.MAX_VALUE)));

export function activate(context: vscode.ExtensionContext) {

// play command
Expand All @@ -26,6 +28,41 @@ export function activate(context: vscode.ExtensionContext) {
'extension.csoundEvalSco',
commands.evalSco);
context.subscriptions.push(evalScoCommand);

vscode.languages.registerDocumentFormattingEditProvider('csound-csd', {
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
let changes: vscode.TextEdit[] = [];
let section : "OPTIONS" | "INSTRUMENTS" | "SCORE" | "EMPTY" = "EMPTY";
for(let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i);
switch(true){
case line.text.includes("<CsOptions>"):
section = "OPTIONS";
break;
case line.text.includes("<CsInstruments>"):
section = "INSTRUMENTS";
break;
case line.text.includes("</"):
section = "EMPTY";
break;
}
switch(section){
case "OPTIONS":
changes.push(new vscode.TextEdit(line.range, line.text.replace(/\s/g, "")));
break;
case "INSTRUMENTS":
changes.push(new vscode.TextEdit(line.range, line.text.replace(/(,(?![ ])|,[ ]{2,})/g, ', ')));
break;
case "EMPTY":
default:
break;
}
}
return changes;
}
});


}

// this method is called when your extension is deactivated
Expand Down
14 changes: 14 additions & 0 deletions src/web/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export function activate(context: vscode.ExtensionContext) {
const notYetImplementedForWeb = () => {
vscode.window.showInformationMessage("This command has not yet been reimplemented for the web.");
};

class GoDocumentFormatter implements vscode.DocumentFormattingEditProvider {
public provideDocumentFormattingEdits(document: vscode.TextDocument):
Thenable<vscode.TextEdit[]> {
return Promise.resolve([]);
}
}


// play command
const playCommand = vscode.commands.registerTextEditorCommand(
'extension.csoundPlayActiveDocument', notYetImplementedForWeb
Expand All @@ -29,6 +38,11 @@ export function activate(context: vscode.ExtensionContext) {
'extension.csoundEvalSco',
notYetImplementedForWeb);
context.subscriptions.push(evalScoCommand);

const formatCommand = vscode.languages.registerDocumentFormattingEditProvider(
'extension.csoundFormat', new GoDocumentFormatter());

context.subscriptions.push(formatCommand);
}

// this method is called when your extension is deactivated
Expand Down