Add Linearization

This commit is contained in:
Josh Johnson 2015-11-22 02:37:00 -05:00
parent 30e8222717
commit 4c1e6d05b9
4 changed files with 31 additions and 14 deletions

View File

@ -33,7 +33,11 @@
"commands": [
{
"command": "xmltools.formatXml",
"title": "Format XML"
"title": "XML Tools: Format XML"
},
{
"command": "xmltools.linearizeXml",
"title": "XML Tools: Linearize XML"
}
]
},

View File

@ -1,22 +1,22 @@
'use strict';
import { TextEditor, TextEditorEdit, TextDocument, Position, Range } from 'vscode';
import { TextEditor, TextEditorEdit } from 'vscode';
import { getRangeForDocument } from '../utils/RangeUtils';
let pd = require('pretty-data').pd;
export function formatXml(editor: TextEditor, edit: TextEditorEdit): void {
let current = editor.document.getText();
let pretty = pd.xml(current);
// get the range for the entire document
let lastLineIndex = (editor.document.lineCount - 1);
let lastLine = editor.document.lineAt(lastLineIndex);
let lastPosition = lastLine.rangeIncludingLineBreak.end;
let range = new Range(new Position(0, 0), lastPosition);
// validate the range to ensure it fits inside the document
range = editor.document.validateRange(range);
// replace the existing xml with the pretty xml
let range = getRangeForDocument(editor.document);
edit.replace(range, pretty);
}
export function linearizeXml(editor: TextEditor, edit: TextEditorEdit): void {
let current = editor.document.getText();
let linear = pd.xmlmin(current);
let range = getRangeForDocument(editor.document);
edit.replace(range, linear);
}

View File

@ -1,7 +1,7 @@
'use strict';
import { commands, ExtensionContext } from 'vscode';
import { formatXml } from './features/xmlFormatting';
import { formatXml, linearizeXml } from './features/xmlFormatting';
export function activate(ctx: ExtensionContext) {
// check for update
@ -9,4 +9,5 @@ export function activate(ctx: ExtensionContext) {
// register pallete commands
ctx.subscriptions.push(commands.registerTextEditorCommand('xmltools.formatXml', formatXml));
ctx.subscriptions.push(commands.registerTextEditorCommand('xmltools.linearizeXml', linearizeXml));
}

12
src/utils/RangeUtils.ts Normal file
View File

@ -0,0 +1,12 @@
'use strict';
import { TextDocument, Range, Position } from 'vscode';
export function getRangeForDocument(document: TextDocument): Range {
let lastLineIndex = (document.lineCount - 1);
let range = new Range(new Position(0, 0), new Position(lastLineIndex, Number.MAX_VALUE));
range = document.validateRange(range);
return range;
}