@@ -148,15 +148,13 @@ function findConfig(config, file) {
148
148
149
149
function beautify ( ) {
150
150
151
- console . log ( 'Beautify!!!' ) ;
152
-
153
151
var text ;
154
152
var editor = atom . workspace . getActiveEditor ( ) ;
155
153
var isSelection = ! ! editor . getSelectedText ( ) ;
156
154
var softTabs = editor . softTabs ;
157
155
var tabLength = editor . getTabLength ( ) ;
158
156
159
- var beautifyOptions = {
157
+ var defaultOptions = {
160
158
'indent_size' : softTabs ? tabLength : 1 ,
161
159
'indent_char' : softTabs ? ' ' : '\t' ,
162
160
'indent_with_tabs' : ! softTabs
@@ -165,45 +163,29 @@ function beautify() {
165
163
// Look for .jsbeautifierrc in file and home path, check env variables
166
164
var editedFilePath = editor . getPath ( ) ;
167
165
168
- // Get the path to the config file
169
- var configPath = findConfig ( '.jsbeautifyrc' , editedFilePath ) ;
170
-
171
- var externalOptions ;
172
- if ( configPath ) {
173
- var strip = require ( 'strip-json-comments' ) ;
174
- try {
175
- externalOptions = JSON . parse ( strip ( fs . readFileSync ( configPath , {
176
- encoding : 'utf8'
177
- } ) ) ) ;
178
- } catch ( e ) {
166
+ function getConfig ( startPath ) {
167
+ // Get the path to the config file
168
+ var configPath = findConfig ( '.jsbeautifyrc' , startPath ) ;
169
+
170
+ var externalOptions ;
171
+ if ( configPath ) {
172
+ var strip = require ( 'strip-json-comments' ) ;
173
+ try {
174
+ externalOptions = JSON . parse ( strip ( fs . readFileSync ( configPath , {
175
+ encoding : 'utf8'
176
+ } ) ) ) ;
177
+ } catch ( e ) {
178
+ externalOptions = { } ;
179
+ }
180
+ } else {
179
181
externalOptions = { } ;
180
182
}
181
- } else {
182
- externalOptions = { } ;
183
- }
184
-
185
- var containsNested = false ;
186
- var collectedConfig = { } ;
187
- var key ;
188
-
189
- // Check to see if config file uses nested object format to split up js/css/html options
190
- for ( key in externalOptions ) {
191
- if ( typeof externalOptions [ key ] === 'object' ) {
192
- containsNested = true ;
193
- }
194
- }
195
-
196
- // Create a flat object of config options if nested format was used
197
- if ( ! containsNested ) {
198
- collectedConfig = externalOptions ;
199
- } else {
200
- for ( key in externalOptions ) {
201
- _ . merge ( collectedConfig , externalOptions [ key ] ) ;
202
- }
183
+ return externalOptions ;
203
184
}
204
185
205
- beautifyOptions = extend ( collectedConfig , beautifyOptions ) ;
206
- beautifyOptions = cleanOptions ( beautifyOptions , knownOpts ) ;
186
+ // Get the path to the config file
187
+ var projectOptions = getConfig ( editedFilePath ) ;
188
+ var homeOptions = getConfig ( getUserHome ( ) ) ;
207
189
208
190
if ( isSelection ) {
209
191
text = editor . getSelectedText ( ) ;
@@ -212,17 +194,57 @@ function beautify() {
212
194
}
213
195
var oldText = text ;
214
196
197
+ // All of the options
198
+ // Listed in order from default (base) to the one with the highest priority
199
+ // Left = Default, Right = Will override the left.
200
+ var allOptions = [ defaultOptions , homeOptions , projectOptions ] ;
201
+
202
+ function getOptions ( selection , allOptions ) {
203
+
204
+ // Reduce all options into correctly merged options.
205
+ var options = _ . reduce ( allOptions , function ( result , currOptions ) {
206
+
207
+ var containsNested = false ;
208
+ var collectedConfig = { } ;
209
+ var key ;
210
+
211
+ // Check to see if config file uses nested object format to split up js/css/html options
212
+ for ( key in currOptions ) {
213
+ if ( typeof currOptions [ key ] === 'object' ) {
214
+ containsNested = true ;
215
+ break ; // Found, break out of loop, no need to continue
216
+ }
217
+ }
218
+
219
+ // Create a flat object of config options if nested format was used
220
+ if ( ! containsNested ) {
221
+ collectedConfig = currOptions ;
222
+ } else {
223
+ // Merge with selected options
224
+ // this == `selected`, where `selected` could be `html`, `js`, 'css', etc
225
+ _ . merge ( collectedConfig , currOptions [ this ] ) ;
226
+ }
227
+
228
+ return extend ( result , collectedConfig ) ;
229
+
230
+ } , { } , selection ) ;
231
+
232
+ // Clean
233
+ options = cleanOptions ( options , knownOpts ) ;
234
+ return options ;
235
+ }
236
+
215
237
switch ( editor . getGrammar ( ) . name ) {
216
238
case 'JavaScript' :
217
- text = beautifyJS ( text , beautifyOptions ) ;
239
+ text = beautifyJS ( text , getOptions ( 'js' , allOptions ) ) ;
218
240
break ;
219
241
case 'HTML (Liquid)' :
220
242
case 'HTML' :
221
243
case 'XML' :
222
- text = beautifyHTML ( text , beautifyOptions ) ;
244
+ text = beautifyHTML ( text , getOptions ( 'html' , allOptions ) ) ;
223
245
break ;
224
246
case 'CSS' :
225
- text = beautifyCSS ( text , beautifyOptions ) ;
247
+ text = beautifyCSS ( text , getOptions ( 'css' , allOptions ) ) ;
226
248
break ;
227
249
default :
228
250
return ;
@@ -248,7 +270,7 @@ function beautify() {
248
270
}
249
271
}
250
272
251
- function handleSafeEvent ( ) {
273
+ function handleSaveEvent ( ) {
252
274
atom . workspace . eachEditor ( function ( editor ) {
253
275
var buffer = editor . getBuffer ( ) ;
254
276
plugin . unsubscribe ( buffer ) ;
@@ -265,9 +287,9 @@ plugin.configDefaults = {
265
287
} ;
266
288
267
289
plugin . activate = function ( ) {
268
- handleSafeEvent ( ) ;
290
+ handleSaveEvent ( ) ;
269
291
plugin . subscribe ( atom . config . observe (
270
292
'atom-beautify.beautifyOnSave' ,
271
- handleSafeEvent ) ) ;
293
+ handleSaveEvent ) ) ;
272
294
return atom . workspaceView . command ( 'beautify' , beautify ) ;
273
295
} ;
0 commit comments