From ac131652d935c611036f4b9766e6ff31d1648e00 Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Sat, 1 Apr 2023 13:24:27 +0900 Subject: [PATCH] Avoid being scrolled to the end of the editor in the preview panel for formatting options - Currently, when the values of the formatting options are changed, the preview panel is scrolled to the end of the editor - To avoid it, keep the caret position before reformatting --- .../netbeans/modules/php/editor/indent/FmtOptions.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java index 63fa8aa45e5a..0a3fae7ad10e 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FmtOptions.java @@ -44,6 +44,7 @@ import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; +import javax.swing.SwingUtilities; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; @@ -543,6 +544,10 @@ public void refreshPreview() { // Ignore it } + // keep the caret position + // to avoid being scrolled to the end of the editor + int caretPosition = pane.getCaretPosition(); + Rectangle visibleRectangle = pane.getVisibleRect(); pane.setText(previewText); pane.setIgnoreRepaint(true); @@ -569,8 +574,10 @@ public void run() { } else { LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N } + pane.setCaretPosition(caretPosition); pane.setIgnoreRepaint(false); - pane.scrollRectToVisible(visibleRectangle); + // invoke later because the preview pane is scrolled to the caret position when we change options after we scroll it anywhere + SwingUtilities.invokeLater(() -> pane.scrollRectToVisible(visibleRectangle)); pane.repaint(100); }