forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem.java
More file actions
207 lines (169 loc) · 5.44 KB
/
Problem.java
File metadata and controls
207 lines (169 loc) · 5.44 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.mode.java.pdex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jdt.core.compiler.IProblem;
import processing.app.ui.ErrorTable;
/**
* Wrapper class for IProblem that stores the tabIndex and line number
* according to its tab, including the original IProblem object
*/
public class Problem implements ErrorTable.Entry {
/**
* The IProblem which is being wrapped
*/
private IProblem iProblem;
/**
* The tab number to which the error belongs to
*/
private int tabIndex;
/**
* Line number(pde code) of the error
*/
private int lineNumber;
private int lineStartOffset;
private int lineStopOffset;
/**
* Error Message. Processed form of IProblem.getMessage()
*/
private String message;
/**
* The type of error - WARNING or ERROR.
*/
private int type;
/**
* If the error is a 'cannot find type' contains the list of suggested imports
*/
private String[] importSuggestions;
public static final int ERROR = 1, WARNING = 2;
/**
*
* @param iProblem - The IProblem which is being wrapped
* @param tabIndex - The tab number to which the error belongs to
* @param lineNumber - Line number(pde code) of the error
*/
public Problem(IProblem iProblem, int tabIndex, int lineNumber) {
this.iProblem = iProblem;
if(iProblem.isError()) {
type = ERROR;
}
else if(iProblem.isWarning()) {
type = WARNING;
}
this.tabIndex = tabIndex;
this.lineNumber = lineNumber;
this.message = process(iProblem);
this.message = ErrorMessageSimplifier.getSimplifiedErrorMessage(this);
//ErrorMessageSimplifier.getSimplifiedErrorMessage(this);
}
public void setPDEOffsets(int startOffset, int stopOffset){
lineStartOffset = startOffset;
lineStopOffset = stopOffset;
}
public int getPDELineStartOffset() {
return lineStartOffset;
}
public int getPDELineStopOffset() {
return lineStopOffset;
}
public String toString() {
return new String("TAB " + tabIndex + ",LN " + lineNumber + "LN START OFF: "
+ lineStartOffset + ",LN STOP OFF: " + lineStopOffset + ",PROB: "
+ message);
}
public boolean isError() {
return type == ERROR;
}
public boolean isWarning() {
return type == WARNING;
}
public String getMessage() {
return message;
}
public IProblem getIProblem() {
return iProblem;
}
public int getTabIndex() {
return tabIndex;
}
public int getLineNumber() {
return lineNumber;
}
/**
* Remember to subtract a -1 to line number because in compile check code an
* extra package statement is added, so all line numbers are increased by 1
*
* @return
*/
public int getSourceLineNumber() {
return iProblem.getSourceLineNumber();
}
public void setType(int ProblemType){
if(ProblemType == ERROR)
type = ERROR;
else if(ProblemType == WARNING)
type = WARNING;
else throw new IllegalArgumentException("Illegal Problem type passed to Problem.setType(int)");
}
public String[] getImportSuggestions() {
return importSuggestions;
}
public void setImportSuggestions(String[] a) {
importSuggestions = a;
}
private static Pattern pattern;
private static Matcher matcher;
private static final String tokenRegExp = "\\b token\\b";
public static String process(IProblem problem) {
return process(problem.getMessage());
}
/**
* Processes error messages and attempts to make them a bit more english like.
* Currently performs:
* <li>Remove all instances of token. "Syntax error on token 'blah', delete this token"
* becomes "Syntax error on 'blah', delete this"
* @param message - The message to be processed
* @return String - The processed message
*/
public static String process(String message) {
// Remove all instances of token
// "Syntax error on token 'blah', delete this token"
if(message == null) return null;
pattern = Pattern.compile(tokenRegExp);
matcher = pattern.matcher(message);
message = matcher.replaceAll("");
return message;
}
// Split camel case words into separate words.
// "VaraibleDeclaration" becomes "Variable Declaration"
// But sadly "PApplet" become "P Applet" and so on.
public static String splitCamelCaseWord(String word) {
String newWord = "";
for (int i = 1; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
// System.out.println(word.substring(0, i) + " "
// + word.substring(i));
newWord += word.substring(0, i) + " ";
word = word.substring(i);
i = 1;
}
}
newWord += word;
// System.out.println(newWord);
return newWord.trim();
}
}