forked from external/vscode-xml
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
|
|
@ -1,2 +1,4 @@
|
|||
export * from "./formatAsXml";
|
||||
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
Add a link
Reference in a new issue