Skip to content

Commit 7b55e64

Browse files
committed
fix(language-service): correct kind and order of component completion items
1 parent 2435873 commit 7b55e64

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

packages/language-service/lib/plugins/vue-template.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ export function create(
171171

172172
const {
173173
result: completionList,
174-
components,
175-
propMap,
174+
target,
175+
info: {
176+
components,
177+
propMap,
178+
},
176179
} = await runWithVueData(
177180
sourceScript.id,
178181
root,
@@ -189,9 +192,30 @@ export function create(
189192
return;
190193
}
191194

192-
addDirectiveModifiers(completionList, document);
195+
switch (target) {
196+
case 'tag': {
197+
completionList.items.forEach(transformTag);
198+
break;
199+
}
200+
case 'attribute': {
201+
addDirectiveModifiers(completionList, document);
202+
completionList.items.forEach(transformAttribute);
203+
break;
204+
}
205+
}
206+
207+
updateExtraCustomData([]);
208+
return completionList;
209+
210+
function transformTag(item: html.CompletionItem) {
211+
const tagName = capitalize(camelize(item.label));
212+
if (components?.includes(tagName)) {
213+
item.kind = 6 satisfies typeof CompletionItemKind.Variable;
214+
item.sortText = '\u0000' + (item.sortText ?? item.label);
215+
}
216+
}
193217

194-
for (const item of completionList.items) {
218+
function transformAttribute(item: html.CompletionItem) {
195219
let prop = propMap.get(item.label);
196220

197221
if (prop) {
@@ -201,7 +225,6 @@ export function create(
201225
value: prop.info.documentation,
202226
};
203227
}
204-
205228
if (prop.info?.deprecated) {
206229
item.tags = [1 satisfies typeof CompletionItemTag.Deprecated];
207230
}
@@ -225,14 +248,7 @@ export function create(
225248

226249
const tokens: string[] = [];
227250

228-
if (
229-
item.kind === 10 satisfies typeof CompletionItemKind.Property
230-
&& components?.includes(hyphenateTag(item.label))
231-
) {
232-
item.kind = 6 satisfies typeof CompletionItemKind.Variable;
233-
tokens.push('\u0000');
234-
}
235-
else if (prop) {
251+
if (prop) {
236252
const { isEvent, propName } = getPropName(prop.name, prop.kind === 'event');
237253

238254
if (prop.kind === 'prop') {
@@ -296,9 +312,6 @@ export function create(
296312

297313
item.sortText = tokens.join('') + (item.sortText ?? item.label);
298314
}
299-
300-
updateExtraCustomData([]);
301-
return completionList;
302315
},
303316

304317
provideHover(document, position, token) {
@@ -324,10 +337,7 @@ export function create(
324337
while (lastSync.version !== (lastSync = await sync()).version) {
325338
result = await fn();
326339
}
327-
return {
328-
result,
329-
...lastSync,
330-
};
340+
return { result, ...lastSync };
331341
}
332342

333343
async function provideHtmlData(sourceDocumentUri: URI, root: VueVirtualCode) {
@@ -348,6 +358,7 @@ export function create(
348358
}
349359

350360
let version = 0;
361+
let target: 'tag' | 'attribute' | 'value';
351362
let components: string[] | undefined;
352363
let values: string[] | undefined;
353364

@@ -371,12 +382,13 @@ export function create(
371382
getId: () => htmlDataProvider.getId(),
372383
isApplicable: () => true,
373384
provideTags() {
374-
let tags = htmlDataProvider.provideTags();
375-
tags = tags.filter(tag => !specialTags.has(tag.name));
376-
return tags;
385+
target = 'tag';
386+
return htmlDataProvider.provideTags()
387+
.filter(tag => !specialTags.has(tag.name));
377388
},
378389
provideAttributes(tag) {
379-
let attrs = htmlDataProvider.provideAttributes(tag);
390+
target = 'attribute';
391+
const attrs = htmlDataProvider.provideAttributes(tag);
380392
if (tag === 'slot') {
381393
const nameAttr = attrs.find(attr => attr.name === 'name');
382394
if (nameAttr) {
@@ -386,6 +398,7 @@ export function create(
386398
return attrs;
387399
},
388400
provideValues(tag, attr) {
401+
target = 'value';
389402
return htmlDataProvider.provideValues(tag, attr);
390403
},
391404
},
@@ -619,7 +632,14 @@ export function create(
619632
return {
620633
async sync() {
621634
await Promise.all(tasks);
622-
return { version, components, propMap };
635+
return {
636+
version,
637+
target,
638+
info: {
639+
components,
640+
propMap,
641+
},
642+
};
623643
},
624644
};
625645
}

0 commit comments

Comments
 (0)