Skip to content

Commit 5dd3ea2

Browse files
committed
Fixed documentSymbol update deadlock
1 parent 16fddf1 commit 5dd3ea2

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

Diff for: handler/handler.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -542,43 +542,47 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
542542
}
543543

544544
func (handler *InoHandler) refreshCppDocumentSymbols() error {
545-
prefix := "LS --- "
545+
prefix := "RFSH--- "
546546
defer log.Printf(prefix + "(done)")
547-
log.Printf(prefix + "(queued)")
548-
handler.dataMux.Lock()
549-
defer handler.dataMux.Unlock()
550-
log.Printf(prefix + "(running)")
551547

552548
// Query source code symbols
553549
cppURI := lsp.NewDocumentURIFromPath(handler.buildSketchCpp)
554550
log.Printf(prefix+"sent to clangd: documentSymbol(%s)", cppURI)
555551
result, err := lsp.SendRequest(context.Background(), handler.ClangdConn, "textDocument/documentSymbol", &lsp.DocumentSymbolParams{
556552
TextDocument: lsp.TextDocumentIdentifier{URI: cppURI},
557553
})
554+
555+
log.Printf(prefix + "(queued answer)")
556+
handler.dataMux.Lock()
557+
defer handler.dataMux.Unlock()
558+
558559
if err != nil {
559560
log.Printf(prefix+"error: %s", err)
560561
return errors.WithMessage(err, "quering source code symbols")
561562
}
562563
result = handler.transformClangdResult("textDocument/documentSymbol", cppURI, lsp.NilURI, result)
563-
if symbols, ok := result.([]lsp.DocumentSymbol); !ok {
564+
565+
symbols, ok := result.([]lsp.DocumentSymbol)
566+
if !ok {
564567
log.Printf(prefix + "error: invalid response from clangd")
565568
return errors.New("invalid response from clangd")
566-
} else {
567-
// Filter non-functions symbols
568-
i := 0
569-
for _, symbol := range symbols {
570-
if symbol.Kind != lsp.SKFunction {
571-
continue
572-
}
573-
symbols[i] = symbol
574-
i++
575-
}
576-
symbols = symbols[:i]
577-
for _, symbol := range symbols {
578-
log.Printf(prefix+" symbol: %s %s", symbol.Kind, symbol.Name)
569+
}
570+
571+
// Filter non-functions symbols
572+
i := 0
573+
for _, symbol := range symbols {
574+
if symbol.Kind != lsp.SKFunction {
575+
continue
579576
}
580-
handler.buildSketchSymbols = symbols
577+
symbols[i] = symbol
578+
i++
579+
}
580+
symbols = symbols[:i]
581+
582+
for _, symbol := range symbols {
583+
log.Printf(prefix+" symbol: %s %s %s", symbol.Kind, symbol.Name, symbol.Range)
581584
}
585+
handler.buildSketchSymbols = symbols
582586
return nil
583587
}
584588

Diff for: lsp/protocol_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,26 @@ func TestVariousMessages(t *testing.T) {
127127
var init InitializeResult
128128
err = json.Unmarshal([]byte(msg), &init)
129129
require.NoError(t, err)
130+
131+
msg = `[{"kind":12,"name":"setup","range":{"end":{"character":12,"line":5},"start":{"character":0,"line":5}},"selectionRange":
132+
{"end":{"character":10,"line":5},"start":{"character":5,"line":5}}},{"kind":12,"name":"newfunc","range":{"end":{"character":14,"line":7},
133+
"start":{"character":0,"line":7}},"selectionRange":{"end":{"character":12,"line":7},"start":{"character":5,"line":7}}},{"kind":12,"name":
134+
"altro","range":{"end":{"character":12,"line":9},"start":{"character":0,"line":9}},"selectionRange":{"end":{"character":10,"line":9},"start":
135+
{"character":5,"line":9}}},{"kind":12,"name":"ancora","range":{"end":{"character":18,"line":11},"start":{"character":0,"line":11}},
136+
"selectionRange":{"end":{"character":11,"line":11},"start":{"character":5,"line":11}}},{"kind":12,"name":"loop","range":{"end":{
137+
"character":11,"line":13},"start":{"character":0,"line":13}},"selectionRange":{"end":{"character":9,"line":13},"start":{"character":5,
138+
"line":13}}},{"kind":12,"name":"secondFunction","range":{"end":{"character":21,"line":15},"start":{"character":0,"line":15}},
139+
"selectionRange":{"end":{"character":19,"line":15},"start":{"character":5,"line":15}}},{"kind":12,"name":"setup","range":{"end":{
140+
"character":1,"line":34},"start":{"character":0,"line":17}},"selectionRange":{"end":{"character":10,"line":17},"start":{"character":5,
141+
"line":17}}},{"kind":12,"name":"newfunc","range":{"end":{"character":1,"line":40},"start":{"character":0,"line":36}},"selectionRange":
142+
{"end":{"character":12,"line":36},"start":{"character":5,"line":36}}},{"kind":12,"name":"altro","range":{"end":{"character":38,"line":42},
143+
"start":{"character":0,"line":42}},"selectionRange":{"end":{"character":10,"line":42},"start":{"character":5,"line":42}}},{"kind":12,
144+
"name":"ancora","range":{"end":{"character":21,"line":47},"start":{"character":0,"line":47}},"selectionRange":{"end":{"character":11,
145+
"line":47},"start":{"character":5,"line":47}}},{"kind":12,"name":"loop","range":{"end":{"character":24,"line":49},"start":{"character":0,
146+
"line":49}},"selectionRange":{"end":{"character":9,"line":49},"start":{"character":5,"line":49}}},{"kind":12,"name":"secondFunction",
147+
"range":{"end":{"character":38,"line":53},"start":{"character":0,"line":53}},"selectionRange":{"end":{"character":19,"line":53},"start":
148+
{"character":5,"line":53}}}]`
149+
var symbol DocumentSymbolArrayOrSymbolInformationArray
150+
err = json.Unmarshal([]byte(msg), &symbol)
151+
require.NoError(t, err)
130152
}

0 commit comments

Comments
 (0)