@@ -8,8 +8,10 @@ import (
8
8
"log"
9
9
"net/url"
10
10
"path/filepath"
11
+ "regexp"
11
12
"strings"
12
13
14
+ "github.com/pkg/errors"
13
15
lsp "github.com/sourcegraph/go-lsp"
14
16
"github.com/sourcegraph/jsonrpc2"
15
17
)
@@ -162,8 +164,7 @@ func (handler *InoHandler) createFileData(ctx context.Context, sourceURI lsp.Doc
162
164
fqbn := "arduino:avr:uno"
163
165
targetPath , targetBytes , err := generateCpp ([]byte (sourceText ), filepath .Base (sourcePath ), fqbn )
164
166
if err != nil {
165
- message := "Could not start editor support:\n " + err .Error ()
166
- go handler .showMessage (ctx , lsp .MTError , message )
167
+ handler .handleError (ctx , err , fqbn )
167
168
return nil , nil , err
168
169
}
169
170
@@ -225,6 +226,23 @@ func (handler *InoHandler) deleteFileData(sourceURI lsp.DocumentURI) {
225
226
}
226
227
}
227
228
229
+ func (handler * InoHandler ) handleError (ctx context.Context , err error , fqbn string ) {
230
+ errorStr := err .Error ()
231
+ var message string
232
+ if strings .Contains (errorStr , "platform not installed" ) {
233
+ message = "Editor support is disabled because the platform `" + fqbn + "` is not installed."
234
+ message += " Use the Boards Manager to install it."
235
+ } else if strings .Contains (errorStr , "No such file or directory" ) {
236
+ exp , _ := regexp .Compile ("([\\ w\\ .\\ -]+)\\ .h: No such file or directory" )
237
+ submatch := exp .FindStringSubmatch (errorStr )
238
+ message = "Editor support is disabled because the library `" + submatch [1 ] + "` is not installed."
239
+ message += " Use the Library Manager to install it"
240
+ } else {
241
+ message = "Could not start editor support.\n " + errorStr
242
+ }
243
+ go handler .showMessage (ctx , lsp .MTError , message )
244
+ }
245
+
228
246
func (handler * InoHandler ) ino2cppTextDocumentIdentifier (doc * lsp.TextDocumentIdentifier ) error {
229
247
if data , ok := handler .data [doc .URI ]; ok {
230
248
doc .URI = data .targetURI
@@ -254,17 +272,19 @@ func (handler *InoHandler) ino2cppDidChangeTextDocumentParams(params *lsp.DidCha
254
272
return err
255
273
}
256
274
}
275
+ return nil
257
276
}
258
- return nil
277
+ return unknownURI ( params . TextDocument . URI )
259
278
}
260
279
261
280
func (handler * InoHandler ) ino2cppTextDocumentPositionParams (params * lsp.TextDocumentPositionParams ) error {
262
281
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
263
282
if data , ok := handler .data [params .TextDocument .URI ]; ok {
264
283
targetLine := data .targetLineMap [params .Position .Line ]
265
284
params .Position .Line = targetLine
285
+ return nil
266
286
}
267
- return nil
287
+ return unknownURI ( params . TextDocument . URI )
268
288
}
269
289
270
290
func (handler * InoHandler ) ino2cppCodeActionParams (params * lsp.CodeActionParams ) error {
@@ -277,33 +297,37 @@ func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams)
277
297
r .Start .Line = data .targetLineMap [r .Start .Line ]
278
298
r .End .Line = data .targetLineMap [r .End .Line ]
279
299
}
300
+ return nil
280
301
}
281
- return nil
302
+ return unknownURI ( params . TextDocument . URI )
282
303
}
283
304
284
305
func (handler * InoHandler ) ino2cppDocumentRangeFormattingParams (params * lsp.DocumentRangeFormattingParams ) error {
285
306
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
286
307
if data , ok := handler .data [params .TextDocument .URI ]; ok {
287
308
params .Range .Start .Line = data .targetLineMap [params .Range .Start .Line ]
288
309
params .Range .End .Line = data .targetLineMap [params .Range .End .Line ]
310
+ return nil
289
311
}
290
- return nil
312
+ return unknownURI ( params . TextDocument . URI )
291
313
}
292
314
293
315
func (handler * InoHandler ) ino2cppDocumentOnTypeFormattingParams (params * lsp.DocumentOnTypeFormattingParams ) error {
294
316
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
295
317
if data , ok := handler .data [params .TextDocument .URI ]; ok {
296
318
params .Position .Line = data .targetLineMap [params .Position .Line ]
319
+ return nil
297
320
}
298
- return nil
321
+ return unknownURI ( params . TextDocument . URI )
299
322
}
300
323
301
324
func (handler * InoHandler ) ino2cppRenameParams (params * lsp.RenameParams ) error {
302
325
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
303
326
if data , ok := handler .data [params .TextDocument .URI ]; ok {
304
327
params .Position .Line = data .targetLineMap [params .Position .Line ]
328
+ return nil
305
329
}
306
- return nil
330
+ return unknownURI ( params . TextDocument . URI )
307
331
}
308
332
309
333
func (handler * InoHandler ) transformClangdResult (method string , uri lsp.DocumentURI , result interface {}) interface {} {
@@ -538,3 +562,7 @@ func uriToPath(uri lsp.DocumentURI) string {
538
562
func pathToURI (path string ) lsp.DocumentURI {
539
563
return "file://" + lsp .DocumentURI (filepath .ToSlash (path ))
540
564
}
565
+
566
+ func unknownURI (uri lsp.DocumentURI ) error {
567
+ return errors .New ("Document is not available: " + string (uri ))
568
+ }
0 commit comments