diff --git a/java/src/processing/mode/java/pdex/util/ProblemFactory.java b/java/src/processing/mode/java/pdex/util/ProblemFactory.java index 31363211e..ea7ee2806 100644 --- a/java/src/processing/mode/java/pdex/util/ProblemFactory.java +++ b/java/src/processing/mode/java/pdex/util/ProblemFactory.java @@ -1,5 +1,9 @@ package processing.mode.java.pdex.util; +import java.util.List; +import java.util.OptionalInt; +import java.util.stream.IntStream; + import processing.app.Problem; import processing.app.ui.Editor; import processing.mode.java.preproc.issue.PdePreprocessIssue; @@ -31,7 +35,7 @@ public class ProblemFactory { } // Get local area - TabLine tabLine = TabLineFactory.getTab(tabStarts, line); + TabLine tabLine = getTab(tabStarts, line); int tab = tabLine.getTab(); int localLine = tabLine.getLineInTab(); // Problems emitted in 0 index @@ -65,7 +69,7 @@ public class ProblemFactory { public static Problem build(PdePreprocessIssue pdePreprocessIssue, List tabStarts) { int line = pdePreprocessIssue.getLine(); - TabLine tabLine = TabLineFactory.getTab(tabStarts, line); + TabLine tabLine = getTab(tabStarts, line); int tab = tabLine.getTab(); int localLine = tabLine.getLineInTab(); @@ -86,4 +90,23 @@ public class ProblemFactory { ); } + /** + * Get the local tab and line number for a global line. + * + * @param tabStarts The lines on which each tab starts. + * @param line The global line to locate as a local line. + * @return The local tab number and local line number. + */ + protected static TabLine getTab(List tabStarts, int line) { + OptionalInt tabMaybe = IntStream.range(0, tabStarts.size()) + .filter((index) -> line >= tabStarts.get(index)) + .max(); + + int tab = tabMaybe.orElse(0); + + int localLine = line - tabStarts.get(tab); + + return new TabLine(tab, line, localLine); + } + } diff --git a/java/src/processing/mode/java/pdex/util/TabLineFactory.java b/java/src/processing/mode/java/pdex/util/TabLineFactory.java deleted file mode 100644 index c8883afb8..000000000 --- a/java/src/processing/mode/java/pdex/util/TabLineFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -package processing.mode.java.pdex.util; - -import java.util.List; -import java.util.OptionalInt; -import java.util.stream.IntStream; - - -/** - * Utility which determines the tab and local line number on which a global line number appears. - * - *

- * Processing concatenates tabs into single file for compilation as Java where a source line - * from a tab is a "local" line and the same line in the concatenated file is "global". This - * utility determines the local line and tab number given a global line number. - *

- */ -public class TabLineFactory { - - /** - * Get the local tab and line number for a global line. - * - * @param tabStarts The lines on which each tab starts. - * @param line The global line to locate as a local line. - * @return The local tab number and local line number. - */ - public static TabLine getTab(List tabStarts, int line) { - OptionalInt tabMaybe = IntStream.range(0, tabStarts.size()) - .filter((index) -> line >= tabStarts.get(index)) - .max(); - - int tab = tabMaybe.orElse(0); - - int localLine = line - tabStarts.get(tab); - - return new TabLine(tab, line, localLine); - } - -} diff --git a/java/test/processing/mode/java/pdex/util/ProblemFactoryTest.java b/java/test/processing/mode/java/pdex/util/ProblemFactoryTest.java index eba0a384a..890e5309b 100644 --- a/java/test/processing/mode/java/pdex/util/ProblemFactoryTest.java +++ b/java/test/processing/mode/java/pdex/util/ProblemFactoryTest.java @@ -18,6 +18,7 @@ public class ProblemFactoryTest { private PdePreprocessIssue pdePreprocessIssue; private List tabStarts; private Editor editor; + private List starts; @Before public void setUp() { @@ -29,6 +30,11 @@ public class ProblemFactoryTest { editor = Mockito.mock(Editor.class); Mockito.when(editor.getLineStartOffset(3)).thenReturn(10); Mockito.when(editor.getLineStopOffset(3)).thenReturn(12); + + starts = new ArrayList<>(); + starts.add(0); + starts.add(5); + starts.add(10); } @Test @@ -49,4 +55,31 @@ public class ProblemFactoryTest { Assert.assertEquals("test", problem.getMessage()); } -} \ No newline at end of file + @Test + public void getTabStart() { + Assert.assertEquals(0, ProblemFactory.getTab(starts, 0).getTab()); + } + + @Test + public void getTabMiddleFrontEdge() { + Assert.assertEquals(1, ProblemFactory.getTab(starts, 5).getTab()); + } + + @Test + public void getTabMiddle() { + TabLine tabLine = ProblemFactory.getTab(starts, 7); + Assert.assertEquals(1, tabLine.getTab()); + Assert.assertEquals(2, tabLine.getLineInTab()); + } + + @Test + public void getTabMiddleBackEdge() { + Assert.assertEquals(2, ProblemFactory.getTab(starts, 10).getTab()); + } + + @Test + public void getTabEnd() { + Assert.assertEquals(2, ProblemFactory.getTab(starts, 15).getTab()); + } + +} diff --git a/java/test/processing/mode/java/pdex/util/TabLineFactoryTest.java b/java/test/processing/mode/java/pdex/util/TabLineFactoryTest.java deleted file mode 100644 index d78d14e48..000000000 --- a/java/test/processing/mode/java/pdex/util/TabLineFactoryTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package processing.mode.java.pdex.util; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.*; - -public class TabLineFactoryTest { - - private List starts; - - @Before - public void setUp() { - starts = new ArrayList<>(); - starts.add(0); - starts.add(5); - starts.add(10); - } - - @Test - public void getTabStart() { - Assert.assertEquals(0, TabLineFactory.getTab(starts, 0).getTab()); - } - - @Test - public void getTabMiddleFrontEdge() { - Assert.assertEquals(1, TabLineFactory.getTab(starts, 5).getTab()); - } - - @Test - public void getTabMiddle() { - TabLine tabLine = TabLineFactory.getTab(starts, 7); - Assert.assertEquals(1, tabLine.getTab()); - Assert.assertEquals(2, tabLine.getLineInTab()); - } - - @Test - public void getTabMiddleBackEdge() { - Assert.assertEquals(2, TabLineFactory.getTab(starts, 10).getTab()); - } - - @Test - public void getTabEnd() { - Assert.assertEquals(2, TabLineFactory.getTab(starts, 15).getTab()); - } - -} \ No newline at end of file