Closes #14. Completed support for SQL.

- Updated README
- SQL now can apply the indent_char and indent_size options.
This commit is contained in:
Glavin Wiechert 2014-06-14 02:21:26 -03:00
parent 74e392b410
commit 2e57630275
5 changed files with 40 additions and 13 deletions

View File

@ -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
}
}
```

View File

@ -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
}
}

View File

@ -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

View 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;

View File

@ -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;
};