Skip to content

Commit d9872e2

Browse files
committed
Moved documentSymbol handlers in the proper place
1 parent c8ad100 commit d9872e2

File tree

3 files changed

+89
-76
lines changed

3 files changed

+89
-76
lines changed

Diff for: ls/ls.go

+16-75
Original file line numberDiff line numberDiff line change
@@ -611,24 +611,22 @@ func (ls *INOLanguageServer) TextDocumentDocumentHighlightReqFromIDE(ctx context
611611
return ideHighlights, nil
612612
}
613613

614-
func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, inoParams *lsp.DocumentSymbolParams) ([]lsp.DocumentSymbol, []lsp.SymbolInformation, *jsonrpc.ResponseError) {
614+
func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, ideParams *lsp.DocumentSymbolParams) ([]lsp.DocumentSymbol, []lsp.SymbolInformation, *jsonrpc.ResponseError) {
615615
ls.readLock(logger, true)
616616
defer ls.readUnlock(logger)
617+
ideTextDocument := ideParams.TextDocument
617618

618-
inoTextDocument := inoParams.TextDocument
619-
inoURI := inoTextDocument.URI
620-
logger.Logf("--> %s")
621-
622-
cppTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, inoTextDocument)
619+
// Convert request for clang
620+
cppTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideTextDocument)
623621
if err != nil {
624622
logger.Logf("Error: %s", err)
625623
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
626624
}
625+
clangParams := *ideParams
626+
clangParams.TextDocument = cppTextDocument
627627

628-
cppParams := *inoParams
629-
cppParams.TextDocument = cppTextDocument
630-
logger.Logf(" --> documentSymbol(%s)", cppTextDocument)
631-
cppDocSymbols, cppSymbolInformation, clangErr, err := ls.Clangd.conn.TextDocumentDocumentSymbol(ctx, &cppParams)
628+
// Send request to clang
629+
clangDocSymbols, clangSymbolsInformation, clangErr, err := ls.Clangd.conn.TextDocumentDocumentSymbol(ctx, &clangParams)
632630
if err != nil {
633631
logger.Logf("clangd connectiono error: %v", err)
634632
ls.Close()
@@ -639,17 +637,16 @@ func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Co
639637
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: clangErr.AsError().Error()}
640638
}
641639

642-
var inoDocSymbols []lsp.DocumentSymbol
643-
if cppDocSymbols != nil {
644-
logger.Logf(" <-- documentSymbol(%d document symbols)", len(cppDocSymbols))
645-
inoDocSymbols = ls.cpp2inoDocumentSymbols(logger, cppDocSymbols, inoURI)
640+
// Convert response for IDE
641+
var ideDocSymbols []lsp.DocumentSymbol
642+
if clangDocSymbols != nil {
643+
ideDocSymbols = ls.clang2IdeDocumentSymbols(logger, clangDocSymbols, ideTextDocument.URI)
646644
}
647-
var inoSymbolInformation []lsp.SymbolInformation
648-
if cppSymbolInformation != nil {
649-
logger.Logf(" <-- documentSymbol(%d symbol information)", len(cppSymbolInformation))
650-
inoSymbolInformation = ls.clang2IdeSymbolInformation(cppSymbolInformation)
645+
var ideSymbolsInformation []lsp.SymbolInformation
646+
if clangSymbolsInformation != nil {
647+
ideSymbolsInformation = ls.clang2IdeSymbolsInformation(logger, clangSymbolsInformation)
651648
}
652-
return inoDocSymbols, inoSymbolInformation, nil
649+
return ideDocSymbols, ideSymbolsInformation, nil
653650
}
654651

655652
func (ls *INOLanguageServer) TextDocumentCodeActionReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, inoParams *lsp.CodeActionParams) ([]lsp.CommandOrCodeAction, *jsonrpc.ResponseError) {
@@ -1221,18 +1218,6 @@ func (ls *INOLanguageServer) ino2cppVersionedTextDocumentIdentifier(logger jsonr
12211218
return res, err
12221219
}
12231220

1224-
func (ls *INOLanguageServer) ide2ClangRange(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, ideRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
1225-
clangURI, err := ls.ide2ClangDocumentURI(logger, ideURI)
1226-
if err != nil {
1227-
return lsp.DocumentURI{}, lsp.Range{}, err
1228-
}
1229-
if clangURI.AsPath().EquivalentTo(ls.buildSketchCpp) {
1230-
cppRange := ls.sketchMapper.InoToCppLSPRange(ideURI, ideRange)
1231-
return clangURI, cppRange, nil
1232-
}
1233-
return clangURI, ideRange, nil
1234-
}
1235-
12361221
func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger, cppLocations []lsp.Location) ([]lsp.Location, error) {
12371222
inoLocations := []lsp.Location{}
12381223
for _, cppLocation := range cppLocations {
@@ -1402,50 +1387,6 @@ func (ls *INOLanguageServer) cpp2inoTextEdit(logger jsonrpc.FunctionLogger, cppU
14021387
return inoURI, inoEdit, inPreprocessed, err
14031388
}
14041389

1405-
func (ls *INOLanguageServer) cpp2inoDocumentSymbols(logger jsonrpc.FunctionLogger, cppSymbols []lsp.DocumentSymbol, inoRequestedURI lsp.DocumentURI) []lsp.DocumentSymbol {
1406-
inoRequested := inoRequestedURI.AsPath().String()
1407-
logger.Logf(" filtering for requested ino file: %s", inoRequested)
1408-
if inoRequestedURI.Ext() != ".ino" || len(cppSymbols) == 0 {
1409-
return cppSymbols
1410-
}
1411-
1412-
inoSymbols := []lsp.DocumentSymbol{}
1413-
for _, symbol := range cppSymbols {
1414-
logger.Logf(" > convert %s %s", symbol.Kind, symbol.Range)
1415-
if ls.sketchMapper.IsPreprocessedCppLine(symbol.Range.Start.Line) {
1416-
logger.Logf(" symbol is in the preprocessed section of the sketch.ino.cpp")
1417-
continue
1418-
}
1419-
1420-
inoFile, inoRange := ls.sketchMapper.CppToInoRange(symbol.Range)
1421-
inoSelectionURI, inoSelectionRange := ls.sketchMapper.CppToInoRange(symbol.SelectionRange)
1422-
1423-
if inoFile != inoSelectionURI {
1424-
logger.Logf(" ERROR: symbol range and selection belongs to different URI!")
1425-
logger.Logf(" symbol %s != selection %s", symbol.Range, symbol.SelectionRange)
1426-
logger.Logf(" %s:%s != %s:%s", inoFile, inoRange, inoSelectionURI, inoSelectionRange)
1427-
continue
1428-
}
1429-
1430-
if inoFile != inoRequested {
1431-
logger.Logf(" skipping symbol related to %s", inoFile)
1432-
continue
1433-
}
1434-
1435-
inoSymbols = append(inoSymbols, lsp.DocumentSymbol{
1436-
Name: symbol.Name,
1437-
Detail: symbol.Detail,
1438-
Deprecated: symbol.Deprecated,
1439-
Kind: symbol.Kind,
1440-
Range: inoRange,
1441-
SelectionRange: inoSelectionRange,
1442-
Children: ls.cpp2inoDocumentSymbols(logger, symbol.Children, inoRequestedURI),
1443-
})
1444-
}
1445-
1446-
return inoSymbols
1447-
}
1448-
14491390
type UnknownURI struct {
14501391
URI lsp.DocumentURI
14511392
}

Diff for: ls/ls_clang_to_ide.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,60 @@ func (ls *INOLanguageServer) clang2IdeDiagnosticRelatedInformationArray(logger j
130130
return ideInfos, nil
131131
}
132132

133-
func (ls *INOLanguageServer) clang2IdeSymbolInformation(clangSymbolsInformation []lsp.SymbolInformation) []lsp.SymbolInformation {
133+
func (ls *INOLanguageServer) clang2IdeDocumentSymbols(logger jsonrpc.FunctionLogger, clangSymbols []lsp.DocumentSymbol, ideRequestedURI lsp.DocumentURI) []lsp.DocumentSymbol {
134+
logger.Logf("documentSymbol(%d document symbols)", len(clangSymbols))
135+
ideRequestedPath := ideRequestedURI.AsPath().String()
136+
logger.Logf(" filtering for requested ino file: %s", ideRequestedPath)
137+
if ideRequestedURI.Ext() != ".ino" || len(clangSymbols) == 0 {
138+
return clangSymbols
139+
}
140+
141+
ideSymbols := []lsp.DocumentSymbol{}
142+
for _, clangSymbol := range clangSymbols {
143+
logger.Logf(" > convert %s %s", clangSymbol.Kind, clangSymbol.Range)
144+
if ls.sketchMapper.IsPreprocessedCppLine(clangSymbol.Range.Start.Line) {
145+
logger.Logf(" symbol is in the preprocessed section of the sketch.ino.cpp")
146+
continue
147+
}
148+
149+
idePath, ideRange := ls.sketchMapper.CppToInoRange(clangSymbol.Range)
150+
ideSelectionPath, ideSelectionRange := ls.sketchMapper.CppToInoRange(clangSymbol.SelectionRange)
151+
152+
if idePath != ideSelectionPath {
153+
logger.Logf(" ERROR: symbol range and selection belongs to different URI!")
154+
logger.Logf(" symbol %s != selection %s", clangSymbol.Range, clangSymbol.SelectionRange)
155+
logger.Logf(" %s:%s != %s:%s", idePath, ideRange, ideSelectionPath, ideSelectionRange)
156+
continue
157+
}
158+
159+
if idePath != ideRequestedPath {
160+
logger.Logf(" skipping symbol related to %s", idePath)
161+
continue
162+
}
163+
164+
ideSymbols = append(ideSymbols, lsp.DocumentSymbol{
165+
Name: clangSymbol.Name,
166+
Detail: clangSymbol.Detail,
167+
Deprecated: clangSymbol.Deprecated,
168+
Kind: clangSymbol.Kind,
169+
Range: ideRange,
170+
SelectionRange: ideSelectionRange,
171+
Children: ls.clang2IdeDocumentSymbols(logger, clangSymbol.Children, ideRequestedURI),
172+
Tags: ls.clang2IdeSymbolTags(logger, clangSymbol.Tags),
173+
})
174+
}
175+
176+
return ideSymbols
177+
}
178+
179+
func (ls *INOLanguageServer) clang2IdeSymbolTags(logger jsonrpc.FunctionLogger, clangSymbolTags []lsp.SymbolTag) []lsp.SymbolTag {
180+
if len(clangSymbolTags) == 0 || clangSymbolTags == nil {
181+
return clangSymbolTags
182+
}
183+
panic("not implemented")
184+
}
185+
186+
func (ls *INOLanguageServer) clang2IdeSymbolsInformation(logger jsonrpc.FunctionLogger, clangSymbolsInformation []lsp.SymbolInformation) []lsp.SymbolInformation {
187+
logger.Logf("SymbolInformation (%d elements):", len(clangSymbolsInformation))
134188
panic("not implemented")
135189
}

Diff for: ls/ls_ide_to_clang.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ls
22

33
import (
4+
"fmt"
5+
46
"github.com/arduino/arduino-language-server/sourcemapper"
57
"go.bug.st/lsp"
68
"go.bug.st/lsp/jsonrpc"
@@ -89,3 +91,19 @@ func (ls *INOLanguageServer) ide2ClangTextDocumentPositionParams(logger jsonrpc.
8991
logger.Logf("%s -> %s", ideParams, clangParams)
9092
return clangParams, nil
9193
}
94+
95+
func (ls *INOLanguageServer) ide2ClangRange(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, ideRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
96+
clangURI, err := ls.ide2ClangDocumentURI(logger, ideURI)
97+
if err != nil {
98+
return lsp.DocumentURI{}, lsp.Range{}, err
99+
}
100+
clangRange := ideRange
101+
if ls.clangURIRefersToIno(clangURI) {
102+
if r, ok := ls.sketchMapper.InoToCppLSPRangeOk(ideURI, ideRange); ok {
103+
clangRange = r
104+
} else {
105+
return lsp.DocumentURI{}, lsp.Range{}, fmt.Errorf("invalid range %s:%s: could not be mapped to Arduino-preprocessed sketck.ino.cpp", ideURI, ideRange)
106+
}
107+
}
108+
return clangURI, clangRange, nil
109+
}

0 commit comments

Comments
 (0)