From 2db49ea860125478bf69c8a3f1aa3afeca122a10 Mon Sep 17 00:00:00 2001 From: Denis Zhdanov Date: Thu, 23 Jun 2011 16:59:20 +0400 Subject: [PATCH] IDEA-71147 Automatically 'scroll to the end' Scrolling to the console end in case long soft wrapped lines were added while the console was not showing. --- .../execution/impl/ConsoleViewImpl.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java index 569747269246..ab2add867638 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java +++ b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java @@ -39,10 +39,7 @@ import com.intellij.openapi.editor.actionSystem.*; import com.intellij.openapi.editor.actions.ScrollToTheEndToolbarAction; import com.intellij.openapi.editor.actions.ToggleUseSoftWrapsToolbarAction; import com.intellij.openapi.editor.colors.EditorColorsScheme; -import com.intellij.openapi.editor.event.DocumentAdapter; -import com.intellij.openapi.editor.event.DocumentEvent; -import com.intellij.openapi.editor.event.DocumentListener; -import com.intellij.openapi.editor.event.EditorMouseEvent; +import com.intellij.openapi.editor.event.*; import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.editor.ex.FoldingModelEx; import com.intellij.openapi.editor.ex.util.EditorUtil; @@ -403,6 +400,25 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo } } }); + + myEditor.getScrollingModel().addVisibleAreaListener(new VisibleAreaListener() { + @Override + public void visibleAreaChanged(VisibleAreaEvent e) { + // There is a possible case that the console text is populated while the console is not shown (e.g. we're debugging and + // 'Debugger' tab is active while 'Console' is not). It's also possible that newly added text contains long lines that + // are soft wrapped. We want to update viewport position then when the console becomes visible. + final Rectangle oldRectangle = e.getOldRectangle(); + final Rectangle newRectangle = e.getNewRectangle(); + if (oldRectangle == null || newRectangle == null) { + return; + } + + if (oldRectangle.height <= 0 && newRectangle.height > 0 && myEditor.getSoftWrapModel().isSoftWrappingEnabled() + && myEditor.getCaretModel().getOffset() == myEditor.getDocument().getTextLength()) { + EditorUtil.scrollToTheEnd(myEditor); + } + } + }); } return this; }