Skip to content

Commit fccf3b4

Browse files
authored
Merge pull request #752 from processing/revert-715-differentiate-offset
Fix the language server / PDE error highlighting another way
2 parents 33aab6d + d2262f9 commit fccf3b4

File tree

9 files changed

+50
-228
lines changed

9 files changed

+50
-228
lines changed

app/src/processing/app/Problem.java

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@
2828
*/
2929
public interface Problem {
3030

31-
/**
32-
* Strategy converting line number in tab to character offset from tab start.
33-
*/
34-
public interface LineToTabOffsetGetter {
35-
36-
/**
37-
* Convert a line number to the number of characters past tab start.
38-
*
39-
* @param line The line number to convert.
40-
* @return The number of characters past tab start where that line starts.
41-
*/
42-
public int get(int line);
43-
44-
}
45-
4631
/**
4732
* Get if the problem is an error that prevented compilation.
4833
*
@@ -81,56 +66,21 @@ public interface LineToTabOffsetGetter {
8166
*/
8267
public String getMessage();
8368

84-
/**
85-
* Get the exact character on which this problem starts in code tab relative.
86-
*
87-
* @return Number of characters past the start of the tab if known where the
88-
* code associated with the Problem starts. Returns empty if not provided.
89-
*/
90-
public Optional<Integer> getTabStartOffset();
91-
92-
/**
93-
* Get the exact character on which this problem ends in code tab relative.
94-
*
95-
* @return Number of characters past the start of the tab if known where the
96-
* code associated with the Problem ends. Returns empty if not provided.
97-
*/
98-
public Optional<Integer> getTabStopOffset();
99-
10069
/**
10170
* Get the exact character on which this problem starts in code line relative.
10271
*
10372
* @return Number of characters past the start of the line if known where the
104-
* code associated with the Problem starts. Returns empty if not provided.
73+
* code associated with the Problem starts.
10574
*/
106-
public Optional<Integer> getLineStartOffset();
75+
public int getStartOffset();
10776

10877
/**
10978
* Get the exact character on which this problem ends in code line relative.
11079
*
11180
* @return Number of characters past the start of the line if known where the
112-
* code associated with the Problem ends. Returns empty if not provided.
113-
*/
114-
public Optional<Integer> getLineStopOffset();
115-
116-
/**
117-
* Get the exact character on which this problem ends in code tab relative.
118-
*
119-
* @param strategy Strategy to convert line to tab start if needed.
120-
* @return Number of characters past the start of the tab if known where the
121-
* code associated with the Problem ends, using the provided conversion
122-
* if needed. Returns line start if character position not given.
81+
* code associated with the Problem ends.
12382
*/
124-
public int computeTabStartOffset(LineToTabOffsetGetter strategy);
83+
public int getStopOffset();
12584

126-
/**
127-
* Get the exact character on which this problem ends in code tab relative.
128-
*
129-
* @param strategy Strategy to convert line to tab start if needed.
130-
* @return Number of characters past the start of the tab if known where the
131-
* code associated with the Problem ends, using the provided conversion
132-
* if needed. Returns line start if character position not given.
133-
*/
134-
public int computeTabStopOffset(LineToTabOffsetGetter strategy);
13585
}
13686

app/src/processing/app/syntax/PdeTextAreaPainter.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public class PdeTextAreaPainter extends TextAreaPainter {
4646
protected Color gutterTextInactiveColor;
4747
protected Color gutterHighlightColor;
4848

49-
private final Problem.LineToTabOffsetGetter lineToTabOffsetGetter;
50-
5149

5250
public PdeTextAreaPainter(JEditTextArea textArea, TextAreaDefaults defaults) {
5351
super(textArea, defaults);
@@ -78,10 +76,6 @@ public void mousePressed(MouseEvent event) {
7876
}
7977
}
8078
});
81-
82-
lineToTabOffsetGetter = (x) -> {
83-
return textArea.getLineStartOffset(x);
84-
};
8579
}
8680

8781

@@ -153,14 +147,11 @@ protected void paintLine(Graphics gfx, int line, int x, TokenMarkerState marker)
153147
protected void paintErrorLine(Graphics gfx, int line, int x) {
154148
List<Problem> problems = getEditor().findProblems(line);
155149
for (Problem problem : problems) {
156-
int startOffset = problem.computeTabStartOffset(lineToTabOffsetGetter);
157-
int stopOffset = problem.computeTabStopOffset(lineToTabOffsetGetter);
158-
159150
int lineOffsetStart = textArea.getLineStartOffset(line);
160151
int lineOffsetStop = textArea.getLineStopOffset(line);
161152

162-
int wiggleStart = Math.max(startOffset, lineOffsetStart);
163-
int wiggleStop = Math.min(stopOffset, lineOffsetStop);
153+
int wiggleStart = lineOffsetStart + problem.getStartOffset();
154+
int wiggleStop = lineOffsetStart + problem.getStopOffset();
164155

165156
int y = textArea.lineToY(line) + getLineDisplacement();
166157

@@ -338,8 +329,8 @@ public String getToolTipText(MouseEvent event) {
338329
int lineStart = textArea.getLineStartOffset(line);
339330
int lineEnd = textArea.getLineStopOffset(line);
340331

341-
int errorStart = problem.computeTabStartOffset(lineToTabOffsetGetter);
342-
int errorEnd = problem.computeTabStopOffset(lineToTabOffsetGetter) + 1;
332+
int errorStart = lineStart + problem.getStartOffset();
333+
int errorEnd = lineStart + problem.getStopOffset();
343334

344335
int startOffset = Math.max(errorStart, lineStart) - lineStart;
345336
int stopOffset = Math.min(errorEnd, lineEnd) - lineStart;

app/src/processing/app/ui/Editor.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,16 +2556,16 @@ public void updateErrorTable(List<Problem> problems) {
25562556

25572557

25582558
public void highlight(Problem p) {
2559-
Problem.LineToTabOffsetGetter getter = (x) -> {
2560-
return textarea.getLineStartOffset(x);
2561-
};
2562-
2563-
if (p != null) {
2564-
int tabIndex = p.getTabIndex();
2565-
int tabToStartOffset = p.computeTabStartOffset(getter);
2566-
int tabToStopOffset = p.computeTabStopOffset(getter);
2567-
highlight(tabIndex, tabToStartOffset, tabToStopOffset);
2559+
if (p == null) {
2560+
return;
25682561
}
2562+
2563+
int tabIndex = p.getTabIndex();
2564+
int lineNumber = p.getLineNumber();
2565+
int lineStart = textarea.getLineStartOffset(lineNumber);
2566+
int tabToStartOffset = lineStart + p.getStartOffset();
2567+
int tabToStopOffset = lineStart + p.getStopOffset();
2568+
highlight(tabIndex, tabToStartOffset, tabToStopOffset);
25692569
}
25702570

25712571

@@ -2630,11 +2630,10 @@ public List<Problem> findProblems(int line) {
26302630
.filter(p -> p.getTabIndex() == currentTab)
26312631
.filter(p -> {
26322632
int pStartLine = p.getLineNumber();
2633-
int pEndOffset = p.computeTabStopOffset(
2634-
(startLine) -> textarea.getLineStartOffset(pStartLine)
2635-
);
2633+
int lineOffset = textarea.getLineStartOffset(pStartLine);
2634+
int pEndOffset = lineOffset + p.getStopOffset();
26362635
int pEndLine = textarea.getLineOfOffset(pEndOffset);
2637-
2636+
26382637
return line >= pStartLine && line <= pEndLine;
26392638
})
26402639
.collect(Collectors.toList());

java/src/processing/mode/java/ErrorChecker.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static private JavaProblem convertIProblem(IProblem iproblem, PreprocSketch ps)
199199
String badCode = ps.getPdeCode(in);
200200
int line = ps.tabOffsetToTabLine(in.tabIndex, in.startTabOffset);
201201
JavaProblem p = JavaProblem.fromIProblem(iproblem, in.tabIndex, line, badCode);
202-
p.setPDEOffsets(in.startTabOffset, in.stopTabOffset);
202+
p.setPDEOffsets(0, iproblem.getSourceEnd() - iproblem.getSourceStart());
203203
return p;
204204
}
205205
return null;
@@ -252,7 +252,6 @@ static private List<JavaProblem> checkForCurlyQuotes(PreprocSketch ps) {
252252

253253
String message = Language.interpolate("editor.status.bad_curly_quote", q);
254254
JavaProblem problem = new JavaProblem(message, JavaProblem.ERROR, tabIndex, tabLine);
255-
problem.setPDEOffsets(tabOffset, tabOffset+1);
256255

257256
problems.add(problem);
258257
}
@@ -294,7 +293,6 @@ static private List<JavaProblem> checkForCurlyQuotes(PreprocSketch ps) {
294293
message = Language.interpolate("editor.status.bad_curly_quote", q);
295294
}
296295
JavaProblem p = new JavaProblem(message, JavaProblem.ERROR, in.tabIndex, line);
297-
p.setPDEOffsets(tabStart, tabStop);
298296
problems2.add(p);
299297
}
300298
}

java/src/processing/mode/java/JavaProblem.java

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
package processing.mode.java;
2222

23-
import java.util.Optional;
24-
2523
import org.eclipse.jdt.core.compiler.IProblem;
2624

2725
import processing.app.Problem;
@@ -44,9 +42,9 @@ public class JavaProblem implements Problem {
4442
/** Line number (pde code) of the error */
4543
private final int lineNumber;
4644

47-
private Optional<Integer> startOffset;
45+
private int startOffset;
4846

49-
private Optional<Integer> stopOffset;
47+
private int stopOffset;
5048

5149
/**
5250
* If the error is a 'cannot find type' contains the list of suggested imports
@@ -62,8 +60,10 @@ public JavaProblem(String message, int type, int tabIndex, int lineNumber) {
6260
this.type = type;
6361
this.tabIndex = tabIndex;
6462
this.lineNumber = lineNumber;
65-
this.startOffset = Optional.empty();
66-
this.stopOffset = Optional.empty();
63+
64+
// Default to 0, 1 unless a longer section is specified
65+
this.startOffset = 0;
66+
this.stopOffset = 1;
6767
}
6868

6969

@@ -87,32 +87,22 @@ static public JavaProblem fromIProblem(IProblem iProblem, int tabIndex,
8787

8888

8989
public void setPDEOffsets(int startOffset, int stopOffset){
90-
this.startOffset = Optional.of(startOffset);
91-
this.stopOffset = Optional.of(stopOffset);
90+
this.startOffset = startOffset;
91+
this.stopOffset = stopOffset;
9292
}
9393

9494

9595
@Override
96-
public Optional<Integer> getTabStartOffset() {
96+
public int getStartOffset() {
9797
return startOffset;
9898
}
9999

100100

101101
@Override
102-
public Optional<Integer> getTabStopOffset() {
102+
public int getStopOffset() {
103103
return stopOffset;
104104
}
105105

106-
@Override
107-
public Optional<Integer> getLineStartOffset() {
108-
return Optional.empty();
109-
}
110-
111-
@Override
112-
public Optional<Integer> getLineStopOffset() {
113-
return Optional.empty();
114-
}
115-
116106
@Override
117107
public boolean isError() {
118108
return type == ERROR;
@@ -163,36 +153,4 @@ public String toString() {
163153
+ message;
164154
}
165155

166-
@Override
167-
public int computeTabStartOffset(LineToTabOffsetGetter strategy) {
168-
Optional<Integer> nativeTabStartOffset = getTabStartOffset();
169-
if (nativeTabStartOffset.isPresent()) {
170-
return nativeTabStartOffset.get();
171-
}
172-
173-
Optional<Integer> lineStartOffset = getLineStartOffset();
174-
int lineOffset = strategy.get(getLineNumber());
175-
if (lineStartOffset.isPresent()) {
176-
return lineOffset + lineStartOffset.get();
177-
} else {
178-
return lineOffset;
179-
}
180-
}
181-
182-
@Override
183-
public int computeTabStopOffset(LineToTabOffsetGetter strategy) {
184-
Optional<Integer> nativeTabStopOffset = getTabStopOffset();
185-
if (nativeTabStopOffset.isPresent()) {
186-
return nativeTabStopOffset.get();
187-
}
188-
189-
Optional<Integer> lineStopOffset = getLineStopOffset();
190-
int lineOffset = strategy.get(getLineNumber());
191-
if (lineStopOffset.isPresent()) {
192-
return lineOffset + lineStopOffset.get();
193-
} else {
194-
return lineOffset;
195-
}
196-
}
197-
198156
}

java/src/processing/mode/java/PreprocService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class PreprocService {
9999
}};
100100

101101
/**
102-
* Create a new preprocessing service to support an editor.
102+
* Create a new preprocessing service to support the language server.
103103
*/
104104
public PreprocService(JavaMode javaMode, Sketch sketch) {
105105
this.javaMode = javaMode;
@@ -411,10 +411,12 @@ private PreprocSketch preprocessSketch(PreprocSketch prevResult) {
411411
throw new RuntimeException("Unexpected sketch exception in preprocessing: " + e);
412412
}
413413

414+
final int endNumLines = numLines;
415+
414416
if (preprocessorResult.getPreprocessIssues().size() > 0) {
415417
preprocessorResult.getPreprocessIssues().stream()
416-
.map((x) -> ProblemFactory.build(x, tabLineStarts))
417-
.forEach(result.otherProblems::add);
418+
.map((x) -> ProblemFactory.build(x, tabLineStarts))
419+
.forEach(result.otherProblems::add);
418420

419421
result.hasSyntaxErrors = true;
420422
}

java/src/processing/mode/java/ProblemFactory.java

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,6 @@
1414
*/
1515
public class ProblemFactory {
1616

17-
/**
18-
* Create a new {Problem}.
19-
*
20-
* @param pdePreprocessIssue The preprocess issue found.
21-
* @param tabStarts The list of line numbers on which each tab starts.
22-
* @param editor The editor in which errors will appear.
23-
* @return Newly created problem.
24-
*/
25-
public static Problem build(PdePreprocessIssue pdePreprocessIssue, List<Integer> tabStarts,
26-
int numLines, Editor editor) {
27-
28-
int line = pdePreprocessIssue.getLine();
29-
30-
// Sometimes errors are reported one line past end of sketch. Fix that.
31-
if (line >= numLines) {
32-
line = numLines - 1;
33-
}
34-
35-
// Get local area
36-
TabLine tabLine = getTab(tabStarts, line);
37-
38-
int tab = tabLine.getTab();
39-
int localLine = tabLine.getLineInTab(); // Problems emitted in 0 index
40-
41-
// Generate syntax problem
42-
String message = pdePreprocessIssue.getMsg();
43-
44-
int lineStart = editor.getLineStartOffset(localLine);
45-
int lineStop = editor.getLineStopOffset(localLine) - 1;
46-
47-
if (lineStart == lineStop) {
48-
lineStop++;
49-
}
50-
51-
return new SyntaxProblem(
52-
tab,
53-
localLine,
54-
message,
55-
lineStart,
56-
lineStop,
57-
false
58-
);
59-
}
60-
6117
/**
6218
* Create a new {Problem}.
6319
*
@@ -85,8 +41,7 @@ public static Problem build(PdePreprocessIssue pdePreprocessIssue, List<Integer>
8541
localLine,
8642
message,
8743
0,
88-
col,
89-
true
44+
col
9045
);
9146
}
9247

0 commit comments

Comments
 (0)