Merge pull request #341 from DotJoshJohnson/feature/302-minify-selection

Add Minify XML (Selection) Command and Context Menu Item
This commit is contained in:
Josh Johnson 2020-11-25 16:47:24 -05:00 committed by GitHub
commit dc3d28b962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 3 deletions

View file

@ -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",

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));
});
}