Skip to content

Commit

Permalink
Merge pull request #71 from avli/feature/format-on-save
Browse files Browse the repository at this point in the history
Feature/format on save
  • Loading branch information
avli authored Oct 26, 2017
2 parents ac6a4f1 + cc712ae commit 836aa64
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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."
}
}
}
Expand Down
22 changes: 19 additions & 3 deletions src/clojureFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
}
Expand Down Expand Up @@ -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);
}
});
}
6 changes: 4 additions & 2 deletions src/clojureMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 836aa64

Please sign in to comment.