Skip to content

Commit d420ea8

Browse files
trancexpressiloveeclipse
authored andcommitted
Trim console lines that are longer than limit
Fixes: #2465
1 parent 251693f commit d420ea8

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

debug/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,52 @@ public void testTrim() throws Exception {
715715

716716
c.getConsole().setWaterMarks(50, 100);
717717
c.waitForScheduledJobs();
718-
c.verifyContentByOffset("0123456789", 0);
718+
c.verifyContentByOffset("0123456789", 1);
719719
assertTrue(c.getDocument().getNumberOfLines() < 15, "Document not trimmed.");
720720
}
721721
closeConsole(c);
722722
}
723723
}
724724

725+
/**
726+
* Check that trimming within a line doesn't split '\r\n'.
727+
*/
728+
@Test
729+
public void testTrimNewline() throws Exception {
730+
final IOConsoleTestUtil c = getTestUtil("Test trim newline");
731+
try (IOConsoleOutputStream out = c.getDefaultOutputStream()) {
732+
c.writeFast("first\n");
733+
c.writeFast("0123456789\r\n", out);
734+
c.write("last\n");
735+
c.verifyContentByLine("first", 0).verifyContentByLine("last", -2);
736+
assertTrue(c.getDocument().getNumberOfLines() > 2, "Document not filled.");
737+
c.getConsole().setWaterMarks(6, 16);
738+
c.waitForScheduledJobs();
739+
c.verifyContent("\r\nlast\n");
740+
closeConsole(c);
741+
}
742+
}
743+
744+
/**
745+
* Check that trimming within a line doesn't split surrogate pairs, e.g.
746+
* emoji symbols.
747+
*/
748+
@Test
749+
public void testTrimSurrogateCharacters() throws Exception {
750+
final IOConsoleTestUtil c = getTestUtil("Test trim newline");
751+
try (IOConsoleOutputStream out = c.getDefaultOutputStream()) {
752+
c.writeFast("first\n");
753+
c.writeFast("1😀2😀3\n", out);
754+
c.write("last\n");
755+
c.verifyContentByLine("first", 0).verifyContentByLine("last", -2);
756+
assertTrue(c.getDocument().getNumberOfLines() > 2, "Document not filled.");
757+
c.getConsole().setWaterMarks(8, 16);
758+
c.waitForScheduledJobs();
759+
c.verifyContentByOffset("😀3\nlas", 0);
760+
closeConsole(c);
761+
}
762+
}
763+
725764
/**
726765
* Some extra tests for IOConsolePartitioner.
727766
*/

debug/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,16 @@ private void trim(int truncateOffset, boolean truncateToOffsetLineStart) {
12031203
if (truncateToOffsetLineStart) {
12041204
int cutoffLine = document.getLineOfOffset(truncateOffset);
12051205
cutOffset = document.getLineOffset(cutoffLine);
1206+
// deal with case of one long line
1207+
int offset = Math.max(cutOffset, truncateOffset);
1208+
char c = document.getChar(offset);
1209+
if (c == '\n' && offset > 0 && document.getChar(offset - 1) == '\r') {
1210+
cutOffset = offset - 1;
1211+
} else if (!Character.isLowSurrogate(c)) {
1212+
cutOffset = offset;
1213+
} else if (offset > 1) {
1214+
cutOffset = offset - 1;
1215+
}
12061216
}
12071217
if (cutOffset >= length) {
12081218
updateType = DocUpdateType.TRIM;

0 commit comments

Comments
 (0)