-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathlanguage-options.js
193 lines (175 loc) · 5.86 KB
/
language-options.js
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
/**
Language Support and default options.
*/
'use strict';
var _ = require('lodash');
var extend = require('extend');
// Language Beautifiers
var beautifyJS = require('js-beautify');
var beautifyHTML = require('js-beautify').html;
var beautifyCSS = require('js-beautify').css;
var beautifySQL = require('./langs/sql-beautify');
var beautifyPHP = require('./langs/php-beautify');
var beautifyPython = require('./langs/python-beautify');
var beautifyRuby = require('./langs/ruby-beautify');
var NA = require('nodealytics');
var pkg = require('../package.json');
module.exports = {
// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
languages: ['js', 'html', 'css', 'sql', 'php', 'python', 'ruby'],
// Default options per language
defaultLanguageOptions: {
/* jshint ignore: start */
// JavaScript
js_indent_size: 2,
js_indent_char: ' ',
js_indent_level: 0,
js_indent_with_tabs: false,
js_preserve_newlines: true,
js_max_preserve_newlines: 10,
js_jslint_happy: false,
js_brace_style: 'collapse',
js_keep_array_indentation: false,
js_keep_function_indentation: false,
js_space_before_conditional: true,
js_break_chained_methods: false,
js_eval_code: false,
js_unescape_strings: false,
js_wrap_line_length: 0,
// CSS
css_indent_size: 2,
css_indent_Char: ' ',
// HTML
html_indent_inner_html: false,
html_indent_size: 2,
html_indent_char: ' ',
html_brace_style: 'collapse',
html_indent_scripts: 'normal',
html_wrap_line_length: 250,
// SQL
sql_indent_size: 2,
sql_indent_char: ' ',
// PHP
php_beautifier_path: '',
// Python
python_autopep8_path: '',
python_max_line_length: 79,
python_indent_size: 4,
python_ignore: ['E24'],
// Ruby
ruby_rbeautify_path: ''
/* jshint ignore: end */
},
// Process each language
beautify: function (text, grammar, allOptions, beautifyCompleted) {
var self = this;
// Beautify!
var unsupportedGrammar = false;
switch (grammar) {
case 'JSON':
// Treat JSON as JavaScript, because it will support comments.
// And Glavin001 has tested JSON beauifying with beautifyJS.
case 'JavaScript':
text = beautifyJS(text, self.getOptions('js', allOptions));
beautifyCompleted(text);
break;
case 'Handlebars':
// jshint ignore: start
allOptions.push({
indent_handlebars: true // Force jsbeautify to indent_handlebars
});
// jshint ignore: end
case 'HTML (Liquid)':
case 'HTML':
case 'XML':
text = beautifyHTML(text, self.getOptions('html', allOptions));
beautifyCompleted(text);
break;
case 'Sass':
case 'SCSS':
case 'LESS':
case 'CSS':
text = beautifyCSS(text, self.getOptions('css', allOptions));
beautifyCompleted(text);
break;
case 'SQL (Rails)':
case 'SQL':
text = beautifySQL(text, self.getOptions('sql', allOptions));
beautifyCompleted(text);
break;
case 'PHP':
beautifyPHP(text, self.getOptions('php', allOptions), beautifyCompleted);
break;
case 'Python':
beautifyPython(text, self.getOptions('python', allOptions),
beautifyCompleted);
break;
case 'Ruby':
beautifyRuby(text, self.getOptions('ruby', allOptions), beautifyCompleted);
break;
default:
unsupportedGrammar = true;
}
// Google Analytics
if (atom.config.get('atom-beautify.googleAnalytics')) {
NA.initialize('UA-52729731-2', 'https://atom.io/packages/atom-beautify',
function () {
// category, action, label, value
NA.trackEvent('grammar', grammar, function (err, resp) {
// console.log(err, resp);
// if (!err && resp.statusCode === 200) {
// console.log('Event has been tracked with Google Analytics');
// }
});
NA.trackEvent('version', pkg.version, function (err, resp) {
// console.log(err, resp);
// if (!err && resp.statusCode === 200) {
// console.log('Event has been tracked with Google Analytics');
// }
});
if (unsupportedGrammar) {
NA.trackEvent('unsupportedGrammar', grammar, function (err, resp) {
//
});
}
});
}
},
getOptions: function (selection, allOptions) {
var self = this;
console.log(selection, allOptions);
// Reduce all options into correctly merged options.
var options = _.reduce(allOptions, function (result, currOptions) {
var containsNested = false;
var collectedConfig = {};
var key;
// Check to see if config file uses nested object format to split up js/css/html options
for (key in currOptions) {
if (_.indexOf(self.languages, key) >= 0 && // Check if is supported language
typeof currOptions[key] === 'object') { // Check if nested object (more options in value)
containsNested = true;
break; // Found, break out of loop, no need to continue
}
}
console.log(containsNested, currOptions);
// Create a flat object of config options if nested format was used
if (!containsNested) {
_.merge(collectedConfig, currOptions);
} else {
// Merge with selected options
// where `selection` could be `html`, `js`, 'css', etc
console.log(selection, currOptions[selection]);
_.merge(collectedConfig, currOptions[selection]);
}
return extend(result, collectedConfig);
}, {});
// TODO: Clean.
// There is a bug in nopt
// See https://github.com/npm/nopt/issues/38#issuecomment-45971505
console.log('pre-clean', JSON.stringify(options));
//options = cleanOptions(options, knownOpts);
//console.log('post-clean', JSON.stringify(options));
return options;
}
};