Implemented conversion from XML to text and viceversa

This commit is contained in:
Eliasib García 2018-12-13 19:01:01 +00:00
parent 84adff578f
commit 51b6b4fbaa
7 changed files with 70 additions and 2 deletions

View file

@ -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";
}

View file

@ -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)

View file

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

View file

@ -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(/&lt;/g, '<').replace(/&gt;/g, '>');
edit.replace(selection, transformed);
});
});
}

View file

@ -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, '&lt;').replace(/>/g, '&gt;');
edit.replace(selection, transformed);
});
});
}