Skip to content

Commit

Permalink
Fixing MultilineTranslatorTest.emptyLinesSplitMerge()
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Mar 18, 2024
1 parent d2c076e commit 6bdb095
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
47 changes: 40 additions & 7 deletions src/main/java/org/luwrain/controls/edit/MultilineTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ public class MultilineTranslator
{
protected final MutableLines lines;
protected final HotPointControl hotPoint;
protected String tabSeq = "\t";
// protected String tabSeq = "\t";
protected final boolean adjustHotPoint;

public MultilineTranslator(MutableLines lines, HotPointControl hotPoint)
MultilineTranslator(MutableLines lines, HotPointControl hotPoint, boolean adjustHotPoint)
{
notNull(lines, "lines");
notNull(hotPoint, "hotPoint");
this.lines = lines;
this.hotPoint = hotPoint;
this.adjustHotPoint = adjustHotPoint;
}

public MultilineTranslator(MutableLines lines, HotPointControl hotPoint)
{
this(lines, hotPoint, true);
}

/*
public MultilineTranslator(MutableLines lines, HotPointControl hotPoint, String tabSeq)
{
notNull(lines, "lines");
Expand All @@ -49,6 +57,7 @@ public MultilineTranslator(MutableLines lines, HotPointControl hotPoint, String
this.hotPoint = hotPoint;
this.tabSeq = tabSeq;
}
*/

public void change(MultilineCorrector.Change c)
{
Expand Down Expand Up @@ -261,12 +270,10 @@ private ModificationResult mergeLines(int firstLine)
return new ModificationResult(true);
}

ModificationResult splitLine(int line, int pos)
private ModificationResult splitLine(int line, int pos)
{
checkPos(pos, line);
final String newLine;
try (var op = operation(false)){

//Adding the line to the empty lines list
if (pos == 0 && line == 0 && lines.getLineCount() == 0)
lines.addLine("");
Expand All @@ -278,7 +285,7 @@ ModificationResult splitLine(int line, int pos)
if (pos > l.length())
throw new IllegalArgumentException("pos (" + String.valueOf(pos) + ") can't be greater than the length of the line (" + String.valueOf(l.length()) + ")");
lines.setLine(line, l.substring(0, pos));
newLine = l.substring(pos);
final String newLine = l.substring(pos);
lines.insertLine(line + 1, newLine);
if (hotPoint.getHotPointY() == line && hotPoint.getHotPointX() >= pos)
{
Expand All @@ -287,8 +294,8 @@ ModificationResult splitLine(int line, int pos)
} else
if (hotPoint.getHotPointY() > line)
hotPoint.setHotPointY(hotPoint.getHotPointY() + 1);
return new ModificationResult(true, newLine);
}
return new ModificationResult(true, newLine);
}

protected int getLineCount()
Expand Down Expand Up @@ -358,6 +365,32 @@ protected void endEditTrans(boolean cleanSingleEmptyLine)

protected OperationFinishing operation(boolean cleanEmptyLine)
{
if (adjustHotPoint)
{
//Validating the hot point
if (hotPoint.getHotPointX() < 0)
hotPoint.setHotPointX(0);
if (hotPoint.getHotPointY() < 0)
hotPoint.setHotPointY(0);
//Hot point is below the last line
if (hotPoint.getHotPointY() >= lines.getLineCount())
{
if (lines.getLineCount() == 0)
hotPoint.setHotPointY(0); else
hotPoint.setHotPointY(lines.getLineCount() - 1);
}
//Checking the position on the line
if (lines.getLineCount() > 0)
{
final String line = lines.getLine(hotPoint.getHotPointY());
if (line == null)
throw new NullPointerException("The line with the index " + String.valueOf(hotPoint.getHotPointY()) + " is null");
if (hotPoint.getHotPointX() > line.length())
hotPoint.setHotPointX(line.length());
} else
if (hotPoint.getHotPointX() != 0)
hotPoint.setHotPointX(0);
}
beginEditTrans();
return new OperationFinishing(cleanEmptyLine);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,25 @@ public class MultilineTranslatorTest
assertTrue(hotPoint.getHotPointY() == 0);
}

@Disabled @Test public void emptyLinesSplitMerge()
@Test public void emptyLinesSplitMerge()
{
final MutableLinesImpl lines = new MutableLinesImpl(new String[0]);
final TestingHotPointControl hotPoint = new TestingHotPointControl();
final MultilineEditTranslator translator = new MultilineEditTranslator(lines, hotPoint);
assertTrue(translator.splitLine(0, 0).equals(""));
assertTrue(lines.getLineCount() == 2);
assertTrue(translator.getLineCount() == 2);
assertTrue(translator.getLine(0).equals(""));
assertTrue(translator.getLine(1).equals(""));
assertTrue(hotPoint.getHotPointX() == 0);
assertTrue(hotPoint.getHotPointY() == 1);
translator.mergeLines(0);
assertTrue(lines.getLineCount() == 0);
assertTrue(translator.getLineCount() == 1);
assertTrue(hotPoint.getHotPointX() == 0);
assertTrue(hotPoint.getHotPointY() == 0);
final var lines = new MutableLinesImpl(new String[0]);
final var hotPoint = new TestingHotPointControl();
final var translator = new MultilineTranslator(lines, hotPoint);
Change c = new SplitLineChange(0, 0);
translator.change(c);
assertNotNull(c.getResult());
assertEquals("", c.getResult().getStringArg());
assertEquals(2, lines.getLineCount());
assertTrue(lines.getLine(0).isEmpty());
assertTrue(translator.getLine(1).isEmpty());
assertEquals(0, hotPoint.getHotPointX());
assertEquals(1, hotPoint.getHotPointY());
c = new MergeLinesChange(0);
translator.change(c);
assertEquals(0, lines.getLineCount());
assertEquals(0, hotPoint.getHotPointX());
assertEquals(0, hotPoint.getHotPointY());
}

@Disabled @Test public void deleteChar3x3()
Expand Down Expand Up @@ -131,25 +133,27 @@ public class MultilineTranslatorTest
@Test public void linesMerge3x3()
{
final var initial = new String[]{"123", "456", "789"};
for(int x = 0;x <= 3;++x)
for(int y = 0;y <= 3;++y)
for(int x = 0;x < 10;++x)
for(int y = 0;y < 10;++y)
for(int lineIndex = 0;lineIndex < 2;++lineIndex)
{
final var lines = new MutableLinesImpl(initial);
final TestingHotPointControl hotPoint = new TestingHotPointControl();
hotPoint.x = x;
hotPoint.y = y;
final var translator = new MultilineTranslator(lines, hotPoint);
final var translator = new MultilineTranslator(lines, hotPoint, false);
translator.change(new MergeLinesChange(lineIndex));
assertEquals(2, lines.getLineCount());
assertEquals(initial[lineIndex] + initial[lineIndex + 1], lines.getLine(lineIndex));
//If hot point on the second of two merged lines
if (y == lineIndex + 1)
{
assertEquals(x + initial[lineIndex].length(), hotPoint.x);
assertEquals(lineIndex, hotPoint.y);
} else
if (y <= lineIndex)
{
//Hot point left unchanged
assertEquals(x, hotPoint.x);
assertEquals(y, hotPoint.y);
} else
Expand All @@ -163,16 +167,16 @@ public class MultilineTranslatorTest
@Test public void linesSplitting3x3()
{
final var initial = new String[]{"123", "456", "789"};
for(int x = 0;x <= 3;++x)
for(int y = 0;y <= 3;++y)
for(int x = 0;x < 10;++x)
for(int y = 0;y < 10;++y)
for(int lineIndex = 0;lineIndex < 3;++lineIndex)
for(int pos = 0;pos <= 3;++pos)
{
final var lines = new MutableLinesImpl(initial);
final var hotPoint = new TestingHotPointControl();
hotPoint.x = x;
hotPoint.y = y;
final var translator = new MultilineTranslator(lines, hotPoint);
final var translator = new MultilineTranslator(lines, hotPoint, false);
final var c = new SplitLineChange(lineIndex, pos);
translator.change(c);
assertNotNull(c.getResult());
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/luwrain/script/AsyncFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public class AsyncFunctionTest
"var foo = await f();" +
"finished(foo);" +
"})");
System.out.println(fn.toString());
final var r = fn.execute();
System.out.println(r.toString());
assertNotNull(f.get());
assertNull(finishedValue.get());
f.get().complete("Testing value");
Expand Down

0 comments on commit 6bdb095

Please sign in to comment.