html: observe insertFinalNewline (#152446)

This commit is contained in:
Martin Aeschlimann 2022-06-17 12:38:36 +02:00 committed by GitHub
parent 223ea2c4b5
commit 630809a1ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 20 deletions

View file

@ -7,7 +7,7 @@ import {
Connection, TextDocuments, InitializeParams, InitializeResult, ServerCapabilities, ConfigurationRequest, WorkspaceFolder, TextDocumentSyncKind, NotificationType, Disposable, TextDocumentIdentifier, Range, FormattingOptions, TextEdit, Diagnostic
} from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService, Stylesheet, TextDocument, Position, CSSFormatConfiguration } from 'vscode-css-languageservice';
import { getCSSLanguageService, getSCSSLanguageService, getLESSLanguageService, LanguageSettings, LanguageService, Stylesheet, TextDocument, Position } from 'vscode-css-languageservice';
import { getLanguageModelCache } from './languageModelCache';
import { runSafeAsync } from './utils/runner';
import { DiagnosticsSupport, registerDiagnosticsPullSupport, registerDiagnosticsPushSupport } from './utils/validation';
@ -356,8 +356,7 @@ export function startServer(connection: Connection, runtime: RuntimeEnvironment)
async function onFormat(textDocument: TextDocumentIdentifier, range: Range | undefined, options: FormattingOptions): Promise<TextEdit[]> {
const document = documents.get(textDocument.uri);
if (document) {
console.log(JSON.stringify(options));
const edits = getLanguageService(document).format(document, range ?? getFullRange(document), options as CSSFormatConfiguration);
const edits = getLanguageService(document).format(document, range ?? getFullRange(document), options);
if (edits.length > formatterMaxNumberOfEdits) {
const newText = TextDocument.applyEdits(document, edits);
return [TextEdit.replace(getFullRange(document), newText)];

View file

@ -119,12 +119,6 @@
"default": false,
"markdownDescription": "%html.format.indentHandlebars.desc%"
},
"html.format.endWithNewline": {
"type": "boolean",
"scope": "resource",
"default": false,
"description": "%html.format.endWithNewline.desc%"
},
"html.format.extraLiners": {
"type": [
"string",

View file

@ -10,7 +10,6 @@
"html.format.preserveNewLines.desc": "Controls whether existing line breaks before elements should be preserved. Only works before elements, not inside tags or for text.",
"html.format.maxPreserveNewLines.desc": "Maximum number of line breaks to be preserved in one chunk. Use `null` for unlimited.",
"html.format.indentHandlebars.desc": "Format and indent `{{#foo}}` and `{{/foo}}`.",
"html.format.endWithNewline.desc": "End with a newline.",
"html.format.extraLiners.desc": "List of tags, comma separated, that should have an extra newline before them. `null` defaults to `\"head, body, /html\"`.",
"html.format.wrapAttributes.desc": "Wrap attributes.",
"html.format.wrapAttributes.auto": "Wrap attributes only when line length is exceeded.",

View file

@ -49,6 +49,9 @@ 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,16 +85,12 @@ suite('HTML Embedded Formatting', () => {
});
test('EndWithNewline', async () => {
const options = {
html: {
format: {
endWithNewline: 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><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);
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><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);
});
test('Inside script', async () => {