diff --git a/package.json b/package.json index b30d95e..b07d9c9 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,11 @@ "commands": [ { "command": "xmltools.formatXml", - "title": "Format XML" + "title": "XML Tools: Format XML" + }, + { + "command": "xmltools.linearizeXml", + "title": "XML Tools: Linearize XML" } ] }, diff --git a/src/features/xmlFormatting.ts b/src/features/xmlFormatting.ts index 5a593d1..e3bf197 100644 --- a/src/features/xmlFormatting.ts +++ b/src/features/xmlFormatting.ts @@ -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); } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index d376fb6..8173a73 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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)); } \ No newline at end of file diff --git a/src/utils/RangeUtils.ts b/src/utils/RangeUtils.ts new file mode 100644 index 0000000..ad8f840 --- /dev/null +++ b/src/utils/RangeUtils.ts @@ -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; +} \ No newline at end of file