diff --git a/app/src/cc/arduino/ConsoleOutputStream.java b/app/src/cc/arduino/ConsoleOutputStream.java index 452c190b414..ea93b31a508 100644 --- a/app/src/cc/arduino/ConsoleOutputStream.java +++ b/app/src/cc/arduino/ConsoleOutputStream.java @@ -95,7 +95,7 @@ private void printInConsole(String text) { if (editorConsole != null) { SwingUtilities.invokeLater(() -> { try { - editorConsole.insertString(text, attributes); + editorConsole.insertString(this, text, attributes); } catch (BadLocationException ble) { //ignore } diff --git a/app/src/processing/app/EditorConsole.java b/app/src/processing/app/EditorConsole.java index 851f46e8af3..29381aea927 100644 --- a/app/src/processing/app/EditorConsole.java +++ b/app/src/processing/app/EditorConsole.java @@ -57,6 +57,7 @@ public static void setCurrentEditorConsole(EditorConsole console) { private final DefaultStyledDocument document; private final JTextPane consoleTextPane; + private int firstErrorOffset = -1; public EditorConsole() { document = new DefaultStyledDocument(); @@ -117,21 +118,42 @@ public void clear() { // ignore the error otherwise this will cause an infinite loop // maybe not a good idea in the long run? } + firstErrorOffset = -1; } public void scrollDown() { - getHorizontalScrollBar().setValue(0); - getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum()); + if (firstErrorOffset >= 0) { + try { + // Scroll to the first error, making sure that the line above the error + // is the first one shown. + int offset = Utilities.getPositionAbove(consoleTextPane, + firstErrorOffset, 0); + if (offset < 0) + offset = firstErrorOffset; + Rectangle rect = consoleTextPane.modelToView(offset); + this.getViewport().setViewPosition(new Point(0, rect.y)); + } catch (BadLocationException e) { + // Ignore + } + } else { + getHorizontalScrollBar().setValue(0); + getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum()); + } } public boolean isEmpty() { return document.getLength() == 0; } - public void insertString(String line, SimpleAttributeSet attributes) throws BadLocationException { + public void insertString(ConsoleOutputStream from, String line, + SimpleAttributeSet attributes) + throws BadLocationException { line = line.replace("\r\n", "\n").replace("\r", "\n"); int offset = document.getLength(); document.insertString(offset, line, attributes); + if (from == err && firstErrorOffset < 0) { + firstErrorOffset = offset; + } } public String getText() {