Skip to content

Commit 7030ba9

Browse files
committed
feat(vint): support vint
Fix iamcco#97
1 parent f6e1808 commit 7030ba9

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- select range
2121
- rename
2222
- snippets
23-
- diagnostic
23+
- diagnostic, include support for [vint](https://github.com/Vimjas/vint)
2424

2525
![autocomplete](https://user-images.githubusercontent.com/5492542/81493984-909c2e80-92d7-11ea-9638-d7be3e18e1d1.gif)
2626

src/handles/diagnostic.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { spawnSync } from "child_process";
12
import {
23
DiagnosticSeverity,
34
Position,
45
Range,
56
TextDocument,
67
} from "vscode-languageserver";
8+
import { URI } from 'vscode-uri'
79
import { errorLinePattern } from "../common/patterns";
810
import { connection } from "../server/connection";
911

@@ -17,29 +19,55 @@ const fixNegativeNum = (num: number): number => {
1719
export async function handleDiagnostic(
1820
textDoc: TextDocument,
1921
error: string,
22+
save: boolean,
2023
) {
24+
let diagnostics = []
25+
if (save) {
26+
let cp = spawnSync("vint", [URI.parse(textDoc.uri).fsPath], { encoding: "utf8" });
27+
if (cp.stdout) {
28+
// example output:
29+
// "/home/wzy/.config/nvim/init.vim:21:3: warning! " "Do not use nocompatible which has unexpected effects(:help nocompatible)"
30+
for (let line of cp.stdout.trim().split("\n")) {
31+
let [_1, info, _2, message, _3] = line.split('"');
32+
let [_path, _row, _col, _severity] = info.split(":");
33+
let row = Number(_row);
34+
let col = Number(_col);
35+
_severity = _severity.trim().replace("!", "");
36+
let severity: DiagnosticSeverity = DiagnosticSeverity.Error;
37+
if (_severity === "warning") {
38+
severity = DiagnosticSeverity.Warning;
39+
}
40+
diagnostics = [...diagnostics, {
41+
source: "vint",
42+
message: message,
43+
range: Range.create(
44+
Position.create(row - 1, col - 1),
45+
Position.create(row, 0),
46+
),
47+
severity: severity,
48+
}];
49+
}
50+
}
51+
}
52+
2153
const m = (error || "").match(errorLinePattern);
2254
if (m) {
2355
const lines = textDoc.lineCount;
2456
const line = fixNegativeNum(parseFloat(m[2]) - 1);
2557
const col = fixNegativeNum(parseFloat(m[3]) - 1);
26-
return connection.sendDiagnostics({
27-
uri: textDoc.uri,
28-
diagnostics: [{
29-
source: "vimlsp",
30-
message: m[1],
31-
range: Range.create(
32-
Position.create(line > lines ? lines : line, col),
33-
Position.create(line > lines ? lines : line, col + 1),
34-
),
35-
severity: DiagnosticSeverity.Error,
36-
}],
37-
});
58+
diagnostics = [...diagnostics, {
59+
source: "vimlsp",
60+
message: m[1],
61+
range: Range.create(
62+
Position.create(line > lines ? lines : line, col),
63+
Position.create(line > lines ? lines : line, col + 1),
64+
),
65+
severity: DiagnosticSeverity.Error,
66+
}];
3867
}
3968

40-
// clear diagnostics
41-
connection.sendDiagnostics({
69+
return connection.sendDiagnostics({
4270
uri: textDoc.uri,
43-
diagnostics: [],
71+
diagnostics: diagnostics,
4472
});
4573
}

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ connection.onInitialize((param: InitializeParams) => {
103103

104104
// document change or open
105105
documents.onDidChangeContent(( change ) => {
106-
next(change.document);
106+
next(change.document, false);
107+
});
108+
109+
documents.onDidOpen(( change ) => {
110+
next(change.document, true);
111+
});
112+
113+
documents.onDidSave(( change ) => {
114+
next(change.document, true);
107115
});
108116

109117
documents.onDidClose((evt) => {

src/server/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function startIndex() {
111111

112112
export function next(
113113
textDoc: TextDocument,
114+
save: boolean,
114115
) {
115116
if (!parserHandles[textDoc.uri]) {
116117
const { uri } = textDoc;
@@ -152,7 +153,7 @@ export function next(
152153
if (res) {
153154
if (config.diagnostic.enable) {
154155
// handle diagnostic
155-
handleDiagnostic(textDoc, res[1]);
156+
handleDiagnostic(textDoc, res[1], save);
156157
}
157158
// handle node
158159
workspace.updateBuffer(uri, res[0]);

0 commit comments

Comments
 (0)