Merge pull request #249 from eliasib13/xml-to-text
Implemented conversion from XML to text and viceversa
This commit is contained in:
		
						commit
						c0411015d5
					
				
					 7 changed files with 70 additions and 2 deletions
				
			
		
							
								
								
									
										2
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    "name": "xml",
 | 
					    "name": "xml",
 | 
				
			||||||
    "version": "2.0.0-preview.2",
 | 
					    "version": "2.3.2",
 | 
				
			||||||
    "lockfileVersion": 1,
 | 
					    "lockfileVersion": 1,
 | 
				
			||||||
    "requires": true,
 | 
					    "requires": true,
 | 
				
			||||||
    "dependencies": {
 | 
					    "dependencies": {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										10
									
								
								package.json
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								package.json
									
										
									
									
									
								
							| 
						 | 
					@ -35,6 +35,8 @@
 | 
				
			||||||
        "onCommand:xmlTools.evaluateXPath",
 | 
					        "onCommand:xmlTools.evaluateXPath",
 | 
				
			||||||
        "onCommand:xmlTools.executeXQuery",
 | 
					        "onCommand:xmlTools.executeXQuery",
 | 
				
			||||||
        "onCommand:xmlTools.formatAsXml",
 | 
					        "onCommand:xmlTools.formatAsXml",
 | 
				
			||||||
 | 
					        "onCommand:xmlTools.textToXml",
 | 
				
			||||||
 | 
					        "onCommand:xmlTools.xmlToText",
 | 
				
			||||||
        "onCommand:xmlTools.minifyXml",
 | 
					        "onCommand:xmlTools.minifyXml",
 | 
				
			||||||
        "onLanguage:xml",
 | 
					        "onLanguage:xml",
 | 
				
			||||||
        "onLanguage:xquery",
 | 
					        "onLanguage:xquery",
 | 
				
			||||||
| 
						 | 
					@ -55,6 +57,14 @@
 | 
				
			||||||
                "command": "xmlTools.formatAsXml",
 | 
					                "command": "xmlTools.formatAsXml",
 | 
				
			||||||
                "title": "XML Tools: Format as XML"
 | 
					                "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",
 | 
					                "command": "xmlTools.getCurrentXPath",
 | 
				
			||||||
                "title": "XML Tools: Get Current XPath"
 | 
					                "title": "XML Tools: Get Current XPath"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,8 @@ export namespace commands {
 | 
				
			||||||
    export const evaluateXPath = "xmlTools.evaluateXPath";
 | 
					    export const evaluateXPath = "xmlTools.evaluateXPath";
 | 
				
			||||||
    export const executeXQuery = "xmlTools.executeXQuery";
 | 
					    export const executeXQuery = "xmlTools.executeXQuery";
 | 
				
			||||||
    export const formatAsXml = "xmlTools.formatAsXml";
 | 
					    export const formatAsXml = "xmlTools.formatAsXml";
 | 
				
			||||||
 | 
					    export const xmlToText = "xmlTools.xmlToText";
 | 
				
			||||||
 | 
					    export const textToXml = "xmlTools.textToXml";
 | 
				
			||||||
    export const getCurrentXPath = "xmlTools.getCurrentXPath";
 | 
					    export const getCurrentXPath = "xmlTools.getCurrentXPath";
 | 
				
			||||||
    export const minifyXml = "xmlTools.minifyXml";
 | 
					    export const minifyXml = "xmlTools.minifyXml";
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,7 @@ import {
 | 
				
			||||||
import { createDocumentSelector, ExtensionState, Configuration } from "./common";
 | 
					import { createDocumentSelector, ExtensionState, Configuration } from "./common";
 | 
				
			||||||
import { XQueryCompletionItemProvider } from "./completion";
 | 
					import { XQueryCompletionItemProvider } from "./completion";
 | 
				
			||||||
import { XmlFormatterFactory, XmlFormattingEditProvider } from "./formatting";
 | 
					import { XmlFormatterFactory, XmlFormattingEditProvider } from "./formatting";
 | 
				
			||||||
import { formatAsXml, minifyXml } from "./formatting/commands";
 | 
					import { formatAsXml, minifyXml, xmlToText, textToXml } from "./formatting/commands";
 | 
				
			||||||
import { XQueryLinter } from "./linting";
 | 
					import { XQueryLinter } from "./linting";
 | 
				
			||||||
import { XmlTreeDataProvider } from "./tree-view";
 | 
					import { XmlTreeDataProvider } from "./tree-view";
 | 
				
			||||||
import { evaluateXPath, getCurrentXPath } from "./xpath/commands";
 | 
					import { evaluateXPath, getCurrentXPath } from "./xpath/commands";
 | 
				
			||||||
| 
						 | 
					@ -32,6 +32,8 @@ export function activate(context: ExtensionContext) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context.subscriptions.push(
 | 
					    context.subscriptions.push(
 | 
				
			||||||
        commands.registerTextEditorCommand(constants.commands.formatAsXml, formatAsXml),
 | 
					        commands.registerTextEditorCommand(constants.commands.formatAsXml, formatAsXml),
 | 
				
			||||||
 | 
					        commands.registerTextEditorCommand(constants.commands.xmlToText, xmlToText),
 | 
				
			||||||
 | 
					        commands.registerTextEditorCommand(constants.commands.textToXml, textToXml),
 | 
				
			||||||
        commands.registerTextEditorCommand(constants.commands.minifyXml, minifyXml),
 | 
					        commands.registerTextEditorCommand(constants.commands.minifyXml, minifyXml),
 | 
				
			||||||
        languages.registerDocumentFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider),
 | 
					        languages.registerDocumentFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider),
 | 
				
			||||||
        languages.registerDocumentRangeFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider)
 | 
					        languages.registerDocumentRangeFormattingEditProvider(xmlXsdDocSelector, xmlFormattingEditProvider)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,2 +1,4 @@
 | 
				
			||||||
export * from "./formatAsXml";
 | 
					export * from "./formatAsXml";
 | 
				
			||||||
export * from "./minifyXml";
 | 
					export * from "./minifyXml";
 | 
				
			||||||
 | 
					export * from "./xmlToText";
 | 
				
			||||||
 | 
					export * from "./textToXml";
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										26
									
								
								src/formatting/commands/textToXml.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/formatting/commands/textToXml.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,26 @@
 | 
				
			||||||
 | 
					import { workspace } from "vscode";
 | 
				
			||||||
 | 
					import { ProviderResult, Range, TextEdit, TextEditor, 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(textEditor: TextEditor): void {
 | 
				
			||||||
 | 
					    textEditor.edit(textEdit => {
 | 
				
			||||||
 | 
					        const selections = textEditor.selections;
 | 
				
			||||||
 | 
					        selections.forEach(selection => {
 | 
				
			||||||
 | 
					            if (selection.isEmpty) {
 | 
				
			||||||
 | 
					                selection = new Selection(
 | 
				
			||||||
 | 
					                    textEditor.document.positionAt(0),
 | 
				
			||||||
 | 
					                    textEditor.document.positionAt(textEditor.document.getText().length)
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            const txt = textEditor.document.getText(new Range(selection.start, selection.end));
 | 
				
			||||||
 | 
					            const transformed = txt.replace(/</g, "<").replace(/>/g, ">");
 | 
				
			||||||
 | 
					            textEdit.replace(selection, transformed);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										26
									
								
								src/formatting/commands/xmlToText.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/formatting/commands/xmlToText.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,26 @@
 | 
				
			||||||
 | 
					import { workspace } from "vscode";
 | 
				
			||||||
 | 
					import { ProviderResult, Range, TextEdit, TextEditor, 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(textEditor: TextEditor): void {
 | 
				
			||||||
 | 
					    textEditor.edit(textEdit => {
 | 
				
			||||||
 | 
					        const selections = textEditor.selections;
 | 
				
			||||||
 | 
					        selections.forEach(selection => {
 | 
				
			||||||
 | 
					            if (selection.isEmpty) {
 | 
				
			||||||
 | 
					                selection = new Selection(
 | 
				
			||||||
 | 
					                    textEditor.document.positionAt(0),
 | 
				
			||||||
 | 
					                    textEditor.document.positionAt(textEditor.document.getText().length)
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            const txt = textEditor.document.getText(new Range(selection.start, selection.end));
 | 
				
			||||||
 | 
					            const transformed = txt.replace(/</g, "<").replace(/>/g, ">");
 | 
				
			||||||
 | 
					            textEdit.replace(selection, transformed);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue