This commit is contained in:
Josh Johnson 2020-11-26 03:27:49 +00:00 committed by GitHub
commit d45a81ff77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 118 additions and 15 deletions

View file

@ -2,7 +2,7 @@
"name": "xml",
"displayName": "XML Tools",
"description": "XML Formatting, XQuery, and XPath Tools for Visual Studio Code",
"version": "2.5.1",
"version": "2.6.0",
"preview": false,
"publisher": "DotJoshJohnson",
"author": {
@ -72,6 +72,10 @@
{
"command": "xmlTools.minifyXml",
"title": "XML Tools: Minify XML"
},
{
"command": "xmlTools.minifyXmlSelection",
"title": "XML Tools: Minify XML (Selection)"
}
],
"configuration": {
@ -159,7 +163,7 @@
"type": "string",
"default": "",
"description": "The full path to the executable to run when executing XQuery scripts.",
"scope": "window"
"scope": "machine"
},
"xmlTools.xqueryExecutionInputLimit": {
"type": "integer",
@ -172,6 +176,12 @@
"default": "**/*.xml",
"description": "The pattern used to search for input XML files when executing XQuery scripts.",
"scope": "window"
},
"xmlTools.preserveSpacesBetweenAttributes": {
"type": "boolean",
"default": false,
"description": "Preserves any spaces between attributes during formatting.",
"scope": "resource"
}
}
},
@ -245,6 +255,16 @@
"command": "xmlTools.minifyXml",
"group": "1_modification@100",
"when": "editorLangId == 'xml'"
},
{
"command": "xmlTools.minifyXmlSelection",
"group": "1_modification@100",
"when": "editorLangId == 'xml' && editorHasSelection"
},
{
"command": "xmlTools.getCurrentXPath",
"group": "z_commands",
"when": "editorLangId == 'xml'"
}
]
},
@ -280,4 +300,4 @@
"xpath": "0.0.27",
"xqlint": "^0.4.1"
}
}
}

View file

@ -59,11 +59,15 @@ export class Configuration {
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 {
return workspace.getConfiguration(ExtensionTopLevelSection, resource).get<T>(section);
}
private static _getForWindow<T>(section: string): T {
private static _getForWindow<T>(section: string): T {
return workspace.getConfiguration(ExtensionTopLevelSection).get<T>(section);
}
}

View file

@ -6,6 +6,7 @@ export namespace commands {
export const textToXml = "xmlTools.textToXml";
export const getCurrentXPath = "xmlTools.getCurrentXPath";
export const minifyXml = "xmlTools.minifyXml";
export const minifyXmlSelection = "xmlTools.minifyXmlSelection";
}
export namespace contextKeys {

View file

@ -1,12 +1,12 @@
import {
commands, languages, window, workspace, ExtensionContext, Memento,
TextEditor, TextEditorSelectionChangeEvent, TextEditorSelectionChangeKind, DiagnosticCollection
} from "vscode";
} from "vscode";
import { createDocumentSelector, ExtensionState, Configuration } from "./common";
import { XQueryCompletionItemProvider } from "./completion";
import { XmlFormatterFactory, XmlFormattingEditProvider } from "./formatting";
import { formatAsXml, minifyXml, xmlToText, textToXml } from "./formatting/commands";
import { formatAsXml, minifyXml, xmlToText, textToXml, minifyXmlSelection } from "./formatting/commands";
import { XQueryLinter } from "./linting";
import { XmlTreeDataProvider } from "./tree-view";
import { evaluateXPath, getCurrentXPath } from "./xpath/commands";
@ -35,6 +35,7 @@ export function activate(context: ExtensionContext) {
commands.registerTextEditorCommand(constants.commands.xmlToText, xmlToText),
commands.registerTextEditorCommand(constants.commands.textToXml, textToXml),
commands.registerTextEditorCommand(constants.commands.minifyXml, minifyXml),
commands.registerTextEditorCommand(constants.commands.minifyXmlSelection, minifyXmlSelection),
languages.registerDocumentFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider),
languages.registerDocumentRangeFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider)
);
@ -91,7 +92,7 @@ function _handleContextChange(editor: TextEditor): void {
switch (editor.document.languageId) {
case constants.languageIds.xquery:
diagnosticCollectionXQuery.set(editor.document.uri, new XQueryLinter().lint(editor.document.getText()));
diagnosticCollectionXQuery.set(editor.document.uri, new XQueryLinter().lint(editor.document.getText()));
break;
}
}

View file

@ -2,3 +2,4 @@ export * from "./formatAsXml";
export * from "./minifyXml";
export * from "./xmlToText";
export * from "./textToXml";
export * from "./minifyXmlSelection";

View file

@ -0,0 +1,19 @@
import { workspace } from "vscode";
import { Range, TextEditor, TextEditorEdit } from "vscode";
import { XmlFormatterFactory } from "../xml-formatter";
import { XmlFormattingOptionsFactory } from "../xml-formatting-options";
export function minifyXmlSelection(editor: TextEditor, edit: TextEditorEdit): void {
const xmlFormatter = XmlFormatterFactory.getXmlFormatter();
const xmlFormattingOptions = XmlFormattingOptionsFactory.getXmlFormattingOptions({
insertSpaces: <boolean>editor.options.insertSpaces,
tabSize: <number>editor.options.tabSize
}, editor.document);
editor.selections.reverse().forEach(selection => {
const range = new Range(selection.start, selection.end);
edit.replace(range, xmlFormatter.minifyXml(editor.document.getText(range), xmlFormattingOptions));
});
}

View file

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

View file

@ -1,7 +1,6 @@
import { EndOfLine, FormattingOptions, TextDocument } from "vscode";
import { Configuration } from "../common";
import * as constants from "../constants";
export interface XmlFormattingOptions {
editorOptions: FormattingOptions;
@ -11,6 +10,7 @@ export interface XmlFormattingOptions {
splitAttributesOnFormat: boolean;
splitXmlnsOnFormat: boolean;
initialIndentLevel?: number;
preserveSpacesBetweenAttributes: boolean;
}
export class XmlFormattingOptionsFactory {
@ -22,7 +22,8 @@ export class XmlFormattingOptionsFactory {
removeCommentsOnMinify: Configuration.removeCommentsOnMinify(document.uri),
splitAttributesOnFormat: Configuration.splitAttributesOnFormat(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",
removeCommentsOnMinify: false,
splitAttributesOnFormat: false,
splitXmlnsOnFormat: true
splitXmlnsOnFormat: true,
preserveSpacesBetweenAttributes: false
};
it("should handle basic XML", () => {
@ -107,6 +108,18 @@ describe("V2XmlFormatter", () => {
it("should not add trailing whitespace", () => {
testFormatter(xmlFormatter, options, "issue-288");
});
it("should handle mixed content on the same line as another element without error", () => {
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)", () => {
@ -120,7 +133,8 @@ describe("V2XmlFormatter", () => {
newLine: "\r\n",
removeCommentsOnMinify: false,
splitAttributesOnFormat: false,
splitXmlnsOnFormat: true
splitXmlnsOnFormat: true,
preserveSpacesBetweenAttributes: false
};
it("should preserve whitespace on minify if xml:space is set to 'preserve-whitespace'", () => {

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0">
<div>
ThisCausesBug <img src="test"/>
<img src="test"/>
</div>
</xsl:stylesheet>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0">
<div>
ThisCausesBug <img src="test"/>
<img src="test"/>
</div>
</xsl:stylesheet>

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>