Skip to content

Commit

Permalink
fix(language-service): don't provide modifier completion for @ and …
Browse files Browse the repository at this point in the history
…`:` (vuejs#5052)
  • Loading branch information
KazariEX authored Dec 20, 2024
1 parent 526ce74 commit 9abe5d9
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions packages/language-service/lib/plugins/vue-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export function create(

// https://vuejs.org/api/built-in-directives.html#v-on
// https://vuejs.org/api/built-in-directives.html#v-bind
const eventModifiers: Record<string, string> = {};
const propModifiers: Record<string, string> = {};
const vOnModifiers: Record<string, string> = {};
const vBindModifiers: Record<string, string> = {};
const vOn = builtInData.globalAttributes?.find(x => x.name === 'v-on');
const vBind = builtInData.globalAttributes?.find(x => x.name === 'v-bind');

Expand All @@ -119,8 +119,8 @@ export function create(
.split('\n').slice(2, -1);
for (let text of modifiers) {
text = text.slice(' - `.'.length);
const [name, disc] = text.split('` - ');
eventModifiers[name] = disc;
const [name, desc] = text.split('` - ');
vOnModifiers[name] = desc;
}
}
if (vBind) {
Expand All @@ -130,8 +130,8 @@ export function create(
.split('\n').slice(2, -1);
for (let text of modifiers) {
text = text.slice(' - `.'.length);
const [name, disc] = text.split('` - ');
propModifiers[name] = disc;
const [name, desc] = text.split('` - ');
vBindModifiers[name] = desc;
}
}

Expand Down Expand Up @@ -733,37 +733,42 @@ export function create(

function afterHtmlCompletion(completionList: vscode.CompletionList, document: TextDocument) {

const replacement = getReplacement(completionList, document);

if (replacement) {
do {
const replacement = getReplacement(completionList, document);
if (!replacement) {
break;
}

const isEvent = replacement.text.startsWith('v-on:') || replacement.text.startsWith('@');
const isProp = replacement.text.startsWith('v-bind:') || replacement.text.startsWith(':');
const isModel = replacement.text.startsWith('v-model:') || replacement.text.split('.')[0] === 'v-model';
const hasModifier = replacement.text.includes('.');
if (!hasModifier) {
break;
}

const [text, ...modifiers] = replacement.text.split('.');
const isVOn = text.startsWith('v-on:') || text.startsWith('@') && text.length > 1;
const isVBind = text.startsWith('v-bind:') || text.startsWith(':') && text.length > 1;
const isVModel = text.startsWith('v-model:') || text === 'v-model';
const validModifiers =
isEvent ? eventModifiers
: isProp ? propModifiers
isVOn ? vOnModifiers
: isVBind ? vBindModifiers
: undefined;
const modifiers = replacement.text.split('.').slice(1);
const textWithoutModifier = replacement.text.split('.')[0];

if (validModifiers && hasModifier) {
if (validModifiers) {

for (const modifier in validModifiers) {

if (modifiers.includes(modifier)) {
continue;
}

const modifierDes = validModifiers[modifier];
const insertText = textWithoutModifier + modifiers.slice(0, -1).map(m => '.' + m).join('') + '.' + modifier;
const description = validModifiers[modifier];
const insertText = text + modifiers.slice(0, -1).map(m => '.' + m).join('') + '.' + modifier;
const newItem: html.CompletionItem = {
label: modifier,
filterText: insertText,
documentation: {
kind: 'markdown',
value: modifierDes,
value: description,
},
textEdit: {
range: replacement.textEdit.range,
Expand All @@ -775,15 +780,15 @@ export function create(
completionList.items.push(newItem);
}
}
else if (hasModifier && isModel) {
else if (isVModel) {

for (const modifier of modelData.globalAttributes ?? []) {

if (modifiers.includes(modifier.name)) {
continue;
}

const insertText = textWithoutModifier + modifiers.slice(0, -1).map(m => '.' + m).join('') + '.' + modifier.name;
const insertText = text + modifiers.slice(0, -1).map(m => '.' + m).join('') + '.' + modifier.name;
const newItem: html.CompletionItem = {
label: modifier.name,
filterText: insertText,
Expand All @@ -802,7 +807,7 @@ export function create(
completionList.items.push(newItem);
}
}
}
} while (0);

completionList.items = completionList.items.filter(item => !specialTags.has(parseLabel(item.label).name));

Expand Down

0 comments on commit 9abe5d9

Please sign in to comment.