Skip to content

Commit ff52cd2

Browse files
committed
run linter on eval
1 parent f21075e commit ff52cd2

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

src/clojureEval.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,48 @@ import { cljConnection } from './cljConnection';
44
import { cljParser } from './cljParser';
55
import { nreplClient } from './nreplClient';
66
import {readBooleanConfiguration} from './utils';
7+
import { ClojureLintingProvider } from './clojureLintingProvider';
78

89
function getAlertOnEvalResult() {
910
return readBooleanConfiguration('aletOnEval');
1011
}
1112

12-
export function clojureEval(outputChannel: vscode.OutputChannel): void {
13-
evaluate(outputChannel, false);
13+
export function clojureEval(outputChannel: vscode.OutputChannel, linter: ClojureLintingProvider): void {
14+
evaluate(outputChannel, linter, false);
1415
}
1516

16-
export function clojureEvalAndShowResult(outputChannel: vscode.OutputChannel): void {
17-
evaluate(outputChannel, true);
17+
export function clojureEvalAndShowResult(outputChannel: vscode.OutputChannel, linter: ClojureLintingProvider): void {
18+
evaluate(outputChannel, linter, true);
1819
}
1920

2021
export function evaluateText(outputChannel: vscode.OutputChannel,
21-
showResults: boolean,
22-
fileName: string,
23-
text: string): Promise<any[]> {
22+
showResults: boolean,
23+
fileName: string,
24+
text: string): Promise<any[]> {
2425
return cljConnection.sessionForFilename(fileName).then(session => {
2526
return (fileName.length === 0 && session.type == 'ClojureScript')
26-
// Piggieback's evalFile() ignores the text sent as part of the request
27-
// and just loads the whole file content from disk. So we use eval()
28-
// here, which as a drawback will give us a random temporary filename in
29-
// the stacktrace should an exception occur.
30-
? nreplClient.evaluate(text, session.id)
31-
: nreplClient.evaluateFile(text, fileName, session.id);
27+
// Piggieback's evalFile() ignores the text sent as part of the request
28+
// and just loads the whole file content from disk. So we use eval()
29+
// here, which as a drawback will give us a random temporary filename in
30+
// the stacktrace should an exception occur.
31+
? nreplClient.evaluate(text, session.id)
32+
: nreplClient.evaluateFile(text, fileName, session.id);
33+
}).then(result=> {
34+
35+
36+
return result;
3237
});
3338
}
3439

35-
function evaluate(outputChannel: vscode.OutputChannel, showResults: boolean): void {
40+
function evaluate(outputChannel: vscode.OutputChannel, linter: ClojureLintingProvider, showResults: boolean): void {
3641
if (!cljConnection.isConnected()) {
3742
vscode.window.showWarningMessage('You should connect to nREPL first to evaluate code.');
3843
return;
3944
}
4045

4146
const editor = vscode.window.activeTextEditor;
47+
linter.runLinter(editor.document);
48+
4249
const selection = editor.selection;
4350
let text = editor.document.getText();
4451
if (!selection.isEmpty) {
@@ -52,7 +59,7 @@ function evaluate(outputChannel: vscode.OutputChannel, showResults: boolean): vo
5259
return handleError(outputChannel, selection, showResults, respObjs[0].session);
5360

5461
return handleSuccess(outputChannel, showResults, respObjs);
55-
});
62+
});
5663
}
5764

5865
export function handleError(outputChannel: vscode.OutputChannel, selection: vscode.Selection, showResults: boolean, session: string): Promise<void> {

src/clojureLintingProvider.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ export class ClojureLintingProvider {
148148
handleError(this.outputChannel,
149149
new vscode.Selection(0,0,0,0),
150150
false,
151-
result[0].session);
151+
result[0].session);
152+
153+
const diagnostics = [
154+
{}
155+
];
156+
152157
} else {
153158
let lintResult: LinterResult = this.parseLintSuccessResponse(result[0].value);
154159
const diagnostics = this.createDiagnosticCollectionFromLintResult(textDocument, lintResult);
@@ -165,6 +170,11 @@ export class ClojureLintingProvider {
165170
});
166171
}
167172

173+
public runLinter(textDocument: TextDocument) {
174+
this.diagnosticCollection.delete(textDocument.uri);
175+
this.lint(textDocument);
176+
}
177+
168178
public activate(subscriptions: vscode.Disposable[]) {
169179
subscriptions.push(this);
170180
this.diagnosticCollection = vscode.languages.createDiagnosticCollection();
@@ -174,10 +184,7 @@ export class ClojureLintingProvider {
174184
this.diagnosticCollection.delete(textDocument.uri);
175185
}, null, subscriptions);
176186

177-
vscode.workspace.onDidSaveTextDocument((textDocument: vscode.TextDocument) => {
178-
this.diagnosticCollection.delete(textDocument.uri);
179-
this.lint(textDocument);
180-
}, this);
187+
vscode.workspace.onDidSaveTextDocument(this.runLinter, this);
181188
}
182189

183190
public dispose(): void {

src/clojureMain.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ export function activate(context: vscode.ExtensionContext) {
3131
vscode.commands.registerCommand('clojureVSCode.startNRepl', cljConnection.startNRepl);
3232

3333
const evaluationResultChannel = vscode.window.createOutputChannel('Evaluation results');
34-
vscode.commands.registerCommand('clojureVSCode.eval', () => clojureEval(evaluationResultChannel));
35-
vscode.commands.registerCommand('clojureVSCode.evalAndShowResult', () => clojureEvalAndShowResult(evaluationResultChannel));
34+
let linter = new ClojureLintingProvider(evaluationResultChannel);
35+
36+
vscode.commands.registerCommand('clojureVSCode.eval', () => clojureEval(evaluationResultChannel, linter));
37+
vscode.commands.registerCommand('clojureVSCode.evalAndShowResult', () => clojureEvalAndShowResult(evaluationResultChannel, linter));
3638
vscode.commands.registerTextEditorCommand('clojureVSCode.formatFile', formatFile);
37-
vscode.commands.registerTextEditorCommand('clojureVSCode.reloadNamespace', ()=> { reloadNamespaceCommand(evaluationResultChannel); });
39+
vscode.commands.registerTextEditorCommand('clojureVSCode.reloadNamespace', ()=> { reloadNamespaceCommand(evaluationResultChannel, linter); });
3840
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(CLOJURE_MODE, new ClojureCompletionItemProvider(), '.', '/'));
3941
context.subscriptions.push(vscode.languages.registerDefinitionProvider(CLOJURE_MODE, new ClojureDefinitionProvider()));
4042
context.subscriptions.push(vscode.languages.registerHoverProvider(CLOJURE_MODE, new ClojureHoverProvider()));
@@ -46,11 +48,11 @@ export function activate(context: vscode.ExtensionContext) {
4648
if(getReloadOnFileSave()) {
4749
vscode.workspace.onDidSaveTextDocument(
4850
function (textDocument: vscode.TextDocument) {
49-
reloadNamespaceCommand(evaluationResultChannel);
51+
reloadNamespaceCommand(evaluationResultChannel, linter);
5052
}, this);
5153
}
5254

53-
let linter = new ClojureLintingProvider(evaluationResultChannel);
55+
5456
linter.activate(context.subscriptions);
5557
}
5658

src/clojureReloadNamespace.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ import { cljConnection } from './cljConnection';
33
import { cljParser } from './cljParser';
44
import { handleError, evaluateText } from './clojureEval';
55
import {readBooleanConfiguration} from './utils';
6+
import { ClojureLintingProvider } from './clojureLintingProvider';
67

78
export function getReloadOnFileSave() :boolean {
89
return readBooleanConfiguration('autoReloadNamespaceOnSave')
910
}
1011

1112
export function reloadNamespaceCommand(
12-
outputChannel: vscode.OutputChannel) {
13+
outputChannel: vscode.OutputChannel,
14+
linter: ClojureLintingProvider) {
1315

1416
if (!cljConnection.isConnected()) {
1517
return;
1618
}
1719

1820
const textDocument = vscode.window.activeTextEditor.document;
21+
linter.runLinter(textDocument);
22+
1923
const fileName = textDocument.fileName;
2024
if(!fileName.endsWith(".clj")) {
2125
return;
@@ -27,12 +31,15 @@ export function reloadNamespaceCommand(
2731

2832
evaluateText(outputChannel, false, fileName, commantText)
2933
.then(respObjs => {
30-
return (!!respObjs[0].ex)
31-
? handleError(outputChannel,
34+
if (!!respObjs[0].ex) {
35+
36+
return handleError(outputChannel,
3237
new vscode.Selection(0,0,0,0),
3338
false,
3439
respObjs[0].session)
35-
.then(value=>Promise.reject(value))
36-
: Promise.resolve();
40+
.then(value=>Promise.reject(value));
41+
} else {
42+
Promise.resolve();
43+
}
3744
})
3845
}

0 commit comments

Comments
 (0)