From 2e57630275773b331becc2fcadaf558e370750e6 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sat, 14 Jun 2014 02:21:26 -0300 Subject: [PATCH] Closes #14. Completed support for SQL. - Updated README - SQL now can apply the indent_char and indent_size options. --- README.md | 13 +++++++++++-- examples/nested-jsbeautifyrc/.jsbeautifyrc | 6 ++++++ examples/nested-jsbeautifyrc/test.sql | 1 + lib/atom-beautify.js | 11 ++++++----- lib/sql-beautify.js | 22 ++++++++++++++++------ 5 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 examples/nested-jsbeautifyrc/test.sql diff --git a/README.md b/README.md index a109d30f5..0de28aaef 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,18 @@ HTML (including [Handlebars](http://handlebarsjs.com/)), CSS (including [Sass](http://sass-lang.com/) and [LESS](http://lesscss.org/)) and JavaScript in Atom. +Atom Package: https://atom.io/packages/atom-beautify + ## Language Support -- JavaScript +- JavaScript and JSON - HTML, including - [Handlebars](http://handlebarsjs.com/) - - XML is supported as an *experimental feature*. + - XML - CSS, including - [Sass](http://sass-lang.com/) - [LESS](http://lesscss.org/) +- SQL, special thanks to [pretty-data](https://github.com/vkiryukhin/pretty-data) ## Usage @@ -89,6 +92,12 @@ See [examples/nested-jsbeautifyrc/.jsbeautifyrc](https://github.com/donaldpipowi "preserve_newlines": true, "max_preserve_newlines": 2, "jslint_happy": true + }, + "sql": { + "indent_size": 4, + "indent_char": " ", + "indent_level": 0, + "indent_with_tabs": false } } ``` diff --git a/examples/nested-jsbeautifyrc/.jsbeautifyrc b/examples/nested-jsbeautifyrc/.jsbeautifyrc index 9ffa733e4..e8e138c51 100644 --- a/examples/nested-jsbeautifyrc/.jsbeautifyrc +++ b/examples/nested-jsbeautifyrc/.jsbeautifyrc @@ -21,5 +21,11 @@ "preserve_newlines": true, "max_preserve_newlines": 2, "jslint_happy": true + }, + "sql": { + "indent_size": 4, + "indent_char": " ", + "indent_level": 0, + "indent_with_tabs": false } } diff --git a/examples/nested-jsbeautifyrc/test.sql b/examples/nested-jsbeautifyrc/test.sql new file mode 100644 index 000000000..617439f8a --- /dev/null +++ b/examples/nested-jsbeautifyrc/test.sql @@ -0,0 +1 @@ +SELECT ca.proj_id AS proj_id, ca.ca_name AS proj_name, ca.ca_date_start AS proj_start, ca.ca_date_end AS proj_end,(SELECT COUNT(*) FROM rotations r WHERE r.proj_id = proj_id AND r.r_status = 'R' GROUP BY r.proj_id) r_count, (SELECT count(*) FROM rotations r WHERE r.proj_id = proj_id AND r.channel_id = 24 ) r_rtb_count FROM projs ca, clients c, proj_auth caa WHERE ca.client_id = 12345 AND ca.client_id = c.client_id AND ca_type = 'zzz' AND c.agency_id = 0 AND ca.client_id = NVL( caa.client_id, ca.client_id ) AND proj_id = NVL( caa.proj_id, proj_id ) AND caa.contact_id = 7890 diff --git a/lib/atom-beautify.js b/lib/atom-beautify.js index 0b4c48420..d6657552c 100644 --- a/lib/atom-beautify.js +++ b/lib/atom-beautify.js @@ -83,7 +83,7 @@ function setCursors(editor, posArray) { } function verifyExists(fullPath) { - return fs.existsSync(fullPath) ? fullPath : null; + return fs.existsSync(fullPath) ? fullPath : null; } // Storage for memoized results from find file @@ -155,7 +155,7 @@ function beautify() { var text; var editor = atom.workspace.getActiveEditor(); - var isSelection = !! editor.getSelectedText(); + var isSelection = !!editor.getSelectedText(); var softTabs = editor.softTabs; var tabLength = editor.getTabLength(); @@ -182,6 +182,7 @@ function beautify() { encoding: 'utf8' }))); } catch (e) { + console.log('Failed parsing config JSON at '+configPath); externalOptions = {}; } } else { @@ -209,7 +210,7 @@ function beautify() { function getOptions(selection, allOptions) { // Reduce all options into correctly merged options. - var options = _.reduce(allOptions, function(result, currOptions) { + var options = _.reduce(allOptions, function (result, currOptions) { var containsNested = false; var collectedConfig = {}; @@ -225,7 +226,7 @@ function beautify() { // Create a flat object of config options if nested format was used if (!containsNested) { - collectedConfig = currOptions; + _.merge(collectedConfig, currOptions); } else { // Merge with selected options // where `selection` could be `html`, `js`, 'css', etc @@ -236,7 +237,6 @@ function beautify() { }, {}); - // TODO: Clean. // There is a bug in nopt // See https://github.com/npm/nopt/issues/38#issuecomment-45971505 @@ -267,6 +267,7 @@ function beautify() { case 'CSS': text = beautifyCSS(text, getOptions('css', allOptions)); break; + case 'SQL (Rails)': case 'SQL': text = beautifySQL(text, getOptions('sql', allOptions)); break; diff --git a/lib/sql-beautify.js b/lib/sql-beautify.js index 03d3cf3c5..ddfdaa5ef 100644 --- a/lib/sql-beautify.js +++ b/lib/sql-beautify.js @@ -1,13 +1,10 @@ /** - -Original SQL Source code from https://github.com/vkiryukhin/pretty-data +Original SQL Beautifier Source code from https://github.com/vkiryukhin/pretty-data */ 'use strict'; module.exports = function (text, options) { - console.log('SQL input', text); - function SQL() { var self = this; @@ -73,7 +70,21 @@ module.exports = function (text, options) { .split('~::~'); } - SQL.prototype.beautify = function(text, options) { + SQL.prototype.beautify = function (text, options) { + + /* jshint camelcase: false */ + // Apply options + // options.indent_size = Indentation size [4] + // options.indent_char = Indentation character [" "] + this.step = new Array(options.indent_size).join(options.indent_char); + // Initial indentation level [0] + if (options.indent_level) { + // Not supported. + } + // Indent with tabs, overrides indent_size and indent_char + if (!!options.indent_with_tabs) { + this.step = '\t'; + } var arByQuote = text.replace(/\s{1,}/g, ' ') .replace(/\'/ig, '~::~\'') @@ -124,7 +135,6 @@ module.exports = function (text, options) { } str = str.replace(/^\n{1,}/, '').replace(/\n{1,}/g, '\n'); - console.log('SQL output', str); return str; };