See #57. Add Uncrustify beautifier for multiple language support.

Uncrustify Language support:
- C
- C++
- C#
- Objective-C
- Java
- D
- Pawn
- Vala

Closes #45. Add Java Support.

Currently, use the configPath option to set an absolute or relative path
(relative to the currently beautified file) to specific the .cfg file
for Uncrustify to use as configuration options.
This commit is contained in:
Glavin Wiechert 2014-08-08 13:38:28 -04:00
parent faa62366b1
commit 5d6751cb4b
14 changed files with 4828 additions and 66 deletions

View File

@ -1,5 +1,7 @@
# [atom-beautify](https://github.com/donaldpipowitch/atom-beautify)
[![Gitter chat](https://badges.gitter.im/Glavin001/atom-beautify.png)](https://gitter.im/Glavin001/atom-beautify)
> [Beautify](https://github.com/einars/js-beautify)
HTML (including [Handlebars](http://handlebarsjs.com/)),
CSS (including [Sass](http://sass-lang.com/) and [LESS](http://lesscss.org/)),
@ -7,6 +9,13 @@ JavaScript, and much more in Atom.
Atom Package: https://atom.io/packages/atom-beautify
```bash
apm install atom-beautify
```
Or Settings/Preferences ➔ Packages ➔ Search for `atom-beautify`
## Language Support
- [x] JavaScript and JSON
@ -25,12 +34,30 @@ Atom Package: https://atom.io/packages/atom-beautify
- [x] [Ruby](https://github.com/donaldpipowitch/atom-beautify/issues/25)
- Requires [RBeautify](https://github.com/erniebrodeur/ruby-beautify)
- [x] [CoffeeScript](https://github.com/donaldpipowitch/atom-beautify/issues/31)
- [x] [Java](https://github.com/Glavin001/atom-beautify/issues/45)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [C](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [C++](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [C#](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [Objective-C](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [D](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [Pawn](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
- [x] [Vala](https://github.com/Glavin001/atom-beautify/issues/57)
- Requires [Uncrustify](http://sourceforge.net/projects/uncrustify/)
### Road Map
- [ ] [TypeScript support](https://github.com/Glavin001/atom-beautify/issues/49)
- [ ] [Java support](https://github.com/Glavin001/atom-beautify/issues/45)
- [ ] [Perl support](https://github.com/Glavin001/atom-beautify/issues/33)
#### Language support
- [ ] [TypeScript](https://github.com/Glavin001/atom-beautify/issues/49)
- [ ] [Perl](https://github.com/Glavin001/atom-beautify/issues/33)
## Usage

View File

@ -8,5 +8,6 @@
"max_preserve_newlines": 2,
"jslint_happy": true,
"indent_handlebars": true,
"object": {}
"object": {},
"configPath": "uncrustify.cfg"
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,27 @@
/* ctest4 */
fm_status fm2000EventHandlingInitialize(fm_int sw);
fm_status fm2000MacTableOverflowStart(fm_int sw);
fm_bool fm2000ProcessMATableEntry( fm_mac_table_work_list *pWork,
fm_int sw,
fm_int index,
fm_thread * event_handler,
fm_uint32 * numUpdates,
fm_event **event);
void foo()
{
Logger log = new Logger();
Logger log= new Logger( );
log.foo.bar = 5;
log.narf.sweat = "cat";
for (i = 0; i<5; i++) bar(i);
}
int this_works(int x);
int bug(int); // BUG: left-aligned
typedef int fooman;
enum FLAGS
{
FLAGS_decimal = 1, // decimal
FLAGS_unsigned = 2, // u or U suffix
FLAGS_long = 4, // l or L suffix
};

View File

@ -0,0 +1,113 @@
class X : Y {
bool Method (int argument_1, int argument_2)
{
#region something
int foo = 0;
#endregion
if (argument_1 == argument_2)
throw new Exception (Locale.GetText ("They are equal!"));
if (argument_1 < argument_2) {
if (argument_1 * 3 > 4)
return true;
else
return false;
}
//
// This sample helps keep your sanity while using 8-spaces for tabs
//
VeryLongIdentifierWhichTakesManyArguments (
Argument1,
Argument2, Argument3,
NestedCallHere (
MoreNested));
}
bool MyProperty {
get { return x; }
set { x = value; }
}
void AnotherMethod ()
{
Logger log = new Logger();
log.foo.bar = 5;
log.narf.sweat = "cat";
if ((a + 5) != 4) {
}
while (blah) {
if (a)
continue;
b++;
}
}
}
object lockA;
object lockB;
void Foo () {
lock (lockA) {
lock (lockB) {
}
}
}
void Bar () {
lock (lockB) {
lock (lockA) {
}
}
}
// class library
class Blah {
Hashtable ht;
void Foo (int zzz, Entry blah) {
lock (ht) {
ht.Add (zzz, blah);
}
}
void Bar ()
{
lock (ht) {
foreach (Entry e in ht)
EachBar (e);
}
}
virtual void EachBar (Entry e)
{
}
}
// User
class MyBlah {
byte[] box = new byte[6];
box[2] = 56;
void DoStuff ()
{
lock (this) {
int i = GetNumber ();
Entry e = GetEntry ();
Foo (i, e);
}
}
override void EachBar (Entry e)
{
lock (this) {
DoSomething (e);
}
}
}

View File

@ -0,0 +1,10 @@
class Example
{
Example()
: member(0)
{}
int member;
};

View File

@ -0,0 +1,35 @@
class Aligns {
final int SZF = 4;
final int SZ2F = 4;
final int aBarF;
final int someIntsF[];
final int someIntsSzdF[4];
final int[] someMoreIntsF;
final int lotsOfIntsF[][][][][][];
final int[][][][] lotsMoreIntsF;
final Tem<Plate> edVarF;
final int aBarSetF=null;
final int someIntsSetF[]=null;
final int someIntsSzdSetF[4]=null;
final int[] someMoreIntsSetF=null;
final int lotsOfIntsSetF[][][][][][]=null;
final int[][][][] lotsMoreIntsSetF=null;
final Tem<Plate> edVarF=null;
int SZ = 4;
int SZ2 = 4;
int aBar;
int someInts[];
int someIntsSzd[4];
int spacer;
int[] someMoreInts;
int lotsOfInts[][][][][][];
int[][][][] lotsMoreInts;
Tem<Plate> edVar;
int aBarSet=null;
int someIntsSet[]=null;
int someIntsSzdSet[4]=null;
int[] someMoreIntsSet=null;
int lotsOfIntsSet[][][][][][]=null;
int[][][][] lotsMoreIntsSet=null;
Tem<Plate> edVar=null;
}

View File

@ -0,0 +1,30 @@
// Turn the document contents into a single savable lump of data
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
#pragma unused(typeName)
// Produce the data lump:
NSData * retval = [NSKeyedArchiver archivedDataWithRootObject:model];
// If the lump is nil something went wrong
// fill out the error object to explain what wrent wrong
if ( outError != NULL ) {
// The sender wanted an error reported. If there
// was a problem, fill in an NSError object
if (retval == nil) {
// The error object should include an (unhelpful)
// explanation of what happened
NSDictionary * userInfoDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Internal error formatting data", NSLocalizedDescriptionKey,
@"Archiving of data failed. Probably a bug.", NSLocalizedFailureReasonErrorKey,
@"There's nothing you can do.", NSLocalizedRecoverySuggestionErrorKey, nil];
*outError = [NSError errorWithDomain:LinearInternalErrorDomain
code:linErrCantFormatDocumentData
userInfo:userInfoDict];
} else {
// No problem. Don't supply an error object.
*outError = nil;
}
}
return retval;
}

File diff suppressed because it is too large Load Diff

View File

@ -15,65 +15,69 @@ module.exports = function (getCmd, isStdout) {
temp.open('input', function (err, info) {
if (!err) {
// Save current text to input file
fs.write(info.fd, text || '');
fs.close(info.fd, function (err) {
if (!err) {
// Create temp output file
var outputPath = temp.path();
var deleteOutputFile = function () {
// Delete the output path
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
if (cmd) {
var config = {
env: process.env
};
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) {
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) {
// Execute callback with resulting output text
callback(newText);
deleteOutputFile();
});
fs.write(info.fd, text || '', function () {
fs.close(info.fd, function (err) {
if (!err) {
// Create temp output file
var outputPath = temp.path();
var deleteOutputFile = function () {
// Delete the output path
fs.unlink(outputPath, function (err) {
if (err) {
console.log('Deleting output file', err);
}
} else {
console.log('Beautifcation Error: ', err);
deleteOutputFile();
});
};
// Get the command
var cmd = getCmd(info.path, outputPath, options); // jshint ignore: line
if (cmd) {
var config = {
env: process.env
};
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;
}
});
} else {
console.log('Beautify CLI command not valid.');
// 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) {
// Execute callback with resulting output text
callback(newText);
deleteOutputFile();
});
}
} else {
console.log('Beautifcation Error: ', err);
deleteOutputFile();
}
});
} else {
console.log('Beautify CLI command not valid.');
}
}
}
});
});
}
});

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,33 @@
/**
Requires http://pear.php.net/package/PHP_Beautifier
*/
'use strict';
var cliBeautify = require('../cli-beautify');
var path = require('path');
function getCmd(inputPath, outputPath, 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);
// Use command available in $PATH
var cmd = 'uncrustify -c "' + configPath +
'" -f "' + inputPath +
'" -o "' + outputPath +
'" -l "' + lang + '"';
console.log(cmd);
return cmd;
}
module.exports = cliBeautify(getCmd);

View File

@ -15,8 +15,8 @@ var beautifyPHP = require('./langs/php-beautify');
var beautifyPython = require('./langs/python-beautify');
var beautifyRuby = require('./langs/ruby-beautify');
var beautifyLESS = require('./langs/less-beautify');
var beautifySASS = beautifyLESS;
var beautifyCoffeeScript = require('./langs/coffeescript-beautify');
var uncrustifyBeautifier = require('./langs/uncrustify/');
// Misc
var Analytics = require('analytics-node');
var pkg = require('../package.json');
@ -27,8 +27,23 @@ module.exports = {
// Supported unique configuration keys
// Used for detecting nested configurations in .jsbeautifyrc
languages: ['js', 'html', 'css', 'sql', 'php', 'python', 'ruby',
'coffeescript'
languages: [
'js',
'html',
'css',
'sql',
'php',
'python',
'ruby',
'coffeescript',
'c',
'cpp',
'cs',
'objectivec',
'java',
'd',
'pawn',
'vala'
],
// Default options per language
@ -71,7 +86,23 @@ module.exports = {
python_indent_size: 4,
python_ignore: ['E24'],
// Ruby
ruby_rbeautify_path: ''
ruby_rbeautify_path: '',
// C
c_configPath: '',
// C++
cpp_configPath: '',
// Objective-C
objectivec_configPath: '',
// C#
cs_configPath: '',
// Objective-C
d_configPath: '',
// Java
java_configPath: '',
// Pawn
pawn_configPath: '',
// VALA
vala_configPath: ''
/* jshint ignore: end */
},
@ -80,6 +111,7 @@ module.exports = {
var self = this;
// Beautify!
var unsupportedGrammar = false;
var options;
switch (grammar) {
case 'JSON':
// Treat JSON as JavaScript, because it will support comments.
@ -128,6 +160,55 @@ module.exports = {
case 'Ruby':
beautifyRuby(text, self.getOptions('ruby', allOptions), beautifyCompleted);
break;
case 'C':
options = self.getOptions('c', allOptions);
options.languageOverride = 'C';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'C++':
options = self.getOptions('cpp', allOptions);
options.languageOverride = 'CPP';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'C#':
options = self.getOptions('cs', allOptions);
options.languageOverride = 'CS';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'Objective-C':
case 'Objective-C++':
options = self.getOptions('objectivec', allOptions);
options.languageOverride = 'OC+';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'D':
options = self.getOptions('d', allOptions);
options.languageOverride = 'D';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'Pawn':
options = self.getOptions('pawn', allOptions);
options.languageOverride = 'PAWN';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'Vala':
options = self.getOptions('vala', allOptions);
options.languageOverride = 'VALA';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
case 'Java':
options = self.getOptions('java', allOptions);
options.languageOverride = 'JAVA';
uncrustifyBeautifier(text, options,
beautifyCompleted);
break;
default:
unsupportedGrammar = true;
}

View File

@ -3,7 +3,7 @@
"main": "./lib/atom-beautify",
"version": "0.5.2",
"private": true,
"description": "Beautify HTML, CSS and Javascript in Atom",
"description": "Beautify HTML, CSS, JavaScript, PHP, Python, Ruby, Java, C, C++, C#, Objective-C, CoffeeScript, and SQL in Atom",
"repository": {
"type": "git",
"url": "https://github.com/Glavin001/atom-beautify"
@ -61,7 +61,18 @@
"php",
"python",
"ruby",
"coffeescript"
"coffeescript",
"c",
"c++",
"cpp",
"objective-c",
"c-sharp",
"c#",
"uncrustify",
"java",
"pawn",
"vala",
"d"
],
"engines": {
"atom": ">0.50.0"