forked from benfry/processing4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemFactory.java
More file actions
110 lines (86 loc) · 2.79 KB
/
Copy pathProblemFactory.java
File metadata and controls
110 lines (86 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package processing.mode.java;
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.PdePreprocessIssue;
/**
* Factory which helps create {Problem}s during preprocessing.
*/
public class ProblemFactory {
/**
* Create a new {Problem}.
*
* @param pdePreprocessIssue The preprocess issue found.
* @param tabStarts The list of line numbers on which each tab starts.
* @param editor The editor in which errors will appear.
* @return Newly created problem.
*/
public static Problem build(PdePreprocessIssue pdePreprocessIssue, List<Integer> tabStarts,
int numLines, Editor editor) {
int line = pdePreprocessIssue.getLine();
// Sometimes errors are reported one line past end of sketch. Fix that.
if (line >= numLines) {
line = numLines - 1;
}
// Get local area
TabLine tabLine = getTab(tabStarts, line);
int tab = tabLine.getTab();
int localLine = tabLine.getLineInTab(); // Problems emitted in 0 index
// Generate syntax problem
String message = pdePreprocessIssue.getMsg();
int lineStart = editor.getLineStartOffset(localLine);
int lineStop = editor.getLineStopOffset(localLine) - 1;
if (lineStart == lineStop) {
lineStop++;
}
return new SyntaxProblem(
tab,
localLine,
message,
lineStart,
lineStop
);
}
/**
* Create a new {Problem}.
*
* @param pdePreprocessIssue The preprocess issue found.
* @param tabStarts The list of line numbers on which each tab starts.
* @return Newly created problem.
*/
public static Problem build(PdePreprocessIssue pdePreprocessIssue, List<Integer> tabStarts) {
int line = pdePreprocessIssue.getLine();
TabLine tabLine = getTab(tabStarts, line);
int tab = tabLine.getTab();
int localLine = tabLine.getLineInTab();
int col = pdePreprocessIssue.getCharPositionInLine();
String message = pdePreprocessIssue.getMsg();
if (col == 0) {
col = 1;
}
return new SyntaxProblem(
tab,
localLine,
message,
localLine,
localLine + col
);
}
/**
* 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);
}
}