forked from hyprland-community/hyprls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.go
211 lines (183 loc) · 6.05 KB
/
completion.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package hyprls
import (
"context"
"errors"
"fmt"
"strings"
"unicode"
"github.com/ewen-lbh/hyprls/parser"
parser_data "github.com/ewen-lbh/hyprls/parser/data"
"go.lsp.dev/protocol"
"go.uber.org/zap"
)
func (h Handler) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
line, err := currentLine(params.TextDocument.URI, params.Position)
if err != nil {
return nil, nil
}
file, err := parse(params.TextDocument.URI)
if err != nil {
return nil, nil
}
sec := currentSection(file, params.Position)
if sec == nil {
sec = &parser.Section{}
}
cursorIsAfterEquals := err == nil && strings.Contains(line, "=") && strings.Index(line, "=") < int(params.Position.Character)
// we are after the equals sign, suggest custom properties only
if cursorIsAfterEquals {
items := make([]protocol.CompletionItem, 0)
cursorOrLineEnd := min(int(params.Position.Character), len(line)-1)
characterBeforeCursorIsDollarSign := line[cursorOrLineEnd] == '$'
// Don't propose custom variables if in the middle of typing a word
// Only propose if a dollar sign was typed or is just before the cursor
// Or we are after whitespace
// Or we are in the middle of a color completion (typed a r, and key is a color or gradient)
if !characterBeforeCursorIsDollarSign && !unicode.IsSpace(rune(line[params.Position.Character-1])) {
return nil, nil
}
// Prevent duplicate dollar signs upon completion accept
var textEditRange protocol.Range
if characterBeforeCursorIsDollarSign {
textEditRange = protocol.Range{
protocol.Position{params.Position.Line, params.Position.Character - 1},
protocol.Position{params.Position.Line, params.Position.Character},
}
} else {
textEditRange = collapsedRange(params.Position)
}
file.WalkCustomVariables(func(v *parser.CustomVariable) {
items = append(items, protocol.CompletionItem{
Label: "$" + v.Key,
Kind: protocol.CompletionItemKindVariable,
Documentation: protocol.MarkupContent{
Kind: protocol.PlainText,
Value: v.ValueRaw,
},
TextEdit: &protocol.TextEdit{
Range: textEditRange,
NewText: "$" + v.Key,
},
})
})
textedit := func(t string) *protocol.TextEdit {
return &protocol.TextEdit{
Range: collapsedRange(params.Position),
NewText: t,
}
}
valueKind := parser.String
if sec != nil {
assignment := currentAssignment(*sec, params.Position)
if assignment != nil {
valueKind, err = parser.ValueKindFromString(assignment.ParserTypeString())
if err != nil {
valueKind = parser.String
}
}
}
logger.Debug("LSP:Completion", zap.Any("valueKind", valueKind))
switch valueKind {
case parser.Color, parser.Gradient:
items = append(items, protocol.CompletionItem{
Label: "rgba(⋯)",
Kind: protocol.CompletionItemKindColor,
InsertTextFormat: protocol.InsertTextFormatSnippet,
Documentation: "Define a color with an alpha channel of the form rgba(RRGGBBAA) in hexadecimal notation.",
TextEdit: textedit("rgba(${0:ffffffff})"),
})
items = append(items, protocol.CompletionItem{
Label: "rgb(⋯)",
Kind: protocol.CompletionItemKindColor,
InsertTextFormat: protocol.InsertTextFormatSnippet,
TextEdit: textedit("rgb(${0:ffffff})"),
Documentation: "Define a color of the form rgb(RRGGBB) in hexadecimal notation.",
})
items = append(items, protocol.CompletionItem{
Label: "0xAARRGGBB",
Kind: protocol.CompletionItemKindColor,
InsertTextFormat: protocol.InsertTextFormatSnippet,
TextEdit: textedit("0x${1:ffffffff}"),
Deprecated: true,
Documentation: "Define a color of the form 0xAARRGGBB in hexadecimal notation.",
})
case parser.Bool:
items = append(items, protocol.CompletionItem{
Label: "true",
Kind: protocol.CompletionItemKindValue,
})
items = append(items, protocol.CompletionItem{
Label: "false",
Kind: protocol.CompletionItemKindValue,
})
case parser.Modmask:
for keystring := range parser.ModKeyNames {
items = append(items, protocol.CompletionItem{
Label: keystring,
Kind: protocol.CompletionItemKindEnumMember,
})
}
}
return &protocol.CompletionList{
Items: items,
}, nil
}
availableVariables := make([]parser_data.VariableDefinition, 0)
if sec != nil {
secDef := parser_data.FindSectionDefinitionByName(sec.Name)
if secDef != nil {
availableVariables = append(availableVariables, secDef.Variables...)
}
}
items := make([]protocol.CompletionItem, 0)
vars:
for _, vardef := range availableVariables {
// Don't suggest variables that are already defined
for _, definedvar := range sec.Assignments {
if vardef.Name == definedvar.Key {
continue vars
}
}
items = append(items, protocol.CompletionItem{
Label: vardef.Name,
Kind: protocol.CompletionItemKindField,
Documentation: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: fmt.Sprintf("Type: %s\n\n%s", vardef.Type, vardef.Description),
},
})
}
for _, kw := range parser_data.Keywords {
items = append(items, protocol.CompletionItem{
Label: kw.Name,
Kind: protocol.CompletionItemKindKeyword,
Documentation: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: kw.Description,
},
})
}
subsections:
for _, subsections := range sec.Subsections {
// Don't suggest subsections that are already defined
for _, definedSubsection := range sec.Subsections {
if subsections.Name == definedSubsection.Name {
continue subsections
}
}
items = append(items, protocol.CompletionItem{
Label: subsections.Name,
Kind: protocol.CompletionItemKindModule,
Documentation: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: fmt.Sprintf("Subsection of %s", sec.Name),
},
})
}
return &protocol.CompletionList{
Items: items,
}, nil
}
func (h Handler) CompletionResolve(ctx context.Context, params *protocol.CompletionItem) (*protocol.CompletionItem, error) {
return nil, errors.New("unimplemented")
}