mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
Formatter: cursor position is saved when invoking autoformat. Fixes #2293
This commit is contained in:
parent
170816181a
commit
1b783fa48c
@ -3,6 +3,7 @@ package cc.arduino.packages.formatter;
|
|||||||
import processing.app.Base;
|
import processing.app.Base;
|
||||||
import processing.app.Editor;
|
import processing.app.Editor;
|
||||||
import processing.app.helpers.FileUtils;
|
import processing.app.helpers.FileUtils;
|
||||||
|
import processing.app.syntax.JEditTextArea;
|
||||||
import processing.app.tools.Tool;
|
import processing.app.tools.Tool;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -54,8 +55,13 @@ public class AStyle implements Tool {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JEditTextArea textArea = editor.getTextArea();
|
||||||
|
int line = textArea.getLineOfOffset(textArea.getCaretPosition());
|
||||||
|
int lineOffset = textArea.getCaretPosition() - textArea.getLineStartOffset(line);
|
||||||
|
|
||||||
editor.setText(formattedText);
|
editor.setText(formattedText);
|
||||||
editor.getSketch().setModified(true);
|
editor.getSketch().setModified(true);
|
||||||
|
textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getSafeLineStopOffset(line) - 1));
|
||||||
// mark as finished
|
// mark as finished
|
||||||
editor.statusNotice(_("Auto Format finished."));
|
editor.statusNotice(_("Auto Format finished."));
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package processing.app;
|
|||||||
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
|
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
|
||||||
import org.fest.swing.edt.GuiActionRunner;
|
import org.fest.swing.edt.GuiActionRunner;
|
||||||
import org.fest.swing.edt.GuiQuery;
|
import org.fest.swing.edt.GuiQuery;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import processing.app.helpers.ArduinoFrameFixture;
|
import processing.app.helpers.ArduinoFrameFixture;
|
||||||
|
|
||||||
@ -32,4 +33,9 @@ public abstract class AbstractGUITest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void stopTheIDE() {
|
||||||
|
window.cleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
package processing.app;
|
||||||
|
|
||||||
|
import org.fest.swing.fixture.JMenuItemFixture;
|
||||||
|
import org.junit.Test;
|
||||||
|
import processing.app.helpers.JEditTextAreaFixture;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class AutoformatSavesCaretPositionTest extends AbstractGUITest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSaveCaretPositionAfterAutoformat() {
|
||||||
|
JMenuItemFixture menuToolsAutoFormat = window.menuItem("menuToolsAutoFormat");
|
||||||
|
menuToolsAutoFormat.requireEnabled();
|
||||||
|
|
||||||
|
JEditTextAreaFixture editor = window.jEditTextArea("editor");
|
||||||
|
editor.setText("void setup() {\n" +
|
||||||
|
" // put your setup code here, to run once:\n" +
|
||||||
|
"\n" +
|
||||||
|
"}\n" +
|
||||||
|
"\n" +
|
||||||
|
"void loop() {\n" +
|
||||||
|
" // put your main code here, to run repeatedly:\n" +
|
||||||
|
"\n" +
|
||||||
|
"}");
|
||||||
|
|
||||||
|
editor.setCaretPosition(29); // right before the first // (double slash)
|
||||||
|
|
||||||
|
menuToolsAutoFormat.click();
|
||||||
|
|
||||||
|
String formattedText = editor.getText();
|
||||||
|
assertEquals("void setup() {\n" +
|
||||||
|
" // put your setup code here, to run once:\n" +
|
||||||
|
"\n" +
|
||||||
|
"}\n" +
|
||||||
|
"\n" +
|
||||||
|
"void loop() {\n" +
|
||||||
|
" // put your main code here, to run repeatedly:\n" +
|
||||||
|
"\n" +
|
||||||
|
"}", formattedText);
|
||||||
|
|
||||||
|
assertEquals(29, editor.getCaretPosition());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -51,4 +51,29 @@ public class JEditTextAreaComponentDriver extends JComponentDriver {
|
|||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getCaretPosition(final JEditTextArea target) {
|
||||||
|
focusAndWaitForFocusGain(target);
|
||||||
|
return GuiActionRunner.execute(new GuiQuery<Integer>() {
|
||||||
|
|
||||||
|
protected Integer executeInEDT() {
|
||||||
|
return target.getCaretPosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCaretPosition(final JEditTextArea target, final int caretPosition) {
|
||||||
|
focusAndWaitForFocusGain(target);
|
||||||
|
GuiActionRunner.execute(new GuiQuery<JEditTextArea>() {
|
||||||
|
|
||||||
|
protected JEditTextArea executeInEDT() {
|
||||||
|
target.setCaretPosition(caretPosition);
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
robot.waitForIdle();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package processing.app.helpers;
|
|||||||
|
|
||||||
import org.fest.swing.core.Robot;
|
import org.fest.swing.core.Robot;
|
||||||
import org.fest.swing.fixture.ComponentFixture;
|
import org.fest.swing.fixture.ComponentFixture;
|
||||||
|
|
||||||
import processing.app.syntax.JEditTextArea;
|
import processing.app.syntax.JEditTextArea;
|
||||||
|
|
||||||
public class JEditTextAreaFixture extends ComponentFixture {
|
public class JEditTextAreaFixture extends ComponentFixture {
|
||||||
@ -42,4 +41,12 @@ public class JEditTextAreaFixture extends ComponentFixture {
|
|||||||
driver.selectAll((JEditTextArea) target);
|
driver.selectAll((JEditTextArea) target);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getCaretPosition() {
|
||||||
|
return driver.getCaretPosition((JEditTextArea) target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCaretPosition(int caretPosition) {
|
||||||
|
driver.setCaretPosition((JEditTextArea) target, caretPosition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user