Closes #22. Use Atom Package Settings as configuration options.

- Fix issue for beautifying unsaved files.
This commit is contained in:
Glavin Wiechert 2014-06-15 01:03:01 -03:00
parent 78e0382c15
commit 479034dae7
2 changed files with 86 additions and 13 deletions

View File

@ -1,3 +1,3 @@
function hell() {
console.log('world');
console.log('world');
}

View File

@ -153,30 +153,94 @@ function findConfig(config, file) {
return null;
}
function beautify() {
// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
var languages = ['js', 'html', 'css', 'sql'];
var 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: ' '
/* jshint ignore: end */
};
function getConfigOptionsFromSettings(langs) {
var config = atom.config.getSettings()['atom-beautify'];
var options = {};
// console.log(langs, config);
// Iterate over keys of the settings
_.every(_.keys(config), function (k) {
// Check if keys start with a language
var p = k.split('_')[0];
var idx = _.indexOf(langs, p);
// console.log(k, p, idx);
if (idx >= 0) {
// Remove the language prefix and nest in options
var lang = langs[idx];
var opt = k.replace(new RegExp('^' + lang + '_'), '');
options[lang] = options[lang] || {};
options[lang][opt] = config[k];
// console.log(lang, opt);
}
return true;
});
// console.log(options);
return options;
}
function beautify() {
var text;
var editor = atom.workspace.getActiveEditor();
var isSelection = !!editor.getSelectedText();
var softTabs = editor.softTabs;
var tabLength = editor.getTabLength();
// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
var languages = ['js', 'html', 'css', 'sql'];
var defaultOptions = {
var editorOptions = {
'indent_size': softTabs ? tabLength : 1,
'indent_char': softTabs ? ' ' : '\t',
'indent_with_tabs': !softTabs
};
var configOptions = getConfigOptionsFromSettings(languages);
// Look for .jsbeautifierrc in file and home path, check env variables
var editedFilePath = editor.getPath();
function getConfig(startPath) {
// Verify that startPath is a string
startPath = (typeof startPath === 'string') ? startPath : __dirname || '';
startPath = (typeof startPath === 'string') ? startPath : '';
if (!startPath) {
return {};
}
// Get the path to the config file
var configPath = findConfig('.jsbeautifyrc', startPath);
@ -223,9 +287,15 @@ function beautify() {
// All of the options
// Listed in order from default (base) to the one with the highest priority
// Left = Default, Right = Will override the left.
var allOptions = [defaultOptions, homeOptions, projectOptions];
var allOptions = [
editorOptions, // Atom Editor
configOptions, //
homeOptions, // User's Home path
projectOptions // Project path
];
function getOptions(selection, allOptions) {
console.log(selection, allOptions);
// Reduce all options into correctly merged options.
var options = _.reduce(allOptions, function (result, currOptions) {
@ -243,12 +313,15 @@ function beautify() {
}
}
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]);
}
@ -259,7 +332,7 @@ function beautify() {
// 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));
console.log('pre-clean', JSON.stringify(options));
//options = cleanOptions(options, knownOpts);
//console.log('post-clean', JSON.stringify(options));
@ -326,9 +399,9 @@ function handleSaveEvent() {
});
}
plugin.configDefaults = {
plugin.configDefaults = _.merge({
beautifyOnSave: false
};
}, defaultLanguageOptions);
plugin.activate = function () {
handleSaveEvent();