mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 13:00:58 +00:00
Trim inner tag leading and trailing whitespace
Fixes https://github.com/microsoft/vscode/issues/138063
This commit is contained in:
parent
db1f865720
commit
bdc04a9b22
|
@ -51,20 +51,79 @@ function getRangesToRemove(document: vscode.TextDocument, rootNode: HtmlFlatNode
|
|||
closeTagRange = offsetRangeToVsRange(document, nodeToUpdate.close.start, nodeToUpdate.close.end);
|
||||
}
|
||||
|
||||
if (openTagRange && closeTagRange) {
|
||||
const innerCombinedRange = new vscode.Range(
|
||||
openTagRange.end.line,
|
||||
openTagRange.end.character,
|
||||
closeTagRange.start.line,
|
||||
closeTagRange.start.character);
|
||||
const outerCombinedRange = new vscode.Range(
|
||||
openTagRange.start.line,
|
||||
openTagRange.start.character,
|
||||
closeTagRange.end.line,
|
||||
closeTagRange.end.character);
|
||||
// Special case: there is only whitespace in between.
|
||||
if (document.getText(innerCombinedRange).trim() === '' && nodeToUpdate.name !== 'pre') {
|
||||
return [outerCombinedRange];
|
||||
}
|
||||
}
|
||||
|
||||
let rangesToRemove = [];
|
||||
if (openTagRange) {
|
||||
rangesToRemove.push(openTagRange);
|
||||
if (closeTagRange) {
|
||||
const indentAmountToRemove = calculateIndentAmountToRemove(document, openTagRange, closeTagRange);
|
||||
let firstInnerNonEmptyLine: number | undefined;
|
||||
let lastInnerNonEmptyLine: number | undefined;
|
||||
for (let i = openTagRange.start.line + 1; i < closeTagRange.start.line; i++) {
|
||||
rangesToRemove.push(new vscode.Range(i, 0, i, indentAmountToRemove));
|
||||
if (!document.lineAt(i).isEmptyOrWhitespace) {
|
||||
rangesToRemove.push(new vscode.Range(i, 0, i, indentAmountToRemove));
|
||||
if (firstInnerNonEmptyLine === undefined) {
|
||||
// We found the first non-empty inner line.
|
||||
firstInnerNonEmptyLine = i;
|
||||
}
|
||||
lastInnerNonEmptyLine = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the entire last line + empty lines preceding it
|
||||
// if it is just the tag, otherwise remove just the tag.
|
||||
if (entireLineIsTag(document, closeTagRange) && lastInnerNonEmptyLine) {
|
||||
rangesToRemove.push(new vscode.Range(
|
||||
lastInnerNonEmptyLine,
|
||||
document.lineAt(lastInnerNonEmptyLine).range.end.character,
|
||||
closeTagRange.end.line,
|
||||
closeTagRange.end.character));
|
||||
} else {
|
||||
rangesToRemove.push(closeTagRange);
|
||||
}
|
||||
|
||||
// Remove the entire first line + empty lines proceding it
|
||||
// if it is just the tag, otherwise keep on removing just the tag.
|
||||
if (entireLineIsTag(document, openTagRange) && firstInnerNonEmptyLine) {
|
||||
rangesToRemove[1] = new vscode.Range(
|
||||
openTagRange.start.line,
|
||||
openTagRange.start.character,
|
||||
firstInnerNonEmptyLine,
|
||||
document.lineAt(firstInnerNonEmptyLine).firstNonWhitespaceCharacterIndex);
|
||||
rangesToRemove.shift();
|
||||
}
|
||||
rangesToRemove.push(closeTagRange);
|
||||
}
|
||||
}
|
||||
return rangesToRemove;
|
||||
}
|
||||
|
||||
function entireLineIsTag(document: vscode.TextDocument, range: vscode.Range): boolean {
|
||||
if (range.start.line === range.end.line) {
|
||||
const lineText = document.lineAt(range.start).text;
|
||||
const tagText = document.getText(range);
|
||||
if (lineText.trim() === tagText) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the amount of indent to remove for getRangesToRemove.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue