From 0b8bc580de092cde08c08221cbf3540524d390fe Mon Sep 17 00:00:00 2001 From: modrz Date: Thu, 18 Jun 2020 09:46:54 +0200 Subject: [PATCH] RED-1418 single replace used for mode switch Change-Id: Ia8061c3e9d30b0348a5c3a4804cb5ebd4d82d321 --- .../views/debugshell/ShellDocumentTest.java | 38 +++++++++++++ .../views/debugshell/ShellDocument.java | 56 +++++++++++++------ 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin.tests/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocumentTest.java b/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin.tests/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocumentTest.java index 847e38be5..ea8e5a9f2 100644 --- a/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin.tests/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocumentTest.java +++ b/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin.tests/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocumentTest.java @@ -109,6 +109,44 @@ public void modeChangeIsDoneProperly_whenExpressionIsContinued() { assertThat(doc.getPromptContinuationPositionsStream()).containsExactly(continuationPosition(21, 10)); } + @Test + public void modeChangeIsDoneProperly_whenDocumentContainsSeveralExpressions() { + final ShellDocument doc = new ShellDocument("\n"); + doc.append("expr1"); + doc.continueExpressionInNewLine(); + doc.append("cont1"); + doc.executeExpression(s -> 1); + doc.putEvaluationResult(1, ExpressionType.ROBOT, Optional.of("result1"), Optional.empty()); + doc.switchTo(ExpressionType.PYTHON, "expr2"); + doc.continueExpressionInNewLine(); + doc.append("cont2"); + doc.continueExpressionInNewLine(); + doc.append("cont22"); + doc.continueExpressionInNewLine(); + doc.append("cont222"); + + assertThat(doc.getMode()).isEqualTo(ExpressionType.PYTHON); + assertThat(doc.get()).isEqualTo(content( + "ROBOT> expr1", "...... cont1", "PASS: result1", + "PYTHON> expr2", "....... cont2", "....... cont22", "....... cont222")); + assertThat(doc.getModePromptPositionsStream()).containsExactly(promptPosition(0, 7), promptPosition(40, 8)); + + doc.switchToMode(ExpressionType.VARIABLE); + assertThat(doc.getMode()).isEqualTo(ExpressionType.VARIABLE); + assertThat(doc.get()).isEqualTo(content( + "ROBOT> expr1", "...... cont1", "PASS: result1", + "VARIABLE> expr2", "......... cont2", "......... cont22", "......... cont222")); + assertThat(doc.getModePromptPositionsStream()).containsExactly(promptPosition(0, 7), promptPosition(40, 10)); + + doc.switchToMode(ExpressionType.ROBOT); + assertThat(doc.getMode()).isEqualTo(ExpressionType.ROBOT); + assertThat(doc.get()).isEqualTo(content( + "ROBOT> expr1", "...... cont1", "PASS: result1", + "ROBOT> expr2", "...... cont2", "...... cont22", "...... cont222")); + assertThat(doc.getModePromptPositionsStream()).containsExactly(promptPosition(0, 7), promptPosition(40, 7)); + } + + @Test public void modeChangeWithNewExpressionIsDoneProperly() { final ShellDocument doc = new ShellDocument("\n"); diff --git a/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocument.java b/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocument.java index fbfb51379..ba86e11b2 100644 --- a/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocument.java +++ b/src/Eclipse-IDE/org.robotframework.ide.eclipse.main.plugin/src/org/robotframework/ide/eclipse/main/plugin/views/debugshell/ShellDocument.java @@ -242,33 +242,53 @@ void switchToMode(final ExpressionType mode) { } private void switchMode(final ExpressionType newType) { - final CategorizedPosition lastModePosition = getModePromptPositionsStream() + final CategorizedPosition lastMode = getModePromptPositionsStream() .collect(maxBy(Comparator.comparing(CategorizedPosition::getOffset))) .orElseThrow(IllegalStateException::new); - final int modeOffset = lastModePosition.getOffset(); + final List lastModeContinuations = getPromptContinuationPositionsStream() + .filter(p -> p.getOffset() > lastMode.getOffset()) + .sorted(Comparator.comparing(CategorizedPosition::getOffset)) + .collect(toList()); - final int delta = newType.name().length() - type.name().length(); + final String lastExpression = getLastExpression(lastMode); + final String newExpression = createNewExpression(newType, lastMode, lastModeContinuations, lastExpression); - replace(modeOffset, lastModePosition.getLength(), formatPrompt(newType)); - extendPositions(modeOffset, delta); - shiftPositions(modeOffset, delta); + replace(lastMode.getOffset(), lastExpression.length(), newExpression); - if (delta != 0) { - final List continuations = getPromptContinuationPositionsStream() - .filter(p -> p.getOffset() > modeOffset) - .sorted((p1, p2) -> Integer.compare(p1.getOffset(), p2.getOffset())) - .collect(toList()); + Stream.concat(Stream.of(lastMode), lastModeContinuations.stream()) + .map(CategorizedPosition::getOffset) + .forEach(offset -> { + final int delta = newType.name().length() - type.name().length(); + extendPositions(offset, delta); + shiftPositions(offset, delta); + }); - while (!continuations.isEmpty()) { - final CategorizedPosition p = continuations.remove(0); + this.type = newType; + } - replace(p.getOffset(), p.getLength(), formatContinuation(newType)); - extendPositions(p.getOffset(), delta); - shiftPositions(p.getOffset(), delta); - } + private String getLastExpression(final CategorizedPosition lastMode) { + try { + return get(lastMode.getOffset(), getLength() - lastMode.getOffset()); + } catch (final BadLocationException e) { + throw new IllegalStateException(e); } + } - this.type = newType; + private String createNewExpression(final ExpressionType newType, final CategorizedPosition lastMode, + final List lastModeContinuations, final String lastExpression) { + final StringBuilder newExpression = new StringBuilder(); + newExpression.append(formatPrompt(newType)); + int beginIndex = lastMode.getLength(); + int endIndex; + for (final CategorizedPosition categorizedPosition : lastModeContinuations) { + endIndex = categorizedPosition.getOffset() - lastMode.getOffset(); + newExpression.append(lastExpression.substring(beginIndex, endIndex)); + newExpression.append(formatContinuation(newType)); + beginIndex = endIndex + categorizedPosition.getLength(); + } + endIndex = lastExpression.length(); + newExpression.append(lastExpression.substring(beginIndex, endIndex)); + return newExpression.toString(); } void switchToPreviousExpression() {