Skip to content

Commit 76cda4a

Browse files
committed
Refactored and straighted up TextDocumentDocumentSymbolReqFromIDE
1 parent a5d6bfd commit 76cda4a

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

Diff for: ls/ls.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,9 @@ func (ls *INOLanguageServer) TextDocumentDocumentHighlightReqFromIDE(ctx context
728728
func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Context, logger jsonrpc.FunctionLogger, ideParams *lsp.DocumentSymbolParams) ([]lsp.DocumentSymbol, []lsp.SymbolInformation, *jsonrpc.ResponseError) {
729729
ls.readLock(logger, true)
730730
defer ls.readUnlock(logger)
731-
ideTextDocument := ideParams.TextDocument
732731

733732
// Convert request for clang
734-
clangTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideTextDocument)
733+
clangTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideParams.TextDocument)
735734
if err != nil {
736735
logger.Logf("Error: %s", err)
737736
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
@@ -757,7 +756,13 @@ func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Co
757756
// Convert response for IDE
758757
var ideDocSymbols []lsp.DocumentSymbol
759758
if clangDocSymbols != nil {
760-
ideDocSymbols = ls.clang2IdeDocumentSymbols(logger, clangDocSymbols, ideTextDocument.URI)
759+
if s, err := ls.clang2IdeDocumentSymbols(logger, clangDocSymbols, clangParams.TextDocument.URI, ideParams.TextDocument.URI); err != nil {
760+
logger.Logf("Error: %s", err)
761+
ls.Close()
762+
return nil, nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
763+
} else {
764+
ideDocSymbols = s
765+
}
761766
}
762767
var ideSymbolsInformation []lsp.SymbolInformation
763768
if clangSymbolsInformation != nil {

Diff for: ls/ls_clang_to_ide.go

+26-23
Original file line numberDiff line numberDiff line change
@@ -132,50 +132,53 @@ func (ls *INOLanguageServer) clang2IdeDiagnosticRelatedInformationArray(logger j
132132
return ideInfos, nil
133133
}
134134

135-
func (ls *INOLanguageServer) clang2IdeDocumentSymbols(logger jsonrpc.FunctionLogger, clangSymbols []lsp.DocumentSymbol, ideRequestedURI lsp.DocumentURI) []lsp.DocumentSymbol {
136-
logger.Logf("documentSymbol(%d document symbols)", len(clangSymbols))
137-
ideRequestedPath := ideRequestedURI.AsPath().String()
138-
logger.Logf(" filtering for requested ino file: %s", ideRequestedPath)
139-
if ideRequestedURI.Ext() != ".ino" || len(clangSymbols) == 0 {
140-
return clangSymbols
141-
}
135+
func (ls *INOLanguageServer) clang2IdeDocumentSymbols(logger jsonrpc.FunctionLogger, clangSymbols []lsp.DocumentSymbol, clangURI lsp.DocumentURI, origIdeURI lsp.DocumentURI) ([]lsp.DocumentSymbol, error) {
136+
logger.Logf("%s (%d document symbols)", clangURI, len(clangSymbols))
142137

143138
ideSymbols := []lsp.DocumentSymbol{}
144139
for _, clangSymbol := range clangSymbols {
145-
logger.Logf(" > convert %s %s", clangSymbol.Kind, clangSymbol.Range)
146-
if ls.sketchMapper.IsPreprocessedCppLine(clangSymbol.Range.Start.Line) {
147-
logger.Logf(" symbol is in the preprocessed section of the sketch.ino.cpp")
140+
logger.Logf(" > convert %s %s", clangSymbol.Kind, clangSymbol.Range)
141+
ideURI, ideRange, isPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, clangURI, clangSymbol.Range)
142+
if err != nil {
143+
return nil, err
144+
}
145+
if isPreprocessed {
146+
logger.Logf(" symbol is in the preprocessed section of the sketch.ino.cpp, skipping")
148147
continue
149148
}
150-
151-
idePath, ideRange := ls.sketchMapper.CppToInoRange(clangSymbol.Range)
152-
ideSelectionPath, ideSelectionRange := ls.sketchMapper.CppToInoRange(clangSymbol.SelectionRange)
153-
154-
if idePath != ideSelectionPath {
155-
logger.Logf(" ERROR: symbol range and selection belongs to different URI!")
156-
logger.Logf(" symbol %s != selection %s", clangSymbol.Range, clangSymbol.SelectionRange)
157-
logger.Logf(" %s:%s != %s:%s", idePath, ideRange, ideSelectionPath, ideSelectionRange)
149+
if ideURI != origIdeURI {
150+
logger.Logf(" filtering out symbol related to %s", ideURI)
158151
continue
159152
}
160-
161-
if idePath != ideRequestedPath {
162-
logger.Logf(" skipping symbol related to %s", idePath)
153+
ideSelectionURI, ideSelectionRange, isSelectionPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, clangURI, clangSymbol.SelectionRange)
154+
if err != nil {
155+
return nil, err
156+
}
157+
if ideSelectionURI != ideURI || isSelectionPreprocessed {
158+
logger.Logf(" ERROR: doc of symbol-selection-range does not match doc of symbol-range")
159+
logger.Logf(" range %s > %s:%s", clangSymbol.Range, ideURI, ideRange)
160+
logger.Logf(" selection %s > %s:%s", clangSymbol.SelectionRange, ideSelectionURI, ideSelectionRange)
163161
continue
164162
}
165163

164+
ideChildren, err := ls.clang2IdeDocumentSymbols(logger, clangSymbol.Children, clangURI, origIdeURI)
165+
if err != nil {
166+
return nil, err
167+
}
168+
166169
ideSymbols = append(ideSymbols, lsp.DocumentSymbol{
167170
Name: clangSymbol.Name,
168171
Detail: clangSymbol.Detail,
169172
Deprecated: clangSymbol.Deprecated,
170173
Kind: clangSymbol.Kind,
171174
Range: ideRange,
172175
SelectionRange: ideSelectionRange,
173-
Children: ls.clang2IdeDocumentSymbols(logger, clangSymbol.Children, ideRequestedURI),
176+
Children: ideChildren,
174177
Tags: ls.clang2IdeSymbolTags(logger, clangSymbol.Tags),
175178
})
176179
}
177180

178-
return ideSymbols
181+
return ideSymbols, nil
179182
}
180183

181184
func (ls *INOLanguageServer) cland2IdeTextEdits(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangTextEdits []lsp.TextEdit) (map[lsp.DocumentURI][]lsp.TextEdit, error) {

0 commit comments

Comments
 (0)