Skip to content

Commit 7e49497

Browse files
committed
fix(language-core): improve multiple lines event formatting result
1 parent 74eeda3 commit 7e49497

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,32 @@ const plugin: VueLanguagePlugin = ctx => {
105105
) {
106106
if (prop.name === 'on' && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
107107
const ast = createTsAst(ctx.modules.typescript, prop.exp, prop.exp.content);
108-
addFormatCodes(
109-
prop.exp.content,
110-
prop.exp.loc.start.offset,
111-
isCompoundExpression(ctx.modules.typescript, ast)
112-
? formatBrackets.event
113-
: formatBrackets.normal
114-
);
108+
if (isCompoundExpression(ctx.modules.typescript, ast)) {
109+
addFormatCodes(
110+
prop.exp.content,
111+
prop.exp.loc.start.offset,
112+
formatBrackets.event
113+
);
114+
}
115+
else {
116+
const lines = prop.exp.content.split('\n');
117+
const firstLineEmpty = lines[0].trim() === '';
118+
const lastLineEmpty = lines[lines.length - 1].trim() === '';
119+
if (lines.length <= 1 || (!firstLineEmpty && !lastLineEmpty)) {
120+
addFormatCodes(
121+
prop.exp.content,
122+
prop.exp.loc.start.offset,
123+
formatBrackets.normal
124+
);
125+
}
126+
else {
127+
addFormatCodes(
128+
prop.exp.content,
129+
prop.exp.loc.start.offset,
130+
['(', ');']
131+
);
132+
}
133+
}
115134
}
116135
else if (prop.name === 'slot') {
117136
addFormatCodes(
@@ -186,17 +205,27 @@ const plugin: VueLanguagePlugin = ctx => {
186205
else if (node.type === CompilerDOM.NodeTypes.INTERPOLATION) {
187206
// {{ ... }}
188207
const [content, start] = parseInterpolationNode(node, templateContent);
208+
const lines = content.split('\n');
209+
const firstLineEmpty = lines[0].trim() === '';
210+
const lastLineEmpty = lines[lines.length - 1].trim() === '';
211+
189212
if (content.includes('=>')) { // arrow function
190-
addFormatCodes(
191-
content,
192-
start,
193-
formatBrackets.normal
194-
);
213+
if (lines.length <= 1 || (!firstLineEmpty && !lastLineEmpty)) {
214+
addFormatCodes(
215+
content,
216+
start,
217+
formatBrackets.normal
218+
);
219+
}
220+
else {
221+
addFormatCodes(
222+
content,
223+
start,
224+
['(', ');']
225+
);
226+
}
195227
}
196228
else {
197-
const lines = content.split('\n');
198-
const firstLineEmpty = lines[0].trim() === '';
199-
const lastLineEmpty = lines[lines.length - 1].trim() === '';
200229
if (lines.length <= 1 || (!firstLineEmpty && !lastLineEmpty)) {
201230
addFormatCodes(
202231
content,

0 commit comments

Comments
 (0)