@@ -8,8 +8,10 @@ import (
88 "log"
99 "net/url"
1010 "path/filepath"
11+ "regexp"
1112 "strings"
1213
14+ "github.com/pkg/errors"
1315 lsp "github.com/sourcegraph/go-lsp"
1416 "github.com/sourcegraph/jsonrpc2"
1517)
@@ -162,8 +164,7 @@ func (handler *InoHandler) createFileData(ctx context.Context, sourceURI lsp.Doc
162164 fqbn := "arduino:avr:uno"
163165 targetPath , targetBytes , err := generateCpp ([]byte (sourceText ), filepath .Base (sourcePath ), fqbn )
164166 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 )
167168 return nil , nil , err
168169 }
169170
@@ -225,6 +226,23 @@ func (handler *InoHandler) deleteFileData(sourceURI lsp.DocumentURI) {
225226 }
226227}
227228
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+
228246func (handler * InoHandler ) ino2cppTextDocumentIdentifier (doc * lsp.TextDocumentIdentifier ) error {
229247 if data , ok := handler .data [doc .URI ]; ok {
230248 doc .URI = data .targetURI
@@ -254,17 +272,19 @@ func (handler *InoHandler) ino2cppDidChangeTextDocumentParams(params *lsp.DidCha
254272 return err
255273 }
256274 }
275+ return nil
257276 }
258- return nil
277+ return unknownURI ( params . TextDocument . URI )
259278}
260279
261280func (handler * InoHandler ) ino2cppTextDocumentPositionParams (params * lsp.TextDocumentPositionParams ) error {
262281 handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
263282 if data , ok := handler .data [params .TextDocument .URI ]; ok {
264283 targetLine := data .targetLineMap [params .Position .Line ]
265284 params .Position .Line = targetLine
285+ return nil
266286 }
267- return nil
287+ return unknownURI ( params . TextDocument . URI )
268288}
269289
270290func (handler * InoHandler ) ino2cppCodeActionParams (params * lsp.CodeActionParams ) error {
@@ -277,33 +297,37 @@ func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams)
277297 r .Start .Line = data .targetLineMap [r .Start .Line ]
278298 r .End .Line = data .targetLineMap [r .End .Line ]
279299 }
300+ return nil
280301 }
281- return nil
302+ return unknownURI ( params . TextDocument . URI )
282303}
283304
284305func (handler * InoHandler ) ino2cppDocumentRangeFormattingParams (params * lsp.DocumentRangeFormattingParams ) error {
285306 handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
286307 if data , ok := handler .data [params .TextDocument .URI ]; ok {
287308 params .Range .Start .Line = data .targetLineMap [params .Range .Start .Line ]
288309 params .Range .End .Line = data .targetLineMap [params .Range .End .Line ]
310+ return nil
289311 }
290- return nil
312+ return unknownURI ( params . TextDocument . URI )
291313}
292314
293315func (handler * InoHandler ) ino2cppDocumentOnTypeFormattingParams (params * lsp.DocumentOnTypeFormattingParams ) error {
294316 handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
295317 if data , ok := handler .data [params .TextDocument .URI ]; ok {
296318 params .Position .Line = data .targetLineMap [params .Position .Line ]
319+ return nil
297320 }
298- return nil
321+ return unknownURI ( params . TextDocument . URI )
299322}
300323
301324func (handler * InoHandler ) ino2cppRenameParams (params * lsp.RenameParams ) error {
302325 handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
303326 if data , ok := handler .data [params .TextDocument .URI ]; ok {
304327 params .Position .Line = data .targetLineMap [params .Position .Line ]
328+ return nil
305329 }
306- return nil
330+ return unknownURI ( params . TextDocument . URI )
307331}
308332
309333func (handler * InoHandler ) transformClangdResult (method string , uri lsp.DocumentURI , result interface {}) interface {} {
@@ -538,3 +562,7 @@ func uriToPath(uri lsp.DocumentURI) string {
538562func pathToURI (path string ) lsp.DocumentURI {
539563 return "file://" + lsp .DocumentURI (filepath .ToSlash (path ))
540564}
565+
566+ func unknownURI (uri lsp.DocumentURI ) error {
567+ return errors .New ("Document is not available: " + string (uri ))
568+ }
0 commit comments