-
Notifications
You must be signed in to change notification settings - Fork 6
/
hover.go
80 lines (71 loc) · 2.25 KB
/
hover.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package hyprls
import (
"context"
"fmt"
"strings"
"github.com/MakeNowJust/heredoc"
parser_data "github.com/ewen-lbh/hyprls/parser/data"
"go.lsp.dev/protocol"
)
func (h Handler) Hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
line, err := currentLine(params.TextDocument.URI, params.Position)
if err != nil {
return nil, fmt.Errorf("while getting current line of file: %w", err)
}
if !strings.Contains(line, "=") {
return nil, nil
}
// key is word before the equal sign. [0] is safe since we checked for "=" above
key := strings.TrimSpace(strings.Split(line, "=")[0])
indexOfFirstNonWhitespace := strings.IndexFunc(line, func(r rune) bool {
return r != ' ' && r != '\t'
})
indexOfLastNonWhitespace := strings.LastIndexFunc(line, func(r rune) bool {
return r != ' ' && r != '\t'
}) + 1
for _, section := range parser_data.Sections {
if def := section.VariableDefinition(key); def != nil {
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: heredoc.Docf(`### %s: %s (%s)
%s
- Defaults to: %s
`, strings.Join(section.Path, ":"), def.Name, def.Type, def.Description, def.PrettyDefault()),
},
Range: &protocol.Range{
Start: protocol.Position{
Line: params.Position.Line,
Character: uint32(indexOfFirstNonWhitespace),
},
End: protocol.Position{
Line: params.Position.Line,
Character: uint32(indexOfLastNonWhitespace),
},
},
}, nil
} else if kw, found := parser_data.FindKeyword(key); found {
flagsLine := ""
if len(kw.Flags) > 0 {
flagsLine = fmt.Sprintf("\n- Accepts the following flags: %s\n", strings.Join(kw.Flags, ", "))
}
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: fmt.Sprintf("### %s [[docs]](%s)%s\n%s", kw.Name, kw.DocumentationLink(), flagsLine, kw.Description),
},
Range: &protocol.Range{
Start: protocol.Position{
Line: params.Position.Line,
Character: uint32(indexOfFirstNonWhitespace),
},
End: protocol.Position{
Line: params.Position.Line,
Character: uint32(indexOfLastNonWhitespace),
},
},
}, nil
}
}
return nil, nil
}