Skip to content

Commit

Permalink
Add code autoformat feature (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
avli authored Aug 21, 2017
1 parent d8d82cf commit bd766e2
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 14 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.8.0

Adds code formatting support [57](https://github.com/avli/clojureVSCode/issues/57).

# Version 0.7.10

Adds workaround for the hanging Java process issue on the Windows platform [56](https://github.com/avli/clojureVSCode/issues/56).
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ Make sure that [Leiningen](https://leiningen.org/) is installed on your machine,

* Interaction with REPL
* Showing documentation on hover
* Code formatting ([cljfmt](https://github.com/weavejester/cljfmt))
* Function signatures

![Code completion example](https://github.com/avli/clojureVSCode/raw/master/images/function%20signature%20example.png)

## Features That Are not Supported (But Nice to Have)
## Features That Are not Supported (but Nice to Have)

* Linting
* [Debug](https://github.com/indiejames/vscode-clojure-debug)
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions 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.7.10",
"version": "0.8.0",
"publisher": "avli",
"author": {
"name": "Andrey Lisin",
Expand Down Expand Up @@ -64,6 +64,10 @@
{
"command": "clojureVSCode.stopDisconnectNRepl",
"title": "Clojure: Stop/disconnect nREPL"
},
{
"command": "clojureVSCode.formatFile",
"title": "Clojure: Format file or selection"
}
]
},
Expand All @@ -73,12 +77,12 @@
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"typescript": "^2.4.1",
"vscode": "^1.0.0",
"mocha": "^2.3.3",
"@types/node": "^6.0.40",
"@types/jszip": "^0.0.31",
"@types/mocha": "^2.2.32"
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.85",
"mocha": "^2.3.3",
"typescript": "^2.4.2",
"vscode": "^1.0.0"
},
"dependencies": {
"bencoder": "^0.0.5",
Expand Down
49 changes: 49 additions & 0 deletions src/clojureFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as vscode from 'vscode';

import { cljConnection } from './cljConnection';
import { cljParser } from './cljParser';
import { nreplClient } from './nreplClient';

export const formatFile = (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit): void => {

if (!cljConnection.isConnected()) {
vscode.window.showErrorMessage("Formatting functions don't work, connect to nREPL first.");
return;
}

const selection = textEditor.selection;
let contents: string = selection.isEmpty ? textEditor.document.getText() : textEditor.document.getText(selection);

// Escaping the string before sending it to nREPL
contents = contents.replace(/\\/g, '\\\\');
contents = contents.replace(/"/g, '\\"');
contents = contents.replace(/\n/g, '\\n');

// Running "(require 'cljfmt.core)" in right after we have checked we are connected to nREPL
// would be a better option but in this case "cljfmt.core/reformat-string" fails the first
// time it is called. I have no idea what causes this behavior so I decided to put the require
// statement right here - don't think it does any harm. If someone knows how to fix it
// please send a pull request with a fix.
nreplClient.evaluate(`(require 'cljfmt.core) (cljfmt.core/reformat-string "${contents}" nil)`)
.then(value => {
if ('ex' in value[0]) {
vscode.window.showErrorMessage(value[1].err);
return;
};
if (('value' in value[1]) && (value[1].value != 'nil')) {
const new_content: string = value[1].value.slice(1, -1)
.replace(/\\n/g, '\n')
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\') // remove quotes, backslashes, and unescape
let selection = textEditor.selection;
if (textEditor.selection.isEmpty) {
const lines: string[] = textEditor.document.getText().split(/\r?\n/g);
const lastChar: number = lines[lines.length - 1].length;
selection = new vscode.Selection(new vscode.Position(0, 0), new vscode.Position(textEditor.document.lineCount, lastChar));
}
textEditor.edit(editBuilder => {
editBuilder.replace(selection, new_content);
});
};
});
}
3 changes: 3 additions & 0 deletions src/clojureMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ClojureSignatureProvider } from './clojureSignature';
import { JarContentProvider } from './jarContentProvider';
import { nreplController } from './nreplController';
import { cljConnection } from './cljConnection';
import { formatFile } from './clojureFormat';

export function activate(context: vscode.ExtensionContext) {
cljConnection.setCljContext(context);
Expand All @@ -26,6 +27,8 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('clojureVSCode.eval', () => clojureEval(evaluationResultChannel));
vscode.commands.registerCommand('clojureVSCode.evalAndShowResult', () => clojureEvalAndShowResult(evaluationResultChannel));

vscode.commands.registerTextEditorCommand('clojureVSCode.formatFile', formatFile);

context.subscriptions.push(vscode.languages.registerCompletionItemProvider(CLOJURE_MODE, new ClojureCompletionItemProvider(), '.', '/'));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(CLOJURE_MODE, new ClojureDefinitionProvider()));
context.subscriptions.push(vscode.languages.registerHoverProvider(CLOJURE_MODE, new ClojureHoverProvider()));
Expand Down
5 changes: 5 additions & 0 deletions src/nreplController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const LEIN_ARGS: string[] = [
'[org.clojure/tools.nrepl "0.2.12" :exclusions [org.clojure/clojure]]',
'--',
'update-in',
':dependencies',
'conj',
'[cljfmt "0.5.7"]',
'--',
'update-in',
':plugins',
'conj',
'[refactor-nrepl "2.3.0-SNAPSHOT"]',
Expand Down

0 comments on commit bd766e2

Please sign in to comment.