Skip to content

Commit

Permalink
feat(vint): support vint
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Oct 17, 2024
1 parent f6e1808 commit 7030ba9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- select range
- rename
- snippets
- diagnostic
- diagnostic, include support for [vint](https://github.com/Vimjas/vint)

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

Expand Down
58 changes: 43 additions & 15 deletions src/handles/diagnostic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { spawnSync } from "child_process";
import {
DiagnosticSeverity,
Position,
Range,
TextDocument,
} from "vscode-languageserver";
import { URI } from 'vscode-uri'
import { errorLinePattern } from "../common/patterns";
import { connection } from "../server/connection";

Expand All @@ -17,29 +19,55 @@ const fixNegativeNum = (num: number): number => {
export async function handleDiagnostic(
textDoc: TextDocument,
error: string,
save: boolean,
) {
let diagnostics = []
if (save) {
let cp = spawnSync("vint", [URI.parse(textDoc.uri).fsPath], { encoding: "utf8" });
if (cp.stdout) {
// example output:
// "/home/wzy/.config/nvim/init.vim:21:3: warning! " "Do not use nocompatible which has unexpected effects(:help nocompatible)"
for (let line of cp.stdout.trim().split("\n")) {
let [_1, info, _2, message, _3] = line.split('"');
let [_path, _row, _col, _severity] = info.split(":");
let row = Number(_row);
let col = Number(_col);
_severity = _severity.trim().replace("!", "");
let severity: DiagnosticSeverity = DiagnosticSeverity.Error;
if (_severity === "warning") {
severity = DiagnosticSeverity.Warning;
}
diagnostics = [...diagnostics, {
source: "vint",
message: message,
range: Range.create(
Position.create(row - 1, col - 1),
Position.create(row, 0),
),
severity: severity,
}];
}
}
}

const m = (error || "").match(errorLinePattern);
if (m) {
const lines = textDoc.lineCount;
const line = fixNegativeNum(parseFloat(m[2]) - 1);
const col = fixNegativeNum(parseFloat(m[3]) - 1);
return connection.sendDiagnostics({
uri: textDoc.uri,
diagnostics: [{
source: "vimlsp",
message: m[1],
range: Range.create(
Position.create(line > lines ? lines : line, col),
Position.create(line > lines ? lines : line, col + 1),
),
severity: DiagnosticSeverity.Error,
}],
});
diagnostics = [...diagnostics, {
source: "vimlsp",
message: m[1],
range: Range.create(
Position.create(line > lines ? lines : line, col),
Position.create(line > lines ? lines : line, col + 1),
),
severity: DiagnosticSeverity.Error,
}];
}

// clear diagnostics
connection.sendDiagnostics({
return connection.sendDiagnostics({
uri: textDoc.uri,
diagnostics: [],
diagnostics: diagnostics,
});
}
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ connection.onInitialize((param: InitializeParams) => {

// document change or open
documents.onDidChangeContent(( change ) => {
next(change.document);
next(change.document, false);
});

documents.onDidOpen(( change ) => {
next(change.document, true);
});

documents.onDidSave(( change ) => {
next(change.document, true);
});

documents.onDidClose((evt) => {
Expand Down
3 changes: 2 additions & 1 deletion src/server/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function startIndex() {

export function next(
textDoc: TextDocument,
save: boolean,
) {
if (!parserHandles[textDoc.uri]) {
const { uri } = textDoc;
Expand Down Expand Up @@ -152,7 +153,7 @@ export function next(
if (res) {
if (config.diagnostic.enable) {
// handle diagnostic
handleDiagnostic(textDoc, res[1]);
handleDiagnostic(textDoc, res[1], save);
}
// handle node
workspace.updateBuffer(uri, res[0]);
Expand Down

0 comments on commit 7030ba9

Please sign in to comment.