forked from mikeburgh/vscode-xml-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
208 lines (188 loc) · 6.46 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
200
201
202
203
204
205
206
207
208
const vscode = require('vscode');
var vkbeautify;
///Following is based on https://github.com/vkiryukhin/vkBeautify
//Extracted the xml format method only.
/**
* vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON, CSS and SQL formats.
*
* Version - 0.99.00.beta
* Copyright (c) 2012 Vadim Kiryukhin
* vkiryukhin @ gmail.com
* http://www.eslinstructor.net/vkbeautify/
*
* MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
function createShiftArr(step) {
var space = ' ';
if ( isNaN(parseInt(step)) ) { // argument is string
space = step;
} else { // argument is integer
switch(step) {
case 1: space = ' '; break;
case 2: space = ' '; break;
case 3: space = ' '; break;
case 4: space = ' '; break;
case 5: space = ' '; break;
case 6: space = ' '; break;
case 7: space = ' '; break;
case 8: space = ' '; break;
case 9: space = ' '; break;
case 10: space = ' '; break;
case 11: space = ' '; break;
case 12: space = ' '; break;
}
}
var shift = ['\n']; // array of shifts
for(ix=0;ix<100;ix++){
shift.push(shift[ix]+space);
}
return shift;
}
function VKBeautify(){
var settings = vscode.workspace.getConfiguration();
this.step = settings.editor.insertSpaces ? settings.editor.tabSize : '\t';
this.shift = createShiftArr( this.step );
};
VKBeautify.prototype.xml = function(text,step) {
var ar = text.trim()
.replace(/</g,"~::~<")
//.replace(/(\s)(xmlns[:=])/g,"$1~::~$2")
.split('~::~'),
len = ar.length,
inComment = false,
inCDATA = false,
deep_real = 0,
deep = 0,
str = '',
ix = 0,
shift = step ? createShiftArr(step) : this.shift;
for(ix=0;ix<len;ix++) {
// in comment //
if(inComment) {
str += ar[ix];
// end comment //
if(ar[ix].search(/-->/) > -1) {
inComment = false;
}
}
// in <![CDATA[...]]> //
else if(inCDATA) {
str += ar[ix];
// end <![CDATA[...]]> //
if(ar[ix].search(/\]\]>/) > -1) {
inCDATA = false;
}
}
// start comment //
else if(ar[ix].search(/<!--/) > -1) {
str = str.trim()+shift[deep]+ar[ix];
inComment = true;
// end comment //
if(ar[ix].search(/-->/) > -1) {
inComment = false;
}
}
// end comment //
else if(ar[ix].search(/-->/) > -1) {
str += ar[ix];
inComment = false;
}
// start <![CDATA[...]]> //
else if(ar[ix].search(/<!\[CDATA\[/) > -1) {
str = str.trim()+shift[deep]+ar[ix];
inCDATA = true;
// end <![CDATA[...]]> //
if(ar[ix].search(/\]\]>/) > -1) {
inCDATA = false;
}
}
// end <![CDATA[...]]> //
else if(ar[ix].search(/\]\]>/) > -1) {
str += ar[ix];
inCDATA = false;
}
// <!DOCTYPE //
else if(ar[ix].search(/<!/) > -1) {
str = str.trim()+shift[deep]+ar[ix];
}
// <?xml //
else if(ar[ix].search(/<\?/) > -1) {
str = str.trim()+shift[deep]+ar[ix];
}
// <elm></elm> //
else if( ix > 0 &&
/^<\/[^<>!?\/\s]/.exec(ar[ix]) && /^<[^<>!?\/\s]/.exec(ar[ix-1]) &&
/^<\/[^<>!?\/\s]+/.exec(ar[ix])[0].replace('/','') == /^<[^<>!?\/\s]+/.exec(ar[ix-1])[0] ) {
if(deep_real > 0) deep_real--;
if(deep_real <= 100) deep = deep_real;
str += ar[ix];
}
// </elm> //
else if(ar[ix].search(/<\//) > -1) {
if(deep_real > 0) deep_real--;
if(deep_real <= 100) deep = deep_real;
str = str.trim()+shift[deep]+ar[ix];
}
// <elm> or <elm/> //
else if(ar[ix].search(/<[^<>!?\/\s]/) > -1) {
str = str.trim()+shift[deep]+ar[ix];
deep_real++;
// <elm/> //
if(ar[ix].search(/\/>/) > -1) {
deep_real--;
}
if(deep_real <= 100) deep = deep_real;
}
// xmlns //
//else if(ar[ix].search(/xmlns[:=]/) > -1) {
// str = str.trim()+shift[deep]+ar[ix];
//}
else {
str += ar[ix];
}
}
return str.trim();
}
function updateVKBeautify()
{
vkbeautify = new VKBeautify();
}
updateVKBeautify();
//Build a range of the entire document!
function getRange(document) {
return new vscode.Range(
// line 0, char 0:
0, 0,
// last line:
document.lineCount - 1,
// last character:
document.lineAt( document.lineCount - 1 ).range.end.character
)
}
// this method is called when your extension is activated
function activate( context )
{
var disposableXML = vscode.languages.registerDocumentFormattingEditProvider( { language: 'xml' }, {
provideDocumentFormattingEdits: function( document )
{
return [ vscode.TextEdit.replace( getRange(document), vkbeautify.xml( document.getText() ) ) ]
}
} );
vscode.workspace.onDidChangeConfiguration( updateVKBeautify, this, context.subscriptions );
context.subscriptions.push( disposableXML );
var disposableXSL = vscode.languages.registerDocumentFormattingEditProvider( { language: 'xsl' }, {
provideDocumentFormattingEdits: function( document )
{
return [ vscode.TextEdit.replace( getRange(document), vkbeautify.xml( document.getText() ) ) ]
}
} );
vscode.workspace.onDidChangeConfiguration( updateVKBeautify, this, context.subscriptions );
context.subscriptions.push( disposableXSL );
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate()
{
}
exports.deactivate = deactivate;