Skip to content

Commit f57b90c

Browse files
matthijskooijmanfacchinm
authored andcommitted
Remove code that preserves caret position during auto format
`EditorTab.setText()` now already preserves the caret position. The code used during auto-format tried a bit harder to preserve the position correctly, and probably worked better in a few specific cases, but for most cases they would both end up approximating the caret position anyway. To make the code simpler, better just stick to the simpler approach.
1 parent 6558c06 commit f57b90c

File tree

1 file changed

+0
-43
lines changed

1 file changed

+0
-43
lines changed

app/src/cc/arduino/packages/formatter/AStyle.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
import processing.app.BaseNoGui;
3434
import processing.app.Editor;
3535
import processing.app.helpers.FileUtils;
36-
import processing.app.syntax.SketchTextArea;
3736
import processing.app.tools.Tool;
3837

39-
import javax.swing.text.BadLocationException;
4038
import java.io.File;
4139
import java.io.IOException;
4240

@@ -86,53 +84,12 @@ public void run() {
8684
return;
8785
}
8886

89-
SketchTextArea textArea = editor.getCurrentTab().getTextArea();
90-
91-
int line = getLineOfOffset(textArea);
92-
int lineOffset = getLineOffset(textArea, line);
93-
9487
editor.getCurrentTab().setText(formattedText);
9588

96-
if (line != -1 && lineOffset != -1) {
97-
try {
98-
setCaretPosition(textArea, line, lineOffset);
99-
} catch (BadLocationException e) {
100-
e.printStackTrace();
101-
}
102-
}
103-
10489
// mark as finished
10590
editor.statusNotice(tr("Auto Format finished."));
10691
}
10792

108-
private void setCaretPosition(SketchTextArea textArea, int line, int lineOffset) throws BadLocationException {
109-
int caretPosition;
110-
if (line < textArea.getLineCount()) {
111-
caretPosition = Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getLineEndOffset(line) - 1);
112-
} else {
113-
caretPosition = textArea.getText().length() - 1;
114-
}
115-
textArea.setCaretPosition(caretPosition);
116-
}
117-
118-
private int getLineOffset(SketchTextArea textArea, int line) {
119-
try {
120-
return textArea.getCaretPosition() - textArea.getLineStartOffset(line);
121-
} catch (BadLocationException e) {
122-
e.printStackTrace();
123-
}
124-
return -1;
125-
}
126-
127-
private int getLineOfOffset(SketchTextArea textArea) {
128-
try {
129-
return textArea.getLineOfOffset(textArea.getCaretPosition());
130-
} catch (BadLocationException e) {
131-
e.printStackTrace();
132-
}
133-
return -1;
134-
}
135-
13693
@Override
13794
public String getMenuTitle() {
13895
return tr("Auto Format");

0 commit comments

Comments
 (0)