Refactored TabLineFactory into ProblemFactory. (#7)

* Refactored TabLineFactory into ProblemFactory.

As part of https://github.com/processing/processing4/pull/1, refactored TabLineFactory into ProblemFactory, merging tests in the process.

* Fix imports on https://github.com/sampottinger/processing4/pull/7.
This commit is contained in:
A Samuel Pottinger
2019-10-06 18:24:20 -07:00
committed by GitHub
parent 0febfce456
commit 2b05da132a
4 changed files with 59 additions and 92 deletions

View File

@@ -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<Integer> 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<Integer> 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);
}
}

View File

@@ -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.
*
* <p>
* 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.
* </p>
*/
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<Integer> 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);
}
}