From 36e1cbd26d018812ace3aa25358232634abb825d Mon Sep 17 00:00:00 2001 From: Sam Pottinger Date: Sun, 16 Jul 2023 18:39:17 -0700 Subject: [PATCH] Compiling again but highlighting wrong. --- app/src/processing/app/Problem.java | 8 +-- .../app/syntax/PdeTextAreaPainter.java | 16 ++--- app/src/processing/app/ui/Editor.java | 23 ++++---- .../src/processing/mode/java/JavaProblem.java | 58 ++----------------- .../processing/mode/java/SyntaxProblem.java | 46 +++------------ .../processing/mode/java/lsp/PdeAdapter.java | 11 ++-- 6 files changed, 39 insertions(+), 123 deletions(-) diff --git a/app/src/processing/app/Problem.java b/app/src/processing/app/Problem.java index 778f4c5af..6a0faf380 100644 --- a/app/src/processing/app/Problem.java +++ b/app/src/processing/app/Problem.java @@ -70,17 +70,17 @@ public interface Problem { * Get the exact character on which this problem starts in code line relative. * * @return Number of characters past the start of the line if known where the - * code associated with the Problem starts. Returns empty if not provided. + * code associated with the Problem starts. */ - public Optional getStartOffset(); + public int getStartOffset(); /** * Get the exact character on which this problem ends in code line relative. * * @return Number of characters past the start of the line if known where the - * code associated with the Problem ends. Returns empty if not provided. + * code associated with the Problem ends. */ - public Optional getStopOffset(); + public int getStopOffset(); } diff --git a/app/src/processing/app/syntax/PdeTextAreaPainter.java b/app/src/processing/app/syntax/PdeTextAreaPainter.java index be0d5fdc8..ddd34af0e 100644 --- a/app/src/processing/app/syntax/PdeTextAreaPainter.java +++ b/app/src/processing/app/syntax/PdeTextAreaPainter.java @@ -46,8 +46,6 @@ public class PdeTextAreaPainter extends TextAreaPainter { protected Color gutterTextInactiveColor; protected Color gutterHighlightColor; - private final Problem.LineToTabOffsetGetter lineToTabOffsetGetter; - public PdeTextAreaPainter(JEditTextArea textArea, TextAreaDefaults defaults) { super(textArea, defaults); @@ -78,10 +76,6 @@ public class PdeTextAreaPainter extends TextAreaPainter { } } }); - - lineToTabOffsetGetter = (x) -> { - return textArea.getLineStartOffset(x); - }; } @@ -153,12 +147,12 @@ public class PdeTextAreaPainter extends TextAreaPainter { protected void paintErrorLine(Graphics gfx, int line, int x) { List problems = getEditor().findProblems(line); for (Problem problem : problems) { - int startOffset = problem.computeTabStartOffset(lineToTabOffsetGetter); - int stopOffset = problem.computeTabStopOffset(lineToTabOffsetGetter); - int lineOffsetStart = textArea.getLineStartOffset(line); int lineOffsetStop = textArea.getLineStopOffset(line); + int startOffset = lineOffsetStart + problem.getStartOffset(); + int stopOffset = lineOffsetStart + problem.getStopOffset(); + int wiggleStart = Math.max(startOffset, lineOffsetStart); int wiggleStop = Math.min(stopOffset, lineOffsetStop); @@ -338,8 +332,8 @@ public class PdeTextAreaPainter extends TextAreaPainter { int lineStart = textArea.getLineStartOffset(line); int lineEnd = textArea.getLineStopOffset(line); - int errorStart = problem.computeTabStartOffset(lineToTabOffsetGetter); - int errorEnd = problem.computeTabStopOffset(lineToTabOffsetGetter) + 1; + int errorStart = lineStart + problem.getStartOffset(); + int errorEnd = lineStart + problem.getStopOffset(); int startOffset = Math.max(errorStart, lineStart) - lineStart; int stopOffset = Math.min(errorEnd, lineEnd) - lineStart; diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 824f8ac20..d81ffdc2a 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -2556,16 +2556,16 @@ public abstract class Editor extends JFrame implements RunnerListener { public void highlight(Problem p) { - Problem.LineToTabOffsetGetter getter = (x) -> { - return textarea.getLineStartOffset(x); - }; - - if (p != null) { - int tabIndex = p.getTabIndex(); - int tabToStartOffset = p.computeTabStartOffset(getter); - int tabToStopOffset = p.computeTabStopOffset(getter); - highlight(tabIndex, tabToStartOffset, tabToStopOffset); + if (p == null) { + return; } + + int tabIndex = p.getTabIndex(); + int lineNumber = p.getLineNumber(); + int lineStart = textarea.getLineStartOffset(lineNumber); + int tabToStartOffset = lineStart + p.getStartOffset(); + int tabToStopOffset = lineStart + p.getStopOffset(); + highlight(tabIndex, tabToStartOffset, tabToStopOffset); } @@ -2630,9 +2630,8 @@ public abstract class Editor extends JFrame implements RunnerListener { .filter(p -> p.getTabIndex() == currentTab) .filter(p -> { int pStartLine = p.getLineNumber(); - int pEndOffset = p.computeTabStopOffset( - (startLine) -> textarea.getLineStartOffset(pStartLine) - ); + int lineOffset = textarea.getLineOfOffset(pStartLine); + int pEndOffset = lineOffset + p.getStopOffset(); int pEndLine = textarea.getLineOfOffset(pEndOffset); return line >= pStartLine && line <= pEndLine; diff --git a/java/src/processing/mode/java/JavaProblem.java b/java/src/processing/mode/java/JavaProblem.java index 5233c785c..5de937a09 100644 --- a/java/src/processing/mode/java/JavaProblem.java +++ b/java/src/processing/mode/java/JavaProblem.java @@ -20,8 +20,6 @@ along with this program; if not, write to the Free Software Foundation, Inc. package processing.mode.java; -import java.util.Optional; - import org.eclipse.jdt.core.compiler.IProblem; import processing.app.Problem; @@ -44,9 +42,9 @@ public class JavaProblem implements Problem { /** Line number (pde code) of the error */ private final int lineNumber; - private Optional startOffset; + private int startOffset; - private Optional stopOffset; + private int stopOffset; /** * If the error is a 'cannot find type' contains the list of suggested imports @@ -62,8 +60,6 @@ public class JavaProblem implements Problem { this.type = type; this.tabIndex = tabIndex; this.lineNumber = lineNumber; - this.startOffset = Optional.empty(); - this.stopOffset = Optional.empty(); } @@ -87,32 +83,22 @@ public class JavaProblem implements Problem { public void setPDEOffsets(int startOffset, int stopOffset){ - this.startOffset = Optional.of(startOffset); - this.stopOffset = Optional.of(stopOffset); + this.startOffset = startOffset; + this.stopOffset = stopOffset; } @Override - public Optional getTabStartOffset() { + public int getStartOffset() { return startOffset; } @Override - public Optional getTabStopOffset() { + public int getStopOffset() { return stopOffset; } - @Override - public Optional getLineStartOffset() { - return Optional.empty(); - } - - @Override - public Optional getLineStopOffset() { - return Optional.empty(); - } - @Override public boolean isError() { return type == ERROR; @@ -163,36 +149,4 @@ public class JavaProblem implements Problem { + message; } - @Override - public int computeTabStartOffset(LineToTabOffsetGetter strategy) { - Optional nativeTabStartOffset = getTabStartOffset(); - if (nativeTabStartOffset.isPresent()) { - return nativeTabStartOffset.get(); - } - - Optional lineStartOffset = getLineStartOffset(); - int lineOffset = strategy.get(getLineNumber()); - if (lineStartOffset.isPresent()) { - return lineOffset + lineStartOffset.get(); - } else { - return lineOffset; - } - } - - @Override - public int computeTabStopOffset(LineToTabOffsetGetter strategy) { - Optional nativeTabStopOffset = getTabStopOffset(); - if (nativeTabStopOffset.isPresent()) { - return nativeTabStopOffset.get(); - } - - Optional lineStopOffset = getLineStopOffset(); - int lineOffset = strategy.get(getLineNumber()); - if (lineStopOffset.isPresent()) { - return lineOffset + lineStopOffset.get(); - } else { - return lineOffset; - } - } - } diff --git a/java/src/processing/mode/java/SyntaxProblem.java b/java/src/processing/mode/java/SyntaxProblem.java index bf037b971..e549c7561 100644 --- a/java/src/processing/mode/java/SyntaxProblem.java +++ b/java/src/processing/mode/java/SyntaxProblem.java @@ -29,10 +29,8 @@ public class SyntaxProblem extends JavaProblem { private final int tabIndex; private final int lineNumber; private final String message; - private final Optional tabStartOffset; - private final Optional tabStopOffset; - private final Optional lineStartOffset; - private final Optional lineStopOffset; + private final int lineStartOffset; + private final int lineStopOffset; /** * Create a new syntax problem. @@ -40,33 +38,19 @@ public class SyntaxProblem extends JavaProblem { * @param newTabIndex The tab number containing the source with the syntax issue. * @param newLineNumber The line number within the tab at which the offending code can be found. * @param newMessage Human readable message describing the issue. - * @param newStartOffset The character index at which the issue starts. This is relative to start - * of tab / file not relative to start of line if newUsesLineOffset is true else it is line - * offset. - * @param newStopOffset The character index at which the issue ends. This is relative to start - * of tab / file not relative to start of line if newUsesLineOffset is true else it is line - * offset. + * @param newStartOffset The character index at which the issue starts relative to line. + * @param newStopOffset The character index at which the issue end relative to line. */ public SyntaxProblem(int newTabIndex, int newLineNumber, String newMessage, int newStartOffset, - int newStopOffset, boolean newUsesLineOffset) { + int newStopOffset) { super(newMessage, JavaProblem.ERROR, newLineNumber, newLineNumber); tabIndex = newTabIndex; lineNumber = newLineNumber; message = newMessage; - - if (newUsesLineOffset) { - lineStartOffset = Optional.of(newStartOffset); - lineStopOffset = Optional.of(newStopOffset); - tabStartOffset = Optional.empty(); - tabStopOffset = Optional.empty(); - } else { - lineStartOffset = Optional.empty(); - lineStopOffset = Optional.empty(); - tabStartOffset = Optional.of(newStartOffset); - tabStopOffset = Optional.of(newStopOffset); - } + lineStartOffset = newStartOffset; + lineStopOffset = newStopOffset; } @Override @@ -94,23 +78,11 @@ public class SyntaxProblem extends JavaProblem { return message; } - @Override - public Optional getTabStartOffset() { - return tabStartOffset; - } - - @Override - public Optional getTabStopOffset() { - return tabStopOffset; - } - - @Override - public Optional getLineStartOffset() { + public int getStartOffset() { return lineStartOffset; } - @Override - public Optional getLineStopOffset() { + public int getStopOffset() { return lineStopOffset; } diff --git a/java/src/processing/mode/java/lsp/PdeAdapter.java b/java/src/processing/mode/java/lsp/PdeAdapter.java index 83d2b3096..1a58f3c8e 100644 --- a/java/src/processing/mode/java/lsp/PdeAdapter.java +++ b/java/src/processing/mode/java/lsp/PdeAdapter.java @@ -229,24 +229,21 @@ class PdeAdapter { .map(prob -> { SketchCode code = sketch.getCode(prob.getTabIndex()); - Optional startOffset = prob.getTabStartOffset(); - Optional endOffset = prob.getTabStopOffset(); - - assert startOffset.isPresent(); - assert endOffset.isPresent(); + int startOffset = prob.getStartOffset(); + int endOffset = prob.getStopOffset(); Diagnostic dia = new Diagnostic( new Range( new Position( prob.getLineNumber(), PdeAdapter - .toLineCol(code.getProgram(), startOffset.get()) + .toLineCol(code.getProgram(), startOffset) .col - 1 ), new Position( prob.getLineNumber(), PdeAdapter - .toLineCol(code.getProgram(), endOffset.get()) + .toLineCol(code.getProgram(), endOffset) .col - 1 ) ),