Skip to content

Commit

Permalink
fix(language-service): ignore html wrapAttributes format settings f…
Browse files Browse the repository at this point in the history
…or vue document

close vuejs#3987
  • Loading branch information
johnsoncodehk committed Mar 27, 2024
1 parent d6f2519 commit 416882d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
45 changes: 22 additions & 23 deletions packages/language-service/lib/plugins/vue-sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,29 @@ export function create(): LanguageServicePlugin {
sfcDataProvider ??= html.newHTMLDataProvider('vue', loadLanguageBlocks(context.env.locale ?? 'en'));
return [sfcDataProvider];
},
async getFormattingOptions(document, options, context) {
return await worker(document, async vueCode => {

const formatSettings = await context.env.getConfiguration?.<html.HTMLFormatConfiguration>('html.format') ?? {};
const blockTypes = ['template', 'script', 'style'];

for (const customBlock of vueCode.sfc.customBlocks) {
blockTypes.push(customBlock.type);
}

return {
...options,
...formatSettings,
wrapAttributes: 'auto',
unformatted: '',
contentUnformatted: blockTypes.join(','),
endWithNewline: options.insertFinalNewline ? true
: options.trimFinalNewlines ? false
: document.getText().endsWith('\n'),
};
}) ?? {};
},
}).create(context);
const htmlLanguageService: html.LanguageService = htmlPlugin.provide['html/languageService']();

return {

Expand Down Expand Up @@ -148,28 +169,6 @@ export function create(): LanguageServicePlugin {
return result;
});
},

provideDocumentFormattingEdits(document, range, options) {
return worker(document, async vueCode => {

const formatSettings = await context.env.getConfiguration?.<html.HTMLFormatConfiguration>('html.format') ?? {};
const blockTypes = ['template', 'script', 'style'];

for (const customBlock of vueCode.sfc.customBlocks) {
blockTypes.push(customBlock.type);
}

return htmlLanguageService.format(document, range, {
...options,
...formatSettings,
unformatted: '',
contentUnformatted: blockTypes.join(','),
endWithNewline: options.insertFinalNewline ? true
: options.trimFinalNewlines ? false
: document.getText().endsWith('\n'),
});
});
},
};

function worker<T>(document: TextDocument, callback: (vueSourceFile: vue.VueGeneratedCode) => T) {
Expand Down
24 changes: 24 additions & 0 deletions packages/language-service/tests/format/3987.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineFormatTest } from '../utils/format';

defineFormatTest({
title: '#' + __filename.split('.')[0],
languageId: 'vue',
input: `
<style lang="scss"
scoped>
.wrapper {
display: block;
}
</style>
`.trim(),
output: `
<style lang="scss" scoped>
.wrapper {
display: block;
}
</style>
`.trim(),
settings: {
'html.format.wrapAttributes': 'force-aligned',
},
});

0 comments on commit 416882d

Please sign in to comment.