diff options
Diffstat (limited to 'extensions/html-language-features/server/src/modes/formatting.ts')
-rw-r--r-- | extensions/html-language-features/server/src/modes/formatting.ts | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/extensions/html-language-features/server/src/modes/formatting.ts b/extensions/html-language-features/server/src/modes/formatting.ts index 49c3a0dd68e..aca8ee3b3c2 100644 --- a/extensions/html-language-features/server/src/modes/formatting.ts +++ b/extensions/html-language-features/server/src/modes/formatting.ts @@ -8,14 +8,14 @@ import { pushAll } from '../utils/arrays'; import { isEOL } from '../utils/strings'; export async function format(languageModes: LanguageModes, document: TextDocument, formatRange: Range, formattingOptions: FormattingOptions, settings: Settings | undefined, enabledModes: { [mode: string]: boolean }) { - let result: TextEdit[] = []; + const result: TextEdit[] = []; - let endPos = formatRange.end; + const endPos = formatRange.end; let endOffset = document.offsetAt(endPos); - let content = document.getText(); + const 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)); + const prevLineStart = document.offsetAt(Position.create(endPos.line - 1, 0)); while (isEOL(content, endOffset - 1) && endOffset > prevLineStart) { endOffset--; } @@ -31,15 +31,15 @@ export async function format(languageModes: LanguageModes, document: TextDocumen // - no worrying of overlapping edits // make sure we start in html - let allRanges = languageModes.getModesInRange(document, formatRange); + const allRanges = languageModes.getModesInRange(document, formatRange); let i = 0; let startPos = formatRange.start; - let isHTML = (range: LanguageModeRange) => range.mode && range.mode.getId() === 'html'; + const isHTML = (range: LanguageModeRange) => range.mode && range.mode.getId() === 'html'; while (i < allRanges.length && !isHTML(allRanges[i])) { - let range = allRanges[i]; + const range = allRanges[i]; if (!range.attributeValue && range.mode && range.mode.format) { - let edits = await range.mode.format(document, Range.create(startPos, range.end), formattingOptions, settings); + const edits = await range.mode.format(document, Range.create(startPos, range.end), formattingOptions, settings); pushAll(result, edits); } startPos = range.end; @@ -52,23 +52,23 @@ export async function format(languageModes: LanguageModes, document: TextDocumen formatRange = Range.create(startPos, formatRange.end); // perform a html format and apply changes to a new document - let htmlMode = languageModes.getMode('html')!; - let htmlEdits = await htmlMode.format!(document, formatRange, formattingOptions, settings); - let htmlFormattedContent = TextDocument.applyEdits(document, htmlEdits); - let newDocument = TextDocument.create(document.uri + '.tmp', document.languageId, document.version, htmlFormattedContent); + const htmlMode = languageModes.getMode('html')!; + const htmlEdits = await htmlMode.format!(document, formatRange, formattingOptions, settings); + const htmlFormattedContent = TextDocument.applyEdits(document, htmlEdits); + 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 - let afterFormatRangeLength = document.getText().length - document.offsetAt(formatRange.end); // length of unchanged content after replace range - let newFormatRange = Range.create(formatRange.start, newDocument.positionAt(htmlFormattedContent.length - afterFormatRangeLength)); - let embeddedRanges = languageModes.getModesInRange(newDocument, newFormatRange); + const afterFormatRangeLength = document.getText().length - document.offsetAt(formatRange.end); // length of unchanged content after replace range + const newFormatRange = Range.create(formatRange.start, newDocument.positionAt(htmlFormattedContent.length - afterFormatRangeLength)); + const embeddedRanges = languageModes.getModesInRange(newDocument, newFormatRange); - let embeddedEdits: TextEdit[] = []; + const embeddedEdits: TextEdit[] = []; - for (let r of embeddedRanges) { - let mode = r.mode; + for (const r of embeddedRanges) { + const mode = r.mode; if (mode && mode.format && enabledModes[mode.getId()] && !r.attributeValue) { - let edits = await mode.format(newDocument, r, formattingOptions, settings); - for (let edit of edits) { + const edits = await mode.format(newDocument, r, formattingOptions, settings); + for (const edit of edits) { embeddedEdits.push(edit); } } @@ -80,8 +80,8 @@ export async function format(languageModes: LanguageModes, document: TextDocumen } // apply all embedded format edits and create a single edit for all changes - let resultContent = TextDocument.applyEdits(newDocument, embeddedEdits); - let resultReplaceText = resultContent.substring(document.offsetAt(formatRange.start), resultContent.length - afterFormatRangeLength); + const resultContent = TextDocument.applyEdits(newDocument, embeddedEdits); + const resultReplaceText = resultContent.substring(document.offsetAt(formatRange.start), resultContent.length - afterFormatRangeLength); result.push(TextEdit.replace(formatRange, resultReplaceText)); return result; |