From cebe5c34feecd84278a3137e11028ba4a7a5f23d Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 21 Oct 2021 15:14:06 +1100 Subject: [PATCH 1/2] feat: basic formatting ability --- package.json | 9 ++++++++- src/csoundCommands.ts | 7 +++++++ src/extension.ts | 17 +++++++++++++++++ src/web/extension.ts | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 941807f..955c486 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": [ diff --git a/src/csoundCommands.ts b/src/csoundCommands.ts index 1861457..5f36f4e 100644 --- a/src/csoundCommands.ts +++ b/src/csoundCommands.ts @@ -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 { + return Promise.resolve([]); + } } \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 3e8a625..dbc1aff 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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 @@ -26,6 +28,21 @@ 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 text = document.getText(); + // Replace all commas without a following space or with more than one with a single space + text = text.replace(/(,(?![ ])|,[ ]{2,})/g, ', '); + // Replace all space before instr headers with a single tab + text = text.replace(/[ ]{0,}instr/g, '\tinstr'); + // Replace all space before instr footers with a single tab + text = text.replace(/[ ]{0,}endin/g, '\endin'); + return [vscode.TextEdit.replace(fullRange(document), text)]; + } + }); + + } // this method is called when your extension is deactivated diff --git a/src/web/extension.ts b/src/web/extension.ts index df0f107..0565a3b 100644 --- a/src/web/extension.ts +++ b/src/web/extension.ts @@ -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 { + return Promise.resolve([]); + } + } + + // play command const playCommand = vscode.commands.registerTextEditorCommand( 'extension.csoundPlayActiveDocument', notYetImplementedForWeb @@ -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 From bbcb04e38c1ce58a7ece0feadc1f299fc561a4d0 Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 22 Oct 2021 13:42:32 +1100 Subject: [PATCH 2/2] chore: refractor to switch --- src/extension.ts | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index dbc1aff..45eeebd 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -31,14 +31,34 @@ export function activate(context: vscode.ExtensionContext) { vscode.languages.registerDocumentFormattingEditProvider('csound-csd', { provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] { - let text = document.getText(); - // Replace all commas without a following space or with more than one with a single space - text = text.replace(/(,(?![ ])|,[ ]{2,})/g, ', '); - // Replace all space before instr headers with a single tab - text = text.replace(/[ ]{0,}instr/g, '\tinstr'); - // Replace all space before instr footers with a single tab - text = text.replace(/[ ]{0,}endin/g, '\endin'); - return [vscode.TextEdit.replace(fullRange(document), text)]; + 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(""): + section = "OPTIONS"; + break; + case line.text.includes(""): + section = "INSTRUMENTS"; + break; + case line.text.includes("