diff --git a/examples/nested-jsbeautifyrc/.jsbeautifyrc b/examples/nested-jsbeautifyrc/.jsbeautifyrc index c9999ee9e..7c173cb6d 100644 --- a/examples/nested-jsbeautifyrc/.jsbeautifyrc +++ b/examples/nested-jsbeautifyrc/.jsbeautifyrc @@ -35,3 +35,7 @@ #max_line_length: 79 #ignore: # - "E24" + java: + indent_class: true + indent_with_tabs: 0 + indent_size: 1 diff --git a/examples/simple-jsbeautifyrc/test.c b/examples/nested-jsbeautifyrc/test.c similarity index 100% rename from examples/simple-jsbeautifyrc/test.c rename to examples/nested-jsbeautifyrc/test.c diff --git a/examples/simple-jsbeautifyrc/test.cpp b/examples/nested-jsbeautifyrc/test.cpp similarity index 100% rename from examples/simple-jsbeautifyrc/test.cpp rename to examples/nested-jsbeautifyrc/test.cpp diff --git a/examples/simple-jsbeautifyrc/test.cs b/examples/nested-jsbeautifyrc/test.cs similarity index 100% rename from examples/simple-jsbeautifyrc/test.cs rename to examples/nested-jsbeautifyrc/test.cs diff --git a/examples/simple-jsbeautifyrc/test.h b/examples/nested-jsbeautifyrc/test.h similarity index 100% rename from examples/simple-jsbeautifyrc/test.h rename to examples/nested-jsbeautifyrc/test.h diff --git a/examples/simple-jsbeautifyrc/test.java b/examples/nested-jsbeautifyrc/test.java similarity index 100% rename from examples/simple-jsbeautifyrc/test.java rename to examples/nested-jsbeautifyrc/test.java diff --git a/examples/simple-jsbeautifyrc/test.m b/examples/nested-jsbeautifyrc/test.m similarity index 100% rename from examples/simple-jsbeautifyrc/test.m rename to examples/nested-jsbeautifyrc/test.m diff --git a/examples/simple-jsbeautifyrc/uncrustify.cfg b/examples/nested-jsbeautifyrc/uncrustify.cfg similarity index 100% rename from examples/simple-jsbeautifyrc/uncrustify.cfg rename to examples/nested-jsbeautifyrc/uncrustify.cfg diff --git a/examples/simple-jsbeautifyrc/.jsbeautifyrc b/examples/simple-jsbeautifyrc/.jsbeautifyrc index f6b4e6b02..bd461289b 100644 --- a/examples/simple-jsbeautifyrc/.jsbeautifyrc +++ b/examples/simple-jsbeautifyrc/.jsbeautifyrc @@ -8,6 +8,5 @@ "max_preserve_newlines": 2, "jslint_happy": true, "indent_handlebars": true, - "object": {}, - "configPath": "uncrustify.cfg" + "object": {} } diff --git a/lib/langs/cli-beautify.js b/lib/langs/cli-beautify.js index bd2b60ab4..747ba9cc1 100644 --- a/lib/langs/cli-beautify.js +++ b/lib/langs/cli-beautify.js @@ -21,6 +21,7 @@ module.exports = function (getCmd, isStdout) { // Create temp output file var outputPath = temp.path(); var deleteOutputFile = function () { + temp.cleanup(); // Delete the output path fs.unlink(outputPath, function (err) { if (err) { @@ -28,53 +29,64 @@ module.exports = function (getCmd, isStdout) { } }); }; - // Get the command - var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line - if (cmd) { - var config = { - env: process.env - }; + // Process the command + var processCmd = function (cmd) { + if (cmd) { - var isWin = /^win/.test(process.platform); - if (!isWin) { - // We need the $PATH to be correct when executing the command. - // This should normalize the $PATH - // by calling the external files that would usually - // change the $PATH variable on user login. - var $path = - '[ -f ~/.bash_profile ] && source ~/.bash_profile;'; - $path += '[ -f ~/.bash_rc ] && source ~/.bash_rc;'; - // See http://stackoverflow.com/a/638980/2578205 - // for checking if file exists in Bash - cmd = $path + cmd; - } + var config = { + env: process.env + }; - // Execute and beautify! - exec(cmd, config, function (err, stdout, stderr) { - console.log(stderr); - if (!err) { - // Beautification has completed - if (isStdout) { - // Execute callback with resulting output text - callback(stdout); - deleteOutputFile(); - } else { - // Read contents of output file - fs.readFile(outputPath, 'utf8', function (err, - newText) { + var isWin = /^win/.test(process.platform); + if (!isWin) { + // We need the $PATH to be correct when executing the command. + // This should normalize the $PATH + // by calling the external files that would usually + // change the $PATH variable on user login. + var $path = + '[ -f ~/.bash_profile ] && source ~/.bash_profile;'; + $path += + '[ -f ~/.bash_rc ] && source ~/.bash_rc;'; + // See http://stackoverflow.com/a/638980/2578205 + // for checking if file exists in Bash + cmd = $path + cmd; + } + + // Execute and beautify! + exec(cmd, config, function (err, stdout, stderr) { + // console.log(stderr); + if (!err) { + // Beautification has completed + if (isStdout) { // Execute callback with resulting output text - callback(newText); + callback(stdout); deleteOutputFile(); - }); + } else { + // Read contents of output file + fs.readFile(outputPath, 'utf8', function ( + err, + newText) { + // Execute callback with resulting output text + callback(newText); + deleteOutputFile(); + }); + } + } else { + console.log('Beautifcation Error: ', err); + deleteOutputFile(); } - } else { - console.log('Beautifcation Error: ', err); - deleteOutputFile(); - } - }); - } else { - console.log('Beautify CLI command not valid.'); + }); + } else { + console.log('Beautify CLI command not valid.'); + } + }; + + // Get the command + var cmd = getCmd(info.path, outputPath, options, + processCmd); // jshint ignore: line + if (typeof cmd === 'string') { + processCmd(cmd); } } }); diff --git a/lib/langs/uncrustify/cfg.js b/lib/langs/uncrustify/cfg.js new file mode 100644 index 000000000..fc9599293 --- /dev/null +++ b/lib/langs/uncrustify/cfg.js @@ -0,0 +1,76 @@ +/** +Requires http://pear.php.net/package/PHP_Beautifier +*/ + +'use strict'; + +var fs = require('fs'); +var temp = require('temp').track(); + +module.exports = function (options, cb) { + var text = ''; + // Apply indent_size to output_tab_size + options.output_tab_size = options.output_tab_size || options.indent_size; // jshint ignore: line + options.input_tab_size = options.input_tab_size || options.indent_size; // jshint ignore: line + delete options.indent_size; // jshint ignore: line + // Indent with Tabs? + // How to use tabs when indenting code + // 0=spaces only + // 1=indent with tabs to brace level, align with spaces + // 2=indent and align with tabs, using spaces when not on a tabstop + // jshint ignore: start + var ic = options.indent_char; + if (options.indent_with_tabs === 0 || + options.indent_with_tabs === 1 || + options.indent_with_tabs === 2) { + // Ignore indent_char option + } else if (ic === ' ') { + options.indent_with_tabs = 0; // Spaces only + } else if (ic === '\t') { + options.indent_with_tabs = 2; // indent and align with tabs, using spaces when not on a tabstop + } else { + options.indent_with_tabs = 1; // indent with tabs to brace level, align with spaces + } + delete options.indent_char; + // jshint ignore: end + // Remove misc + delete options.languageOverride; + delete options.configPath; + // Iterate over each property and write to configuration file + for (var k in options) { + var v = options[k]; + var vs = v; + if (typeof vs === 'boolean') { + if (vs === true) { + vs = 'True'; + } else { + vs = 'False'; + } + } + text += k + ' = ' + vs + '\n'; + } + + // Create temp input file + temp.open({ + suffix: '.cfg' + }, function (err, info) { + if (!err) { + // Save current text to input file + fs.write(info.fd, text || '', function (err) { + // console.log(err); + if (err) { + return cb(err); + } + fs.close(info.fd, function (err) { + // console.log(err); + if (err) { + return cb(err); + } + return cb(null, info.path); + }); + }); + } else { + return cb(err); + } + }); +}; diff --git a/lib/langs/uncrustify/index.js b/lib/langs/uncrustify/index.js index c69bf647f..9f3392325 100644 --- a/lib/langs/uncrustify/index.js +++ b/lib/langs/uncrustify/index.js @@ -5,29 +5,40 @@ Requires http://pear.php.net/package/PHP_Beautifier 'use strict'; var cliBeautify = require('../cli-beautify'); +var cfg = require('./cfg'); var path = require('path'); -function getCmd(inputPath, outputPath, options) { +function getCmd(inputPath, outputPath, options, cb) { + // console.log('Uncrustify options:', options); var configPath = options.configPath; var lang = options.languageOverride || 'C'; + + function done(configPath) { + // console.log(configPath); + // Use command available in $PATH + var cmd = 'uncrustify -c "' + configPath + + '" -f "' + inputPath + + '" -o "' + outputPath + + '" -l "' + lang + '"'; + // console.log(cmd); + return cb(cmd); + } if (!configPath) { // No custom config path - // Use Default config - configPath = path.resolve(__dirname, 'default.cfg'); + cfg(options, function (error, path) { + if (error) { + throw error; + } + return done(path); + }); } else { // Has custom config path var editor = atom.workspace.getActiveEditor(); var basePath = path.dirname(editor.getPath()); - console.log(basePath); + // console.log(basePath); configPath = path.resolve(basePath, configPath); + done(configPath); } - console.log(configPath); - // Use command available in $PATH - var cmd = 'uncrustify -c "' + configPath + - '" -f "' + inputPath + - '" -o "' + outputPath + - '" -l "' + lang + '"'; - console.log(cmd); - return cmd; + return; } module.exports = cliBeautify(getCmd);