Skip to content

Commit 5f326e2

Browse files
committed
feat(language-service): only show shorthand props when no prefix
1 parent 2e0e5e0 commit 5f326e2

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

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

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,7 @@ export function create(
242242
}
243243

244244
const prevText = document.getText({ start: { line: 0, character: 0 }, end: position });
245-
const hint: 'v' | ':' | '@' | undefined = prevText.match(/\bv[\S]*$/)
246-
? 'v'
247-
: prevText.match(/[:][\S]*$/)
248-
? ':'
249-
: prevText.match(/[@][\S]*$/)
250-
? '@'
251-
: undefined;
245+
const hint = prevText.match(/(\S)[\S]*$/)?.[1];
252246

253247
const {
254248
result: htmlCompletion,
@@ -274,7 +268,7 @@ export function create(
274268
if (!htmlCompletion) {
275269
return;
276270
}
277-
if (!prevText.match(/[\S]+$/)) {
271+
if (!hint) {
278272
htmlCompletion.isIncomplete = true;
279273
}
280274

@@ -605,7 +599,7 @@ export function create(
605599
async function runWithVueDataProvider<T>(
606600
sourceDocumentUri: URI,
607601
root: VueVirtualCode,
608-
hint: 'v' | ':' | '@' | undefined,
602+
hint: string | undefined,
609603
mode: 'completion' | 'hover',
610604
fn: () => T,
611605
) {
@@ -624,7 +618,7 @@ export function create(
624618
async function provideHtmlData(
625619
sourceDocumentUri: URI,
626620
root: VueVirtualCode,
627-
hint: 'v' | ':' | '@' | undefined,
621+
hint: string | undefined,
628622
mode: 'completion' | 'hover',
629623
) {
630624
const [tagNameCasing, attrNameCasing] = await Promise.all([
@@ -694,6 +688,30 @@ export function create(
694688
const { attrs, meta } = getTagData(tag);
695689
const attributes: html.IAttributeData[] = [];
696690

691+
let addPlainAttrs = false;
692+
let addVBinds = false;
693+
let addVBindShorthands = false;
694+
let addVOns = false;
695+
let addVOnShorthands = false;
696+
697+
if (!hint) {
698+
addVBindShorthands = true;
699+
addVOnShorthands = true;
700+
}
701+
else if (hint === ':') {
702+
addVBindShorthands = true;
703+
}
704+
else if (hint === '@') {
705+
addVOnShorthands = true;
706+
}
707+
else {
708+
addPlainAttrs = true;
709+
addVBinds = true;
710+
addVOns = true;
711+
addVBindShorthands = true;
712+
addVOnShorthands = true;
713+
}
714+
697715
for (const attr of builtInData?.globalAttributes ?? []) {
698716
if (attr.name === 'is' && tag.toLowerCase() !== 'component') {
699717
continue;
@@ -702,21 +720,14 @@ export function create(
702720
attributes.push(attr);
703721
continue;
704722
}
705-
if (!hint || hint === ':') {
706-
attributes.push({
707-
...attr,
708-
name: V_BIND_SHORTHAND + attr.name,
709-
});
723+
if (addPlainAttrs) {
724+
attributes.push({ ...attr, name: attr.name });
710725
}
711-
if (!hint || hint === 'v') {
712-
attributes.push({
713-
...attr,
714-
name: DIRECTIVE_V_BIND + attr.name,
715-
});
716-
attributes.push({
717-
...attr,
718-
name: attr.name,
719-
});
726+
if (addVBindShorthands) {
727+
attributes.push({ ...attr, name: V_BIND_SHORTHAND + attr.name });
728+
}
729+
if (addVBinds) {
730+
attributes.push({ ...attr, name: DIRECTIVE_V_BIND + attr.name });
720731
}
721732
}
722733

@@ -733,13 +744,13 @@ export function create(
733744
labelName = hyphenateAttr(labelName);
734745
}
735746

736-
if (!hint || hint === '@') {
747+
if (addVOnShorthands) {
737748
attributes.push({
738749
name: V_ON_SHORTHAND + labelName,
739750
description: propMeta && createDescription(propMeta),
740751
});
741752
}
742-
if (!hint || hint === 'v') {
753+
if (addVOns) {
743754
attributes.push({
744755
name: DIRECTIVE_V_ON + labelName,
745756
description: propMeta && createDescription(propMeta),
@@ -752,19 +763,21 @@ export function create(
752763
const name = attrNameCasing === AttrNameCasing.Camel ? prop.name : hyphenateAttr(prop.name);
753764
return name === labelName;
754765
});
755-
if (!hint || hint === ':') {
766+
if (addPlainAttrs) {
756767
attributes.push({
757-
name: V_BIND_SHORTHAND + labelName,
768+
name: labelName,
758769
description: propMeta2 && createDescription(propMeta2),
759770
});
760771
}
761-
if (!hint || hint === 'v') {
772+
if (addVBindShorthands) {
762773
attributes.push({
763-
name: DIRECTIVE_V_BIND + labelName,
774+
name: V_BIND_SHORTHAND + labelName,
764775
description: propMeta2 && createDescription(propMeta2),
765776
});
777+
}
778+
if (addVBinds) {
766779
attributes.push({
767-
name: labelName,
780+
name: DIRECTIVE_V_BIND + labelName,
768781
description: propMeta2 && createDescription(propMeta2),
769782
});
770783
}
@@ -773,13 +786,13 @@ export function create(
773786
for (const event of meta?.events ?? []) {
774787
const eventName = attrNameCasing === AttrNameCasing.Camel ? event.name : hyphenateAttr(event.name);
775788

776-
if (!hint || hint === '@') {
789+
if (addVOnShorthands) {
777790
attributes.push({
778791
name: V_ON_SHORTHAND + eventName,
779792
description: event && createDescription(event),
780793
});
781794
}
782-
if (!hint || hint === 'v') {
795+
if (addVOns) {
783796
attributes.push({
784797
name: DIRECTIVE_V_ON + eventName,
785798
description: event && createDescription(event),
@@ -809,17 +822,15 @@ export function create(
809822
});
810823
}
811824
}
812-
if (!hint || hint === 'v') {
813-
for (const event of meta?.events ?? []) {
814-
if (event.name.startsWith(UPDATE_EVENT_PREFIX)) {
815-
const model = event.name.slice(UPDATE_EVENT_PREFIX.length);
816-
const label = DIRECTIVE_V_MODEL
817-
+ (attrNameCasing === AttrNameCasing.Camel ? model : hyphenateAttr(model));
818-
attributes.push({
819-
name: label,
820-
description: createDescription(event),
821-
});
822-
}
825+
for (const event of meta?.events ?? []) {
826+
if (event.name.startsWith(UPDATE_EVENT_PREFIX)) {
827+
const model = event.name.slice(UPDATE_EVENT_PREFIX.length);
828+
const label = DIRECTIVE_V_MODEL
829+
+ (attrNameCasing === AttrNameCasing.Camel ? model : hyphenateAttr(model));
830+
attributes.push({
831+
name: label,
832+
description: createDescription(event),
833+
});
823834
}
824835
}
825836

0 commit comments

Comments
 (0)