From 3c9836f920bea72396d03ac0b022b5f753776b55 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sun, 3 Aug 2014 23:31:34 -0400 Subject: [PATCH] Closes #31. Add CoffeeScript support. --- README.md | 8 +++++--- examples/simple-jsbeautifyrc/test.coffee | 10 ++++++++++ lib/langs/coffeescript-beautify.js | 17 +++++++++++++++++ lib/language-options.js | 7 ++++++- package.json | 6 ++++-- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 examples/simple-jsbeautifyrc/test.coffee create mode 100644 lib/langs/coffeescript-beautify.js diff --git a/README.md b/README.md index e7bf79b86..d9176bc98 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,13 @@ Atom Package: https://atom.io/packages/atom-beautify - 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) +- [x] [CoffeeScript](https://github.com/donaldpipowitch/atom-beautify/issues/31) -### Coming Soon - -- [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31 +### 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) ## Usage diff --git a/examples/simple-jsbeautifyrc/test.coffee b/examples/simple-jsbeautifyrc/test.coffee new file mode 100644 index 000000000..680f1b25c --- /dev/null +++ b/examples/simple-jsbeautifyrc/test.coffee @@ -0,0 +1,10 @@ +hello derek how is it going? +for c, i in "Hello World!" + k = 1+1- 2>=3<= 4>5 <6 + + for c, i in "Hello World" + k = (a,b)-> if b? return a + +f = b()[0] +for c, i in "Hello World" + f(b()) diff --git a/lib/langs/coffeescript-beautify.js b/lib/langs/coffeescript-beautify.js new file mode 100644 index 000000000..d2e30a4c3 --- /dev/null +++ b/lib/langs/coffeescript-beautify.js @@ -0,0 +1,17 @@ +'use strict'; + +var CF = require('coffee-formatter'); +module.exports = function (text, options, callback) { + var lines = text.split('\n'); + var resultArr = []; + for (var i = 0, len = lines.length; i < len; i++) { + var curr = lines[i]; + var p = CF.formatTwoSpaceOperator(curr); + p = CF.formatOneSpaceOperator(p); + p = CF.shortenSpaces(p); + resultArr.push(p); + } + var result = resultArr.join('\n'); + callback(result); + return result; +}; diff --git a/lib/language-options.js b/lib/language-options.js index 7dc362e30..c6c2ee2f3 100644 --- a/lib/language-options.js +++ b/lib/language-options.js @@ -15,6 +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 beautifyCoffeeScript = require('./langs/coffeescript-beautify'); +// Misc var Analytics = require('analytics-node'); var pkg = require('../package.json'); @@ -22,7 +24,7 @@ module.exports = { // Supported unique configuration keys // Used for detecting nested configurations in .jsbeautifyrc - languages: ['js', 'html', 'css', 'sql', 'php', 'python', 'ruby'], + languages: ['js', 'html', 'css', 'sql', 'php', 'python', 'ruby','coffeescript'], // Default options per language defaultLanguageOptions: { @@ -81,6 +83,9 @@ module.exports = { text = beautifyJS(text, self.getOptions('js', allOptions)); beautifyCompleted(text); break; + case 'CoffeeScript': + beautifyCoffeeScript(text, self.getOptions('js', allOptions), beautifyCompleted); + break; case 'Handlebars': // jshint ignore: start allOptions.push({ diff --git a/package.json b/package.json index eb7509541..a6e996310 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "sql", "php", "python", - "ruby" + "ruby", + "coffeescript" ], "engines": { "atom": ">0.50.0" @@ -75,7 +76,8 @@ "temp": "^0.8.0", "prettydiff": "^1.0.23", "node-uuid": "^1.4.1", - "analytics-node": "^1.0.2" + "analytics-node": "^1.0.2", + "coffee-formatter": "git+https://github.com/Glavin001/Coffee-Formatter.git" }, "activationEvents": [ "beautify"