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++) {
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() {

View File

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