Skip to content

Commit da0af99

Browse files
committed
version 0.1.0 , see changeLog
1 parent 4f1eba2 commit da0af99

File tree

10 files changed

+127
-26
lines changed

10 files changed

+127
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
2929

3030
### 0.0.13
3131
- 支持多 `style``script`标签格式化
32+
33+
### 0.1.0
34+
- 命令执行环境判断由 resourceLangId 改为 editorLangId,支持非 ".vue" 文件直接更改右下角语言为vue
35+
- 自行尝试修复 [issues#1](https://github.com/win7killer/vue-format/issues/1) 中涉及到的 js-beauty 格式化强制换行问题
36+
- 暂时停用 js-beautify.html.wrap_attributes 配置项(有上一条更改决定)
37+
- 增加 break_attr_limit 配置项,当tag的属性数量大于这个值,则该tag所有属性强制换行;-1时强制不换行

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
format single file with '.vue'.(格式化单组件“.vue”文件代码)
44
> If you have some issue, just let me know https://github.com/win7killer/vue-format
5+
6+
`Welcome to star && fork`
7+
8+
## Big Notice
9+
> 0.1.0 版本 尝试解决了 js-beauty 的标签换行导致缩进异常问题,如有问题,随时 issue
10+
511
## Features
612
<img src="https://raw.githubusercontent.com/win7killer/vue-format/master/images/command.gif" alt="command" width=600/>
713

@@ -16,6 +22,7 @@ format single file with '.vue'.(格式化单组件“.vue”文件代码)
1622
```json
1723
{
1824
"html_indent_root": false, // 是否缩进vue template中的根节点
25+
"break_attr_limit": -1, // tag 的 attrs 大于该数值时,强制 attrs 换行,-1时不换行
1926
"js-beautify": {
2027
"indent_size": "editor.tabSize",
2128
"indent_char": " ",
@@ -26,8 +33,6 @@ format single file with '.vue'.(格式化单组件“.vue”文件代码)
2633
"js": {},
2734
"html": {
2835
"force_format": ["template"],
29-
// tag 的多个 attribute 换行展示 [auto|force-expand-multiline|force|force-aligned]
30-
"wrap_attributes": "force-expand-multiline"
3136
}
3237
},
3338
"pug-beautify": {
@@ -36,6 +41,15 @@ format single file with '.vue'.(格式化单组件“.vue”文件代码)
3641
}
3742

3843
```
44+
45+
### 暂时停用该配置
46+
<del>// tag 的多个 attribute 换行展示 [auto|force-expand-multiline|force|force-aligned]</del>
47+
48+
<del>// js-beautify.html.wrap_attributes: "force-expand-multiline"</del>
49+
3950
### Change Log
4051
[CHANGELOG.md](./CHANGELOG.md)
4152

53+
### Todo List
54+
[todo.md](./todo.md)
55+

images/command.gif

175 KB
Loading

js-beautify.conf.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"html_indent_root": false,
3+
"break_attr_limit": -1,
34
"js-beautify": {
45
"indent_size": "editor.tabSize",
56
"indent_char": " ",

package-lock.json

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

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "vue-format",
33
"displayName": "vue-format",
44
"description": "A beautify extension for .vue file",
5-
"version": "0.0.13",
5+
"version": "0.1.0",
66
"publisher": "febean",
77
"engines": {
8-
"vscode": "^1.2.0"
8+
"vscode": "^1.12.0"
99
},
1010
"icon": "images/logo.png",
1111
"categories": [
@@ -29,6 +29,11 @@
2929
"default": false,
3030
"description": "If indent the root-tag in .vue's template(是否缩进vue组件template中的根节点)"
3131
},
32+
"vue-format.break_attr_limit": {
33+
"type": "number",
34+
"default": -1,
35+
"description": "Break attributes when tag's attributes.length > this number, no break when -1.(tag 的 attrs 大于该数值时,强制 attrs 换行,-1时不换行)"
36+
},
3237
"vue-format.js-beautify": {
3338
"type": "object",
3439
"default": {
@@ -42,8 +47,7 @@
4247
"html": {
4348
"force_format": [
4449
"template"
45-
],
46-
"wrap_attributes": "auto"
50+
]
4751
}
4852
},
4953
"description": "The config use some js-beautify options, see js-beautify.(使用js-beautify配置项,详情查看js-beautify)"
@@ -59,15 +63,15 @@
5963
},
6064
"commands": [
6165
{
62-
"when": "resourceLangId == vue",
66+
"when": "editorLangId == vue",
6367
"command": "extension.vueFormat",
6468
"title": "Vue Format"
6569
}
6670
],
6771
"menus": {
6872
"editor/context": [
6973
{
70-
"when": "resourceLangId == vue",
74+
"when": "editorLangId == vue",
7175
"command": "extension.vueFormat",
7276
"group": "navigation"
7377
}

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
} = require('vscode');
1111
const beautify = require('js-beautify');
1212
const pugBeautify = require('pug-beautify');
13+
const { breakTagAttr } = require('./plugins');
1314
let defaultConf = require('../js-beautify.conf.json');
1415

1516
let editor;
@@ -99,9 +100,12 @@ let methods = {
99100
.trim();
100101
} else {
101102
tempConf = Object.assign(tempConf, this.jsBeautifyConf, this.jsBeautifyConf.html);
103+
delete tempConf.wrap_attributes;
102104
str = beautify.html(text, tempConf);
103105
}
104-
106+
if (+this.vueFormatConf.break_attr_limit > -1) {
107+
str = breakTagAttr(str, +this.vueFormatConf.break_attr_limit);
108+
}
105109
return indentRoot ? `${str}\n\n` : `<template${lang}>\n${str}\n</template>\n\n`;
106110
},
107111
beautyCss(text) {

src/plugins.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
1-
const HtmlParseTag = require('html-parse-stringify');
2-
let methods = {
3-
init(str) {
4-
let res = this.format(str);
5-
return res;
6-
},
7-
format(str) {
8-
return HtmlParseTag.parse(str);
1+
/**
2+
* 强制多属性换行
3+
* @param {String} str 需要格式化的字符串
4+
* @param {Number} breakLimitNum 多余这个数量的属性,才会断行
5+
*/
6+
7+
function breakTagAttr(str = '', breakLimitNum = 1) {
8+
if (breakLimitNum === -1) {
9+
return str;
910
}
10-
};
11+
const TAG_REG = /[\n\r\t]*(\s*)\<[A-z\-\_0-9]+/;
12+
const TAG_END_REG = /\s*(>|\/>)/;
13+
14+
const ATTR_REG = /(\s(\:|\@)?[A-z0-9\-\_\.\:]+(=("[^"]+"|'[^']+'|`[^`]+`|[A-z0-9\_]+))?)/g;
15+
const TAG_ATTR_REG = new RegExp(TAG_REG.source + ATTR_REG.source + '+' + TAG_END_REG.source, 'g');
16+
const TAG_CLOSE_REG = new RegExp(TAG_END_REG.source + '$');
1117

12-
module.exports = methods;
18+
let loop = true;
19+
while (loop) {
20+
// 匹配带有属性的 start tag
21+
let res = TAG_ATTR_REG.exec(str);
22+
if (res) {
23+
// 缩进
24+
let indent = res[1];
25+
// tag 内容
26+
let tagContent = res[0];
27+
// console.log(tagContent + '\n\n');
28+
// 匹配 tagContent 的attr
29+
let matchRes = tagContent.match(ATTR_REG);
30+
// matchRes 处理
31+
// console.log(matchRes);
32+
if (matchRes.length > breakLimitNum) { // 一个属性强制断行,或者多属性
33+
// 每个 attr 先 trim,然后加换行,空格
34+
let newStr = tagContent.replace(ATTR_REG, (match, $1) => {
35+
return '\n' + indent + ' ' + $1.trim();
36+
});
37+
// tag 结束括号换行
38+
newStr = newStr.replace(TAG_CLOSE_REG, '\n' + indent + '$1');
39+
// 替换整个字符串中的对应内容
40+
str = str.replace(tagContent, newStr);
41+
}
42+
} else {
43+
// console.log('stop');
44+
loop = false;
45+
}
46+
}
47+
return str;
48+
}
49+
50+
module.exports = {
51+
breakTagAttr
52+
};

src/test.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const Plugins = require('./plugins');
1+
const { breakTagAttr } = require('./plugins');
22

33
let str = `
44
<template>
5-
<div v-show="isShow" v-transfer-dom >
6-
<x-dialog class="lm-dialog sssss" v-model="show" >
5+
<div v-show="isShow" v-transfer-dom>
6+
<x-dialog class="lm-dialog sssss" v-model="show">
77
<div class="dialog-content-wrapper">
88
<div class="dialog-title"></div>
99
<slot name="dialog-content">
@@ -20,6 +20,37 @@ let str = `
2020
</template>
2121
`;
2222

23-
let str1 = Plugins.init(str);
23+
let str1 = `
24+
<template>
25+
<div v-if="{
26+
ss: 1 > 0
27+
}" :style="{
28+
29+
}" :class="{
30+
show: 1 > 2
31+
}" :ccccc='[
32+
"haha"
33+
]' data-text=">>>>>>>>>>>" @click="clickFn" class="article" data-id=123 ff="><<><><><">
34+
<ArticleList :data="123" name="cccss" class="go"></ArticleList>
35+
<p id="sdsd" name="cc"><span></span></p>
36+
<div class="class">shit</div>
37+
<div class="test" :data-cc="getCC">{{getCC.replace(/\s/, '')}}</div>
38+
<div class="test" :data-cc="getCC">{{getCC > 123 ? 'sss' : 'dddd'}}</div>
39+
40+
<div class="left clearfix" id="haha">
41+
<ul class="clearfix">
42+
<li v-for="(item, index) of list" :key="index" :data="item" class="item">{{test}}</li>
43+
</ul>
44+
</div>
45+
<x-man class="sdsd" id="123"></x-man>
46+
</div>
47+
</template>
48+
`;
49+
50+
let str2 = `
51+
<x-man class="sdsd" id="123"></x-man>
52+
`;
53+
54+
let res = breakTagAttr(str1, 1);
2455

25-
console.log(`\n`, JSON.stringify(str1, null, 2));
56+
console.log(res);

todo.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
none
1+
# todo list
2+
- 升级到 ts

0 commit comments

Comments
 (0)