Merge pull request #342 from DotJoshJohnson/feature/308-preserve-space

Add Option to Preserve Whitespace Between Attributes on Format
This commit is contained in:
Josh Johnson 2020-11-25 22:27:46 -05:00 committed by GitHub
commit f6827fdf53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 9 deletions

View file

@ -176,6 +176,12 @@
"default": "**/*.xml", "default": "**/*.xml",
"description": "The pattern used to search for input XML files when executing XQuery scripts.", "description": "The pattern used to search for input XML files when executing XQuery scripts.",
"scope": "window" "scope": "window"
},
"xmlTools.preserveSpacesBetweenAttributes": {
"type": "boolean",
"default": false,
"description": "Preserves any spaces between attributes during formatting.",
"scope": "resource"
} }
} }
}, },

View file

@ -59,6 +59,10 @@ export class Configuration {
return this._getForResource<boolean>("splitXmlnsOnFormat", resource); return this._getForResource<boolean>("splitXmlnsOnFormat", resource);
} }
static preserveSpacesBetweenAttributes(resource: Uri): boolean {
return this._getForResource<boolean>("preserveSpacesBetweenAttributes", resource);
}
private static _getForResource<T>(section: string, resource: Uri): T { private static _getForResource<T>(section: string, resource: Uri): T {
return workspace.getConfiguration(ExtensionTopLevelSection, resource).get<T>(section); return workspace.getConfiguration(ExtensionTopLevelSection, resource).get<T>(section);
} }

View file

@ -17,12 +17,18 @@ export class V2XmlFormatter implements XmlFormatter {
}); });
// do some light minification to get rid of insignificant whitespace // do some light minification to get rid of insignificant whitespace
if (!options.preserveSpacesBetweenAttributes) {
xml = xml.replace(/"\s+(?=[^\s]+=)/g, "\" "); // spaces between attributes xml = xml.replace(/"\s+(?=[^\s]+=)/g, "\" "); // spaces between attributes
}
xml = xml.replace(/"\s+(?=>)/g, "\""); // spaces between the last attribute and tag close (>) xml = xml.replace(/"\s+(?=>)/g, "\""); // spaces between the last attribute and tag close (>)
xml = xml.replace(/"\s+(?=\/>)/g, "\" "); // spaces between the last attribute and tag close (/>) xml = xml.replace(/"\s+(?=\/>)/g, "\" "); // spaces between the last attribute and tag close (/>)
if (!options.preserveSpacesBetweenAttributes) {
xml = xml.replace(/(?!<!\[CDATA\[)[^ <>="]\s+[^ <>="]+=(?![^<]*?\]\]>)/g, (match: string) => { // spaces between the node name and the first attribute xml = xml.replace(/(?!<!\[CDATA\[)[^ <>="]\s+[^ <>="]+=(?![^<]*?\]\]>)/g, (match: string) => { // spaces between the node name and the first attribute
return match.replace(/\s+/g, " "); return match.replace(/\s+/g, " ");
}); });
}
// the coast is clear - we can drop those "<" brackets back in // the coast is clear - we can drop those "<" brackets back in
xml = this._unsanitizeCommentsAndCDATA(xml); xml = this._unsanitizeCommentsAndCDATA(xml);

View file

@ -1,7 +1,6 @@
import { EndOfLine, FormattingOptions, TextDocument } from "vscode"; import { EndOfLine, FormattingOptions, TextDocument } from "vscode";
import { Configuration } from "../common"; import { Configuration } from "../common";
import * as constants from "../constants";
export interface XmlFormattingOptions { export interface XmlFormattingOptions {
editorOptions: FormattingOptions; editorOptions: FormattingOptions;
@ -11,6 +10,7 @@ export interface XmlFormattingOptions {
splitAttributesOnFormat: boolean; splitAttributesOnFormat: boolean;
splitXmlnsOnFormat: boolean; splitXmlnsOnFormat: boolean;
initialIndentLevel?: number; initialIndentLevel?: number;
preserveSpacesBetweenAttributes: boolean;
} }
export class XmlFormattingOptionsFactory { export class XmlFormattingOptionsFactory {
@ -22,7 +22,8 @@ export class XmlFormattingOptionsFactory {
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 initialIndentLevel: 0,
preserveSpacesBetweenAttributes: Configuration.preserveSpacesBetweenAttributes(document.uri)
}; };
} }
} }

View file

@ -21,7 +21,8 @@ describe("V2XmlFormatter", () => {
newLine: "\r\n", newLine: "\r\n",
removeCommentsOnMinify: false, removeCommentsOnMinify: false,
splitAttributesOnFormat: false, splitAttributesOnFormat: false,
splitXmlnsOnFormat: true splitXmlnsOnFormat: true,
preserveSpacesBetweenAttributes: false
}; };
it("should handle basic XML", () => { it("should handle basic XML", () => {
@ -111,6 +112,14 @@ describe("V2XmlFormatter", () => {
it("should handle mixed content on the same line as another element without error", () => { it("should handle mixed content on the same line as another element without error", () => {
testFormatter(xmlFormatter, options, "issue-294"); testFormatter(xmlFormatter, options, "issue-294");
}); });
it("should optionally preserve whitespace between attributes", () => {
options.preserveSpacesBetweenAttributes = true;
testFormatter(xmlFormatter, options, "issue-308");
options.preserveSpacesBetweenAttributes = false;
});
}); });
describe("#minifyXml(xml, options)", () => { describe("#minifyXml(xml, options)", () => {
@ -124,7 +133,8 @@ describe("V2XmlFormatter", () => {
newLine: "\r\n", newLine: "\r\n",
removeCommentsOnMinify: false, removeCommentsOnMinify: false,
splitAttributesOnFormat: false, splitAttributesOnFormat: false,
splitXmlnsOnFormat: true splitXmlnsOnFormat: true,
preserveSpacesBetweenAttributes: false
}; };
it("should preserve whitespace on minify if xml:space is set to 'preserve-whitespace'", () => { it("should preserve whitespace on minify if xml:space is set to 'preserve-whitespace'", () => {

View file

@ -0,0 +1,11 @@
<object type="output parameters">
<!-- output directory -->
<param name="output directory" type="string" default="output"></param>
<!-- output files -->
<param name="crystal file" required="false" type="string" default="Slime_-_crystals"></param>
<param name="macrocell file" required="false" type="string" default="Slime_-_macrocells"></param>
<param name="macrolayer file" required="false" type="string" default="Slime_-_macrolayers"></param>
<param name="area file" required="false" type="string" default="Nerd_-_Area"></param>
<param name="final stack file" required="false" type="string" default="Mads_-_Final_Stack"></param>
<param name="all stacks file" required="false" type="string" default="Mads_-_All_Stacks"></param>
</object>

View file

@ -0,0 +1,11 @@
<object type="output parameters">
<!-- output directory -->
<param name="output directory" type="string" default="output"></param>
<!-- output files -->
<param name="crystal file" required="false" type="string" default="Slime_-_crystals"></param>
<param name="macrocell file" required="false" type="string" default="Slime_-_macrocells"></param>
<param name="macrolayer file" required="false" type="string" default="Slime_-_macrolayers"></param>
<param name="area file" required="false" type="string" default="Nerd_-_Area"></param>
<param name="final stack file" required="false" type="string" default="Mads_-_Final_Stack"></param>
<param name="all stacks file" required="false" type="string" default="Mads_-_All_Stacks"></param>
</object>