[html] files.insertFinalNewline affects embedded styles (#179118)

This commit is contained in:
Martin Aeschlimann 2023-04-04 12:55:17 +02:00 committed by GitHub
parent f481836a89
commit 92b87b0fd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View file

@ -54,7 +54,11 @@ export async function format(languageModes: LanguageModes, document: TextDocumen
// perform a html format and apply changes to a new document
const htmlMode = languageModes.getMode('html')!;
const htmlEdits = await htmlMode.format!(document, formatRange, formattingOptions, settings);
const htmlFormattedContent = TextDocument.applyEdits(document, htmlEdits);
let htmlFormattedContent = TextDocument.applyEdits(document, htmlEdits);
if (formattingOptions.insertFinalNewline && endOffset === content.length && !htmlFormattedContent.endsWith('\n')) {
htmlFormattedContent = htmlFormattedContent + '\n';
htmlEdits.push(TextEdit.insert(endPos, '\n'));
}
const newDocument = TextDocument.create(document.uri + '.tmp', document.languageId, document.version, htmlFormattedContent);
try {
// run embedded formatters on html formatted content: - formatters see correct initial indent

View file

@ -49,9 +49,6 @@ export function getHTMLMode(htmlLanguageService: HTMLLanguageService, workspace:
} else {
formatSettings.contentUnformatted = 'script';
}
if (formatParams.insertFinalNewline) {
formatSettings.endWithNewline = true;
}
merge(formatParams, formatSettings);
return htmlLanguageService.format(document, range, formatSettings);
},

View file

@ -85,11 +85,12 @@ suite('HTML Embedded Formatting', () => {
});
test('EndWithNewline', async () => {
const options : FormattingOptions = FormattingOptions.create(2, true);
const options: FormattingOptions = FormattingOptions.create(2, true);
options.insertFinalNewline = true;
await assertFormat('<html><body><p>Hello</p></body></html>', '<html>\n\n<body>\n <p>Hello</p>\n</body>\n\n</html>\n', {}, options);
await assertFormat('<html>|<body><p>Hello</p></body>|</html>', '<html><body>\n <p>Hello</p>\n</body></html>', {}, options);
await assertFormat('<html>|<body><p>Hello</p></body></html>|', '<html><body>\n <p>Hello</p>\n</body>\n\n</html>\n', {}, options);
await assertFormat('<html><head><script>\nvar x=1;\n</script></head></html>', '<html>\n\n<head>\n <script>\n var x = 1;\n </script>\n</head>\n\n</html>\n', {}, options);
});