-
Notifications
You must be signed in to change notification settings - Fork 453
/
index.coffee
742 lines (565 loc) · 22.1 KB
/
index.coffee
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
_ = require('lodash')
_plus = require('underscore-plus')
Promise = require('bluebird')
Languages = require('../languages/')
path = require('path')
logger = require('../logger')(__filename)
# Lazy loaded dependencies
extend = null
Analytics = null
fs = null
strip = null
yaml = null
editorconfig = null
# Misc
{allowUnsafeEval} = require 'loophole'
allowUnsafeEval ->
Analytics = require("analytics-node")
pkg = require("../../package.json")
# Analytics
analyticsWriteKey = "u3c26xkae8"
###
Register all supported beautifiers
###
module.exports = class Beautifiers
###
List of beautifier names
To register a beautifier add it's name here
###
beautifierNames : [
'uncrustify'
'autopep8'
'coffee-formatter'
'coffee-fmt'
'htmlbeautifier'
'csscomb'
'gofmt'
'js-beautify'
'perltidy'
'php-cs-fixer'
'prettydiff'
'rubocop'
'ruby-beautify'
'sqlformat'
'tidy-markdown'
'typescript-formatter'
]
###
List of loaded beautifiers
Autogenerated in `constructor` from `beautifierNames`
###
beautifiers : null
###
All beautifier options
Autogenerated in `constructor`
###
options : null
###
Languages
###
languages : new Languages()
###
Constructor
###
constructor : ->
# Load beautifiers
@beautifiers = _.map( @beautifierNames, (name) ->
Beautifier = require("./#{name}")
new Beautifier()
)
# Build options from @beautifiers and @languages
@options = @buildOptionsForBeautifiers( @beautifiers)
buildOptionsForBeautifiers : (beautifiers) ->
# Get all Options for Languages
langOptions = {}
languages = {} # Hash map of languages with their names
for lang in @languages.languages
langOptions[lang.name] ?= {}
languages[lang.name] ?= lang
options = langOptions[lang.name]
# Init field for supported beautifiers
lang.beautifiers = []
# Process all language options
for field, op of lang.options
if not op.title?
op.title = _plus.uncamelcase(field).split('.')
.map(_plus.capitalize).join(' ')
op.title = "#{lang.name} - #{op.title}"
# Init field for supported beautifiers
op.beautifiers = []
# Add option
options[field] = op
# Find supported beautifiers for each language
for beautifier in beautifiers
beautifierName = beautifier.name
# Iterate over supported languages
for languageName, options of beautifier.options
laOp = langOptions[languageName]
# Is a valid Language name
if typeof options is "boolean"
# Enable / disable all options
# Add Beautifier support to Language
languages[languageName]?.beautifiers.push(beautifierName)
# Check for beautifier's options support
if options is true
# Beautifier supports all options for this language
if laOp
# logger.verbose('add supported beautifier', languageName, beautifierName)
for field, op of laOp
op.beautifiers.push(beautifierName)
else
# Supports language but no options specifically
logger.warn("Could not find options for language: #{languageName}")
else if typeof options is "object"
# Iterate over beautifier's options for this language
for field, op of options
if typeof op is "boolean"
# Transformation
if op is true
languages[languageName]?.beautifiers.push(beautifierName)
laOp?[field]?.beautifiers.push(beautifierName)
else if typeof op is "string"
# Rename
# logger.verbose('support option with rename:', field, op, languageName, beautifierName, langOptions)
languages[languageName]?.beautifiers.push(beautifierName)
laOp?[op]?.beautifiers.push(beautifierName)
else if typeof op is "function"
# Transformation
languages[languageName]?.beautifiers.push(beautifierName)
laOp?[field]?.beautifiers.push(beautifierName)
else if _.isArray(op)
# Complex Function
[fields..., fn] = op
# Add beautifier support to all required fields
languages[languageName]?.beautifiers.push(beautifierName)
for f in fields
# Add beautifier to required field
laOp?[f]?.beautifiers.push(beautifierName)
else
# Unsupported
logger.warn("Unsupported option:", beautifierName, languageName, field, op, langOptions)
# Prefix language's options with namespace
for langName, ops of langOptions
# Get language with name
lang = languages[langName]
# Use the namespace from language as key prefix
prefix = lang.namespace
# logger.verbose(langName, lang, prefix, ops)
# Iterate over all language options and rename fields
for field, op of ops
# Rename field
delete ops[field]
ops["#{prefix}_#{field}"] = op
# Flatten Options per language to array of all options
allOptions = _.values(langOptions)
# logger.verbose('allOptions', allOptions)
# Flatten array of objects to single object for options
flatOptions = _.reduce(allOptions, ((result, languageOptions, language) ->
# Iterate over fields (keys) in Language's Options
# and merge them into single result
# logger.verbose('language options', language, languageOptions, result)
return _.reduce(languageOptions, ((result, optionDef, optionName) ->
# TODO: Add supported beautifiers to option description
# logger.verbose('optionDef', optionDef, optionName)
if optionDef.beautifiers.length > 0
# optionDef.title = "
optionDef.description = "#{optionDef.description} (Supported by #{optionDef.beautifiers.join(', ')})"
else
# optionDef.title = "(DEPRECATED)
optionDef.description = "#{optionDef.description} (Not supported by any beautifiers)"
if result[optionName]?
logger.warn("Duplicate option detected: ", optionName, optionDef)
result[optionName] = optionDef
return result
), result)
), {})
# Generate Language configurations
# logger.verbose('languages', languages)
for langName, lang of languages
# logger.verbose(langName, lang)
name = lang.name
beautifiers = lang.beautifiers
optionName = "language_#{lang.namespace}"
# Add Language configurations
flatOptions["#{optionName}_disabled"] = {
title : "Language Config - #{name} - Disable Beautifying Language"
type : 'boolean'
default : false
description : "Disable #{name} Beautification"
}
flatOptions["#{optionName}_default_beautifier"] = {
title : "Language Config - #{name} - Default Beautifier"
type : 'string'
default : lang.defaultBeautifier ? beautifiers[0]
description : "Default Beautifier to be used for #{name}"
enum : _.uniq(beautifiers)
}
flatOptions["#{optionName}_beautify_on_save"] = {
title : "Language Config - #{name} - Beautify On Save"
type : 'boolean'
default : false
description : "Automatically beautify #{name} files on save"
}
# logger.verbose('flatOptions', flatOptions)
return flatOptions
###
From https://github.com/atom/notifications/blob/01779ade79e7196f1603b8c1fa31716aa4a33911/lib/notification-issue.coffee#L130
###
encodeURI : (str) ->
str = encodeURI(str)
str.replace(/#/g, '%23').replace(/;/g, '%3B')
getBeautifiers : (language, options) ->
# logger.verbose(@beautifiers)
_.filter( @beautifiers, (beautifier) ->
# logger.verbose('beautifier',beautifier, language)
_.contains(beautifier.languages, language)
)
beautify : (text, allOptions, grammar, filePath, {onSave} = {}) ->
return new Promise((resolve, reject)=>
logger.info('beautify', text, allOptions, grammar, filePath)
# Get language
fileExtension = path.extname(filePath)
languages = @languages.getLanguages({grammar, fileExtension})
# Check if unsupported language
if languages.length < 1
unsupportedGrammar = true
# Check if on save
if onSave
# Ignore this, as it was just a general file save, and
# not intended to be beautified
return resolve( null )
else
# TODO: select appropriate language
language = languages[0]
# Get language config
langDisabled = atom.config.get("atom-beautify.language_#{language.namespace}_disabled")
# Beautify!
unsupportedGrammar = false
# Check if Language is disabled
if langDisabled
return resolve( null )
# Get more language config
preferredBeautifierName = atom.config.get("atom-beautify.language_#{language.namespace}_default_beautifier")
beautifyOnSave = atom.config.get("atom-beautify.language_#{language.namespace}_beautify_on_save")
legacyBeautifyOnSave = atom.config.get("atom-beautify.beautifyOnSave")
# Verify if beautifying on save
if onSave and not (beautifyOnSave or legacyBeautifyOnSave)
# Saving, and beautify on save is disabled
return resolve( null )
# Options for Language
options = @getOptions(language.namespace, allOptions) or {}
# Support fallback for options
if language.fallback?
for fallback in language.fallback
# Merge current options on top of fallback options
options = _.merge( @getOptions(fallback, allOptions) or {}, options)
# Get Beautifier
logger.verbose(grammar, language)
beautifiers = @getBeautifiers(language.name, options)
# logger.verbose('beautifiers', beautifiers)
#
# Check if unsupported language
if beautifiers.length < 1
unsupportedGrammar = true
else
# Select beautifier from language config preferences
beautifier = _.find(beautifiers, (beautifier) ->
beautifier.name is preferredBeautifierName
) or beautifiers[0]
logger.verbose('beautifier', beautifier.name, beautifiers)
transformOptions = (beautifier, languageName, options) ->
# Transform options, if applicable
beautifierOptions = beautifier.options[languageName]
if typeof beautifierOptions is "boolean"
# Language is supported by beautifier
# If true then all options are directly supported
# If falsy then pass all options to beautifier,
# although no options are directly supported.
return options
else if typeof beautifierOptions is "object"
# Transform the options
transformedOptions = {}
# Transform for fields
for field, op of beautifierOptions
if typeof op is "string"
# Rename
transformedOptions[field] = options[op]
else if typeof op is "function"
# Transform
transformedOptions[field] = op(options[field])
else if typeof op is "boolean"
# Enable/Disable
if op is true
transformedOptions[field] = options[field]
else if _.isArray(op)
# Complex function
[fields..., fn] = op
vals = _.map(fields, (f) ->
return options[f]
)
# Apply function
transformedOptions[field] = fn.apply( null , vals)
# Replace old options with new transformed options
return transformedOptions
else
logger.warn("Unsupported Language options: ", beautifierOptions)
return options
# Apply language-specific option transformations
options = transformOptions(beautifier, language.name, options)
# Beautify text with language options
beautifier.beautify(text, language.name, options)
.then(resolve)
.catch(reject)
# Check if Analytics is enabled
if atom.config.get("atom-beautify.analytics")
# Setup Analytics
analytics = new Analytics(analyticsWriteKey)
unless atom.config.get("atom-beautify._analyticsUserId")
uuid = require("node-uuid")
atom.config.set "atom-beautify._analyticsUserId", uuid.v4()
# Setup Analytics User Id
userId = atom.config.get("atom-beautify._analyticsUserId")
analytics.identify userId : userId
version = pkg.version
analytics.track
userId : atom.config.get("atom-beautify._analyticsUserId")
event : "Beautify"
properties :
language : language?.name
grammar : grammar
extension : fileExtension
version : version
options : allOptions
label : language?.name
category : version
if unsupportedGrammar
if atom.config.get("atom-beautify.muteUnsupportedLanguageErrors")
return resolve( null )
else
repoBugsUrl = pkg.bugs.url
# issueTitle = "Add support for language with grammar '
# issueBody = """
#
# **Atom Version**:
# **Atom Beautify Version**:
# **Platform**:
#
# ```
#
# ```
#
# """
# requestLanguageUrl = "
# detail = "If you would like to request this language be supported please create an issue by clicking <a href=\"
title = "Atom Beautify could not find a supported beautifier for this file"
detail = """
Atom Beautify could not determine a supported beautifier to handle this file with grammar \"#{grammar}\" and extension \"#{fileExtension}\". \
If you would like to request support for this file and it's language, please create an issue for Atom Beautify at #{repoBugsUrl}
"""
atom?.notifications.addWarning(title, {
detail
dismissable : true
})
return resolve( null )
)
findFileResults : {}
# CLI
getUserHome : ->
process.env.HOME or process.env.HOMEPATH or process.env.USERPROFILE
verifyExists : (fullPath) ->
fs ?= require("fs")
( if fs.existsSync(fullPath) then fullPath else null )
# Storage for memoized results from find file
# Should prevent lots of directory traversal &
# lookups when liniting an entire project
###
Searches for a file with a specified name starting with
'dir' and going all the way up either until it finds the file
or hits the root.
@param {string} name filename to search for (e.g. .jshintrc)
@param {string} dir directory to start search from (default:
current working directory)
@param {boolean} upwards should recurse upwards on failure? (default: true)
@returns {string} normalized filename
###
findFile : (name, dir, upwards = true) ->
path ?= require("path")
dir = dir or process.cwd()
filename = path.normalize(path.join(dir, name))
return @findFileResults[filename] if @findFileResults[filename] isnt undefined
parent = path.resolve(dir, "../")
if @verifyExists(filename)
@findFileResults[filename] = filename
return filename
if dir is parent
@findFileResults[filename] = null
return null
if upwards
findFile name, parent
else
return null
###
Tries to find a configuration file in either project directory
or in the home directory. Configuration files are named
'.jsbeautifyrc'.
@param {string} config name of the configuration file
@param {string} file path to the file to be linted
@param {boolean} upwards should recurse upwards on failure? (default: true)
@returns {string} a path to the config file
###
findConfig : (config, file, upwards = true) ->
path ?= require("path")
dir = path.dirname(path.resolve(file))
envs = @getUserHome()
home = path.normalize(path.join(envs, config))
proj = @findFile(config, dir, upwards)
return proj if proj
return home if @verifyExists(home)
null
getConfigOptionsFromSettings : (langs) ->
config = atom.config.get('atom-beautify')
options = {}
# logger.verbose(langs, config);
# Iterate over keys of the settings
_.every _.keys(config), (k) ->
# Check if keys start with a language
p = k.split("_")[0]
idx = _.indexOf(langs, p)
# logger.verbose(k, p, idx);
if idx >= 0
# Remove the language prefix and nest in options
lang = langs[idx]
opt = k.replace( new RegExp("^" + lang + "_"), "")
options[lang] = options[lang] or {}
options[lang][opt] = config[k]
# logger.verbose(lang, opt);
true
# logger.verbose(options);
options
# Look for .jsbeautifierrc in file and home path, check env variables
getConfig : (startPath, upwards = true) ->
# Verify that startPath is a string
startPath = ( if ( typeof startPath is "string") then startPath else "")
return {} unless startPath
# Get the path to the config file
configPath = @findConfig(".jsbeautifyrc", startPath, upwards)
externalOptions = undefined
if configPath
fs ?= require("fs")
contents = fs.readFileSync(configPath,
encoding : "utf8"
)
unless contents
externalOptions = {}
else
try
strip ?= require("strip-json-comments")
externalOptions = JSON.parse(strip(contents))
catch e
# logger.verbose "Failed parsing config as JSON: " + configPath
# Attempt as YAML
try
yaml ?= require("yaml-front-matter")
externalOptions = yaml.safeLoad(contents)
catch e
logger.verbose "Failed parsing config as YAML and JSON: " + configPath
externalOptions = {}
else
externalOptions = {}
externalOptions
getOptionsForPath : (editedFilePath, editor) ->
languageNamespaces = @languages.namespaces
# Editor Options
editorOptions = {}
if editor?
# Get current Atom editor configuration
isSelection = !!editor.getSelectedText()
softTabs = editor.softTabs
tabLength = editor.getTabLength()
editorOptions =
indent_size : ( if softTabs then tabLength else 1)
indent_char : ( if softTabs then " " else "\t")
indent_with_tabs : not softTabs
# From Package Settings
configOptions = @getConfigOptionsFromSettings(languageNamespaces)
# Get configuration in User's Home directory
userHome = @getUserHome()
# FAKEFILENAME forces `path` to treat as file path and it's parent directory
# is the userHome. See implementation of findConfig
# and how path.dirname(DIRECTORY) returns the parent directory of DIRECTORY
homeOptions = @getConfig(path.join(userHome, "FAKEFILENAME"), false)
if editedFilePath?
# Handle EditorConfig options
# http://editorconfig.org/
editorconfig ?= require('editorconfig')
editorConfigOptions = editorconfig.parse(editedFilePath)
# Transform EditorConfig to Atom Beautify's config structure and naming
if editorConfigOptions.indent_style is 'space'
editorConfigOptions.indent_char = " "
# if (editorConfigOptions.indent_size)
# editorConfigOptions.indent_size = config.indent_size
else if editorConfigOptions.indent_style is 'tab'
editorConfigOptions.indent_char = "\t"
editorConfigOptions.indent_with_tabs = true
if (editorConfigOptions.tab_width)
editorConfigOptions.indent_size = editorConfigOptions.tab_width
# Get all options in configuration files from this directory upwards to root
projectOptions = []
p = path.dirname(editedFilePath)
# Check if p is root (top directory)
while p isnt path.resolve(p, "../")
# Get config for p
pf = path.join(p, "FAKEFILENAME")
pc = @getConfig(pf, false)
# Add config for p to project's config options
projectOptions.push(pc)
# logger.verbose p, pc
# Move upwards
p = path.resolve(p, "../")
else
editorConfigOptions = {}
projectOptions = []
# Combine all options together
allOptions = [
editorOptions
configOptions
homeOptions
editorConfigOptions
]
allOptions = allOptions.concat(projectOptions)
# logger.verbose(allOptions)
return allOptions
getOptions : (selection, allOptions) ->
self = this
_ ?= require("lodash")
extend ?= require("extend")
# logger.verbose(selection, allOptions);
# Reduce all options into correctly merged options.
options = _.reduce(allOptions, (result, currOptions) ->
containsNested = false
collectedConfig = {}
key = undefined
# Check to see if config file uses nested object format to split up js/css/html options
for key of currOptions
# Check if is supported language
if _.indexOf(self.languages.namespaces, key) >= 0 and typeof currOptions[key] is "object" # Check if nested object (more options in value)
containsNested = true
break # Found, break out of loop, no need to continue
# logger.verbose(containsNested, currOptions);
# Create a flat object of config options if nested format was used
unless containsNested
_.merge collectedConfig, currOptions
else
# Merge with selected options
# where `selection` could be `html`, `js`, 'css', etc
# logger.verbose(selection, currOptions[selection]);
_.merge collectedConfig, currOptions[selection]
extend result, collectedConfig
, {})
# TODO: Clean.
# There is a bug in nopt
# See https://github.com/npm/nopt/issues/38
# logger.verbose('pre-clean', JSON.stringify(options));
# options = cleanOptions(options, knownOpts);
# logger.verbose('post-clean', JSON.stringify(options));
options