diff --git a/app/src/processing/app/Problem.java b/app/src/processing/app/Problem.java index c5f633c67..6a0faf380 100644 --- a/app/src/processing/app/Problem.java +++ b/app/src/processing/app/Problem.java @@ -28,21 +28,6 @@ */ public interface Problem { - /** - * Strategy converting line number in tab to character offset from tab start. - */ - public interface LineToTabOffsetGetter { - - /** - * Convert a line number to the number of characters past tab start. - * - * @param line The line number to convert. - * @return The number of characters past tab start where that line starts. - */ - public int get(int line); - - } - /** * Get if the problem is an error that prevented compilation. * @@ -81,56 +66,21 @@ public interface LineToTabOffsetGetter { */ public String getMessage(); - /** - * Get the exact character on which this problem starts in code tab relative. - * - * @return Number of characters past the start of the tab if known where the - * code associated with the Problem starts. Returns empty if not provided. - */ - public Optional getTabStartOffset(); - - /** - * Get the exact character on which this problem ends in code tab relative. - * - * @return Number of characters past the start of the tab if known where the - * code associated with the Problem ends. Returns empty if not provided. - */ - public Optional getTabStopOffset(); - /** * 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 getLineStartOffset(); + 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. - */ - public Optional getLineStopOffset(); - - /** - * Get the exact character on which this problem ends in code tab relative. - * - * @param strategy Strategy to convert line to tab start if needed. - * @return Number of characters past the start of the tab if known where the - * code associated with the Problem ends, using the provided conversion - * if needed. Returns line start if character position not given. + * code associated with the Problem ends. */ - public int computeTabStartOffset(LineToTabOffsetGetter strategy); + public int getStopOffset(); - /** - * Get the exact character on which this problem ends in code tab relative. - * - * @param strategy Strategy to convert line to tab start if needed. - * @return Number of characters past the start of the tab if known where the - * code associated with the Problem ends, using the provided conversion - * if needed. Returns line start if character position not given. - */ - public int computeTabStopOffset(LineToTabOffsetGetter strategy); } diff --git a/app/src/processing/app/syntax/PdeTextAreaPainter.java b/app/src/processing/app/syntax/PdeTextAreaPainter.java index be0d5fdc8..2c83a518b 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 void mousePressed(MouseEvent event) { } } }); - - lineToTabOffsetGetter = (x) -> { - return textArea.getLineStartOffset(x); - }; } @@ -153,14 +147,11 @@ protected void paintLine(Graphics gfx, int line, int x, TokenMarkerState marker) 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 wiggleStart = Math.max(startOffset, lineOffsetStart); - int wiggleStop = Math.min(stopOffset, lineOffsetStop); + int wiggleStart = lineOffsetStart + problem.getStartOffset(); + int wiggleStop = lineOffsetStart + problem.getStopOffset(); int y = textArea.lineToY(line) + getLineDisplacement(); @@ -338,8 +329,8 @@ public String getToolTipText(MouseEvent event) { 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..911df7a49 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -2556,16 +2556,16 @@ public void updateErrorTable(List problems) { 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,11 +2630,10 @@ public List findProblems(int line) { .filter(p -> p.getTabIndex() == currentTab) .filter(p -> { int pStartLine = p.getLineNumber(); - int pEndOffset = p.computeTabStopOffset( - (startLine) -> textarea.getLineStartOffset(pStartLine) - ); + int lineOffset = textarea.getLineStartOffset(pStartLine); + int pEndOffset = lineOffset + p.getStopOffset(); int pEndLine = textarea.getLineOfOffset(pEndOffset); - + return line >= pStartLine && line <= pEndLine; }) .collect(Collectors.toList()); diff --git a/java/src/processing/mode/java/ErrorChecker.java b/java/src/processing/mode/java/ErrorChecker.java index 1302dd773..e631fbd4f 100644 --- a/java/src/processing/mode/java/ErrorChecker.java +++ b/java/src/processing/mode/java/ErrorChecker.java @@ -199,7 +199,7 @@ static private JavaProblem convertIProblem(IProblem iproblem, PreprocSketch ps) String badCode = ps.getPdeCode(in); int line = ps.tabOffsetToTabLine(in.tabIndex, in.startTabOffset); JavaProblem p = JavaProblem.fromIProblem(iproblem, in.tabIndex, line, badCode); - p.setPDEOffsets(in.startTabOffset, in.stopTabOffset); + p.setPDEOffsets(0, iproblem.getSourceEnd() - iproblem.getSourceStart()); return p; } return null; @@ -252,7 +252,6 @@ static private List checkForCurlyQuotes(PreprocSketch ps) { String message = Language.interpolate("editor.status.bad_curly_quote", q); JavaProblem problem = new JavaProblem(message, JavaProblem.ERROR, tabIndex, tabLine); - problem.setPDEOffsets(tabOffset, tabOffset+1); problems.add(problem); } @@ -294,7 +293,6 @@ static private List checkForCurlyQuotes(PreprocSketch ps) { message = Language.interpolate("editor.status.bad_curly_quote", q); } JavaProblem p = new JavaProblem(message, JavaProblem.ERROR, in.tabIndex, line); - p.setPDEOffsets(tabStart, tabStop); problems2.add(p); } } diff --git a/java/src/processing/mode/java/JavaProblem.java b/java/src/processing/mode/java/JavaProblem.java index 5233c785c..005b48380 100644 --- a/java/src/processing/mode/java/JavaProblem.java +++ b/java/src/processing/mode/java/JavaProblem.java @@ -20,8 +20,6 @@ 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,10 @@ public JavaProblem(String message, int type, int tabIndex, int lineNumber) { this.type = type; this.tabIndex = tabIndex; this.lineNumber = lineNumber; - this.startOffset = Optional.empty(); - this.stopOffset = Optional.empty(); + + // Default to 0, 1 unless a longer section is specified + this.startOffset = 0; + this.stopOffset = 1; } @@ -87,32 +87,22 @@ static public JavaProblem fromIProblem(IProblem iProblem, int tabIndex, 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 +153,4 @@ public String toString() { + 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/PreprocService.java b/java/src/processing/mode/java/PreprocService.java index 8557f6f39..718728d41 100644 --- a/java/src/processing/mode/java/PreprocService.java +++ b/java/src/processing/mode/java/PreprocService.java @@ -99,7 +99,7 @@ public class PreprocService { }}; /** - * Create a new preprocessing service to support an editor. + * Create a new preprocessing service to support the language server. */ public PreprocService(JavaMode javaMode, Sketch sketch) { this.javaMode = javaMode; @@ -411,10 +411,12 @@ private PreprocSketch preprocessSketch(PreprocSketch prevResult) { throw new RuntimeException("Unexpected sketch exception in preprocessing: " + e); } + final int endNumLines = numLines; + if (preprocessorResult.getPreprocessIssues().size() > 0) { preprocessorResult.getPreprocessIssues().stream() - .map((x) -> ProblemFactory.build(x, tabLineStarts)) - .forEach(result.otherProblems::add); + .map((x) -> ProblemFactory.build(x, tabLineStarts)) + .forEach(result.otherProblems::add); result.hasSyntaxErrors = true; } diff --git a/java/src/processing/mode/java/ProblemFactory.java b/java/src/processing/mode/java/ProblemFactory.java index ce5cbff63..fd038a0a8 100644 --- a/java/src/processing/mode/java/ProblemFactory.java +++ b/java/src/processing/mode/java/ProblemFactory.java @@ -14,50 +14,6 @@ */ 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 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, - false - ); - } - /** * Create a new {Problem}. * @@ -85,8 +41,7 @@ public static Problem build(PdePreprocessIssue pdePreprocessIssue, List localLine, message, 0, - col, - true + col ); } 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 String getMessage() { 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 @@ void updateProblems(List problems) { .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 ) ),