-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstate.go
More file actions
105 lines (82 loc) · 2.18 KB
/
state.go
File metadata and controls
105 lines (82 loc) · 2.18 KB
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
102
103
104
105
package hyprls
import (
"os"
"path/filepath"
"slices"
"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)
var defaultIgnores = []string{"hyprlock.conf", "hypridle.conf"}
var Ignores = defaultIgnores
type state struct {
}
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
}
func isFileIgnored(uri protocol.URI) bool {
n := filepath.Base(uri.Filename())
return slices.Contains(Ignores, n)
}