Skip to content

Commit 09c6626

Browse files
committed
Autoformat keeps cursor position after Undo
1 parent 752bcdb commit 09c6626

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

app/src/cc/arduino/packages/formatter/clangformat/ClangFormat.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import processing.app.Base;
4747
import processing.app.BaseNoGui;
4848
import processing.app.Editor;
49-
import processing.app.EditorTab;
49+
import processing.app.syntax.SketchTextArea;
5050

5151
public class ClangFormat implements Runnable {
5252

@@ -60,17 +60,27 @@ public ClangFormat(Editor editor) {
6060

6161
@Override
6262
public void run() {
63-
EditorTab tab = editor.getCurrentTab();
63+
SketchTextArea tab = editor.getCurrentTab().getTextArea();
6464
String originalText = tab.getText();
65-
int cursorOffset = tab.getTextArea().getCaretPosition();
65+
int cursorOffset = tab.getCaretPosition();
6666
try {
6767
FormatResult result = runClangFormatOn(originalText, cursorOffset);
6868
if (result.FormattedText.equals(originalText)) {
6969
editor.statusNotice(tr("No changes necessary for Auto Format."));
7070
return;
7171
}
72+
73+
// To keep cursor position after UNDO we produce a bogus edit (insertion
74+
// and removal of a " " at cursor position) and we compound this change
75+
// with the full auto-format update.
76+
tab.beginAtomicEdit();
77+
tab.insert(" ", cursorOffset);
78+
tab.replaceRange("", cursorOffset, cursorOffset + 1);
7279
tab.setText(result.FormattedText);
73-
tab.getTextArea().setCaretPosition(result.Cursor);
80+
tab.endAtomicEdit();
81+
82+
tab.setCaretPosition(result.Cursor);
83+
7484
editor.statusNotice(tr("Auto Format finished."));
7585
} catch (IOException | InterruptedException e) {
7686
editor.statusError("Auto format error: " + e.getMessage());

app/test/processing/app/AutoformatProducesOneUndoActionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public void shouldSaveCaretPositionAfterAutoformat() {
7474
String formattedText = editor.getText();
7575
assertEquals(SOURCE_AFTER, formattedText);
7676

77-
assertEquals(29, editor.getCaretPosition());
77+
// Autoformat with clang-format keeps cursor relative to source code
78+
assertEquals(17, editor.getCaretPosition());
7879

7980
menuEditUndo.requireEnabled();
8081
menuEditUndo.click();

app/test/processing/app/AutoformatSavesCaretPositionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public void shouldSaveCaretPositionAfterAutoformat() {
6868
"\n" +
6969
"}", formattedText);
7070

71-
assertEquals(29, editor.getCaretPosition());
71+
// Autoformat with clang-format keeps cursor relative to source code
72+
assertEquals(17, editor.getCaretPosition());
7273

7374
}
7475

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy