@@ -301,28 +301,24 @@ func (ls *INOLanguageServer) ShutdownReqFromIDE(ctx context.Context, logger json
301301 return nil
302302}
303303
304- func (ls * INOLanguageServer ) TextDocumentCompletionReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , inoParams * lsp.CompletionParams ) (* lsp.CompletionList , * jsonrpc.ResponseError ) {
304+ func (ls * INOLanguageServer ) TextDocumentCompletionReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.CompletionParams ) (* lsp.CompletionList , * jsonrpc.ResponseError ) {
305305 ls .readLock (logger , true )
306306 defer ls .readUnlock (logger )
307307
308- logger .Logf ("--> completion(%s)\n " , inoParams .TextDocument )
309- cppTextDocPositionParams , err := ls .ide2ClangTextDocumentPositionParams (logger , inoParams .TextDocumentPositionParams )
308+ cppTextDocPositionParams , err := ls .ide2ClangTextDocumentPositionParams (logger , ideParams .TextDocumentPositionParams )
310309 if err != nil {
311310 logger .Logf ("Error: %s" , err )
312311 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
313312 }
314313
315- cppParams := inoParams
316- cppParams .TextDocumentPositionParams = cppTextDocPositionParams
317- logger .Logf (" --> completion(%s)\n " , inoParams .TextDocument )
318- inoURI := inoParams .TextDocument .URI
319-
320- if err != nil {
321- logger .Logf ("Error: %s" , err )
322- return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
314+ clangParams := & lsp.CompletionParams {
315+ TextDocumentPositionParams : cppTextDocPositionParams ,
316+ Context : ideParams .Context ,
317+ WorkDoneProgressParams : ideParams .WorkDoneProgressParams ,
318+ PartialResultParams : ideParams .PartialResultParams ,
323319 }
324320
325- clangResp , clangErr , err := ls .Clangd .conn .TextDocumentCompletion (ctx , cppParams )
321+ clangCompletionList , clangErr , err := ls .Clangd .conn .TextDocumentCompletion (ctx , clangParams )
326322 if err != nil {
327323 logger .Logf ("clangd connection error: %v" , err )
328324 ls .Close ()
@@ -333,21 +329,67 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
333329 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : clangErr .AsError ().Error ()}
334330 }
335331
336- cppToIno := inoURI != lsp .NilURI && inoURI .AsPath ().EquivalentTo (ls .buildSketchCpp )
332+ ideCompletionList := & lsp.CompletionList {
333+ IsIncomplete : clangCompletionList .IsIncomplete ,
334+ }
335+ for _ , clangItem := range clangCompletionList .Items {
336+ if strings .HasPrefix (clangItem .InsertText , "_" ) {
337+ // XXX: Should be really ignored?
338+ continue
339+ }
337340
338- inoResp := * clangResp
339- inoItems := make ([]lsp.CompletionItem , 0 )
340- for _ , item := range clangResp .Items {
341- if ! strings .HasPrefix (item .InsertText , "_" ) {
342- if cppToIno && item .TextEdit != nil {
343- _ , item .TextEdit .Range = ls .sketchMapper .CppToInoRange (item .TextEdit .Range )
341+ var ideTextEdit * lsp.TextEdit
342+ if clangItem .TextEdit != nil {
343+ if ideURI , _ideTextEdit , isPreprocessed , err := ls .cpp2inoTextEdit (logger , clangParams .TextDocument .URI , * clangItem .TextEdit ); err != nil {
344+ logger .Logf ("Error converting textedit: %s" , err )
345+ return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
346+ } else if ideURI != ideParams .TextDocument .URI || isPreprocessed {
347+ err := fmt .Errorf ("text edit is in preprocessed section or is mapped to another file" )
348+ logger .Logf ("Error converting textedit: %s" , err )
349+ return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
350+ } else {
351+ ideTextEdit = & _ideTextEdit
352+ }
353+ }
354+ var ideAdditionalTextEdits []lsp.TextEdit
355+ if len (clangItem .AdditionalTextEdits ) > 0 {
356+ _ideAdditionalTextEdits , err := ls .cland2IdeTextEdits (logger , clangParams .TextDocument .URI , clangItem .AdditionalTextEdits )
357+ if err != nil {
358+ logger .Logf ("Error converting textedit: %s" , err )
359+ return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
344360 }
345- inoItems = append ( inoItems , item )
361+ ideAdditionalTextEdits = _ideAdditionalTextEdits [ ideParams . TextDocument . URI ]
346362 }
363+
364+ var ideCommand * lsp.Command
365+ if clangItem .Command != nil {
366+ c := ls .cpp2inoCommand (logger , * clangItem .Command )
367+ ideCommand = & c
368+ }
369+
370+ ideCompletionList .Items = append (ideCompletionList .Items , lsp.CompletionItem {
371+ Label : clangItem .Label ,
372+ LabelDetails : clangItem .LabelDetails ,
373+ Kind : clangItem .Kind ,
374+ Tags : clangItem .Tags ,
375+ Detail : clangItem .Detail ,
376+ Documentation : clangItem .Documentation ,
377+ Deprecated : clangItem .Deprecated ,
378+ Preselect : clangItem .Preselect ,
379+ SortText : clangItem .SortText ,
380+ FilterText : clangItem .FilterText ,
381+ InsertText : clangItem .InsertText ,
382+ InsertTextFormat : clangItem .InsertTextFormat ,
383+ InsertTextMode : clangItem .InsertTextMode ,
384+ CommitCharacters : clangItem .CommitCharacters ,
385+ Data : clangItem .Data ,
386+ Command : ideCommand ,
387+ TextEdit : ideTextEdit ,
388+ AdditionalTextEdits : ideAdditionalTextEdits ,
389+ })
347390 }
348- inoResp .Items = inoItems
349- logger .Logf ("<-- completion(%d items) cppToIno=%v" , len (inoResp .Items ), cppToIno )
350- return & inoResp , nil
391+ logger .Logf ("<-- completion(%d items)" , len (ideCompletionList .Items ))
392+ return ideCompletionList , nil
351393}
352394
353395func (ls * INOLanguageServer ) TextDocumentHoverReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.HoverParams ) (* lsp.Hover , * jsonrpc.ResponseError ) {
0 commit comments