Merge pull request #233 from DotJoshJohnson/issue-131
Preserve Indent Context
This commit is contained in:
commit
7084872fc4
3 changed files with 41 additions and 6 deletions
|
@ -29,7 +29,7 @@ export class V2XmlFormatter implements XmlFormatter {
|
||||||
|
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
let indentLevel = 0;
|
let indentLevel = options.initialIndentLevel || 0;
|
||||||
let attributeQuote = "";
|
let attributeQuote = "";
|
||||||
let lineBreakSpree = false;
|
let lineBreakSpree = false;
|
||||||
let lastWordCharacter: string | undefined;
|
let lastWordCharacter: string | undefined;
|
||||||
|
|
|
@ -22,10 +22,43 @@ export class XmlFormattingEditProvider implements DocumentFormattingEditProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, options: FormattingOptions, token: CancellationToken): ProviderResult<TextEdit[]> {
|
provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, options: FormattingOptions, token: CancellationToken): ProviderResult<TextEdit[]> {
|
||||||
let xml = document.getText(range);
|
const allXml = document.getText();
|
||||||
|
let selectedXml = document.getText(range);
|
||||||
|
const extFormattingOptions = XmlFormattingOptionsFactory.getXmlFormattingOptions(options, document);
|
||||||
|
|
||||||
xml = this.xmlFormatter.formatXml(xml, XmlFormattingOptionsFactory.getXmlFormattingOptions(options, document));
|
const selectionStartOffset = document.offsetAt(range.start);
|
||||||
|
let tabCount = 0;
|
||||||
|
let spaceCount = 0;
|
||||||
|
|
||||||
return [ TextEdit.replace(range, xml) ];
|
for (let i = (selectionStartOffset - 1); i >= 0; i--) {
|
||||||
|
const cc = allXml.charAt(i);
|
||||||
|
|
||||||
|
if (/\t/.test(cc)) {
|
||||||
|
tabCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (/ /.test(cc)) {
|
||||||
|
spaceCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.insertSpaces) {
|
||||||
|
extFormattingOptions.initialIndentLevel = Math.ceil(spaceCount / (options.tabSize || 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
extFormattingOptions.initialIndentLevel = tabCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedXml = this.xmlFormatter.formatXml(selectedXml, extFormattingOptions);
|
||||||
|
|
||||||
|
// we need to remove the leading whitespace because the formatter will add an indent before the first element
|
||||||
|
selectedXml = selectedXml.replace(/^\s+/, "");
|
||||||
|
|
||||||
|
return [TextEdit.replace(range, selectedXml)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ export interface XmlFormattingOptions {
|
||||||
removeCommentsOnMinify: boolean;
|
removeCommentsOnMinify: boolean;
|
||||||
splitAttributesOnFormat: boolean;
|
splitAttributesOnFormat: boolean;
|
||||||
splitXmlnsOnFormat: boolean;
|
splitXmlnsOnFormat: boolean;
|
||||||
|
initialIndentLevel?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class XmlFormattingOptionsFactory {
|
export class XmlFormattingOptionsFactory {
|
||||||
|
@ -20,7 +21,8 @@ export class XmlFormattingOptionsFactory {
|
||||||
newLine: (document.eol === EndOfLine.CRLF) ? "\r\n" : "\n",
|
newLine: (document.eol === EndOfLine.CRLF) ? "\r\n" : "\n",
|
||||||
removeCommentsOnMinify: Configuration.removeCommentsOnMinify(document.uri),
|
removeCommentsOnMinify: Configuration.removeCommentsOnMinify(document.uri),
|
||||||
splitAttributesOnFormat: Configuration.splitAttributesOnFormat(document.uri),
|
splitAttributesOnFormat: Configuration.splitAttributesOnFormat(document.uri),
|
||||||
splitXmlnsOnFormat: Configuration.splitXmlnsOnFormat(document.uri)
|
splitXmlnsOnFormat: Configuration.splitXmlnsOnFormat(document.uri),
|
||||||
|
initialIndentLevel: 0
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue