1
- import { Range } from ' vscode-languageserver-textdocument' ;
2
- import * as c from ' ./constants' ;
3
- import * as childProcess from ' child_process' ;
1
+ import { Range } from " vscode-languageserver-textdocument" ;
2
+ import * as c from " ./constants" ;
3
+ import * as childProcess from " child_process" ;
4
4
import * as p from "vscode-languageserver-protocol" ;
5
- import * as path from ' path' ;
5
+ import * as path from " path" ;
6
6
import * as t from "vscode-languageserver-types" ;
7
- import * as tmp from 'tmp' ;
8
- import fs from 'fs' ;
9
- import { report } from 'process' ;
7
+ import * as tmp from "tmp" ;
8
+ import fs from "fs" ;
10
9
11
- // TODO: races here
10
+ // TODO: races here?
12
11
// TODO: this doesn't handle file:/// scheme
13
- export let findProjectRootOfFile = ( source : p . DocumentUri ) : null | p . DocumentUri => {
14
- let dir = path . dirname ( source )
12
+ export let findProjectRootOfFile = (
13
+ source : p . DocumentUri
14
+ ) : null | p . DocumentUri => {
15
+ let dir = path . dirname ( source ) ;
15
16
if ( fs . existsSync ( path . join ( dir , c . bsconfigPartialPath ) ) ) {
16
- return dir
17
+ return dir ;
17
18
} else {
18
19
if ( dir === source ) {
19
20
// reached top
20
- return null
21
+ return null ;
21
22
} else {
22
- return findProjectRootOfFile ( dir )
23
+ return findProjectRootOfFile ( dir ) ;
23
24
}
24
25
}
25
- }
26
-
27
- type execResult = {
28
- kind : 'success' ,
29
- result : string
30
- } | {
31
- kind : 'error'
32
- error : string ,
33
26
} ;
34
- export let formatUsingValidBscPath = ( code : string , bscPath : p . DocumentUri , isInterface : boolean ) : execResult => {
27
+
28
+ type execResult =
29
+ | {
30
+ kind : "success" ;
31
+ result : string ;
32
+ }
33
+ | {
34
+ kind : "error" ;
35
+ error : string ;
36
+ } ;
37
+ export let formatUsingValidBscPath = (
38
+ code : string ,
39
+ bscPath : p . DocumentUri ,
40
+ isInterface : boolean
41
+ ) : execResult => {
35
42
// library cleans up after itself. No need to manually remove temp file
36
43
let tmpobj = tmp . fileSync ( ) ;
37
44
let extension = isInterface ? c . resiExt : c . resExt ;
38
45
let fileToFormat = tmpobj . name + extension ;
39
- fs . writeFileSync ( fileToFormat , code , { encoding : ' utf-8' } ) ;
46
+ fs . writeFileSync ( fileToFormat , code , { encoding : " utf-8" } ) ;
40
47
try {
41
- let result = childProcess . execFileSync ( bscPath , [ '-color' , 'never' , '-format' , fileToFormat ] , { stdio : 'pipe' } )
48
+ let result = childProcess . execFileSync (
49
+ bscPath ,
50
+ [ "-color" , "never" , "-format" , fileToFormat ] ,
51
+ { stdio : "pipe" }
52
+ ) ;
42
53
return {
43
- kind : ' success' ,
54
+ kind : " success" ,
44
55
result : result . toString ( ) ,
45
- }
56
+ } ;
46
57
} catch ( e ) {
47
58
return {
48
- kind : ' error' ,
59
+ kind : " error" ,
49
60
error : e . message ,
50
- }
61
+ } ;
51
62
}
52
- }
63
+ } ;
53
64
54
- export let runBsbWatcherUsingValidBsbPath = ( bsbPath : p . DocumentUri , projectRootPath : p . DocumentUri ) => {
55
- let process = childProcess . execFile ( bsbPath , [ '-w' ] , { cwd : projectRootPath } )
56
- return process
65
+ export let runBsbWatcherUsingValidBsbPath = (
66
+ bsbPath : p . DocumentUri ,
67
+ projectRootPath : p . DocumentUri
68
+ ) => {
69
+ let process = childProcess . execFile ( bsbPath , [ "-w" ] , {
70
+ cwd : projectRootPath ,
71
+ } ) ;
72
+ return process ;
57
73
// try {
58
74
// let result = childProcess.execFileSync(bsbPath, [], { stdio: 'pipe', cwd: projectRootPath })
59
75
// return {
@@ -66,7 +82,7 @@ export let runBsbWatcherUsingValidBsbPath = (bsbPath: p.DocumentUri, projectRoot
66
82
// error: e.message,
67
83
// }
68
84
// }
69
- }
85
+ } ;
70
86
71
87
export let parseDiagnosticLocation = ( location : string ) : Range => {
72
88
// example output location:
@@ -76,34 +92,39 @@ export let parseDiagnosticLocation = (location: string): Range => {
76
92
77
93
// language-server position is 0-based. Ours is 1-based. Don't forget to convert
78
94
// also, our end character is inclusive. Language-server's is exclusive
79
- let isRange = location . indexOf ( '-' ) >= 0
95
+ let isRange = location . indexOf ( "-" ) >= 0 ;
80
96
if ( isRange ) {
81
- let [ from , to ] = location . split ( '-' )
82
- let [ fromLine , fromChar ] = from . split ( ':' )
83
- let isSingleLine = to . indexOf ( ':' ) >= 0
84
- let [ toLine , toChar ] = isSingleLine ? to . split ( ':' ) : [ fromLine , to ]
97
+ let [ from , to ] = location . split ( "-" ) ;
98
+ let [ fromLine , fromChar ] = from . split ( ":" ) ;
99
+ let isSingleLine = to . indexOf ( ":" ) >= 0 ;
100
+ let [ toLine , toChar ] = isSingleLine ? to . split ( ":" ) : [ fromLine , to ] ;
85
101
return {
86
- start : { line : parseInt ( fromLine ) - 1 , character : parseInt ( fromChar ) - 1 } ,
102
+ start : {
103
+ line : parseInt ( fromLine ) - 1 ,
104
+ character : parseInt ( fromChar ) - 1 ,
105
+ } ,
87
106
end : { line : parseInt ( toLine ) - 1 , character : parseInt ( toChar ) } ,
88
- }
107
+ } ;
89
108
} else {
90
- let [ line , char ] = location . split ( ':' )
91
- let start = { line : parseInt ( line ) - 1 , character : parseInt ( char ) }
109
+ let [ line , char ] = location . split ( ":" ) ;
110
+ let start = { line : parseInt ( line ) - 1 , character : parseInt ( char ) } ;
92
111
return {
93
112
start : start ,
94
113
end : start ,
95
- }
114
+ } ;
96
115
}
97
- }
116
+ } ;
98
117
99
118
type filesDiagnostics = {
100
119
[ key : string ] : p . Diagnostic [ ] ;
101
- }
120
+ } ;
102
121
type parsedCompilerLogResult = {
103
- done : boolean ,
104
- result : filesDiagnostics ,
105
- }
106
- export let parseCompilerLogOutput = ( content : string ) : parsedCompilerLogResult => {
122
+ done : boolean ;
123
+ result : filesDiagnostics ;
124
+ } ;
125
+ export let parseCompilerLogOutput = (
126
+ content : string
127
+ ) : parsedCompilerLogResult => {
107
128
/* example .compiler.log file content that we're gonna parse:
108
129
109
130
#Start(1600519680823)
@@ -145,27 +166,27 @@ export let parseCompilerLogOutput = (content: string): parsedCompilerLogResult =
145
166
*/
146
167
147
168
type parsedDiagnostic = {
148
- code : number | undefined ,
149
- severity : t . DiagnosticSeverity ,
150
- tag : t . DiagnosticTag | undefined ,
151
- content : string [ ]
152
- }
169
+ code : number | undefined ;
170
+ severity : t . DiagnosticSeverity ;
171
+ tag : t . DiagnosticTag | undefined ;
172
+ content : string [ ] ;
173
+ } ;
153
174
let parsedDiagnostics : parsedDiagnostic [ ] = [ ] ;
154
- let lines = content . split ( '\n' ) ;
175
+ let lines = content . split ( "\n" ) ;
155
176
let done = false ;
156
177
157
178
for ( let i = 0 ; i < lines . length ; i ++ ) {
158
179
let line = lines [ i ] ;
159
- if ( line . startsWith ( ' We\ 've found a bug for you!' ) ) {
180
+ if ( line . startsWith ( " We've found a bug for you!" ) ) {
160
181
parsedDiagnostics . push ( {
161
182
code : undefined ,
162
183
severity : t . DiagnosticSeverity . Error ,
163
184
tag : undefined ,
164
- content : [ ]
165
- } )
166
- } else if ( line . startsWith ( ' Warning number ' ) ) {
167
- let warningNumber = parseInt ( line . slice ( ' Warning number ' . length ) )
168
- let tag : t . DiagnosticTag | undefined = undefined
185
+ content : [ ] ,
186
+ } ) ;
187
+ } else if ( line . startsWith ( " Warning number " ) ) {
188
+ let warningNumber = parseInt ( line . slice ( " Warning number " . length ) ) ;
189
+ let tag : t . DiagnosticTag | undefined = undefined ;
169
190
switch ( warningNumber ) {
170
191
case 11 :
171
192
case 20 :
@@ -183,60 +204,61 @@ export let parseCompilerLogOutput = (content: string): parsedCompilerLogResult =
183
204
case 66 :
184
205
case 67 :
185
206
case 101 :
186
- tag = t . DiagnosticTag . Unnecessary
207
+ tag = t . DiagnosticTag . Unnecessary ;
187
208
break ;
188
209
case 3 :
189
- tag = t . DiagnosticTag . Deprecated
210
+ tag = t . DiagnosticTag . Deprecated ;
190
211
break ;
191
212
}
192
213
parsedDiagnostics . push ( {
193
214
code : Number . isNaN ( warningNumber ) ? undefined : warningNumber ,
194
215
severity : t . DiagnosticSeverity . Warning ,
195
216
tag : tag ,
196
- content : [ ]
197
- } )
198
- } else if ( line . startsWith ( ' Syntax error!' ) ) {
217
+ content : [ ] ,
218
+ } ) ;
219
+ } else if ( line . startsWith ( " Syntax error!" ) ) {
199
220
parsedDiagnostics . push ( {
200
221
code : undefined ,
201
222
severity : t . DiagnosticSeverity . Error ,
202
223
tag : undefined ,
203
- content : [ ]
204
- } )
205
- } else if ( line . startsWith ( ' #Done(' ) ) {
206
- done = true
224
+ content : [ ] ,
225
+ } ) ;
226
+ } else if ( line . startsWith ( " #Done(" ) ) {
227
+ done = true ;
207
228
} else if ( / ^ + [ 0 - 9 ] + / . test ( line ) ) {
208
229
// code display. Swallow
209
- } else if ( line . startsWith ( ' ' ) ) {
210
- parsedDiagnostics [ parsedDiagnostics . length - 1 ] . content . push ( line )
230
+ } else if ( line . startsWith ( " " ) ) {
231
+ parsedDiagnostics [ parsedDiagnostics . length - 1 ] . content . push ( line ) ;
211
232
}
212
233
}
213
234
214
- let result : filesDiagnostics = { }
215
- parsedDiagnostics . forEach ( parsedDiagnostic => {
216
- let [ fileAndLocation , ...diagnosticMessage ] = parsedDiagnostic . content
217
- let locationSeparator = fileAndLocation . indexOf ( ':' )
218
- let file = fileAndLocation . substring ( 2 , locationSeparator )
219
- let location = fileAndLocation . substring ( locationSeparator + 1 )
235
+ let result : filesDiagnostics = { } ;
236
+ parsedDiagnostics . forEach ( ( parsedDiagnostic ) => {
237
+ let [ fileAndLocation , ...diagnosticMessage ] = parsedDiagnostic . content ;
238
+ let locationSeparator = fileAndLocation . indexOf ( ":" ) ;
239
+ let file = fileAndLocation . substring ( 2 , locationSeparator ) ;
240
+ let location = fileAndLocation . substring ( locationSeparator + 1 ) ;
220
241
if ( result [ file ] == null ) {
221
- result [ file ] = [ ]
242
+ result [ file ] = [ ] ;
222
243
}
223
- let cleanedUpDiagnostic = diagnosticMessage
224
- . map ( line => {
225
- // remove the spaces in front
226
- return line . slice ( 2 )
227
- } )
228
- . join ( '\n' )
229
- // remove start and end whitespaces/newlines
230
- . trim ( ) + '\n' ;
244
+ let cleanedUpDiagnostic =
245
+ diagnosticMessage
246
+ . map ( ( line ) => {
247
+ // remove the spaces in front
248
+ return line . slice ( 2 ) ;
249
+ } )
250
+ . join ( "\n" )
251
+ // remove start and end whitespaces/newlines
252
+ . trim ( ) + "\n" ;
231
253
result [ file ] . push ( {
232
254
severity : parsedDiagnostic . severity ,
233
255
tags : parsedDiagnostic . tag === undefined ? [ ] : [ parsedDiagnostic . tag ] ,
234
256
code : parsedDiagnostic . code ,
235
257
range : parseDiagnosticLocation ( location ) ,
236
258
source : "ReScript" ,
237
259
message : cleanedUpDiagnostic ,
238
- } )
239
- } )
260
+ } ) ;
261
+ } ) ;
240
262
241
- return { done, result }
242
- }
263
+ return { done, result } ;
264
+ } ;
0 commit comments