From f57b90c1c803677bc7338cd4ecc5ff56c096b0ce Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Wed, 9 Dec 2015 12:27:00 +0100 Subject: [PATCH] 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. --- .../cc/arduino/packages/formatter/AStyle.java | 43 ------------------- 1 file changed, 43 deletions(-) diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index a0d75d6be..70b6717ff 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -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");