forked from external/vscode-xml
Add XML Formatter
This commit is contained in:
parent
bd6b6240a5
commit
704f6c9a36
3 changed files with 2120 additions and 1 deletions
22
src/features/xmlFormatting.ts
Normal file
22
src/features/xmlFormatting.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
'use strict';
|
||||
|
||||
import { TextEditor, TextEditorEdit, TextDocument, Position, Range } from 'vscode';
|
||||
|
||||
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
|
||||
edit.replace(range, pretty);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue