mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-31 20:52:13 +01:00
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.
This commit is contained in:
parent
6558c0654e
commit
f57b90c1c8
@ -33,10 +33,8 @@ import processing.app.Base;
|
||||
import processing.app.BaseNoGui;
|
||||
import processing.app.Editor;
|
||||
import processing.app.helpers.FileUtils;
|
||||
import processing.app.syntax.SketchTextArea;
|
||||
import processing.app.tools.Tool;
|
||||
|
||||
import javax.swing.text.BadLocationException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@ -86,53 +84,12 @@ public class AStyle implements Tool {
|
||||
return;
|
||||
}
|
||||
|
||||
SketchTextArea textArea = editor.getCurrentTab().getTextArea();
|
||||
|
||||
int line = getLineOfOffset(textArea);
|
||||
int lineOffset = getLineOffset(textArea, line);
|
||||
|
||||
editor.getCurrentTab().setText(formattedText);
|
||||
|
||||
if (line != -1 && lineOffset != -1) {
|
||||
try {
|
||||
setCaretPosition(textArea, line, lineOffset);
|
||||
} catch (BadLocationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// mark as finished
|
||||
editor.statusNotice(tr("Auto Format finished."));
|
||||
}
|
||||
|
||||
private void setCaretPosition(SketchTextArea textArea, int line, int lineOffset) throws BadLocationException {
|
||||
int caretPosition;
|
||||
if (line < textArea.getLineCount()) {
|
||||
caretPosition = Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getLineEndOffset(line) - 1);
|
||||
} else {
|
||||
caretPosition = textArea.getText().length() - 1;
|
||||
}
|
||||
textArea.setCaretPosition(caretPosition);
|
||||
}
|
||||
|
||||
private int getLineOffset(SketchTextArea textArea, int line) {
|
||||
try {
|
||||
return textArea.getCaretPosition() - textArea.getLineStartOffset(line);
|
||||
} catch (BadLocationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int getLineOfOffset(SketchTextArea textArea) {
|
||||
try {
|
||||
return textArea.getLineOfOffset(textArea.getCaretPosition());
|
||||
} catch (BadLocationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMenuTitle() {
|
||||
return tr("Auto Format");
|
||||
|
Loading…
x
Reference in New Issue
Block a user