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; 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() { function beautify() {
var text; var text;
var editor = atom.workspace.getActiveEditor(); var editor = atom.workspace.getActiveEditor();
@ -103,6 +127,7 @@ 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();
@ -111,18 +136,18 @@ function beautify() {
} }
switch (editor.getGrammar().name) { switch (editor.getGrammar().name) {
case 'JavaScript': case 'JavaScript':
text = beautifyJS(text, beautifyOptions); text = beautifyJS(text, beautifyOptions);
break; break;
case 'HTML': case 'HTML':
case 'XML': case 'XML':
text = beautifyHTML(text, beautifyOptions); text = beautifyHTML(text, beautifyOptions);
break; break;
case 'CSS': case 'CSS':
text = beautifyCSS(text, beautifyOptions); text = beautifyCSS(text, beautifyOptions);
break; break;
default: default:
return; return;
} }
if (isSelection) { if (isSelection) {
@ -133,10 +158,11 @@ function beautify() {
} else { } else {
editor.setText(text); editor.setText(text);
} }
setCursors(editor, posArray);
} }
function handleSafeEvent() { function handleSafeEvent() {
atom.workspace.eachEditor(function(editor) { atom.workspace.eachEditor(function (editor) {
var buffer = editor.getBuffer(); var buffer = editor.getBuffer();
plugin.unsubscribe(buffer); plugin.unsubscribe(buffer);
@ -151,10 +177,10 @@ plugin.configDefaults = {
beautifyOnSave: false beautifyOnSave: false
}; };
plugin.activate = function() { plugin.activate = function () {
handleSafeEvent(); handleSafeEvent();
plugin.subscribe(atom.config.observe( plugin.subscribe(atom.config.observe(
'atom-beautify.beautifyOnSave', 'atom-beautify.beautifyOnSave',
handleSafeEvent)); handleSafeEvent));
return atom.workspaceView.command('beautify', beautify); return atom.workspaceView.command('beautify', beautify);
}; };