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
212
213
|
diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts
index 4f41f134af16a977729c7fb1e0df084a667b8a16..0ca4912421f22290fee572e99e5184bc34314bb8 100644
--- a/packages/tailwindcss-language-service/src/completionProvider.ts
+++ b/packages/tailwindcss-language-service/src/completionProvider.ts
@@ -43,12 +43,12 @@ export function completionsFromClassList(
state: State,
classList: string,
classListRange: Range,
- document: TextDocument,
filter?: (item: CompletionItem) => boolean,
+ document?: TextDocument,
context?: CompletionContext
): CompletionList {
- let classNamesAndWhitespace = classList.split(/(\s+)/)
- const partialClassName = classNamesAndWhitespace[classNamesAndWhitespace.length - 1]
+ let classNames = classList.split(/[\s+]/)
+ const partialClassName = classNames[classNames.length - 1]
let sep = state.separator
let parts = partialClassName.split(sep)
let subset: any
@@ -57,10 +57,10 @@ let isSubset: boolean = false
let replacementRange = {
...classListRange,
- start: document.positionAt(
- document.offsetAt(classListRange.start) +
- classNamesAndWhitespace.slice(0, classNamesAndWhitespace.length - 1).join('').length
- ),
+ start: {
+ ...classListRange.start,
+ character: classListRange.end.character - partialClassName.length,
+ },
}
if (state.jit) {
@@ -201,12 +201,10 @@ newText:
resultingVariants.slice(0, resultingVariants.length - 1).join(sep) +
sep,
range: {
- start: document.positionAt(
- document.offsetAt(classListRange.start) +
- classNamesAndWhitespace
- .slice(0, classNamesAndWhitespace.length - 1)
- .join('').length
- ),
+ start: {
+ ...classListRange.start,
+ character: classListRange.end.character - partialClassName.length,
+ },
end: {
...replacementRange.start,
character: replacementRange.start.character,
@@ -414,39 +412,16 @@ state.editor.capabilities.itemDefaults
)
}
-// This might be a VS Code bug?
-// The trigger character is not included in the document text
-function ensureTriggerCharacterIsIncluded(
- text: string,
- document: TextDocument,
- position: Position,
- context?: CompletionContext
-): string {
- if (!context) {
- return text
- }
- if (
- context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter
- text.slice(-1) !== context.triggerCharacter
- ) {
- return `${text.slice(0, text.length - 1)}${context.triggerCharacter}`
- }
- return text
-}
-
async function provideClassAttributeCompletions(
state: State,
document: TextDocument,
position: Position,
context?: CompletionContext
): Promise<CompletionList> {
- let startOffset = Math.max(0, document.offsetAt(position) - 1000)
let str = document.getText({
- start: document.positionAt(startOffset),
+ start: document.positionAt(Math.max(0, document.offsetAt(position) - 1000)),
end: position,
})
-
- str = ensureTriggerCharacterIsIncluded(str, document, position, context)
let matches = matchClassAttributes(
str,
@@ -459,13 +434,11 @@ }
let match = matches[matches.length - 1]
- let lexer =
+ const lexer =
match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']'))
? getComputedClassAttributeLexer()
: getClassAttributeLexer()
- let attributeOffset = match.index + match[0].length - 1
- let attributeText = str.substr(attributeOffset)
- lexer.reset(attributeText)
+ lexer.reset(str.substr(match.index + match[0].length - 1))
try {
let tokens = Array.from(lexer)
@@ -484,13 +457,14 @@ return completionsFromClassList(
state,
classList,
{
- start: document.positionAt(
- startOffset + attributeOffset + (attributeText.length - classList.length)
- ),
+ start: {
+ line: position.line,
+ character: position.character - classList.length,
+ },
end: position,
},
- document,
undefined,
+ document,
context
)
}
@@ -553,18 +527,13 @@ } else {
classList = containerMatch[1].substr(0, cursor - matchStart)
}
- return completionsFromClassList(
- state,
- classList,
- {
- start: {
- line: position.line,
- character: position.character - classList.length,
- },
- end: position,
+ return completionsFromClassList(state, classList, {
+ start: {
+ line: position.line,
+ character: position.character - classList.length,
},
- document
- )
+ end: position,
+ })
}
}
} catch (_) {}
@@ -576,16 +545,13 @@
function provideAtApplyCompletions(
state: State,
document: TextDocument,
- position: Position,
- context?: CompletionContext
+ position: Position
): CompletionList {
let str = document.getText({
start: { line: Math.max(position.line - 30, 0), character: 0 },
end: position,
})
- str = ensureTriggerCharacterIsIncluded(str, document, position, context)
-
const match = findLast(/@apply\s+(?<classList>[^;}]*)$/gi, str)
if (match === null) {
@@ -604,7 +570,6 @@ character: position.character - classList.length,
},
end: position,
},
- document,
(item) => {
if (item.kind === 9) {
return (
@@ -631,7 +596,7 @@ position: Position,
context?: CompletionContext
): Promise<CompletionList> {
if (isCssContext(state, document, position)) {
- return provideAtApplyCompletions(state, document, position, context)
+ return provideAtApplyCompletions(state, document, position)
}
if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) {
@@ -1338,18 +1303,13 @@
const parts = emmetItems.items[0].label.split('.')
if (parts.length < 2) return null
- return completionsFromClassList(
- state,
- parts[parts.length - 1],
- {
- start: {
- line: position.line,
- character: position.character - parts[parts.length - 1].length,
- },
- end: position,
+ return completionsFromClassList(state, parts[parts.length - 1], {
+ start: {
+ line: position.line,
+ character: position.character - parts[parts.length - 1].length,
},
- document
- )
+ end: position,
+ })
}
export async function doComplete(
|