Skip to content

Commit

Permalink
Closes #25. Add Ruby support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Jun 16, 2014
1 parent 612c7dc commit 8865144
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 143 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ Atom Package: https://atom.io/packages/atom-beautify
- [x] [Python](https://github.com/donaldpipowitch/atom-beautify/issues/24)
- Requires [autopep8](https://github.com/hhatto/autopep8) to be already installed.
- 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

- [ ] Ruby, see https://github.com/donaldpipowitch/atom-beautify/issues/25
- [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31


Expand Down
2 changes: 1 addition & 1 deletion examples/simple-jsbeautifyrc/test.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
p {
color: red;
color: red;
}
2 changes: 1 addition & 1 deletion examples/simple-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');
}
25 changes: 25 additions & 0 deletions examples/simple-jsbeautifyrc/test.rb
Original file line number Diff line number Diff line change
@@ -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,

}
148 changes: 12 additions & 136 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

'use strict';

var plugin = module.exports;
// Dependencies
var fs = require('fs');
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');
// 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');
// Language options
var beautifier = require('./language-options');
var languages = beautifier.langauges;

This comment has been minimized.

Copy link
@Glavin001

Glavin001 Jun 28, 2014

Author Owner

FIXME langauges should be languages.

var defaultLanguageOptions = beautifier.defaultLanguageOptions;

// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
Expand Down Expand Up @@ -54,7 +51,6 @@ var knownOpts = {
};

var Subscriber = require('emissary').Subscriber;
var plugin = module.exports;
Subscriber.extend(plugin);

function getUserHome() {
Expand Down Expand Up @@ -157,47 +153,6 @@ function findConfig(config, file) {
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) {
var config = atom.config.getSettings()['atom-beautify'];
var options = {};
Expand Down Expand Up @@ -295,57 +250,12 @@ function beautify() {
// Listed in order from default (base) to the one with the highest priority
// Left = Default, Right = Will override the left.
var allOptions = [
editorOptions, // Atom Editor
configOptions, //
homeOptions, // User's Home path
projectOptions // Project path
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) {

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
function beautifyCompleted(text) {
if (oldText !== text) {
Expand All @@ -369,43 +279,9 @@ function beautify() {
}
}

switch (editor.getGrammar().name) {
case 'JSON':
// 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;
}
// Finally, beautify!
beautifier.beautify(text, editor.getGrammar().name, allOptions, beautifyCompleted);

}

function handleSaveEvent() {
Expand Down
6 changes: 5 additions & 1 deletion lib/langs/cli-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ module.exports = function (getCmd, isStdout) {
var outputPath = temp.path();
var deleteOutputFile = function () {
// Delete the output path
fs.unlink(outputPath, function () {});
fs.unlink(outputPath, function (err) {
if (err) {
console.log('Deleting output file', err);
}
});
};
// Get the command
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line
Expand Down
4 changes: 2 additions & 2 deletions lib/langs/python-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ function getCmd(inputPath, outputPath, options) {
return 'autopep8 "' + inputPath + '"';
}
}
var isStdin = true;
module.exports = cliBeautify(getCmd, isStdin);
var isStdout = true;
module.exports = cliBeautify(getCmd, isStdout);
20 changes: 20 additions & 0 deletions lib/langs/ruby-beautify.js
Original file line number Diff line number Diff line change
@@ -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);
Loading

0 comments on commit 8865144

Please sign in to comment.