Skip to content

Commit

Permalink
Closes #14. Completed support for SQL.
Browse files Browse the repository at this point in the history
- Updated README
- SQL now can apply the indent_char and indent_size options.
  • Loading branch information
Glavin001 committed Jun 14, 2014
1 parent 74e392b commit 2e57630
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
```
Expand Down
6 changes: 6 additions & 0 deletions examples/nested-jsbeautifyrc/.jsbeautifyrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions examples/nested-jsbeautifyrc/test.sql
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -182,6 +182,7 @@ function beautify() {
encoding: 'utf8'
})));
} catch (e) {
console.log('Failed parsing config JSON at '+configPath);
externalOptions = {};
}
} else {
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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
Expand All @@ -236,7 +237,6 @@ function beautify() {

}, {});


// TODO: Clean.
// There is a bug in nopt
// See https://github.com/npm/nopt/issues/38#issuecomment-45971505
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 16 additions & 6 deletions lib/sql-beautify.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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, '~::~\'')
Expand Down Expand Up @@ -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;

};
Expand Down

0 comments on commit 2e57630

Please sign in to comment.