Skip to content

Commit 87a804a

Browse files
authored
fix(language-core): auto-completion for the last line of template block (#4771)
1 parent 4fae9dd commit 87a804a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

packages/language-core/lib/plugins/vue-template-html.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ interface Loc {
88
}
99
type Node = CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.ExpressionNode | CompilerDOM.AttributeNode | CompilerDOM.DirectiveNode;
1010

11+
const shouldAddSuffix = /(?<=<[^>/]+)$/;
12+
1113
const plugin: VueLanguagePlugin = ({ modules }) => {
1214

1315
return {
@@ -20,14 +22,36 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
2022

2123
const compiler = modules['@vue/compiler-dom'];
2224

23-
return compiler.compile(template, {
25+
let addedSuffix = false;
26+
27+
// #4583
28+
if (shouldAddSuffix.test(template)) {
29+
template += '>';
30+
addedSuffix = true;
31+
}
32+
33+
const result = compiler.compile(template, {
2434
...options,
2535
comments: true,
2636
});
37+
// @ts-expect-error
38+
result.__addedSuffix = addedSuffix;
39+
return result;
2740
}
2841
},
2942

3043
updateSFCTemplate(oldResult, change) {
44+
oldResult.code = oldResult.code.slice(0, change.start)
45+
+ change.newText
46+
+ oldResult.code.slice(change.end);
47+
48+
// @ts-expect-error
49+
if (oldResult.__addedSuffix) {
50+
const originalTemplate = oldResult.code.slice(0, -1); // remove added '>'
51+
if (!shouldAddSuffix.test(originalTemplate)) {
52+
return undefined;
53+
}
54+
}
3155

3256
const CompilerDOM = modules['@vue/compiler-dom'];
3357

0 commit comments

Comments
 (0)