Skip to content

Commit 3fe44d5

Browse files
committed
0.0.6 add config
1 parent 4c88b73 commit 3fe44d5

File tree

5 files changed

+134
-79
lines changed

5 files changed

+134
-79
lines changed

extension.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ const formatInit = require('./src');
77
function activate(context) {
88
// 注册命令,该命令和package.json中命令一致
99
let disposable = vscode.commands.registerCommand('extension.vueFormat', function () {
10-
formatInit.init();
10+
let acEditor = vscode.window.activeTextEditor;
11+
12+
if (acEditor && acEditor.document.languageId === 'vue') {
13+
formatInit.init();
14+
} else {
15+
vscode.window.showInformationMessage('It‘s not a .vue file');
16+
}
1117
// vscode.window.showInformationMessage('vue format now');
1218
});
1319

js-beautify.conf.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{
2-
"indent_size": 4,
3-
"indent_char": " ",
4-
"space_after_anon_function": true,
5-
"keep_array_indentation": true,
6-
"css": {},
7-
"js": {},
8-
"html": {}
2+
"js-beautify": {
3+
"indent_size": "editor.tabSize",
4+
"indent_char": " ",
5+
"indent_with_tabs": false,
6+
"brace-style": "collapse",
7+
"space_after_anon_function": true,
8+
"css": {},
9+
"js": {},
10+
"html": {}
11+
},
12+
"pug-beautify": {
13+
"fill_tab": false
14+
}
915
}

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vue-format",
33
"displayName": "vue-format",
44
"description": "",
5-
"version": "0.0.5",
5+
"version": "0.0.6",
66
"publisher": "febean",
77
"engines": {
88
"vscode": "^1.22.0"
@@ -19,49 +19,45 @@
1919
"url": "https://github.com/win7killer/vue-format"
2020
},
2121
"main": "./extension",
22-
2322
"contributes": {
2423
"configuration": {
2524
"type": "object",
2625
"title": "vue-format",
2726
"properties": {
28-
"vue-format.config": {
27+
"vue-format.js-beautify": {
2928
"type": "object",
3029
"default": {
31-
"indent_size": 4,
30+
"indent_size": "editor.tabSize",
3231
"indent_char": " ",
3332
"indent_with_tabs": false,
33+
"brace-style": "collapse",
3434
"space_after_anon_function": true,
35-
"css": {
36-
"indent_size": 4
37-
},
38-
"js": {
39-
"indent_size": 4,
40-
"brace-style": "collapse"
41-
},
42-
"html": {
43-
"indent_size": 4
44-
}
35+
"css": {},
36+
"js": {},
37+
"html": {}
4538
},
4639
"description": "The config use some js-beautify options, see js-beautify.(使用js-beautify配置项,详情查看js-beautify)"
40+
},
41+
"vue-format.pug-beautify": {
42+
"type": "object",
43+
"default": {
44+
"fill_tab": false
45+
},
46+
"description": "The config use some pug-beautify options, see pug-beautify.(使用pug-beautify配置项,详情查看pug-beautify)"
4747
}
4848
}
4949
},
50-
"commands": [
51-
{
50+
"commands": [{
51+
"when": "resourceLangId == vue",
52+
"command": "extension.vueFormat",
53+
"title": "Vue Format"
54+
}],
55+
"menus": {
56+
"editor/context": [{
5257
"when": "resourceLangId == vue",
5358
"command": "extension.vueFormat",
54-
"title": "Vue Format"
55-
}
56-
],
57-
"menus": {
58-
"editor/context": [
59-
{
60-
"when": "resourceLangId == vue",
61-
"command": "extension.vueFormat",
62-
"group": "navigation"
63-
}
64-
]
59+
"group": "navigation"
60+
}]
6561
}
6662
},
6763
"scripts": {
@@ -76,6 +72,7 @@
7672
"@types/mocha": "^2.2.42"
7773
},
7874
"dependencies": {
79-
"js-beautify": "^1.7.5"
75+
"js-beautify": "^1.7.5",
76+
"pug-beautify": "^0.1.1"
8077
}
8178
}

src/index.js

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,83 @@
22
* @file 格式化 单vue组件 文件
33
* @author fe_bean
44
*/
5-
6-
const {window, Position, Range, workspace} = require('vscode');
5+
const {
6+
window,
7+
Position,
8+
Range,
9+
workspace
10+
} = require('vscode');
711
const beautify = require('js-beautify');
8-
9-
// let terminal = window.createTerminal({name: "xmake"});
10-
// terminal.show(true);
11-
// terminal.sendText("xmake");
12-
12+
const pugBeautify = require('pug-beautify');
13+
let defaultConf = require('../js-beautify.conf.json');
1314
let editor;
1415

1516
let methods = {
1617
doc: null,
1718
text: '',
1819
newText: '',
1920
lineCount: 1,
21+
jsBeautifyConf: defaultConf['js-beautify'],
22+
pugBeautifyConf: defaultConf['pug-beautify'],
23+
editorConf: {},
2024
init() {
25+
// 活动窗口tab
2126
editor = window.activeTextEditor;
2227
if (!editor) throw new Error('no active editor');
28+
// 当前窗口document
2329
this.doc = editor.document;
24-
// let conf = workspace.getConfiguration('vue-format');
25-
// let ss = typeof conf.config + '';
26-
// console();
30+
// 获取配置
31+
this.getConfig();
32+
33+
// 行数
2734
this.lineCount = this.doc.lineCount;
35+
// 内容
2836
this.text = this.doc.getText();
37+
// 每次执行清空格式化后的内容
2938
this.newText = '';
39+
// 分别处理 html(pug)、css、js
3040
this.splitContent(this.text);
41+
// 内容回写到文件
3142
this.writeFile();
3243
},
3344
splitContent(text) {
34-
let htmlText = text.match(/[\w\W]+<\/template>\s?/)[0];
35-
let jsText = text.match(/<script[\w\W]+<\/script>\s?/)[0];
36-
let cssText = text.match(/<style[\w\W]+<\/style>\s?/)[0];
37-
this.newText += this.beautyHtml(htmlText);
38-
this.newText += this.beautyJs(jsText);
39-
this.newText += this.beautyCss(cssText);
45+
let htmlText = text.match(/[\w\W]+<\/template>\s?/);
46+
let jsText = text.match(/<script[\w\W]+<\/script>\s?/);
47+
let cssText = text.match(/<style[\w\W]+<\/style>\s?/);
48+
49+
this.newText += htmlText ? this.beautyHtml(htmlText[0]) : '';
50+
this.newText += jsText ? this.beautyJs(jsText[0]) : '';
51+
this.newText += cssText ? this.beautyCss(cssText[0]) : '';
4052
this.newText = this.newText.replace(/(\n)+$/, '\n');
4153
},
4254
beautyHtml(text) {
43-
if (!text) {
44-
throw new Error(text);
45-
}
4655
let lang = this.getLang(text);
4756
text = text.replace(/<template[^>]*>([\w\W]+)<\/template>/, '$1');
48-
let str = beautify.html(text, {
49-
indent_size: 4
50-
});
57+
let tempConf = {};
58+
let str = text;
59+
if (/pug/.test(lang)) {
60+
str = pugBeautify(text, this.pugBeautifyConf)
61+
.trim()
62+
;
63+
} else {
64+
tempConf = Object.assign({}, this.jsBeautifyConf, this.jsBeautifyConf.html);
65+
str = beautify.html(text, tempConf);
66+
}
5167
return `<template${lang}>\n${str}\n</template>\n\n`;
5268
},
5369
beautyCss(text) {
54-
if (!text) {
55-
throw new Error(text);
56-
}
5770
let scoped = /<style[^>]*\s+scoped/.test(text) ? ' scoped' : '';
5871
let lang = this.getLang(text);
5972
text = text.replace(/<style[^>]*>([\w\W]+)<\/style>/, '$1');
60-
let str = beautify.css(text, {
61-
indent_size: 4
62-
});
73+
let tempConf = Object.assign({}, this.jsBeautifyConf, this.jsBeautifyConf.css);
74+
let str = beautify.css(text, tempConf);
6375
return `<style${lang}${scoped}>\n${str}\n</style>\n\n`;
6476
},
6577
beautyJs(text) {
66-
if (!text) {
67-
throw new Error(text);
68-
}
6978
let lang = this.getLang(text);
7079
text = text.replace(/<script[^>]*>([\w\W]+)<\/script>/, '$1');
71-
let str = beautify.js(text, {
72-
indent_size: 4,
73-
space_after_anon_function: true,
74-
keep_array_indentation: true
75-
});
76-
80+
let tempConf = Object.assign({}, this.jsBeautifyConf, this.jsBeautifyConf.js);
81+
let str = beautify.js(text, tempConf);
7782
return `<script${lang}>\n${str}\n</script>\n\n`;
7883
},
7984
getLang(text) {
@@ -82,18 +87,54 @@ let methods = {
8287
},
8388
writeFile() {
8489
let start = new Position(0, 0);
85-
let end = new Position(this.lineCount, 0);
90+
let end = new Position(this.lineCount + 1, 0);
8691
let range = new Range(start, end);
8792
editor.edit((editBuilder, error) => {
8893
error && window.showErrorMessage(error);
8994
editBuilder.replace(range, this.newText);
9095
});
9196
},
92-
config() {
93-
const configuration = workspace.getConfiguration('vue-format');
94-
return {
97+
getConfig() {
9598

96-
};
99+
this.editorConf = Object.assign({}, workspace.getConfiguration('editor'));
100+
this.initDefaultJsBConf(this.jsBeautifyConf);
101+
let vueFormatConf = workspace.getConfiguration('vue-format');
102+
if (!vueFormatConf) {
103+
return;
104+
}
105+
let jsBConf = vueFormatConf.get('js-beautify') || {};
106+
this.mergeBeautifyConf(jsBConf, 'jsBeautifyConf');
107+
let pugBConf = vueFormatConf.get('pug-beautify') || {};
108+
this.mergeBeautifyConf(pugBConf, 'pugBeautifyConf');
109+
},
110+
initDefaultJsBConf(conf) {
111+
this.jsBeautifyConf.indent_size = this.editorConf.tabSize;
112+
if (this.editorConf.insertSpaces) {
113+
this.jsBeautifyConf.indent_char = ' ';
114+
} else {
115+
this.indent_with_tabs = true;
116+
}
117+
},
118+
mergeBeautifyConf(conf, type) {
119+
for (let k in conf) {
120+
// if (!this[type][k]) {
121+
// continue;
122+
// }
123+
let cont = conf[k];
124+
if (typeof cont === 'string') {
125+
let teMatch = cont.match(/editor\.(\w+)/g);
126+
if (teMatch) {
127+
let editKey = teMatch[0].replace('editor.', '');
128+
cont = this.editorConf[editKey];
129+
}
130+
}
131+
if (cont instanceof Object) {
132+
Object.assign(this[type][k], cont);
133+
} else {
134+
this[type][k] = cont;
135+
}
136+
}
137+
return this[type];
97138
}
98139
};
99140

0 commit comments

Comments
 (0)