-
Notifications
You must be signed in to change notification settings - Fork 7
/
symbols.go
53 lines (49 loc) · 1.6 KB
/
symbols.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package hyprls
import (
"context"
"fmt"
"github.com/ewen-lbh/hyprls/parser"
"go.lsp.dev/protocol"
)
func (h Handler) DocumentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]interface{}, error) {
document, err := parse(params.TextDocument.URI)
if err != nil {
return nil, fmt.Errorf("while parsing: %w", err)
}
symbols := make([]interface{}, 0)
for _, symb := range gatherAllSymbols(document) {
symbols = append(symbols, &symb)
}
return symbols, nil
}
func gatherAllSymbols(root parser.Section) []protocol.DocumentSymbol {
symbols := make([]protocol.DocumentSymbol, 0)
for _, variable := range root.Assignments {
symbols = append(symbols, protocol.DocumentSymbol{
Name: variable.Key,
Kind: variable.Value.Kind.LSPSymbol(),
Detail: variable.ValueRaw,
Range: collapsedRange(variable.Position.LSP()),
SelectionRange: collapsedRange(variable.Position.LSP()),
})
}
for _, customVar := range root.Variables {
symbols = append(symbols, protocol.DocumentSymbol{
Name: "$" + customVar.Key,
Kind: protocol.SymbolKindVariable,
Detail: customVar.ValueRaw,
Range: collapsedRange(customVar.Position.LSP()),
SelectionRange: collapsedRange(customVar.Position.LSP()),
})
}
for _, section := range root.Subsections {
symbols = append(symbols, protocol.DocumentSymbol{
Name: section.Name,
Kind: protocol.SymbolKindNamespace,
Range: section.LSPRange(),
SelectionRange: section.LSPRange(),
Children: gatherAllSymbols(section),
})
}
return symbols
}