46
46
import processing .app .Base ;
47
47
import processing .app .BaseNoGui ;
48
48
import processing .app .Editor ;
49
- import processing .app .EditorTab ;
49
+ import processing .app .syntax . SketchTextArea ;
50
50
51
51
public class ClangFormat implements Runnable {
52
52
@@ -60,17 +60,27 @@ public ClangFormat(Editor editor) {
60
60
61
61
@ Override
62
62
public void run () {
63
- EditorTab tab = editor .getCurrentTab ();
63
+ SketchTextArea tab = editor .getCurrentTab (). getTextArea ();
64
64
String originalText = tab .getText ();
65
- int cursorOffset = tab .getTextArea (). getCaretPosition ();
65
+ int cursorOffset = tab .getCaretPosition ();
66
66
try {
67
67
FormatResult result = runClangFormatOn (originalText , cursorOffset );
68
68
if (result .FormattedText .equals (originalText )) {
69
69
editor .statusNotice (tr ("No changes necessary for Auto Format." ));
70
70
return ;
71
71
}
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 );
72
79
tab .setText (result .FormattedText );
73
- tab .getTextArea ().setCaretPosition (result .Cursor );
80
+ tab .endAtomicEdit ();
81
+
82
+ tab .setCaretPosition (result .Cursor );
83
+
74
84
editor .statusNotice (tr ("Auto Format finished." ));
75
85
} catch (IOException | InterruptedException e ) {
76
86
editor .statusError ("Auto format error: " + e .getMessage ());
0 commit comments