-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstate.go
101 lines (78 loc) · 2.04 KB
/
state.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package hyprls
import (
"context"
"os"
"strings"
"github.com/hyprland-community/hyprls/parser"
parser_data "github.com/hyprland-community/hyprls/parser/data"
"go.lsp.dev/protocol"
"go.uber.org/zap"
)
var logger *zap.Logger
var openedFiles = make(map[protocol.URI]string)
type state struct {
}
func (h Handler) state(ctx context.Context) state {
return ctx.Value("state").(state)
}
func parse(uri protocol.URI) (parser.Section, error) {
contents, err := file(uri)
if err != nil {
return parser.Section{}, err
}
return parser.Parse(contents)
}
func currentSection(root parser.Section, position protocol.Position) *parser.Section {
if !within(root.LSPRange(), position) {
return nil
}
for _, section := range root.Subsections {
sec := currentSection(section, position)
if sec != nil {
return sec
}
}
return &root
}
func currentAssignment(root parser.Section, position protocol.Position) *parser_data.VariableDefinition {
if !within(root.LSPRange(), position) {
return nil
}
for _, assignment := range root.Assignments {
if assignment.Position.Line == int(position.Line) {
return parser_data.FindVariableDefinitionInSection(root.Name, assignment.Key)
}
}
return nil
}
func within(rang protocol.Range, position protocol.Position) bool {
if position.Line < rang.Start.Line || position.Line > rang.End.Line {
return false
}
if position.Line == rang.Start.Line && position.Character < rang.Start.Character {
return false
}
if position.Line == rang.End.Line && position.Character > rang.End.Character {
return false
}
return true
}
func file(uri protocol.URI) (string, error) {
if contents, ok := openedFiles[uri]; ok {
return contents, nil
}
contents, err := os.ReadFile(uri.Filename())
if err != nil {
return "", err
}
openedFiles[uri] = string(contents)
return string(contents), nil
}
func currentLine(uri protocol.URI, position protocol.Position) (string, error) {
contents, err := file(uri)
if err != nil {
return "", err
}
lines := strings.Split(contents, "\n")
return lines[position.Line], nil
}