Add Linearization
This commit is contained in:
		
							parent
							
								
									30e8222717
								
							
						
					
					
						commit
						4c1e6d05b9
					
				
					 4 changed files with 31 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -33,7 +33,11 @@
 | 
			
		|||
		"commands": [
 | 
			
		||||
			{
 | 
			
		||||
				"command": "xmltools.formatXml",
 | 
			
		||||
				"title": "Format XML"
 | 
			
		||||
				"title": "XML Tools: Format XML"
 | 
			
		||||
			},
 | 
			
		||||
			{
 | 
			
		||||
				"command": "xmltools.linearizeXml",
 | 
			
		||||
				"title": "XML Tools: Linearize XML"
 | 
			
		||||
			}
 | 
			
		||||
		]
 | 
			
		||||
	},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
	let range = getRangeForDocument(editor.document);
 | 
			
		||||
 | 
			
		||||
	// 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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
									
								
							
							
						
						
									
										12
									
								
								src/utils/RangeUtils.ts
									
										
									
									
									
										Normal 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;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue