Start refactor of #715 to specific optionals.

This commit is contained in:
Sam Pottinger
2023-05-11 13:22:19 -07:00
parent a0cbb64b4d
commit 9fcf7527aa
5 changed files with 188 additions and 58 deletions
+52 -6
View File
@@ -20,6 +20,8 @@ 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;
@@ -42,9 +44,9 @@ public class JavaProblem implements Problem {
/** Line number (pde code) of the error */
private final int lineNumber;
private int startOffset;
private Optional<Integer> startOffset;
private int stopOffset;
private Optional<Integer> stopOffset;
/**
* If the error is a 'cannot find type' contains the list of suggested imports
@@ -60,6 +62,8 @@ public class JavaProblem implements Problem {
this.type = type;
this.tabIndex = tabIndex;
this.lineNumber = lineNumber;
this.startOffset = Optional.empty();
this.stopOffset = Optional.empty();
}
@@ -83,22 +87,31 @@ public class JavaProblem implements Problem {
public void setPDEOffsets(int startOffset, int stopOffset){
this.startOffset = startOffset;
this.stopOffset = stopOffset;
this.startOffset = Optional.of(startOffset);
this.stopOffset = Optional.of(stopOffset);
}
@Override
public int getStartOffset() {
public Optional<Integer> getTabStartOffset() {
return startOffset;
}
@Override
public int getStopOffset() {
public Optional<Integer> getTabStopOffset() {
return stopOffset;
}
@Override
public Optional<Integer> getLineStartOffset() {
return Optional.empty();
}
@Override
public Optional<Integer> getLineStopOffset() {
return Optional.empty();
}
@Override
public boolean isError() {
@@ -149,4 +162,37 @@ public class JavaProblem implements Problem {
+ startOffset + ",LN STOP OFF: " + stopOffset + ",PROB: "
+ message;
}
@Override
public int computeTabStartOffset(LineToTabOffsetGetter strategy) {
Optional<Integer> nativeTabStartOffset = getTabStartOffset();
if (nativeTabStartOffset.isPresent()) {
return nativeTabStartOffset.get();
}
Optional<Integer> lineStartOffset = getLineStartOffset();
int lineOffset = strategy.get(getLineNumber());
if (lineStartOffset.isPresent()) {
return lineOffset + lineStartOffset.get();
} else {
return lineOffset;
}
}
@Override
public int computeTabStopOffset(LineToTabOffsetGetter strategy) {
Optional<Integer> nativeTabStopOffset = getTabStopOffset();
if (nativeTabStopOffset.isPresent()) {
return nativeTabStopOffset.get();
}
Optional<Integer> lineStopOffset = getLineStopOffset();
int lineOffset = strategy.get(getLineNumber());
if (lineStopOffset.isPresent()) {
return lineOffset + lineStopOffset.get();
} else {
return lineOffset;
}
}
}
@@ -1,5 +1,26 @@
/*
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;
import java.util.Optional;
/**
* Problem identifying a syntax error found in preprocessing.
*/
@@ -8,9 +29,10 @@ public class SyntaxProblem extends JavaProblem {
private final int tabIndex;
private final int lineNumber;
private final String message;
private final int startOffset;
private final int stopOffset;
private final boolean lineFlag;
private final Optional<Integer> tabStartOffset;
private final Optional<Integer> tabStopOffset;
private final Optional<Integer> lineStartOffset;
private final Optional<Integer> lineStopOffset;
/**
* Create a new syntax problem.
@@ -33,9 +55,18 @@ public class SyntaxProblem extends JavaProblem {
tabIndex = newTabIndex;
lineNumber = newLineNumber;
message = newMessage;
startOffset = newStartOffset;
stopOffset = newStopOffset;
lineFlag = newUsesLineOffset;
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);
}
}
@Override
@@ -64,17 +95,23 @@ public class SyntaxProblem extends JavaProblem {
}
@Override
public int getStartOffset() {
return startOffset;
public Optional<Integer> getTabStartOffset() {
return tabStartOffset;
}
@Override
public int getStopOffset() {
return stopOffset;
public Optional<Integer> getTabStopOffset() {
return tabStopOffset;
}
public boolean usesLineOffset() {
return lineFlag;
@Override
public Optional<Integer> getLineStartOffset() {
return lineStartOffset;
}
@Override
public Optional<Integer> getLineStopOffset() {
return lineStopOffset;
}
}