Closes #57. Customization in .jsbeautifyrc file now generates the config
(.cfg) file for Uncrustify beautification.
This commit is contained in:
parent
8fb1c8d769
commit
325ada7851
|
@ -35,3 +35,7 @@
|
|||
#max_line_length: 79
|
||||
#ignore:
|
||||
# - "E24"
|
||||
java:
|
||||
indent_class: true
|
||||
indent_with_tabs: 0
|
||||
indent_size: 1
|
||||
|
|
|
@ -8,6 +8,5 @@
|
|||
"max_preserve_newlines": 2,
|
||||
"jslint_happy": true,
|
||||
"indent_handlebars": true,
|
||||
"object": {},
|
||||
"configPath": "uncrustify.cfg"
|
||||
"object": {}
|
||||
}
|
||||
|
|
|
@ -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,8 +29,9 @@ module.exports = function (getCmd, isStdout) {
|
|||
}
|
||||
});
|
||||
};
|
||||
// Get the command
|
||||
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line
|
||||
|
||||
// Process the command
|
||||
var processCmd = function (cmd) {
|
||||
if (cmd) {
|
||||
|
||||
var config = {
|
||||
|
@ -44,7 +46,8 @@ module.exports = function (getCmd, isStdout) {
|
|||
// change the $PATH variable on user login.
|
||||
var $path =
|
||||
'[ -f ~/.bash_profile ] && source ~/.bash_profile;';
|
||||
$path += '[ -f ~/.bash_rc ] && source ~/.bash_rc;';
|
||||
$path +=
|
||||
'[ -f ~/.bash_rc ] && source ~/.bash_rc;';
|
||||
// See http://stackoverflow.com/a/638980/2578205
|
||||
// for checking if file exists in Bash
|
||||
cmd = $path + cmd;
|
||||
|
@ -52,7 +55,7 @@ module.exports = function (getCmd, isStdout) {
|
|||
|
||||
// Execute and beautify!
|
||||
exec(cmd, config, function (err, stdout, stderr) {
|
||||
console.log(stderr);
|
||||
// console.log(stderr);
|
||||
if (!err) {
|
||||
// Beautification has completed
|
||||
if (isStdout) {
|
||||
|
@ -61,7 +64,8 @@ module.exports = function (getCmd, isStdout) {
|
|||
deleteOutputFile();
|
||||
} else {
|
||||
// Read contents of output file
|
||||
fs.readFile(outputPath, 'utf8', function (err,
|
||||
fs.readFile(outputPath, 'utf8', function (
|
||||
err,
|
||||
newText) {
|
||||
// Execute callback with resulting output text
|
||||
callback(newText);
|
||||
|
@ -76,6 +80,14 @@ module.exports = function (getCmd, isStdout) {
|
|||
} 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
};
|
|
@ -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';
|
||||
if (!configPath) {
|
||||
// No custom config path
|
||||
// Use Default config
|
||||
configPath = path.resolve(__dirname, 'default.cfg');
|
||||
} else {
|
||||
// Has custom config path
|
||||
var editor = atom.workspace.getActiveEditor();
|
||||
var basePath = path.dirname(editor.getPath());
|
||||
console.log(basePath);
|
||||
configPath = path.resolve(basePath, configPath);
|
||||
}
|
||||
console.log(configPath);
|
||||
|
||||
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 cmd;
|
||||
// console.log(cmd);
|
||||
return cb(cmd);
|
||||
}
|
||||
if (!configPath) {
|
||||
// No custom config path
|
||||
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);
|
||||
configPath = path.resolve(basePath, configPath);
|
||||
done(configPath);
|
||||
}
|
||||
return;
|
||||
}
|
||||
module.exports = cliBeautify(getCmd);
|
||||
|
|
Loading…
Reference in New Issue