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
|
package lsp
import (
"sort"
"strings"
"github.com/grindlemire/go-tui/internal/lsp/provider"
"github.com/grindlemire/go-tui/internal/tuigen"
)
type CompletionParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"`
Position Position `json:"position"`
Context *CompletionContext `json:"context,omitempty"`
}
type CompletionContext struct {
TriggerKind int `json:"triggerKind"`
TriggerCharacter string `json:"triggerCharacter,omitempty"`
}
type (
CompletionList = provider.CompletionList
CompletionItem = provider.CompletionItem
CompletionItemKind = provider.CompletionItemKind
)
const (
CompletionItemKindText = provider.CompletionItemKindText
CompletionItemKindMethod = provider.CompletionItemKindMethod
CompletionItemKindFunction = provider.CompletionItemKindFunction
CompletionItemKindConstructor = provider.CompletionItemKindConstructor
CompletionItemKindField = provider.CompletionItemKindField
CompletionItemKindVariable = provider.CompletionItemKindVariable
CompletionItemKindClass = provider.CompletionItemKindClass
CompletionItemKindInterface = provider.CompletionItemKindInterface
CompletionItemKindModule = provider.CompletionItemKindModule
CompletionItemKindProperty = provider.CompletionItemKindProperty
CompletionItemKindUnit = provider.CompletionItemKindUnit
CompletionItemKindValue = provider.CompletionItemKindValue
CompletionItemKindEnum = provider.CompletionItemKindEnum
CompletionItemKindKeyword = provider.CompletionItemKindKeyword
CompletionItemKindSnippet = provider.CompletionItemKindSnippet
CompletionItemKindColor = provider.CompletionItemKindColor
CompletionItemKindFile = provider.CompletionItemKindFile
CompletionItemKindReference = provider.CompletionItemKindReference
CompletionItemKindFolder = provider.CompletionItemKindFolder
CompletionItemKindEnumMember = provider.CompletionItemKindEnumMember
CompletionItemKindConstant = provider.CompletionItemKindConstant
CompletionItemKindStruct = provider.CompletionItemKindStruct
CompletionItemKindEvent = provider.CompletionItemKindEvent
CompletionItemKindOperator = provider.CompletionItemKindOperator
CompletionItemKindTypeParameter = provider.CompletionItemKindTypeParameter
)
func (s *Server) isInClassAttribute(doc *Document, pos Position) (bool, string) {
offset := PositionToOffset(doc.Content, pos)
if offset <= 0 {
return false, ""
}
content := doc.Content
classAttrStart := -1
quoteChar := byte(0)
for i := offset - 1; i >= 0; i-- {
if content[i] == '\n' {
break
}
if content[i] == '"' || content[i] == '\'' {
if i >= 6 {
before := string(content[i-6 : i+1])
if before == `class="` || before == `class='` {
classAttrStart = i + 1
quoteChar = content[i]
break
}
}
break
}
}
if classAttrStart == -1 {
return false, ""
}
for i := offset; i < len(content); i++ {
if content[i] == quoteChar {
break
}
if content[i] == '\n' {
break
}
}
valueContent := string(content[classAttrStart:offset])
prefix := ""
lastSpace := strings.LastIndex(valueContent, " ")
if lastSpace == -1 {
prefix = valueContent
} else {
prefix = valueContent[lastSpace+1:]
}
return true, prefix
}
func (s *Server) getTailwindCompletions(prefix string) []CompletionItem {
allClasses := tuigen.AllTailwindClasses()
var items []CompletionItem
for _, classInfo := range allClasses {
if prefix != "" && !strings.HasPrefix(classInfo.Name, prefix) {
continue
}
docValue := classInfo.Description
if classInfo.Example != "" {
docValue += "\n\n**Example:**\n```tui\n" + classInfo.Example + "\n```"
}
items = append(items, CompletionItem{
Label: classInfo.Name,
Kind: CompletionItemKindConstant,
Detail: classInfo.Category,
InsertText: classInfo.Name,
FilterText: classInfo.Name,
Documentation: &MarkupContent{
Kind: "markdown",
Value: docValue,
},
})
}
sortLegacyCompletionsByCategory(items)
return items
}
func sortLegacyCompletionsByCategory(items []CompletionItem) {
categoryOrder := map[string]int{
"layout": 1,
"flex": 2,
"spacing": 3,
"typography": 4,
"visual": 5,
}
sort.Slice(items, func(i, j int) bool {
orderI := categoryOrder[items[i].Detail]
orderJ := categoryOrder[items[j].Detail]
if orderI == 0 {
orderI = 100
}
if orderJ == 0 {
orderJ = 100
}
if orderI != orderJ {
return orderI < orderJ
}
return items[i].Label < items[j].Label
})
}
|