Merge pull request #30 from Glavin001/master

More improvements
This commit is contained in:
László Károlyi 2014-06-15 13:56:53 +02:00
commit 0263976b07
9 changed files with 238 additions and 63 deletions

View File

@ -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). [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) 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). 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 ## License

View File

@ -1,31 +1,32 @@
{ ---
"html": { html:
"brace_style": "collapse", brace_style: "collapse"
"indent_char": " ", indent_char: " "
"indent_scripts": "normal", indent_scripts: "normal"
"indent_size": 6, indent_size: 6
"max_preserve_newlines": 1, max_preserve_newlines: 1
"preserve_newlines": true, preserve_newlines: true
"unformatted": ["a", "sub", "sup", "b", "i", "u"], unformatted:
"wrap_line_length": 0 - "a"
}, - "sub"
"css": { - "sup"
"indent_char": " ", - "b"
"indent_size": 4 - "i"
}, - "u"
"js": { wrap_line_length: 0
"indent_size": 2, css:
"indent_char": " ", indent_char: " "
"indent_level": 0, indent_size: 4
"indent_with_tabs": false, js:
"preserve_newlines": true, indent_size: 2
"max_preserve_newlines": 2, indent_char: " "
"jslint_happy": true indent_level: 0
}, indent_with_tabs: false
"sql": { preserve_newlines: true
"indent_size": 4, max_preserve_newlines: 2
"indent_char": " ", jslint_happy: true
"indent_level": 0, sql:
"indent_with_tabs": false indent_size: 4
} indent_char: " "
} indent_level: 0
indent_with_tabs: false

View File

@ -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
}
}

View File

@ -7,5 +7,6 @@
"preserve_newlines": true, "preserve_newlines": true,
"max_preserve_newlines": 2, "max_preserve_newlines": 2,
"jslint_happy": true, "jslint_happy": true,
"indent_handlebars": true "indent_handlebars": true,
"object": {}
} }

View File

@ -11,6 +11,8 @@ var path = require('path');
var nopt = require('nopt'); var nopt = require('nopt');
var extend = require('extend'); var extend = require('extend');
var _ = require('lodash'); 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 // TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = { var knownOpts = {
@ -151,39 +153,119 @@ function findConfig(config, file) {
return null; 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 text;
var editor = atom.workspace.getActiveEditor(); var editor = atom.workspace.getActiveEditor();
var isSelection = !!editor.getSelectedText(); var isSelection = !!editor.getSelectedText();
var softTabs = editor.softTabs; var softTabs = editor.softTabs;
var tabLength = editor.getTabLength(); var tabLength = editor.getTabLength();
var editorOptions = {
var defaultOptions = {
'indent_size': softTabs ? tabLength : 1, 'indent_size': softTabs ? tabLength : 1,
'indent_char': softTabs ? ' ' : '\t', 'indent_char': softTabs ? ' ' : '\t',
'indent_with_tabs': !softTabs 'indent_with_tabs': !softTabs
}; };
var configOptions = getConfigOptionsFromSettings(languages);
// Look for .jsbeautifierrc in file and home path, check env variables // Look for .jsbeautifierrc in file and home path, check env variables
var editedFilePath = editor.getPath(); var editedFilePath = editor.getPath();
function getConfig(startPath) { function getConfig(startPath) {
// Verify that startPath is a string // 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 // Get the path to the config file
var configPath = findConfig('.jsbeautifyrc', startPath); var configPath = findConfig('.jsbeautifyrc', startPath);
var externalOptions; var externalOptions;
if (configPath) { if (configPath) {
var strip = require('strip-json-comments'); var contents = fs.readFileSync(configPath, {
try {
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
encoding: 'utf8' encoding: 'utf8'
}))); });
} catch (e) { if (!contents) {
console.log('Failed parsing config JSON at '+configPath);
externalOptions = {}; 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 { } else {
externalOptions = {}; externalOptions = {};
@ -205,9 +287,15 @@ function beautify() {
// All of the options // All of the options
// Listed in order from default (base) to the one with the highest priority // Listed in order from default (base) to the one with the highest priority
// Left = Default, Right = Will override the left. // 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) { function getOptions(selection, allOptions) {
console.log(selection, allOptions);
// Reduce all options into correctly merged options. // Reduce all options into correctly merged options.
var options = _.reduce(allOptions, function (result, currOptions) { var options = _.reduce(allOptions, function (result, currOptions) {
@ -218,18 +306,22 @@ function beautify() {
// Check to see if config file uses nested object format to split up js/css/html options // Check to see if config file uses nested object format to split up js/css/html options
for (key in currOptions) { 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; containsNested = true;
break; // Found, break out of loop, no need to continue 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 // Create a flat object of config options if nested format was used
if (!containsNested) { if (!containsNested) {
_.merge(collectedConfig, currOptions); _.merge(collectedConfig, currOptions);
} else { } else {
// Merge with selected options // Merge with selected options
// where `selection` could be `html`, `js`, 'css', etc // where `selection` could be `html`, `js`, 'css', etc
console.log(selection, currOptions[selection]);
_.merge(collectedConfig, currOptions[selection]); _.merge(collectedConfig, currOptions[selection]);
} }
@ -240,7 +332,7 @@ function beautify() {
// TODO: Clean. // TODO: Clean.
// There is a bug in nopt // There is a bug in nopt
// See https://github.com/npm/nopt/issues/38#issuecomment-45971505 // 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); //options = cleanOptions(options, knownOpts);
//console.log('post-clean', JSON.stringify(options)); //console.log('post-clean', JSON.stringify(options));
@ -307,9 +399,9 @@ function handleSaveEvent() {
}); });
} }
plugin.configDefaults = { plugin.configDefaults = _.merge({
beautifyOnSave: false beautifyOnSave: false
}; }, defaultLanguageOptions);
plugin.activate = function () { plugin.activate = function () {
handleSaveEvent(); handleSaveEvent();

View File

@ -4,13 +4,61 @@
"version": "0.3.1", "version": "0.3.1",
"private": true, "private": true,
"description": "Beautify HTML, CSS and Javascript in Atom", "description": "Beautify HTML, CSS and Javascript in Atom",
"repository": "https://github.com/donaldpipowitch/atom-beautify", "repository": {
"type": "git",
"url": "https://github.com/donaldpipowitch/atom-beautify"
},
"bugs": {
"url": "https://github.com/donaldpipowitch/atom-beautify/issues"
},
"license": "MIT", "license": "MIT",
"author": { "author": {
"name": "Donald Pipowitch", "name": "Donald Pipowitch",
"email": "pipo@senaeh.de", "email": "pipo@senaeh.de",
"url": "https://github.com/donaldpipowitch" "url": "https://github.com/donaldpipowitch"
}, },
"contributors": [
{
"name": "László Károlyi",
"url": "https://github.com/karolyi"
},
{
"name": "Glavin Wiechert",
"email": "glavin.wiechert@gmail.com",
"url": "https://github.com/Glavin001"
},
{
"name": "Marco Tanzi",
"url": "https://github.com/mtanzi"
},
{
"name": "gvn lazar suntop",
"url": "https://github.com/gvn"
},
{
"name": "Vadim K.",
"url": "https://github.com/vadirn"
}
],
"keywords": [
"atom",
"beautify",
"beautifier",
"js-beautify",
"format",
"pretty",
"html",
"handlebars",
"xml",
"css",
"javascript",
"json",
"css",
"sass",
"scss",
"less",
"sql"
],
"engines": { "engines": {
"atom": ">0.50.0" "atom": ">0.50.0"
}, },
@ -20,6 +68,7 @@
"js-beautify": "~1.5.1", "js-beautify": "~1.5.1",
"nopt": "^3.0.0", "nopt": "^3.0.0",
"lodash": "2.4.1", "lodash": "2.4.1",
"strip-json-comments": "^0.1.3" "strip-json-comments": "^0.1.3",
"js-yaml": "^3.0.2"
} }
} }