1
+ import { spawnSync } from "child_process" ;
1
2
import {
2
3
DiagnosticSeverity ,
3
4
Position ,
4
5
Range ,
5
6
TextDocument ,
6
7
} from "vscode-languageserver" ;
8
+ import { URI } from 'vscode-uri'
7
9
import { errorLinePattern } from "../common/patterns" ;
8
10
import { connection } from "../server/connection" ;
9
11
@@ -17,29 +19,55 @@ const fixNegativeNum = (num: number): number => {
17
19
export async function handleDiagnostic (
18
20
textDoc : TextDocument ,
19
21
error : string ,
22
+ save : boolean ,
20
23
) {
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
+
21
53
const m = ( error || "" ) . match ( errorLinePattern ) ;
22
54
if ( m ) {
23
55
const lines = textDoc . lineCount ;
24
56
const line = fixNegativeNum ( parseFloat ( m [ 2 ] ) - 1 ) ;
25
57
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
+ } ] ;
38
67
}
39
68
40
- // clear diagnostics
41
- connection . sendDiagnostics ( {
69
+ return connection . sendDiagnostics ( {
42
70
uri : textDoc . uri ,
43
- diagnostics : [ ] ,
71
+ diagnostics : diagnostics ,
44
72
} ) ;
45
73
}
0 commit comments