Fixed #8 and resolved #7, new release

This commit is contained in:
László Károlyi 2014-05-14 23:05:19 +02:00
parent 1006d1c243
commit 693c1248fc
2 changed files with 36 additions and 19 deletions

View File

@ -80,17 +80,15 @@ function getCursors(editor) {
for (var idx = 0; idx < cursors.length; idx++) { for (var idx = 0; idx < cursors.length; idx++) {
var cursor = cursors[idx]; var cursor = cursors[idx];
var bufferPosition = cursor.getBufferPosition(); var bufferPosition = cursor.getBufferPosition();
console.log('order, row:', idx, bufferPosition.row);
posArray.push([bufferPosition.row, bufferPosition.column]); posArray.push([bufferPosition.row, bufferPosition.column]);
} }
return posArray; return posArray;
} }
function setCursors(editor, posArray) { function setCursors(editor, posArray) {
for (var idx = posArray.length - 1; idx >= 0; idx--) { for (var idx = 0; idx < posArray.length; idx++) {
var bufferPosition = posArray[idx]; var bufferPosition = posArray[idx];
console.log('setting row:', bufferPosition[0]); if (idx === 0) {
if (idx === posArray.length - 1) {
editor.setCursorBufferPosition(bufferPosition); editor.setCursorBufferPosition(bufferPosition);
continue; continue;
} }
@ -107,16 +105,26 @@ function beautify() {
var beautifyOptions = { var beautifyOptions = {
'indent_size': softTabs ? tabLength : 1, 'indent_size': softTabs ? tabLength : 1,
'indent_char': softTabs ? ' ' : '\t' 'indent_char': softTabs ? ' ' : '\t',
'indent_with_tabs': !softTabs
}; };
// Look for .jsbeautifierrc in file and home path, check env variables // Look for .jsbeautifierrc in file and home path, check env variables
var editedFilePath = editor.getPath(); var editedFilePath = editor.getPath();
var rcInRecursiveCwd;
if (editedFilePath && (rcInRecursiveCwd = findRecursive(path.dirname(
editedFilePath), '.jsbeautifyrc')) === editedFilePath) {
rcInRecursiveCwd = null;
}
var rcInHomePath;
if (editedFilePath && (rcInHomePath = verifyExists(path.join(getUserHome() ||
'', '.jsbeautifyrc'))) === editedFilePath) {
rcInHomePath = null;
}
var cfg = cc( var cfg = cc(
cleanOptions(cc.env('jsbeautify_'), knownOpts), cleanOptions(cc.env('jsbeautify_'), knownOpts),
editedFilePath ? findRecursive(path.dirname(editedFilePath), rcInRecursiveCwd,
'.jsbeautifyrc') : null, rcInHomePath
verifyExists(path.join(getUserHome() || '', '.jsbeautifyrc'))
).list; ).list;
// cc(...).snapshot SHOULD contain the same what I construct below, // cc(...).snapshot SHOULD contain the same what I construct below,
// however I have not the faintest idea why it doesn't work here. // however I have not the faintest idea why it doesn't work here.
@ -127,13 +135,13 @@ function beautify() {
} }
// Override the indenting options from the editor // Override the indenting options from the editor
beautifyOptions = extend(collectedConfig, beautifyOptions); beautifyOptions = extend(collectedConfig, beautifyOptions);
var posArray = getCursors(editor);
if (isSelection) { if (isSelection) {
text = editor.getSelectedText(); text = editor.getSelectedText();
} else { } else {
text = editor.getText(); text = editor.getText();
} }
var oldText = text;
switch (editor.getGrammar().name) { switch (editor.getGrammar().name) {
case 'JavaScript': case 'JavaScript':
@ -149,16 +157,25 @@ function beautify() {
default: default:
return; return;
} }
if (oldText !== text) {
if (isSelection) { var posArray = getCursors(editor);
editor.setTextInBufferRange( var origScrollTop = editor.getScrollTop();
editor.getSelectedBufferRange(), if (isSelection) {
text editor.setTextInBufferRange(
); editor.getSelectedBufferRange(),
} else { text
editor.setText(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);
} }
setCursors(editor, posArray);
} }
function handleSafeEvent() { function handleSafeEvent() {

View File

@ -1,7 +1,7 @@
{ {
"name": "atom-beautify", "name": "atom-beautify",
"main": "./lib/atom-beautify", "main": "./lib/atom-beautify",
"version": "0.2.4", "version": "0.2.5",
"private": true, "private": true,
"description": "Beautify HTML, CSS and Javascript in Atom", "description": "Beautify HTML, CSS and Javascript in Atom",
"repository": "https://github.com/donaldpipowitch/atom-beautify", "repository": "https://github.com/donaldpipowitch/atom-beautify",