Skip to content

Commit

Permalink
Merge pull request #30 from Glavin001/master
Browse files Browse the repository at this point in the history
More improvements
  • Loading branch information
karolyi committed Jun 15, 2014
2 parents b9d7869 + 479034d commit 0263976
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 63 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ See [examples/nested-jsbeautifyrc/.jsbeautifyrc](https://github.com/donaldpipowi

[See all contributors on GitHub](https://github.com/donaldpipowitch/atom-beautify/graphs/contributors).

Please update the [CHANGELOG.md](https://github.com/donaldpipowitch/atom-beautify/blob/master/CHANGELOG.md)
file and submit a [Pull Request on GitHub](https://help.github.com/articles/using-pull-requests).
Please update the [CHANGELOG.md](https://github.com/donaldpipowitch/atom-beautify/blob/master/CHANGELOG.md),
add yourself as a contributor to the [package.json](https://github.com/donaldpipowitch/atom-beautify/blob/master/package.json),
and submit a [Pull Request on GitHub](https://help.github.com/articles/using-pull-requests).

## License

Expand Down
63 changes: 32 additions & 31 deletions examples/nested-jsbeautifyrc/.jsbeautifyrc
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"html": {
"brace_style": "collapse",
"indent_char": " ",
"indent_scripts": "normal",
"indent_size": 6,
"max_preserve_newlines": 1,
"preserve_newlines": true,
"unformatted": ["a", "sub", "sup", "b", "i", "u"],
"wrap_line_length": 0
},
"css": {
"indent_char": " ",
"indent_size": 4
},
"js": {
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true
},
"sql": {
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false
}
}
---
html:
brace_style: "collapse"
indent_char: " "
indent_scripts: "normal"
indent_size: 6
max_preserve_newlines: 1
preserve_newlines: true
unformatted:
- "a"
- "sub"
- "sup"
- "b"
- "i"
- "u"
wrap_line_length: 0
css:
indent_char: " "
indent_size: 4
js:
indent_size: 2
indent_char: " "
indent_level: 0
indent_with_tabs: false
preserve_newlines: true
max_preserve_newlines: 2
jslint_happy: true
sql:
indent_size: 4
indent_char: " "
indent_level: 0
indent_with_tabs: false
31 changes: 31 additions & 0 deletions examples/nested-jsbeautifyrc/.jsbeautifyrc_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"html": {
"brace_style": "collapse",
"indent_char": " ",
"indent_scripts": "normal",
"indent_size": 6,
"max_preserve_newlines": 1,
"preserve_newlines": true,
"unformatted": ["a", "sub", "sup", "b", "i", "u"],
"wrap_line_length": 0
},
"css": {
"indent_char": " ",
"indent_size": 4
},
"js": {
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true
},
"sql": {
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false
}
}
10 changes: 5 additions & 5 deletions examples/nested-jsbeautifyrc/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<html>

<head>
<title>Test Page</title>
<title>Test Page</title>
</head>

<body>
<h1>Hello</h1>
<p>
World!
</p>
<h1>Hello</h1>
<p>
World!
</p>
</body>

</html>
2 changes: 1 addition & 1 deletion examples/nested-jsbeautifyrc/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function hell() {
console.log('world');
console.log('world');
}
3 changes: 2 additions & 1 deletion examples/simple-jsbeautifyrc/.jsbeautifyrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true,
"indent_handlebars": true
"indent_handlebars": true,
"object": {}
}
10 changes: 5 additions & 5 deletions examples/simple-jsbeautifyrc/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<html>

<head>
<title>Test Page</title>
<title>Test Page</title>
</head>

<body>
<h1>Hello</h1>
<p>
World!
</p>
<h1>Hello</h1>
<p>
World!
</p>
</body>

</html>
124 changes: 108 additions & 16 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var path = require('path');
var nopt = require('nopt');
var extend = require('extend');
var _ = require('lodash');
var strip = require('strip-json-comments');
var yaml = require('js-yaml');

// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
Expand Down Expand Up @@ -151,39 +153,119 @@ 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();

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);

var externalOptions;
if (configPath) {
var strip = require('strip-json-comments');
try {
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
encoding: 'utf8'
})));
} catch (e) {
console.log('Failed parsing config JSON at '+configPath);
var contents = fs.readFileSync(configPath, {
encoding: 'utf8'
});
if (!contents) {
externalOptions = {};
} else {
try {
externalOptions = JSON.parse(strip(contents));
} catch (e) {
console.log('Failed parsing config as JSON: ' + configPath);

// Attempt as YAML
try {
externalOptions = yaml.safeLoad(contents);
} catch (e) {
console.log('Failed parsing config as YAML: ' + configPath);
externalOptions = {};
}

}
}
} else {
externalOptions = {};
Expand All @@ -205,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) {
Expand All @@ -218,18 +306,22 @@ function beautify() {

// Check to see if config file uses nested object format to split up js/css/html options
for (key in currOptions) {
if (typeof currOptions[key] === 'object') {
if (_.indexOf(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]);
}

Expand All @@ -240,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));

Expand Down Expand Up @@ -307,9 +399,9 @@ function handleSaveEvent() {
});
}

plugin.configDefaults = {
plugin.configDefaults = _.merge({
beautifyOnSave: false
};
}, defaultLanguageOptions);

plugin.activate = function () {
handleSaveEvent();
Expand Down
Loading

0 comments on commit 0263976

Please sign in to comment.