2
2
* @file 格式化 单vue组件 文件
3
3
* @author fe_bean
4
4
*/
5
-
6
- const { window, Position, Range, workspace} = require ( 'vscode' ) ;
5
+ const {
6
+ window,
7
+ Position,
8
+ Range,
9
+ workspace
10
+ } = require ( 'vscode' ) ;
7
11
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' ) ;
13
14
let editor ;
14
15
15
16
let methods = {
16
17
doc : null ,
17
18
text : '' ,
18
19
newText : '' ,
19
20
lineCount : 1 ,
21
+ jsBeautifyConf : defaultConf [ 'js-beautify' ] ,
22
+ pugBeautifyConf : defaultConf [ 'pug-beautify' ] ,
23
+ editorConf : { } ,
20
24
init ( ) {
25
+ // 活动窗口tab
21
26
editor = window . activeTextEditor ;
22
27
if ( ! editor ) throw new Error ( 'no active editor' ) ;
28
+ // 当前窗口document
23
29
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
+ // 行数
27
34
this . lineCount = this . doc . lineCount ;
35
+ // 内容
28
36
this . text = this . doc . getText ( ) ;
37
+ // 每次执行清空格式化后的内容
29
38
this . newText = '' ;
39
+ // 分别处理 html(pug)、css、js
30
40
this . splitContent ( this . text ) ;
41
+ // 内容回写到文件
31
42
this . writeFile ( ) ;
32
43
} ,
33
44
splitContent ( text ) {
34
- let htmlText = text . match ( / [ \w \W ] + < \/ t e m p l a t e > \s ? / ) [ 0 ] ;
35
- let jsText = text . match ( / < s c r i p t [ \w \W ] + < \/ s c r i p t > \s ? / ) [ 0 ] ;
36
- let cssText = text . match ( / < s t y l e [ \w \W ] + < \/ s t y l e > \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 ] + < \/ t e m p l a t e > \s ? / ) ;
46
+ let jsText = text . match ( / < s c r i p t [ \w \W ] + < \/ s c r i p t > \s ? / ) ;
47
+ let cssText = text . match ( / < s t y l e [ \w \W ] + < \/ s t y l e > \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 ] ) : '' ;
40
52
this . newText = this . newText . replace ( / ( \n ) + $ / , '\n' ) ;
41
53
} ,
42
54
beautyHtml ( text ) {
43
- if ( ! text ) {
44
- throw new Error ( text ) ;
45
- }
46
55
let lang = this . getLang ( text ) ;
47
56
text = text . replace ( / < t e m p l a t e [ ^ > ] * > ( [ \w \W ] + ) < \/ t e m p l a t e > / , '$1' ) ;
48
- let str = beautify . html ( text , {
49
- indent_size : 4
50
- } ) ;
57
+ let tempConf = { } ;
58
+ let str = text ;
59
+ if ( / p u g / . 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
+ }
51
67
return `<template${ lang } >\n${ str } \n</template>\n\n` ;
52
68
} ,
53
69
beautyCss ( text ) {
54
- if ( ! text ) {
55
- throw new Error ( text ) ;
56
- }
57
70
let scoped = / < s t y l e [ ^ > ] * \s + s c o p e d / . test ( text ) ? ' scoped' : '' ;
58
71
let lang = this . getLang ( text ) ;
59
72
text = text . replace ( / < s t y l e [ ^ > ] * > ( [ \w \W ] + ) < \/ s t y l e > / , '$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 ) ;
63
75
return `<style${ lang } ${ scoped } >\n${ str } \n</style>\n\n` ;
64
76
} ,
65
77
beautyJs ( text ) {
66
- if ( ! text ) {
67
- throw new Error ( text ) ;
68
- }
69
78
let lang = this . getLang ( text ) ;
70
79
text = text . replace ( / < s c r i p t [ ^ > ] * > ( [ \w \W ] + ) < \/ s c r i p t > / , '$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 ) ;
77
82
return `<script${ lang } >\n${ str } \n</script>\n\n` ;
78
83
} ,
79
84
getLang ( text ) {
@@ -82,18 +87,54 @@ let methods = {
82
87
} ,
83
88
writeFile ( ) {
84
89
let start = new Position ( 0 , 0 ) ;
85
- let end = new Position ( this . lineCount , 0 ) ;
90
+ let end = new Position ( this . lineCount + 1 , 0 ) ;
86
91
let range = new Range ( start , end ) ;
87
92
editor . edit ( ( editBuilder , error ) => {
88
93
error && window . showErrorMessage ( error ) ;
89
94
editBuilder . replace ( range , this . newText ) ;
90
95
} ) ;
91
96
} ,
92
- config ( ) {
93
- const configuration = workspace . getConfiguration ( 'vue-format' ) ;
94
- return {
97
+ getConfig ( ) {
95
98
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 ( / e d i t o r \. ( \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 ] ;
97
138
}
98
139
} ;
99
140
0 commit comments