Skip to content

Commit 836aa64

Browse files
authored
Merge pull request #71 from avli/feature/format-on-save
Feature/format on save
2 parents ac6a4f1 + cc712ae commit 836aa64

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.9.1
2+
3+
Adds a configuration option for formatting code on save.
4+
15
# Version 0.9.0
26

37
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.

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "clojure",
33
"displayName": "Clojure",
44
"description": "Clojure nREPL support for Visual Studio Code",
5-
"version": "0.9.0",
5+
"version": "0.9.1",
66
"publisher": "avli",
77
"author": {
88
"name": "Andrey Lisin",
@@ -90,6 +90,11 @@
9090
"type": "boolean",
9191
"default": true,
9292
"description": "Automatically run an embedded nREPL instance and connect to it on Clojure file open."
93+
},
94+
"clojureVSCode.formatOnSave": {
95+
"type": "boolean",
96+
"default": false,
97+
"description": "Format the code on save."
9398
}
9499
}
95100
}

src/clojureFormat.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ function slashEscape(contents: string) {
99
.replace(/\\/g, '\\\\')
1010
.replace(/"/g, '\\"')
1111
.replace(/\n/g, '\\n');
12-
}
12+
}
1313

1414
function slashUnescape(contents: string) {
15-
const replacements = {'\\\\': '\\', '\\n': '\n', '\\"': '"'};
16-
return contents.replace(/\\(\\|n|")/g, function(match) {
15+
const replacements = { '\\\\': '\\', '\\n': '\n', '\\"': '"' };
16+
return contents.replace(/\\(\\|n|")/g, function (match) {
1717
return replacements[match];
1818
});
1919
}
@@ -57,3 +57,19 @@ export const formatFile = (textEditor: vscode.TextEditor, edit: vscode.TextEdito
5757
};
5858
});
5959
}
60+
61+
export const maybeActivateFormatOnSave = () => {
62+
vscode.workspace.onWillSaveTextDocument(e => {
63+
const document = e.document;
64+
if (document.languageId !== "clojure") {
65+
return;
66+
}
67+
let textEditor = vscode.window.activeTextEditor;
68+
let editorConfig = vscode.workspace.getConfiguration('editor');
69+
const globalEditorFormatOnSave = editorConfig && editorConfig.has('formatOnSave') && editorConfig.get('formatOnSave') === true;
70+
let clojureConfig = vscode.workspace.getConfiguration('clojureVSCode');
71+
if ((clojureConfig.formatOnSave || globalEditorFormatOnSave) && textEditor.document === document) {
72+
formatFile(textEditor, null);
73+
}
74+
});
75+
}

src/clojureMain.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ClojureSignatureProvider } from './clojureSignature';
1010
import { JarContentProvider } from './jarContentProvider';
1111
import { nreplController } from './nreplController';
1212
import { cljConnection } from './cljConnection';
13-
import { formatFile } from './clojureFormat';
13+
import { formatFile, maybeActivateFormatOnSave } from './clojureFormat';
1414

1515
export function activate(context: vscode.ExtensionContext) {
1616
cljConnection.setCljContext(context);
@@ -20,7 +20,9 @@ export function activate(context: vscode.ExtensionContext) {
2020
if (config.autoStartNRepl) {
2121
cljConnection.startNRepl();
2222
}
23-
23+
24+
maybeActivateFormatOnSave();
25+
2426
vscode.commands.registerCommand('clojureVSCode.manuallyConnectToNRepl', cljConnection.manuallyConnect);
2527
vscode.commands.registerCommand('clojureVSCode.stopDisconnectNRepl', cljConnection.disconnect);
2628
vscode.commands.registerCommand('clojureVSCode.startNRepl', cljConnection.startNRepl);

0 commit comments

Comments
 (0)