First thought solution for #7

Might not be perfect because of multiple cursor placement
This commit is contained in:
László Károlyi 2014-05-14 11:37:53 +02:00
parent 9143b7b8c3
commit 1006d1c243
1 changed files with 41 additions and 15 deletions

View File

@ -74,6 +74,30 @@ function cleanOptions(data, types) {
return data;
}
function getCursors(editor) {
var cursors = editor.getCursors();
var posArray = [];
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--) {
var bufferPosition = posArray[idx];
console.log('setting row:', bufferPosition[0]);
if (idx === posArray.length - 1) {
editor.setCursorBufferPosition(bufferPosition);
continue;
}
editor.addCursorAtBufferPosition(bufferPosition);
}
}
function beautify() {
var text;
var editor = atom.workspace.getActiveEditor();
@ -103,6 +127,7 @@ function beautify() {
}
// Override the indenting options from the editor
beautifyOptions = extend(collectedConfig, beautifyOptions);
var posArray = getCursors(editor);
if (isSelection) {
text = editor.getSelectedText();
@ -111,18 +136,18 @@ function beautify() {
}
switch (editor.getGrammar().name) {
case 'JavaScript':
text = beautifyJS(text, beautifyOptions);
break;
case 'HTML':
case 'XML':
text = beautifyHTML(text, beautifyOptions);
break;
case 'CSS':
text = beautifyCSS(text, beautifyOptions);
break;
default:
return;
case 'JavaScript':
text = beautifyJS(text, beautifyOptions);
break;
case 'HTML':
case 'XML':
text = beautifyHTML(text, beautifyOptions);
break;
case 'CSS':
text = beautifyCSS(text, beautifyOptions);
break;
default:
return;
}
if (isSelection) {
@ -133,10 +158,11 @@ function beautify() {
} else {
editor.setText(text);
}
setCursors(editor, posArray);
}
function handleSafeEvent() {
atom.workspace.eachEditor(function(editor) {
atom.workspace.eachEditor(function (editor) {
var buffer = editor.getBuffer();
plugin.unsubscribe(buffer);
@ -151,10 +177,10 @@ plugin.configDefaults = {
beautifyOnSave: false
};
plugin.activate = function() {
plugin.activate = function () {
handleSafeEvent();
plugin.subscribe(atom.config.observe(
'atom-beautify.beautifyOnSave',
handleSafeEvent));
return atom.workspaceView.command('beautify', beautify);
};
};