@@ -15,7 +15,7 @@ import { Range } from 'vscode-languageserver-textdocument';
15
15
// See https://microsoft.github.io/language-server-protocol/specification Abstract Message
16
16
// version is fixed to 2.0
17
17
let jsonrpcVersion = '2.0' ;
18
- let bscPartialPath = path . join ( 'node_modules' , '.bin ' , 'bsc' ) ;
18
+ let bscPartialPath = path . join ( 'node_modules' , 'bs-platform ' , process . platform , 'bsc.exe ' ) ;
19
19
let bsbLogPartialPath = 'bsb.log' ;
20
20
let resExt = '.res' ;
21
21
let resiExt = '.resi' ;
@@ -53,15 +53,13 @@ type formattingResult = {
53
53
error : string ,
54
54
} ;
55
55
let formatUsingValidBscPath = ( code : string , bscPath : p . DocumentUri , isInterface : boolean ) : formattingResult => {
56
- // TODO: what if there's space in the path?
57
- let result ;
58
56
// library cleans up after itself. No need to manually remove temp file
59
57
let tmpobj = tmp . fileSync ( ) ;
60
58
let extension = isInterface ? resiExt : resExt ;
61
59
let fileToFormat = tmpobj . name + extension ;
62
60
fs . writeFileSync ( fileToFormat , code , { encoding : 'utf-8' } ) ;
63
61
try {
64
- result = childProcess . execSync ( ` ${ bscPath } -fmt ${ fileToFormat } ` )
62
+ let result = childProcess . execFileSync ( bscPath , [ '-color' , 'never' , '-format' , fileToFormat ] , { stdio : 'pipe' } )
65
63
return {
66
64
kind : 'success' ,
67
65
result : result . toString ( ) ,
@@ -101,7 +99,7 @@ let parseBsbOutputLocation = (location: string): Range => {
101
99
}
102
100
}
103
101
}
104
- type diagnosis = { range : Range , diagnosis : string }
102
+
105
103
let parseBsbLogOutput = ( content : string ) => {
106
104
/* example bsb.log file content:
107
105
@@ -173,7 +171,7 @@ FAILED: src/test.cmj src/test.cmi
173
171
}
174
172
175
173
// map of file path to list of diagnosis
176
- let ret : { [ key : string ] : diagnosis [ ] } = { }
174
+ let ret : { [ key : string ] : t . Diagnostic [ ] } = { }
177
175
res . forEach ( diagnosisLines => {
178
176
let [ fileAndLocation , ...diagnosisMessage ] = diagnosisLines
179
177
let lastSpace = fileAndLocation . lastIndexOf ( ' ' )
@@ -192,15 +190,14 @@ FAILED: src/test.cmj src/test.cmi
192
190
. trim ( ) ;
193
191
ret [ file ] . push ( {
194
192
range : parseBsbOutputLocation ( location ) ,
195
- diagnosis : cleanedUpDiagnosis ,
193
+ message : cleanedUpDiagnosis ,
196
194
} )
197
195
} )
198
196
199
197
return ret
200
198
}
201
199
202
200
let startWatchingBsbOutputFile = ( root : p . DocumentUri , process : NodeJS . Process ) => {
203
- // console.log(root);
204
201
// TOOD: setTimeout instead
205
202
let id = setInterval ( ( ) => {
206
203
let openFiles = Object . keys ( stupidFileContentCache ) ;
@@ -214,7 +211,7 @@ let startWatchingBsbOutputFile = (root: p.DocumentUri, process: NodeJS.Process)
214
211
}
215
212
} ) ;
216
213
217
- let files : { [ key : string ] : diagnosis [ ] } = { }
214
+ let files : { [ key : string ] : t . Diagnostic [ ] } = { }
218
215
219
216
let res = Array . from ( bsbLogDirs )
220
217
. forEach ( bsbLogDir => {
@@ -233,13 +230,7 @@ let startWatchingBsbOutputFile = (root: p.DocumentUri, process: NodeJS.Process)
233
230
uri : file ,
234
231
// there's a new optional version param from https://github.com/microsoft/language-server-protocol/issues/201
235
232
// not using it for now, sigh
236
- diagnostics :
237
- files [ file ] . map ( ( { range, diagnosis } ) => {
238
- return {
239
- range : range ,
240
- message : diagnosis ,
241
- }
242
- } ) ,
233
+ diagnostics : files [ file ] ,
243
234
}
244
235
let notification : m . NotificationMessage = {
245
236
jsonrpc : jsonrpcVersion ,
@@ -313,7 +304,7 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
313
304
// TODO: handle single file
314
305
console . log ( "not handling single file" )
315
306
} else {
316
- diagnosisTimer = startWatchingBsbOutputFile ( root , process )
307
+ // diagnosisTimer = startWatchingBsbOutputFile(root, process)
317
308
}
318
309
// send the list of things we support
319
310
let result : p . InitializeResult = {
@@ -415,16 +406,51 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
415
406
result : result ,
416
407
} ;
417
408
process . send ! ( response ) ;
409
+
410
+ let params2 : p . PublishDiagnosticsParams = {
411
+ uri : params . textDocument . uri ,
412
+ // there's a new optional version param from https://github.com/microsoft/language-server-protocol/issues/201
413
+ // not using it for now, sigh
414
+ diagnostics : [ ] ,
415
+ }
416
+ let notification : m . NotificationMessage = {
417
+ jsonrpc : jsonrpcVersion ,
418
+ method : 'textDocument/publishDiagnostics' ,
419
+ params : params2 ,
420
+ } ;
421
+ process . send ! ( notification ) ;
418
422
} else {
419
423
let response : m . ResponseMessage = {
420
424
jsonrpc : jsonrpcVersion ,
421
425
id : aa . id ,
422
- error : {
423
- code : m . ErrorCodes . ParseError ,
424
- message : formattedResult . error ,
425
- }
426
+ result : [ ] ,
427
+ // technically a formatting failure should return the error but
428
+ // since this is LSP... the idiom seems to be to silently return
429
+ // nothing (to avoid an alert window each time on bad formatting)
430
+ // while sending a diangosis about the error afterward
431
+
432
+ // error: {
433
+ // code: m.ErrorCodes.ParseError,
434
+ // message: formattedResult.error,
435
+ // }
426
436
} ;
427
437
process . send ! ( response ) ;
438
+
439
+ let filesAndErrors = parseBsbLogOutput ( formattedResult . error )
440
+ Object . keys ( filesAndErrors ) . forEach ( file => {
441
+ let params2 : p . PublishDiagnosticsParams = {
442
+ uri : params . textDocument . uri ,
443
+ // there's a new optional version param from https://github.com/microsoft/language-server-protocol/issues/201
444
+ // not using it for now, sigh
445
+ diagnostics : filesAndErrors [ file ] ,
446
+ }
447
+ let notification : m . NotificationMessage = {
448
+ jsonrpc : jsonrpcVersion ,
449
+ method : 'textDocument/publishDiagnostics' ,
450
+ params : params2 ,
451
+ } ;
452
+ process . send ! ( notification ) ;
453
+ } )
428
454
}
429
455
}
430
456
}
0 commit comments