From ab082e3e0ec8f9e3cd4ae111f18c37bb65bb16d4 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Wed, 25 Nov 2020 16:46:31 -0500 Subject: [PATCH] Add Minify XML (Selection) Command closes #302 --- package.json | 9 +++++++++ src/constants.ts | 1 + src/extension.ts | 7 ++++--- src/formatting/commands/index.ts | 1 + src/formatting/commands/minifyXmlSelection.ts | 19 +++++++++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/formatting/commands/minifyXmlSelection.ts diff --git a/package.json b/package.json index 8822f2b..a43c3d6 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,10 @@ { "command": "xmlTools.minifyXml", "title": "XML Tools: Minify XML" + }, + { + "command": "xmlTools.minifyXmlSelection", + "title": "XML Tools: Minify XML (Selection)" } ], "configuration": { @@ -246,6 +250,11 @@ "group": "1_modification@100", "when": "editorLangId == 'xml'" }, + { + "command": "xmlTools.minifyXmlSelection", + "group": "1_modification@100", + "when": "editorLangId == 'xml' && editorHasSelection" + }, { "command": "xmlTools.getCurrentXPath", "group": "z_commands", diff --git a/src/constants.ts b/src/constants.ts index 07d969e..81ebbd0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 { diff --git a/src/extension.ts b/src/extension.ts index 17b41b2..e91b4c5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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; } } diff --git a/src/formatting/commands/index.ts b/src/formatting/commands/index.ts index 3dc6ea2..35a228e 100644 --- a/src/formatting/commands/index.ts +++ b/src/formatting/commands/index.ts @@ -2,3 +2,4 @@ export * from "./formatAsXml"; export * from "./minifyXml"; export * from "./xmlToText"; export * from "./textToXml"; +export * from "./minifyXmlSelection"; diff --git a/src/formatting/commands/minifyXmlSelection.ts b/src/formatting/commands/minifyXmlSelection.ts new file mode 100644 index 0000000..b63762d --- /dev/null +++ b/src/formatting/commands/minifyXmlSelection.ts @@ -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: editor.options.insertSpaces, + tabSize: 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)); + }); +}