From 693c1248fc317d3c36bb198e277dfa1ca6a4bb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?La=CC=81szlo=CC=81=20Ka=CC=81rolyi?= Date: Wed, 14 May 2014 23:05:19 +0200 Subject: [PATCH] Fixed #8 and resolved #7, new release --- lib/atom-beautify.js | 53 +++++++++++++++++++++++++++++--------------- package.json | 2 +- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/atom-beautify.js b/lib/atom-beautify.js index 7f01211..155d5f2 100644 --- a/lib/atom-beautify.js +++ b/lib/atom-beautify.js @@ -80,17 +80,15 @@ function getCursors(editor) { for (var idx = 0; idx < cursors.length; idx++) { var cursor = cursors[idx]; var bufferPosition = cursor.getBufferPosition(); - console.log('order, row:', idx, bufferPosition.row); posArray.push([bufferPosition.row, bufferPosition.column]); } return 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]; - console.log('setting row:', bufferPosition[0]); - if (idx === posArray.length - 1) { + if (idx === 0) { editor.setCursorBufferPosition(bufferPosition); continue; } @@ -107,16 +105,26 @@ function beautify() { var beautifyOptions = { '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 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( cleanOptions(cc.env('jsbeautify_'), knownOpts), - editedFilePath ? findRecursive(path.dirname(editedFilePath), - '.jsbeautifyrc') : null, - verifyExists(path.join(getUserHome() || '', '.jsbeautifyrc')) + rcInRecursiveCwd, + rcInHomePath ).list; // cc(...).snapshot SHOULD contain the same what I construct below, // 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 beautifyOptions = extend(collectedConfig, beautifyOptions); - var posArray = getCursors(editor); if (isSelection) { text = editor.getSelectedText(); } else { text = editor.getText(); } + var oldText = text; switch (editor.getGrammar().name) { case 'JavaScript': @@ -149,16 +157,25 @@ function beautify() { default: return; } - - if (isSelection) { - editor.setTextInBufferRange( - editor.getSelectedBufferRange(), - text - ); - } else { - editor.setText(text); + if (oldText !== text) { + var posArray = getCursors(editor); + var origScrollTop = editor.getScrollTop(); + if (isSelection) { + editor.setTextInBufferRange( + editor.getSelectedBufferRange(), + 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() { diff --git a/package.json b/package.json index 6712229..2e2f9b1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "atom-beautify", "main": "./lib/atom-beautify", - "version": "0.2.4", + "version": "0.2.5", "private": true, "description": "Beautify HTML, CSS and Javascript in Atom", "repository": "https://github.com/donaldpipowitch/atom-beautify",