Closes #25. Add Ruby support.

This commit is contained in:
Glavin Wiechert 2014-06-16 00:09:12 -03:00
parent 612c7dc17c
commit 88651447a7
10 changed files with 229 additions and 143 deletions

View File

@ -22,10 +22,11 @@ Atom Package: https://atom.io/packages/atom-beautify
- [x] [Python](https://github.com/donaldpipowitch/atom-beautify/issues/24) - [x] [Python](https://github.com/donaldpipowitch/atom-beautify/issues/24)
- Requires [autopep8](https://github.com/hhatto/autopep8) to be already installed. - Requires [autopep8](https://github.com/hhatto/autopep8) to be already installed.
- Beautifies to [PEP 8](http://legacy.python.org/dev/peps/pep-0008/). - Beautifies to [PEP 8](http://legacy.python.org/dev/peps/pep-0008/).
- [x] [Ruby](https://github.com/donaldpipowitch/atom-beautify/issues/25)
- Requires [RBeautify](https://github.com/erniebrodeur/ruby-beautify)
### Coming Soon ### Coming Soon
- [ ] Ruby, see https://github.com/donaldpipowitch/atom-beautify/issues/25
- [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31 - [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31

View File

@ -1,3 +1,3 @@
p { p {
color: red; color: red;
} }

View File

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

View File

@ -0,0 +1,25 @@
#!/usr/bin/env ruby
conn_hash = { :hosts => [
{:login => login, :passcode => passcode, :host => host, :port => port},
],
:reliable => false, # Override default
:connect_headers => conn_hdrs,
}
hash = { :hosts => [
{:login => user, :passcode => password, :host => 'noonehome', :port => 2525},
{:login => user, :passcode => password, :host => host, :port => port},
],
:logger => mylog, # This enables callback logging!
:max_reconnect_attempts => 5,
}

View File

@ -2,21 +2,18 @@
'use strict'; 'use strict';
var plugin = module.exports;
// Dependencies // Dependencies
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var nopt = require('nopt'); var nopt = require('nopt');
var extend = require('extend');
var _ = require('lodash'); var _ = require('lodash');
var strip = require('strip-json-comments'); var strip = require('strip-json-comments');
var yaml = require('js-yaml'); var yaml = require('js-yaml');
// Language Beautifiers // Language options
var beautifyJS = require('js-beautify'); var beautifier = require('./language-options');
var beautifyHTML = require('js-beautify').html; var languages = beautifier.langauges;
var beautifyCSS = require('js-beautify').css; var defaultLanguageOptions = beautifier.defaultLanguageOptions;
var beautifySQL = require('./langs/sql-beautify');
var beautifyPHP = require('./langs/php-beautify');
var beautifyPython = require('./langs/python-beautify');
// 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 = {
@ -54,7 +51,6 @@ var knownOpts = {
}; };
var Subscriber = require('emissary').Subscriber; var Subscriber = require('emissary').Subscriber;
var plugin = module.exports;
Subscriber.extend(plugin); Subscriber.extend(plugin);
function getUserHome() { function getUserHome() {
@ -157,47 +153,6 @@ function findConfig(config, file) {
return null; return null;
} }
// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
var languages = ['js', 'html', 'css', 'sql', 'php', 'python'];
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: ' ',
// PHP
php_beautifier_path: '',
// Python
python_autopep8_path: ''
/* jshint ignore: end */
};
function getConfigOptionsFromSettings(langs) { function getConfigOptionsFromSettings(langs) {
var config = atom.config.getSettings()['atom-beautify']; var config = atom.config.getSettings()['atom-beautify'];
var options = {}; var options = {};
@ -295,57 +250,12 @@ function beautify() {
// 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 = [ var allOptions = [
editorOptions, // Atom Editor editorOptions, // Atom Editor
configOptions, // configOptions, //
homeOptions, // User's Home path homeOptions, // User's Home path
projectOptions // Project 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) {
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(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;
}
// Asynchronously and callback-style // Asynchronously and callback-style
function beautifyCompleted(text) { function beautifyCompleted(text) {
if (oldText !== text) { if (oldText !== text) {
@ -369,43 +279,9 @@ function beautify() {
} }
} }
switch (editor.getGrammar().name) { // Finally, beautify!
case 'JSON': beautifier.beautify(text, editor.getGrammar().name, allOptions, beautifyCompleted);
// Treat JSON as JavaScript, because it will support comments.
// And Glavin001 has tested JSON beauifying with beautifyJS.
case 'JavaScript':
text = beautifyJS(text, getOptions('js', allOptions));
beautifyCompleted(text);
break;
case 'Handlebars':
defaultOptions.indent_handlebars = true; // jshint ignore: line
case 'HTML (Liquid)':
case 'HTML':
case 'XML':
text = beautifyHTML(text, getOptions('html', allOptions));
beautifyCompleted(text);
break;
case 'Sass':
case 'SCSS':
case 'LESS':
case 'CSS':
text = beautifyCSS(text, getOptions('css', allOptions));
beautifyCompleted(text);
break;
case 'SQL (Rails)':
case 'SQL':
text = beautifySQL(text, getOptions('sql', allOptions));
beautifyCompleted(text);
break;
case 'PHP':
beautifyPHP(text, getOptions('php', allOptions), beautifyCompleted);
break;
case 'Python':
beautifyPython(text, getOptions('python', allOptions), beautifyCompleted);
break;
default:
return;
}
} }
function handleSaveEvent() { function handleSaveEvent() {

View File

@ -22,7 +22,11 @@ module.exports = function (getCmd, isStdout) {
var outputPath = temp.path(); var outputPath = temp.path();
var deleteOutputFile = function () { var deleteOutputFile = function () {
// Delete the output path // Delete the output path
fs.unlink(outputPath, function () {}); fs.unlink(outputPath, function (err) {
if (err) {
console.log('Deleting output file', err);
}
});
}; };
// Get the command // Get the command
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line

View File

@ -16,5 +16,5 @@ function getCmd(inputPath, outputPath, options) {
return 'autopep8 "' + inputPath + '"'; return 'autopep8 "' + inputPath + '"';
} }
} }
var isStdin = true; var isStdout = true;
module.exports = cliBeautify(getCmd, isStdin); module.exports = cliBeautify(getCmd, isStdout);

View File

@ -0,0 +1,20 @@
/**
Requires https://github.com/erniebrodeur/ruby-beautify
*/
'use strict';
var cliBeautify = require('./cli-beautify');
function getCmd(inputPath, outputPath, options) {
var path = options.rbeautify_path; // jshint ignore: line
if (path) {
// Use absolute path
return 'ruby "' + path + '" "' + inputPath + '"';
} else {
// Use command available in $PATH
return 'rbeautify "' + inputPath + '"';
}
}
var isStdout = true;
module.exports = cliBeautify(getCmd, isStdout);

159
lib/language-options.js Normal file
View File

@ -0,0 +1,159 @@
/**
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');
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: '',
// Ruby
ruby_rbeautify_path: ''
/* jshint ignore: end */
},
// Process each language
beautify: function (text, grammar, allOptions, beautifyCompleted) {
var self = this;
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:
return;
}
},
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;
}
}

View File

@ -59,7 +59,8 @@
"less", "less",
"sql", "sql",
"php", "php",
"python" "python",
"ruby"
], ],
"engines": { "engines": {
"atom": ">0.50.0" "atom": ">0.50.0"