Skip to content

Commit

Permalink
close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
vingorius committed Feb 24, 2016
1 parent a9fc801 commit 11b884a
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 185 deletions.
114 changes: 22 additions & 92 deletions lib/jade-beautify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var pugBeautify = require('pug-beautify');
var CompositeDisposable = require('atom').CompositeDisposable;

var jadeBeautify = {
subscriptions: new CompositeDisposable(),
debug: function() {
return true;
},
DEBUG: false,
config: {
FILL_TAB: {
title: 'Indent with Tabs',
Expand Down Expand Up @@ -32,7 +31,7 @@ var jadeBeautify = {
},
},
activate: function() {
if (jadeBeautify.debug()) console.log('JadeBeautify is activated!!');
if (jadeBeautify.DEBUG) console.log('JadeBeautify is activated with pugBeautify!!');
this.subscriptions.add(atom.commands.add('atom-workspace', 'jade-beautify:convert', this.handleConvert));
this.subscriptions.add(atom.commands.add('atom-workspace', 'core:save', this.handleSaveEvent));
this.subscriptions.add(atom.commands.add('atom-workspace', 'window:save-all', this.handleSaveAllEvent));
Expand All @@ -49,13 +48,13 @@ var jadeBeautify = {
},
// trigger on command
handleConvert: function() {
if (jadeBeautify.debug()) console.log('handleConvert');
if (jadeBeautify.DEBUG) console.log('handleConvert');
var editor = atom.workspace.getActiveTextEditor();
jadeBeautify.convert(editor);
},
// trigger on core:save
handleSaveEvent: function() {
if (jadeBeautify.debug()) console.log('handleSaveEvent');
if (jadeBeautify.DEBUG) console.log('handleSaveEvent');
var beautify = atom.config.get('jade-beautify.BEAUTIFY_ON_SAVE');
if (!beautify) return;
// It works only jade file.
Expand All @@ -64,111 +63,42 @@ var jadeBeautify = {
},
// trigger on window:save-all
handleSaveAllEvent: function() {
if (jadeBeautify.debug()) console.log('handleSaveAllEvent');
if (jadeBeautify.DEBUG) console.log('handleSaveAllEvent');
var beautify = atom.config.get('jade-beautify.BEAUTIFY_ON_SAVE_ALL');
if (!beautify) return;

var editors = atom.workspace.getTextEditors();
editors.forEach(function(editor) {
if(editor.isModified())
if (editor.isModified())
jadeBeautify.convert(editor);
});
},
convert: function(editor) {
// It works only jade file.
if (!jadeBeautify.isJadeFile(editor)) return;
if (jadeBeautify.debug()) console.log('convert ', editor.getPath());

// var debug = false;
var fill_tab = atom.config.get('jade-beautify.FILL_TAB');
var omit_div = atom.config.get('jade-beautify.OMIT_DIV');

if (jadeBeautify.debug()) console.log(fill_tab, omit_div);
var option = {
fill_tab: atom.config.get('jade-beautify.FILL_TAB'),
omit_div: atom.config.get('jade-beautify.OMIT_DIV'),
tab_size: editor.getTabLength()
};

// var editor = atom.workspace.getActiveTextEditor();
var tabSize = editor.getTabLength();
var indentList = [];
if (jadeBeautify.DEBUG) console.log(option);

// Moves the cursor to the end of the file on save. #6
var prev_position = editor.getCursorScreenPosition();

var prevIndent = {
type: 'code', // type 'code','remark'
indent: 0, // count of indent space. it replace tab as space
tab: 0, // count of tab ,line indent will be filled up with this value.
input: '', // input line after removing indent.
};

var lines = editor.getText().split('\n');

lines.forEach(function(line, n) {
// it return matching space --> data[0], data[index] = 0, remained input --> data.input
var data = line.match(/^\s*/);

// when tab and space mixed, it replace all tab to spaces.
var tmp = data[0].replace(/\t/g, Array(tabSize + 1).join(' '));
var remainedInput = data.input.replace(/^\s*/, '');
var indent = (remainedInput.length === 0) ? 0 : tmp.length;

var tab = 0;
var type = (remainedInput.match(/^\/\/|^\/\*|^\*/)) ? 'remark' : 'code';

if (omit_div) {
remainedInput = remainedInput.replace(/^div(\.|#)/i, '$1');
}

if (indent === 0) {
tab = 0;
} else {
// when this line & prev line is 'remark', it fallow prev line tab.
if (indentList.length > 0 && type === 'remark' && indentList[indentList.length - 1].type == 'remark') {
tab = prevIndent.tab;
} else {
if (indent === prevIndent.indent) { // when same indent, follow prev tab.
tab = prevIndent.tab;
} else if (indent > prevIndent.indent) { // when indented, add tab
tab = prevIndent.tab + 1;
} else { // when new indent, if find the same indent, and follow it's tab.
for (i = indentList.length - 1; i >= 0; i--) {
if (indent == indentList[i].indent) {
tab = indentList[i].tab;
break;
}
}
}
}
}
// if (jadeBeautify.debug()) console.log(n + 1, indent, tab, prevIndent.indent);

var curIndent = {
type: type,
indent: indent,
tab: tab,
input: remainedInput,
};

indentList.push(curIndent);

if (remainedInput.length !== 0) { // discard null line
prevIndent = curIndent;
}
});

// // Here,it reformat with it's tab count.
var formatedLine = indentList.map(function(line, n) {
var space = Array(line.tab + 1).join('\t');
//when fill with space
if (!fill_tab) space = space.replace(/\t/g, Array(tabSize + 1).join(' '));

// if (jadeBeautify.debug()) console.log(n + 1, line.indent, line.tab, space + line.input);
return space + line.input;
});

//Rewrite data
editor.setText(formatedLine.join('\n'));
// Beautify Jade(Pug) Template.
try {
var beautifiedCode = pugBeautify(editor.getText(),option);
editor.setText(beautifiedCode);
if (prev_position) editor.setCursorScreenPosition(prev_position);
}catch(error){
// error occured
atom.notifications.addError('Fail to beautify.',{detail:error});
}

// #6 Move Cursor to Previous Postion.
if (prev_position) editor.setCursorScreenPosition(prev_position);
}
};
module.exports = jadeBeautify;
92 changes: 0 additions & 92 deletions lib/jade-beautify.js.bak

This file was deleted.

Loading

0 comments on commit 11b884a

Please sign in to comment.