From 6580fbecdaddafaf92ea9c659c884fdaf8ea2e1c Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 5 Apr 2017 16:37:42 +0300 Subject: [PATCH] expand "\r" treatment on to the previous lines in document to fix IDEA-170961 sys.stdout is severly damaged and does not execute '\r' correctly --- .../intellij/execution/impl/ConsoleViewImpl.java | 2 +- .../com/intellij/execution/impl/TokenBuffer.java | 15 ++++++++------- .../execution/impl/ConsoleViewImplTest.java | 10 ++++++++++ 3 files changed, 19 insertions(+), 8 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 653d13e610cf..3a4e459274a1 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java +++ b/platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewImpl.java @@ -685,7 +685,7 @@ public class ConsoleViewImpl extends JPanel implements ConsoleView, ObservableCo int startIndex = startsWithCR ? 1 : 0; for (int i = startIndex; i < deferredTokens.size(); i++) { TokenBuffer.TokenInfo deferredToken = deferredTokens.get(i); - addedText.append(deferredToken.getText()); + addedText.append(deferredToken.getText()); // can just append texts because \r inside these tokens were already taken care of } if (startsWithCR) { // remove last line if any diff --git a/platform/lang-impl/src/com/intellij/execution/impl/TokenBuffer.java b/platform/lang-impl/src/com/intellij/execution/impl/TokenBuffer.java index acad8565b963..98351d1390a1 100644 --- a/platform/lang-impl/src/com/intellij/execution/impl/TokenBuffer.java +++ b/platform/lang-impl/src/com/intellij/execution/impl/TokenBuffer.java @@ -65,13 +65,8 @@ class TokenBuffer { TokenInfo tokenInfo = new TokenInfo(contentType, text.substring(start, crIndex), info); tokens.addLast(tokenInfo); size += tokenInfo.length(); - removeLastLine(); - } - else if (size == 0) { - // \r at the very beginning. return CR_TOKEN to signal this - tokens.addLast(CR_TOKEN); - size ++; } + removeLastLine(); // text[start..crIndex) should be removed start = crIndex + 1; } @@ -110,10 +105,16 @@ class TokenBuffer { TokenInfo newToken = new TokenInfo(last.contentType, text.substring(0, lfIndex + 1), last.getHyperlinkInfo()); tokens.addLast(newToken); size -= text.length() - newToken.length(); - break; + return; } + // remove the token entirely, move to the previous size -= text.length(); } + if (tokens.isEmpty()) { + // \r at the very beginning. return CR_TOKEN to signal this + tokens.addLast(CR_TOKEN); + size ++; + } } private void trim() { diff --git a/platform/platform-tests/testSrc/com/intellij/execution/impl/ConsoleViewImplTest.java b/platform/platform-tests/testSrc/com/intellij/execution/impl/ConsoleViewImplTest.java index 8ba659972832..1d93ebb40354 100644 --- a/platform/platform-tests/testSrc/com/intellij/execution/impl/ConsoleViewImplTest.java +++ b/platform/platform-tests/testSrc/com/intellij/execution/impl/ConsoleViewImplTest.java @@ -374,4 +374,14 @@ public class ConsoleViewImplTest extends LightPlatformTestCase { () -> EditorTestUtil.executeAction(editor, true, handler), "", null, editor.getDocument()); } + + public void testCRPrintCR() throws Exception { + for (int i=0;i<25;i++) { + myConsole.print("\r"+i, ConsoleViewContentType.NORMAL_OUTPUT); + Thread.sleep(100); + } + myConsole.flushDeferredText(); + myConsole.waitAllRequests(); + assertEquals("24", myConsole.getText()); + } }