diff --git a/README.md b/README.md index 5fe8ff5..e28933a 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,24 @@ Atom Package: https://atom.io/packages/atom-beautify ## Language Support -- JavaScript and JSON -- HTML, including +- [x] JavaScript and JSON +- [x] HTML, including - [Handlebars](http://handlebarsjs.com/) - XML -- CSS, including +- [x] CSS, including - [Sass](http://sass-lang.com/) - [LESS](http://lesscss.org/) -- SQL, special thanks to [pretty-data](https://github.com/vkiryukhin/pretty-data) +- [x] SQL, special thanks to [pretty-data](https://github.com/vkiryukhin/pretty-data) +- [x] [PHP](https://github.com/donaldpipowitch/atom-beautify/issues/26) + - Requires [PHP_Beautifier](http://pear.php.net/package/PHP_Beautifier) to be already installed. + - See below for setup details with `atom-beautify` + +### Coming Soon + +- [ ] Python, see https://github.com/donaldpipowitch/atom-beautify/issues/24 +- [ ] Ruby, see https://github.com/donaldpipowitch/atom-beautify/issues/25 +- [ ] CoffeeScript, see https://github.com/donaldpipowitch/atom-beautify/issues/31 + ## Usage @@ -37,6 +47,8 @@ You can also choose to beautify on every file save. Edit your `.jsbeautifyrc` file in any of the following locations: +- Atom Package Settings + `Atom` ➔ `Preferences` ➔ Search for `atom-beautify` - Same directory as current file - Project root `atom-beautify` will recursively look up from the current file's directory to find `.jsbeautifyrc`. @@ -102,6 +114,18 @@ See [examples/nested-jsbeautifyrc/.jsbeautifyrc](https://github.com/donaldpipowi } ``` +## Advanced Language Setup + +### PHP + +To use with PHP we require [PHP_Beautifier](http://pear.php.net/package/PHP_Beautifier) +and that you set the `Php beautifier path` in the package settings. + +#### Retrieve the path on Mac & Linux + +Run `which php_beautifier` in your Terminal. + + ## Contributing [See all contributors on GitHub](https://github.com/donaldpipowitch/atom-beautify/graphs/contributors). diff --git a/examples/simple-jsbeautifyrc/test.php b/examples/simple-jsbeautifyrc/test.php new file mode 100644 index 0000000..d396865 --- /dev/null +++ b/examples/simple-jsbeautifyrc/test.php @@ -0,0 +1,32 @@ +query(""); +$num = $q->num_rows; +echo ''; +if ($num>0) { echo ''; +}else { echo ''; } +echo ''; + + +// new messages +$q = $mysqli->query(""); +$num = $q->num_rows; +echo ''; +if ($num>0) { +echo ''; +}else { +echo ''; +} +echo ''; + + + + +?> diff --git a/lib/atom-beautify.js b/lib/atom-beautify.js index 9d2a1a6..455ffe7 100644 --- a/lib/atom-beautify.js +++ b/lib/atom-beautify.js @@ -2,10 +2,7 @@ 'use strict'; -var beautifyJS = require('js-beautify'); -var beautifyHTML = require('js-beautify').html; -var beautifyCSS = require('js-beautify').css; -var beautifySQL = require('./sql-beautify'); +// Dependencies var fs = require('fs'); var path = require('path'); var nopt = require('nopt'); @@ -13,6 +10,12 @@ 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'); // TODO: Copied from jsbeautify, please update it from time to time var knownOpts = { @@ -155,8 +158,7 @@ function findConfig(config, file) { // Supported unique configuration keys // Used for detecting nested configurations in .jsbeautifyrc -var languages = ['js', 'html', 'css', 'sql']; - +var languages = ['js', 'html', 'css', 'sql', 'php']; var defaultLanguageOptions = { /* jshint ignore: start */ // JavaScript @@ -187,7 +189,9 @@ var defaultLanguageOptions = { html_wrap_line_length: 250, // SQL sql_indent_size: 2, - sql_indent_char: ' ' + sql_indent_char: ' ', + // PHP + php_beautifier_path: '' /* jshint ignore: end */ }; @@ -339,12 +343,36 @@ function beautify() { return options; } + // Asynchronously and callback-style + function beautifyCompleted(text) { + if (oldText !== text) { + var posArray = getCursors(editor); + var origScrollTop = editor.getScrollTop(); + if (isSelection) { + editor.setTextInBufferRange( + editor.getSelectedBufferRange(), + text + ); + } else { + editor.setText(text); + } + setCursors(editor, posArray); + // Let the scrollTop setting run after all the save related stuff is run, + // otherwise setScrollTop is not working, probably because the cursor + // addition happens asynchronously + setTimeout(function () { + editor.setScrollTop(origScrollTop); + }, 0); + } + } + 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 @@ -352,39 +380,26 @@ function beautify() { 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; default: return; } - if (oldText !== text) { - var posArray = getCursors(editor); - var origScrollTop = editor.getScrollTop(); - if (isSelection) { - editor.setTextInBufferRange( - editor.getSelectedBufferRange(), - text - ); - } else { - editor.setText(text); - } - setCursors(editor, posArray); - // Let the scrollTop setting run after all the save related stuff is run, - // otherwise setScrollTop is not working, probably because the cursor - // addition happens asynchronously - setTimeout(function () { - editor.setScrollTop(origScrollTop); - }, 0); - } } function handleSaveEvent() { diff --git a/lib/langs/php-beautify.js b/lib/langs/php-beautify.js new file mode 100644 index 0000000..e4554a4 --- /dev/null +++ b/lib/langs/php-beautify.js @@ -0,0 +1,52 @@ +/** +Requires http://pear.php.net/package/PHP_Beautifier +*/ + +'use strict'; + +var fs = require('fs'); +var temp = require('temp').track(); +var exec = require('child_process').exec; + +module.exports = function (text, options, callback) { + // Create temp input file + temp.open('input.php', 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 () {}); + }; + + var phpBeautifierPath = options.beautifier_path; // jshint ignore: line + if (phpBeautifierPath) { + // Beautify + var config = { + env: process.env + }; + exec('php "' + phpBeautifierPath + '" "' + info.path + '" "' + outputPath + '"', config, function (err) { + if (!err) { + // Beautification has completed + // Read contents of output file + fs.readFile(outputPath, 'utf8', function (err, newText) { + // Execute callback with resulting output text + callback(newText); + deleteOutputFile(); + }); + } else { + deleteOutputFile(); + } + }); + } else { + console.log('PHP Beautifier Path not set in Package settings.'); + } + } + }); + } + }); +}; diff --git a/lib/sql-beautify.js b/lib/langs/sql-beautify.js similarity index 100% rename from lib/sql-beautify.js rename to lib/langs/sql-beautify.js diff --git a/package.json b/package.json index 7e63ee7..3c251ed 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,8 @@ "sass", "scss", "less", - "sql" + "sql", + "php" ], "engines": { "atom": ">0.50.0" @@ -69,6 +70,7 @@ "nopt": "^3.0.0", "lodash": "2.4.1", "strip-json-comments": "^0.1.3", - "js-yaml": "^3.0.2" + "js-yaml": "^3.0.2", + "temp": "^0.8.0" } }