From 51b6b4fbaa742e38ce219dda26575449ae2c3b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eliasib=20Garc=C3=ADa?= Date: Thu, 13 Dec 2018 19:01:01 +0000 Subject: [PATCH 1/2] Implemented conversion from XML to text and viceversa --- package-lock.json | 2 +- package.json | 10 ++++++++++ src/constants.ts | 2 ++ src/extension.ts | 4 +++- src/formatting/commands/index.ts | 2 ++ src/formatting/commands/textToXml.ts | 26 ++++++++++++++++++++++++++ src/formatting/commands/xmlToText.ts | 26 ++++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/formatting/commands/textToXml.ts create mode 100644 src/formatting/commands/xmlToText.ts diff --git a/package-lock.json b/package-lock.json index d971c4d..fe9ebfa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "xml", - "version": "2.0.0-preview.2", + "version": "2.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 9b5ef17..eee31b8 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ "onCommand:xmlTools.evaluateXPath", "onCommand:xmlTools.executeXQuery", "onCommand:xmlTools.formatAsXml", + "onCommand:xmlTools.textToXml", + "onCommand:xmlTools.xmlToText", "onCommand:xmlTools.minifyXml", "onLanguage:xml", "onLanguage:xquery", @@ -55,6 +57,14 @@ "command": "xmlTools.formatAsXml", "title": "XML Tools: Format as XML" }, + { + "command": "xmlTools.textToXml", + "title": "XML Tools: Convert text to XML (<> -> <>)" + }, + { + "command": "xmlTools.xmlToText", + "title": "XML Tools: Convert XML to text (<> -> <>)" + }, { "command": "xmlTools.getCurrentXPath", "title": "XML Tools: Get Current XPath" diff --git a/src/constants.ts b/src/constants.ts index 6261aee..07d969e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,6 +2,8 @@ export namespace commands { export const evaluateXPath = "xmlTools.evaluateXPath"; export const executeXQuery = "xmlTools.executeXQuery"; export const formatAsXml = "xmlTools.formatAsXml"; + export const xmlToText = "xmlTools.xmlToText"; + export const textToXml = "xmlTools.textToXml"; export const getCurrentXPath = "xmlTools.getCurrentXPath"; export const minifyXml = "xmlTools.minifyXml"; } diff --git a/src/extension.ts b/src/extension.ts index ee975af..4e3ee9e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,7 +6,7 @@ import { import { createDocumentSelector, ExtensionState, Configuration } from "./common"; import { XQueryCompletionItemProvider } from "./completion"; import { XmlFormatterFactory, XmlFormattingEditProvider } from "./formatting"; -import { formatAsXml, minifyXml } from "./formatting/commands"; +import { formatAsXml, minifyXml, xmlToText, textToXml } from "./formatting/commands"; import { XQueryLinter } from "./linting"; import { XmlTreeDataProvider } from "./tree-view"; import { evaluateXPath, getCurrentXPath } from "./xpath/commands"; @@ -30,6 +30,8 @@ export function activate(context: ExtensionContext) { context.subscriptions.push( commands.registerTextEditorCommand(constants.commands.formatAsXml, formatAsXml), + commands.registerTextEditorCommand(constants.commands.xmlToText, xmlToText), + commands.registerTextEditorCommand(constants.commands.textToXml, textToXml), commands.registerTextEditorCommand(constants.commands.minifyXml, minifyXml), languages.registerDocumentFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider), languages.registerDocumentRangeFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider) diff --git a/src/formatting/commands/index.ts b/src/formatting/commands/index.ts index 3e12b40..3dc6ea2 100644 --- a/src/formatting/commands/index.ts +++ b/src/formatting/commands/index.ts @@ -1,2 +1,4 @@ export * from "./formatAsXml"; export * from "./minifyXml"; +export * from "./xmlToText"; +export * from "./textToXml"; diff --git a/src/formatting/commands/textToXml.ts b/src/formatting/commands/textToXml.ts new file mode 100644 index 0000000..d80d475 --- /dev/null +++ b/src/formatting/commands/textToXml.ts @@ -0,0 +1,26 @@ +import { workspace } from "vscode"; +import { ProviderResult, Range, TextEdit, editor, editorEdit, Selection } from "vscode"; + +import { NativeCommands } from "../../common"; +import * as constants from "../../constants"; + +import { XmlFormatterFactory } from "../xml-formatter"; +import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; +import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; + +export function textToXml(editor: editor, edit: editorEdit): void { + editor.edit(edit => { + let selections = editor.selections; + selections.forEach(selection => { + if (selection.isEmpty) { + selection = new Selection( + editor.document.positionAt(0), + editor.document.positionAt(editor.document.getText().length) + ); + } + let txt = editor.document.getText(new Range(selection.start, selection.end)); + let transformed = txt.replace(/</g, '<').replace(/>/g, '>'); + edit.replace(selection, transformed); + }); + }); +} diff --git a/src/formatting/commands/xmlToText.ts b/src/formatting/commands/xmlToText.ts new file mode 100644 index 0000000..f58bf03 --- /dev/null +++ b/src/formatting/commands/xmlToText.ts @@ -0,0 +1,26 @@ +import { workspace } from "vscode"; +import { ProviderResult, Range, TextEdit, editor, editorEdit, Selection } from "vscode"; + +import { NativeCommands } from "../../common"; +import * as constants from "../../constants"; + +import { XmlFormatterFactory } from "../xml-formatter"; +import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; +import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; + +export function xmlToText(editor: editor, edit: editorEdit): void { + editor.edit(edit => { + let selections = editor.selections; + selections.forEach(selection => { + if (selection.isEmpty) { + selection = new Selection( + editor.document.positionAt(0), + editor.document.positionAt(editor.document.getText().length) + ); + } + let txt = editor.document.getText(new Range(selection.start, selection.end)); + let transformed = txt.replace(//g, '>'); + edit.replace(selection, transformed); + }); + }); +} From d753e957a09abe1a77b076b8416b187b9b878372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eliasib=20Garc=C3=ADa?= Date: Thu, 13 Dec 2018 19:13:15 +0000 Subject: [PATCH 2/2] Fixed tslint errors --- src/formatting/commands/textToXml.ts | 18 +++++++++--------- src/formatting/commands/xmlToText.ts | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/formatting/commands/textToXml.ts b/src/formatting/commands/textToXml.ts index d80d475..de2884f 100644 --- a/src/formatting/commands/textToXml.ts +++ b/src/formatting/commands/textToXml.ts @@ -1,5 +1,5 @@ import { workspace } from "vscode"; -import { ProviderResult, Range, TextEdit, editor, editorEdit, Selection } from "vscode"; +import { ProviderResult, Range, TextEdit, TextEditor, Selection } from "vscode"; import { NativeCommands } from "../../common"; import * as constants from "../../constants"; @@ -8,19 +8,19 @@ import { XmlFormatterFactory } from "../xml-formatter"; import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; -export function textToXml(editor: editor, edit: editorEdit): void { - editor.edit(edit => { - let selections = editor.selections; +export function textToXml(textEditor: TextEditor): void { + textEditor.edit(textEdit => { + const selections = textEditor.selections; selections.forEach(selection => { if (selection.isEmpty) { selection = new Selection( - editor.document.positionAt(0), - editor.document.positionAt(editor.document.getText().length) + textEditor.document.positionAt(0), + textEditor.document.positionAt(textEditor.document.getText().length) ); } - let txt = editor.document.getText(new Range(selection.start, selection.end)); - let transformed = txt.replace(/</g, '<').replace(/>/g, '>'); - edit.replace(selection, transformed); + const txt = textEditor.document.getText(new Range(selection.start, selection.end)); + const transformed = txt.replace(/</g, "<").replace(/>/g, ">"); + textEdit.replace(selection, transformed); }); }); } diff --git a/src/formatting/commands/xmlToText.ts b/src/formatting/commands/xmlToText.ts index f58bf03..f3d63ac 100644 --- a/src/formatting/commands/xmlToText.ts +++ b/src/formatting/commands/xmlToText.ts @@ -1,5 +1,5 @@ import { workspace } from "vscode"; -import { ProviderResult, Range, TextEdit, editor, editorEdit, Selection } from "vscode"; +import { ProviderResult, Range, TextEdit, TextEditor, Selection } from "vscode"; import { NativeCommands } from "../../common"; import * as constants from "../../constants"; @@ -8,19 +8,19 @@ import { XmlFormatterFactory } from "../xml-formatter"; import { XmlFormattingEditProvider } from "../xml-formatting-edit-provider"; import { XmlFormattingOptionsFactory } from "../xml-formatting-options"; -export function xmlToText(editor: editor, edit: editorEdit): void { - editor.edit(edit => { - let selections = editor.selections; +export function xmlToText(textEditor: TextEditor): void { + textEditor.edit(textEdit => { + const selections = textEditor.selections; selections.forEach(selection => { if (selection.isEmpty) { selection = new Selection( - editor.document.positionAt(0), - editor.document.positionAt(editor.document.getText().length) + textEditor.document.positionAt(0), + textEditor.document.positionAt(textEditor.document.getText().length) ); } - let txt = editor.document.getText(new Range(selection.start, selection.end)); - let transformed = txt.replace(//g, '>'); - edit.replace(selection, transformed); + const txt = textEditor.document.getText(new Range(selection.start, selection.end)); + const transformed = txt.replace(//g, ">"); + textEdit.replace(selection, transformed); }); }); }