diff --git a/src/extension.ts b/src/extension.ts index 3b35182..eb4fcb3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,6 +2,7 @@ import { languages, window, workspace, commands } from "vscode"; import { ExtensionContext, TextEditor, TextEditorSelectionChangeEvent, WorkspaceConfiguration } from "vscode"; import { FormatAsXmlCommandName, formatAsXml } from "./formatting/commands/formatAsXml"; +import { MinifyXmlCommandName, minifyXml } from "./formatting/commands/minifyXml"; import { XmlFormatterFactory } from "./formatting/xml-formatter"; import { XmlFormattingEditProvider } from "./formatting/xml-formatting-edit-provider"; import { XQueryLinter } from "./linting/xquery-linter"; @@ -16,6 +17,7 @@ export function activate(context: ExtensionContext) { context.subscriptions.push( commands.registerTextEditorCommand(FormatAsXmlCommandName, formatAsXml), + commands.registerTextEditorCommand(MinifyXmlCommandName, minifyXml), languages.registerDocumentFormattingEditProvider("xml", xmlFormattingEditProvider), languages.registerDocumentRangeFormattingEditProvider("xml", xmlFormattingEditProvider) ); diff --git a/src/formatting/commands/formatAsXml.ts b/src/formatting/commands/formatAsXml.ts index bd29f43..cfec9ea 100644 --- a/src/formatting/commands/formatAsXml.ts +++ b/src/formatting/commands/formatAsXml.ts @@ -7,7 +7,7 @@ import { XmlFormatterFactory } from "../xml-formatter"; import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; -export const FormatAsXmlCommandName = "formatAsXml"; +export const FormatAsXmlCommandName = "xmlTools.formatAsXml"; export function formatAsXml(editor: TextEditor, edit: TextEditorEdit): void { const xmlFormattingEditProvider = new XmlFormattingEditProvider(workspace.getConfiguration(constants.extensionPrefix), XmlFormatterFactory.getXmlFormatter()); diff --git a/src/formatting/commands/minifyXml.ts b/src/formatting/commands/minifyXml.ts new file mode 100644 index 0000000..4c8553f --- /dev/null +++ b/src/formatting/commands/minifyXml.ts @@ -0,0 +1,23 @@ +import { commands, workspace } from "vscode"; +import { ProviderResult, Range, TextEdit, TextEditor, TextEditorEdit } from "vscode"; + +import * as constants from "../../constants"; + +import { XmlFormatterFactory } from "../xml-formatter"; +import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; +import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; + +export const MinifyXmlCommandName = "xmlTools.minifyXml"; + +export function minifyXml(editor: TextEditor, edit: TextEditorEdit): void { + const xmlFormatter = XmlFormatterFactory.getXmlFormatter(); + const xmlFormattingOptions = XmlFormattingOptionsFactory.getXmlFormattingOptions({ + insertSpaces: editor.options.insertSpaces, + tabSize: editor.options.tabSize + }, editor.document.eol); + + const endPosition = editor.document.lineAt(editor.document.lineCount - 1).rangeIncludingLineBreak.end; + const range = new Range(editor.document.positionAt(0), endPosition); + + edit.replace(range, xmlFormatter.minifyXml(editor.document.getText(), xmlFormattingOptions)); +} \ No newline at end of file