forked from yurasovm/eCSStractor-for-VSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
328 lines (260 loc) · 9.11 KB
/
extension.js
File metadata and controls
328 lines (260 loc) · 9.11 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const vscode = require('vscode');
const htmlparser2 = require('htmlparser2');
function pasteToClipboard(text) {
vscode.env.clipboard.writeText(text)
.then(() => vscode.window.showInformationMessage('Copied CSS format to clipboard'))
.catch((err) => vscode.window.showErrorMessage(err));
}
function pasteToNewDoc(text) {
vscode.workspace.openTextDocument()
.then((newDoc) => {
return vscode.window.showTextDocument(newDoc, 1, false)
.then((editor) => {
return editor.edit((editBuilder) => {
editBuilder.insert(new vscode.Position(0, 0), text);
});
});
});
}
function parseHtmlClasses(text, opts) {
const classSet = new Set();
const tagClassHadler = (name, attribs) => {
if (!attribs.class) return;
attribs.class.split(/\s+/)
.forEach(cl => cl && classSet.add(cl));
};
const tagClassNameHadler = (name, attribs) => {
if (!attribs.classname) return;
attribs.classname.split(/\s+/)
.forEach(cl => cl && classSet.add(cl));
};
const tagClassAndNameHadler = (name, attribs) => {
tagClassHadler(name, attribs);
tagClassNameHadler(name, attribs);
};
let onOpentagHandler = tagClassHadler;
switch (opts.attributes) {
case 'class':
onOpentagHandler = tagClassHadler;
break;
case 'className':
onOpentagHandler = tagClassNameHadler;
break;
case 'classAndClassName':
onOpentagHandler = tagClassAndNameHadler;
break;
}
const parser = new htmlparser2.Parser({ onopentag: onOpentagHandler }, { lowerCaseAttributeNames: true });
parser.write(text);
parser.end();
return classSet;
}
function process(opts) {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
const selectedText = editor.selection.isEmpty ?
editor.document.getText() :
editor.document.getText(editor.selection);
const classSet = parseHtmlClasses(selectedText, opts);
const finalString = opts.bemNesting ?
generateBEM(classSet, opts) :
generateFlat(classSet, opts);
if (opts.destination === 'clipboard') {
pasteToClipboard(finalString);
} else {
pasteToNewDoc(finalString);
}
}
function tryParseRegex(regStr) {
try {
return new RegExp(regStr);
}
catch(e) {
return null;
}
}
function getOptionts(override = {}) {
const config = vscode.workspace.getConfiguration('ecsstractor-port');
const options = {
indentation: config.get('indentation'),
commentStyle: config.get('commentStyle'),
brackets: config.get('brackets'),
bracketsNewLineAfter: config.get('bracketsNewLineAfter'),
emptyLineBeforeSelector: config.get('emptyLineBeforeSelector'),
destination: config.get('destination'),
attributes: config.get('attributes'),
ignore: config.get('ignore'),
ignoreRegex: config.get('ignoreRegex').map(tryParseRegex).filter(Boolean),
bemNesting: config.get('bemNesting.enable'),
parentSymbol: config.get('bemNesting.parentSymbol'),
elementSeparator: config.get('bemNesting.elementSeparator'),
modifierSeparator: config.get('bemNesting.modifierSeparator'),
addComment: config.get('bemNesting.addComment'),
};
return { ...options, ...override };
}
function activate(context) {
const run = vscode.commands.registerCommand('ecsstractor-port.run', () => {
const options = getOptionts();
process(options);
});
const runWithBem = vscode.commands.registerCommand('ecsstractor-port.runwithbem', () => {
const override = {
addComment: false,
// brackets: true,
bemNesting: true,
};
const options = getOptionts(override);
process(options);
});
const runWithBemAndComments = vscode.commands.registerCommand('ecsstractor-port.runwithbemandcomments', () => {
const override = {
addComment: true,
// brackets: true,
bemNesting: true,
};
const options = getOptionts(override);
process(options);
});
const runWithoutBem = vscode.commands.registerCommand('ecsstractor-port.runwithoutbem', () => {
const override = {
addComment: false,
// brackets: false,
bemNesting: false,
};
const options = getOptionts(override);
process(options);
});
context.subscriptions.push(run);
context.subscriptions.push(runWithBem);
context.subscriptions.push(runWithBemAndComments);
context.subscriptions.push(runWithoutBem);
}
class TextLine {
constructor(text = '', level = 0, autoindent = true) {
this.text = text;
this.level = level;
this.autoindent = autoindent;
}
incIndent(inc = 1) {
const level = this.autoindent ? this.level + inc : this.level;
return new TextLine(this.text, level, this.autoindent);
}
getString(indentation = '') {
return indentation.repeat(this.level) + this.text;
}
}
function buildRuleLines(selector, comment, children = [], opts = {}) {
const output = [];
if (opts.emptyLineBeforeSelector) {
output.push(new TextLine('', 0, false));
}
if (comment) {
const isSCSS = opts.commentStyle === 'scss';
const commentOpen = isSCSS ? '// ' : '/* ';
const commentClose = isSCSS ? '' : ' */';
const text = commentOpen + comment + commentClose;
output.push(new TextLine(text));
}
const bracketOpen = opts.brackets ? ' {' : '';
const bracketClose = opts.brackets ? '}' : '';
if (children.length === 0) {
if (opts.bracketsNewLineAfter) {
output.push(new TextLine(selector + bracketOpen));
output.push(new TextLine(bracketClose));
} else {
output.push(new TextLine(selector + bracketOpen + bracketClose));
}
} else {
output.push(new TextLine(selector + bracketOpen));
children.forEach(s => output.push(s.incIndent()));
output.push(new TextLine(bracketClose));
}
return output;
}
function generateBEM(classesSet, opts) {
const {
indentation,
elementSeparator,
modifierSeparator,
parentSymbol,
addComment
} = opts;
const blocksMap = new Map();
// build map
for (const selector of classesSet) {
const isElement = selector.includes(elementSeparator);
if (isElement) {
const [bName, elRest] = selector.split(elementSeparator);
if (opts.ignore.includes(bName)) continue;
if (opts.ignoreRegex.some(r => r.test(bName))) continue;
const isExistBlock = blocksMap.has(bName);
const block = isExistBlock ? blocksMap.get(bName) : { name: bName, elements: new Map(), modifiers: new Set() };
if (!isExistBlock) blocksMap.set(bName, block);
// get element and its modifier
const [elName, elMod] = elRest.split(modifierSeparator);
const isExistElement = block.elements.has(elName);
const element = isExistElement ? block.elements.get(elName) : { name: elName, modifiers: new Set() };
if (!isExistElement) block.elements.set(elName, element);
if (elMod) {
element.modifiers.add(elMod);
}
} else {
const [bName, bMod] = selector.split(modifierSeparator);
if (opts.ignore.includes(bName)) continue;
if (opts.ignoreRegex.some(r => r.test(bName))) continue;
const isExistBlock = blocksMap.has(bName);
const block = isExistBlock ? blocksMap.get(bName) : { name: bName, elements: new Map(), modifiers: new Set() };
if (!isExistBlock) blocksMap.set(bName, block);
if (bMod) {
block.modifiers.add(bMod);
}
}
}
const indentedStrings = [];
for (const block of blocksMap.values()) {
const blockMods = [];
const blockEls = [];
for (const modifier of block.modifiers) {
const comment = addComment ? '.' + block.name + modifierSeparator + modifier : '';
const lines = buildRuleLines(parentSymbol + modifierSeparator + modifier, comment, [], opts);
blockMods.push(...lines);
}
for (const element of block.elements.values()) {
const elMods = [];
for (const modifier of element.modifiers) {
const comment = addComment ? '.' + block.name + elementSeparator + element.name + modifierSeparator + modifier : '';
const lines = buildRuleLines(parentSymbol + modifierSeparator + modifier, comment, [], opts);
elMods.push(...lines);
}
const comment = addComment ? '.' + block.name + elementSeparator + element.name : '';
const lines = buildRuleLines(parentSymbol + elementSeparator + element.name, comment, elMods, opts);
blockEls.push(...lines);
}
const comment = '';
const lines = buildRuleLines('.' + block.name, comment, [...blockMods, ...blockEls], opts);
indentedStrings.push(...lines);
}
return indentedStrings
.map(s => s.getString(indentation))
.join('\n');
}
function generateFlat(classesSet, opts) {
const indentation = opts.indentation;
const indentedStrings = [];
for (const className of classesSet) {
if (opts.ignore.includes(className)) continue;
if (opts.ignoreRegex.some(r => r.test(className))) continue;
const lines = buildRuleLines('.' + className, null, [], opts);
indentedStrings.push(...lines);
}
return indentedStrings
.map(s => s.getString(indentation))
.join('\n');
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;