提交 a6db6bb4 编写于 作者: M Martin Aeschlimann

HTML format selection formatting entire line issue. Fixes #25521

上级 2a202d16
......@@ -5,13 +5,27 @@
'use strict';
import { applyEdits } from '../utils/edits';
import { TextDocument, Range, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
import { TextDocument, Range, TextEdit, FormattingOptions, Position } from 'vscode-languageserver-types';
import { LanguageModes } from './languageModes';
import { pushAll } from '../utils/arrays';
import { isEOL } from '../utils/strings';
export function format(languageModes: LanguageModes, document: TextDocument, formatRange: Range, formattingOptions: FormattingOptions, enabledModes: { [mode: string]: boolean }) {
let result: TextEdit[] = [];
let endPos = formatRange.end;
let endOffset = document.offsetAt(endPos);
let content = document.getText();
if (endPos.character === 0 && endPos.line > 0 && endOffset !== content.length) {
// if selection ends after a new line, exclude that new line
let prevLineStart = document.offsetAt(Position.create(endPos.line - 1, 0));
while (isEOL(content, endOffset - 1) && endOffset > prevLineStart) {
endOffset--;
}
formatRange = Range.create(formatRange.start, document.positionAt(endOffset));
}
// run the html formatter on the full range and pass the result content to the embedded formatters.
// from the final content create a single edit
// advantages of this approach are
......
......@@ -103,6 +103,10 @@ suite('HTML Embedded Formatting', () => {
assertFormat('<html><head>\n <script>\n|var x=6;\nvar y= 9;|\n</script></head></html>', '<html><head>\n <script>\n var x = 6;\n var y = 9;\n</script></head></html>');
});
test('Range after new line', function (): any {
assertFormat('<html><head>\n |<script>\nvar x=6;\n</script>\n|</head></html>', '<html><head>\n <script>\n var x = 6;\n\n </script>\n</head></html>');
});
});
function applyEdits(document: TextDocument, edits: TextEdit[]): string {
......
......@@ -57,9 +57,12 @@ export function isWhitespaceOnly(str: string) {
return /^\s*$/.test(str);
}
export function isEOL(content: string, offset: number) {
return isNewlineCharacter(content.charCodeAt(offset));
}
const CR = '\r'.charCodeAt(0);
const NL = '\n'.charCodeAt(0);
function isNewlineCharacter(charCode: number) {
export function isNewlineCharacter(charCode: number) {
return charCode === CR || charCode === NL;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册