-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
199 lines (143 loc) · 5.58 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('dumb-xml-prettifier.prettify', function() {
var editor = vscode.window.activeTextEditor;
var selectedText = editor.document.getText(editor.selection);
var pretty = prettifier();
if (selectedText) {
var returnText = pretty.prettifyXml(selectedText);
editor.edit(editBuilder => {
editBuilder.replace(editor.selection, returnText);
});
} else {
var docText = editor.document.getText();
var returnText = pretty.prettifyXml(docText);
var lineCount = vscode.window.activeTextEditor.document.lineCount - 1;
var charCount = editor.document.lineAt(editor.document.lineCount - 1).text.length;
// Overwrite text in active document.
editor.edit(builder => {
builder.delete(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(lineCount, charCount)));
builder.insert(new vscode.Position(0, 0), returnText);
});
}
});
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}
var prettifier = function() {
let outputXmlLines = [];
let paddingLevel = 0;
function header(xmlLines, index) {
paddingLevel--;
outputXmlLines.push((index == 0 ? "" : "\n") + xmlLines[index]);
}
function openingTag(line, previousLine) {
if (previousLine != undefined) {
// /> at the end of the line
if (!previousLine.match(/\/>$/)) {
var onlyElementName = line.match(/^<(.+?)(\s|>$)/)[1];
var pattern = "^</" + onlyElementName + ">";
if (!previousLine.match(pattern)) {
paddingLevel++;
}
} else {
paddingLevel++
}
} // Padding stays the same because its the first line.
let padding = "";
let paddingAmount = paddingLevel * 4;
for (let i = 0; i < paddingAmount; i++) {
padding += " ";
}
line = line.replace(/(.)\s\/>$/, "$1/>") // remove any space before closing tag />
var newStrArr = indentAttributes(line, paddingAmount, padding).flatMap((a) => a);
// Cancel out closing tag />
if (line.match(/\/>$/)) {
paddingLevel--;
}
outputXmlLines.push(newStrArr.join(""));
}
function indentAttributes(line, paddingAmount, existingPadding) {
var elementParts = line
.split(/([a-zA-Z-:_]+="[\w\d\s\-\.\*\+\=]+"\/?>?)/g)
.filter(f => f != "")
.filter(f => f != " ")
.map(m => m.trim());
return elementParts.map(f => {
var amountOfLocalPadding = paddingAmount + 4;
let localPadding = "";
for (let i = 0; i < amountOfLocalPadding; i++) {
localPadding += " ";
}
return (f.substring(0, 1) == "<" ? "" + existingPadding : "\n" + localPadding) + f;
});
}
function closingTag(xmlLines, index) {
if (index != 0) {
// Check that count wasn't already decreased due to previous tag ending with />
if (!xmlLines[index - 1].match(/\/>$/)) {
var openingTag = xmlLines[index].replace('/', '').replace('>', '');
if (!xmlLines[index - 1].match(openingTag)) {
paddingLevel--;
}
}
} else {
paddingLevel--;
}
let padding = "";
var paddingAmount = paddingLevel * 4;
for (let i = 0; i < paddingAmount; i++) {
padding += " ";
}
outputXmlLines.push(padding + xmlLines[index]);
}
return {
prettifyXml: function(xml) {
let xmlLines = xml
.split(/(<[^>]*>)/gm)
.filter(m => m !== "")
.map(m => m.trim())
.filter(f => f != '')
.map(f => f.replace(/\r\n/g, " "));
// Reset state
paddingLevel = 0;
outputXmlLines = [];
for (let index = 0; index < xmlLines.length; index++) {
var currentLine = xmlLines[index];
// Header thing, <?xml version="1.0" encoding="UTF-8"?>
if (currentLine.match(/^<\?/)) {
header(xmlLines, index);
continue;
}
// Comment, <!-- like this -->
if (currentLine.match(/^<!/)) {
outputXmlLines.push(currentLine);
continue;
}
// Opending tag
if (currentLine.match(/^<[^!?\/]/)) {
openingTag(currentLine, xmlLines[index - 1]);
continue;
}
// Closing tag
if (currentLine.match(/^<\//)) {
closingTag(xmlLines, index);
continue;
}
}
return outputXmlLines.join("\n").replace(/\n\s+\n/g, '\n');
}
}
}