From 8c6c44989d79a0375546df5729e28e42855d3d51 Mon Sep 17 00:00:00 2001 From: Andrey Lisin Date: Thu, 26 Oct 2017 13:43:28 +0200 Subject: [PATCH 1/2] Add format on save functionality --- package.json | 5 +++++ src/clojureFormat.ts | 22 +++++++++++++++++++--- src/clojureMain.ts | 6 ++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index d8ff9a6..d660405 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,11 @@ "type": "boolean", "default": true, "description": "Automatically run an embedded nREPL instance and connect to it on Clojure file open." + }, + "clojureVSCode.formatOnSave": { + "type": "boolean", + "default": false, + "description": "Format the code on save." } } } diff --git a/src/clojureFormat.ts b/src/clojureFormat.ts index b5c5040..600af6b 100644 --- a/src/clojureFormat.ts +++ b/src/clojureFormat.ts @@ -9,11 +9,11 @@ function slashEscape(contents: string) { .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') .replace(/\n/g, '\\n'); - } +} function slashUnescape(contents: string) { - const replacements = {'\\\\': '\\', '\\n': '\n', '\\"': '"'}; - return contents.replace(/\\(\\|n|")/g, function(match) { + const replacements = { '\\\\': '\\', '\\n': '\n', '\\"': '"' }; + return contents.replace(/\\(\\|n|")/g, function (match) { return replacements[match]; }); } @@ -57,3 +57,19 @@ export const formatFile = (textEditor: vscode.TextEditor, edit: vscode.TextEdito }; }); } + +export const maybeActivateFormatOnSave = () => { + vscode.workspace.onWillSaveTextDocument(e => { + const document = e.document; + if (document.languageId !== "clojure") { + return; + } + let textEditor = vscode.window.activeTextEditor; + let editorConfig = vscode.workspace.getConfiguration('editor'); + const globalEditorFormatOnSave = editorConfig && editorConfig.has('formatOnSave') && editorConfig.get('formatOnSave') === true; + let clojureConfig = vscode.workspace.getConfiguration('clojureVSCode'); + if ((clojureConfig.formatOnSave || globalEditorFormatOnSave) && textEditor.document === document) { + formatFile(textEditor, null); + } + }); +} \ No newline at end of file diff --git a/src/clojureMain.ts b/src/clojureMain.ts index 12b4a74..9614418 100644 --- a/src/clojureMain.ts +++ b/src/clojureMain.ts @@ -10,7 +10,7 @@ import { ClojureSignatureProvider } from './clojureSignature'; import { JarContentProvider } from './jarContentProvider'; import { nreplController } from './nreplController'; import { cljConnection } from './cljConnection'; -import { formatFile } from './clojureFormat'; +import { formatFile, maybeActivateFormatOnSave } from './clojureFormat'; export function activate(context: vscode.ExtensionContext) { cljConnection.setCljContext(context); @@ -20,7 +20,9 @@ export function activate(context: vscode.ExtensionContext) { if (config.autoStartNRepl) { cljConnection.startNRepl(); } - + + maybeActivateFormatOnSave(); + vscode.commands.registerCommand('clojureVSCode.manuallyConnectToNRepl', cljConnection.manuallyConnect); vscode.commands.registerCommand('clojureVSCode.stopDisconnectNRepl', cljConnection.disconnect); vscode.commands.registerCommand('clojureVSCode.startNRepl', cljConnection.startNRepl); From cc712aebda14316d348505623c170cc6d963d087 Mon Sep 17 00:00:00 2001 From: Andrey Lisin Date: Thu, 26 Oct 2017 14:17:57 +0200 Subject: [PATCH 2/2] Update changelog. Bump version --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15b7aa9..96ee95d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Version 0.9.1 + +Adds a configuration option for formatting code on save. + # Version 0.9.0 Adds experimental ClojureScript support. Please check out [README.md](https://github.com/avli/clojureVSCode#clojurescript-project-setup) to learn how to use the extension for ClojureScript. diff --git a/package.json b/package.json index d660405..ffb951e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "clojure", "displayName": "Clojure", "description": "Clojure nREPL support for Visual Studio Code", - "version": "0.9.0", + "version": "0.9.1", "publisher": "avli", "author": { "name": "Andrey Lisin",