Skip to content

Commit

Permalink
Pretty Self-Closing Tags
Browse files Browse the repository at this point in the history
Issue: #149
  • Loading branch information
DotJoshJohnson committed May 30, 2018
1 parent 07b47c4 commit 156e1ab
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
"description": "Enables auto-reveal of elements in the XML Document view when a start tag is clicked in the editor.",
"scope": "window"
},
"xmlTools.enforcePrettySelfClosingTagOnFormat": {
"type": "boolean",
"default": false,
"description": "Enforces a space before the forward slash at the end of a self-closing XML tag.",
"scope": "resource"
},
"xmlTools.ignoreDefaultNamespace": {
"type": "boolean",
"default": true,
Expand Down
4 changes: 4 additions & 0 deletions src/common/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class Configuration {
return this._getForWindow<string>("xqueryExecutionInputSearchPattern");
}

static enforcePrettySelfClosingTagOnFormat(resource: Uri): boolean {
return this._getForResource<boolean>("enforcePrettySelfClosingTagOnFormat", resource);
}

static removeCommentsOnMinify(resource: Uri): boolean {
return this._getForResource<boolean>("removeCommentsOnMinify", resource);
}
Expand Down
8 changes: 8 additions & 0 deletions src/formatting/formatters/v2-xml-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ export class V2XmlFormatter implements XmlFormatter {
location = Location.StartTag;
}

// approaching the end of a self-closing tag where there was no whitespace (issue #149)
else if ((location === Location.StartTag || location === Location.StartTagName)
&& cc === "/"
&& pc !== " "
&& options.enforcePrettySelfClosingTagOnFormat) {
output += " /";
}

// exiting StartTag or StartTag.StartTagName, entering Text
else if ((location === Location.StartTag || location === Location.StartTagName) && cc === ">") {
// if this was a self-closing tag, we need to decrement the indent level and add a newLine
Expand Down
2 changes: 2 additions & 0 deletions src/formatting/xml-formatting-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as constants from "../constants";

export interface XmlFormattingOptions {
editorOptions: FormattingOptions;
enforcePrettySelfClosingTagOnFormat: boolean;
newLine: string;
removeCommentsOnMinify: boolean;
splitAttributesOnFormat: boolean;
Expand All @@ -15,6 +16,7 @@ export class XmlFormattingOptionsFactory {
static getXmlFormattingOptions(formattingOptions: FormattingOptions, document: TextDocument): XmlFormattingOptions {
return {
editorOptions: formattingOptions,
enforcePrettySelfClosingTagOnFormat: Configuration.enforcePrettySelfClosingTagOnFormat(document.uri),
newLine: (document.eol === EndOfLine.CRLF) ? "\r\n" : "\n",
removeCommentsOnMinify: Configuration.removeCommentsOnMinify(document.uri),
splitAttributesOnFormat: Configuration.splitAttributesOnFormat(document.uri),
Expand Down
5 changes: 5 additions & 0 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("V2XmlFormatter", () => {
insertSpaces: true,
tabSize: 4
},
enforcePrettySelfClosingTagOnFormat: false,
newLine: "\r\n",
removeCommentsOnMinify: false,
splitAttributesOnFormat: false,
Expand Down Expand Up @@ -60,7 +61,11 @@ describe("V2XmlFormatter", () => {
});

it("should allow users to enforce space before self-closing tag slash", () => {
options.enforcePrettySelfClosingTagOnFormat = true;

testFormatter(xmlFormatter, options, "issue-149");

options.enforcePrettySelfClosingTagOnFormat = false;
});

});
Expand Down

0 comments on commit 156e1ab

Please sign in to comment.