From 9941bafa74d9e728a9bb13d33718d13803a7996a Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Sun, 28 Jan 2018 12:09:57 -0500 Subject: [PATCH] Add Attribute Splitting #116 --- README.md | 3 ++- package.json | 6 ++++++ src/formatting/formatters/v2-xml-formatter.ts | 4 ++++ src/formatting/xml-formatting-edit-provider.ts | 1 + src/formatting/xml-formatting-options.ts | 1 + 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d92e6f9..6bcb635 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ * `xmlTools.ignoreDefaultNamespace`: Ignore default xmlns attributes when evaluating XPath. * `xmlTools.persistXPathQuery`: Remember the last XPath query used. * `xmlTools.removeCommentsOnMinify`: Remove XML comments during minification. +* `xmlTools.splitAttributesOnFormat`: Put each attribute on a new line when formatting XML. Overrides `xmlTools.splitXmlsOnFormat` if set to `true`. (V2 Formatter Only) * `xmlTools.splitXmlnsOnFormat`: Put each xmlns attribute on a new line when formatting XML. -* `xmlTools.xmlFormatterImplementation`: Supported XML Formatters: `classic`. +* `xmlTools.xmlFormatterImplementation`: Supported XML Formatters: `classic`, `v2`. * `xmlTools.xqueryExecutionArguments`: Arguments to be passed to the XQuery execution engine. * `xmlTools.xqueryExecutionEngine`: The full path to the executable to run when executing XQuery scripts. diff --git a/package.json b/package.json index 794e3ef..264d23b 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,12 @@ "description": "Remove XML comments during minification.", "scope": "resource" }, + "xmlTools.splitAttributesOnFormat": { + "type": "boolean", + "default": false, + "description": "Put each attribute on a new line when formatting XML. Overrides `xmlTools.splitXmlsOnFormat` if set to `true`.", + "scope": "resource" + }, "xmlTools.splitXmlnsOnFormat": { "type": "boolean", "default": true, diff --git a/src/formatting/formatters/v2-xml-formatter.ts b/src/formatting/formatters/v2-xml-formatter.ts index 5ce20fa..52b5926 100644 --- a/src/formatting/formatters/v2-xml-formatter.ts +++ b/src/formatting/formatters/v2-xml-formatter.ts @@ -87,6 +87,10 @@ export class V2XmlFormatter implements XmlFormatter { // entering StartTag.Attribute else if (location === Location.StartTag && [" ", "/", ">"].indexOf(cc) === -1) { + if (lastNonTextLocation === Location.AttributeValue && ((options.splitXmlnsOnFormat && xml.substr(i, 5).toLowerCase() === "xmlns") || options.splitAttributesOnFormat)) { + output += `${options.newLine}${this._getIndent(options, indentLevel)}`; + } + output += cc; lastNonTextLocation = location; location = Location.Attribute; diff --git a/src/formatting/xml-formatting-edit-provider.ts b/src/formatting/xml-formatting-edit-provider.ts index b744a01..a6c117b 100644 --- a/src/formatting/xml-formatting-edit-provider.ts +++ b/src/formatting/xml-formatting-edit-provider.ts @@ -31,6 +31,7 @@ export class XmlFormattingEditProvider implements DocumentFormattingEditProvider editorOptions: options, newLine: (document.eol === EndOfLine.CRLF) ? "\r\n" : "\n", removeCommentsOnMinify: this.workspaceConfiguration.get("removeCommentsOnMinify"), + splitAttributesOnFormat: this.workspaceConfiguration.get("splitAttributesOnFormat"), splitXmlnsOnFormat: this.workspaceConfiguration.get("splitXmlnsOnFormat") }); diff --git a/src/formatting/xml-formatting-options.ts b/src/formatting/xml-formatting-options.ts index 439bdba..bafed99 100644 --- a/src/formatting/xml-formatting-options.ts +++ b/src/formatting/xml-formatting-options.ts @@ -4,5 +4,6 @@ export interface XmlFormattingOptions { editorOptions: FormattingOptions; newLine: string; removeCommentsOnMinify: boolean; + splitAttributesOnFormat: boolean; splitXmlnsOnFormat: boolean; }