Skip to content

Commit

Permalink
refs #365: Cleaned up Problem ctor, fields, interface
Browse files Browse the repository at this point in the history
  • Loading branch information
carymrobbins committed Jun 25, 2018
1 parent c4d8417 commit 5e6502c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 64 deletions.
16 changes: 9 additions & 7 deletions src/com/haskforce/highlighting/annotation/HaskellProblem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

public abstract class HaskellProblem {
public int startLine;
public int startColumn;
public interface HaskellProblem {

public abstract void createAnnotations(@NotNull PsiFile file, @NotNull HaskellAnnotationHolder holder);
int getStartLine();

public int getOffsetStart(final String fileText) {
return StringUtil.lineColToOffset(fileText, startLine - 1, startColumn - 1);
}
int getStartColumn();

void createAnnotations(@NotNull PsiFile file, @NotNull HaskellAnnotationHolder holder);

default int getOffsetStart(String fileText) {
return StringUtil.lineColToOffset(fileText, getStartLine() - 1, getStartColumn() - 1);
}
}
29 changes: 20 additions & 9 deletions src/com/haskforce/highlighting/annotation/external/GhcMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,24 @@ public static String type(@NotNull Module module, @NotNull String workDir, @NotN
}
}

public static class Problem extends HaskellProblem {
public String file;
public String message;
public boolean isError;
public boolean isUnknownSymbol;
public boolean isUnusedSymbol;
public boolean isUnusedImport;
public static class Problem implements HaskellProblem {
public final String file;
public final int startLine;
public final int startColumn;
public final String message;
public final boolean isError;
public final boolean isUnknownSymbol;
public final boolean isUnusedSymbol;
public final boolean isUnusedImport;

private static Pattern notInScopeRegex = Pattern.compile("(?i)not in scope");

public Problem(String file, int startLine, int startColumn, String message) {
this.file = file;
this.startLine = startLine;
this.startColumn = startColumn;
this.message = message;
this.isError = !message.startsWith("Warning: ");
if (!this.isError) this.message = message.substring("Warning: ".length());
this.message = this.isError ? message : message.substring("Warning: ".length());
isUnknownSymbol = notInScopeRegex.matcher(message).find();
isUnusedSymbol = message.contains("Defined but not used");
isUnusedImport = message.contains("import of") && message.contains("is redundant");
Expand Down Expand Up @@ -336,6 +337,16 @@ public void registerFix(@NotNull Annotation annotation) {
}
}

@Override
public int getStartLine() {
return startLine;
}

@Override
public int getStartColumn() {
return startColumn;
}

/**
* Create an annotation from this problem and add it to the annotation holder.
*/
Expand Down
100 changes: 53 additions & 47 deletions src/com/haskforce/highlighting/annotation/external/HLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public static Problem parseProblemFallback(String lint) {
}
final String from = split.get(0).trim();
final String to = split.get(1).trim();
return new Problem("", "", hint, from, to, "", new String[]{}, "", line, column);
return Problem.forFallback("", "", hint, from, to, "", new String[]{}, "", line, column);
}

/**
Expand Down Expand Up @@ -211,47 +211,40 @@ public static Either<ExecError, String> runHlint(HaskellToolsConsole toolConsole
return ExecUtil.readCommandLine(commandLine);
}

public static class Problem extends HaskellProblem {

public String decl;
public String file;
public String hint;
public String from;
public String to;
public String module;
public String[] note;
public String severity;
public int endLine;
public int endColumn;
public boolean useJson;

/**
* Provide a default constructor so gson objects will default to `useJson = true`.
*/
public Problem() {
useJson = true;
}

public Problem(String decl, String file, String hint, String from, String to, String module, String[] note,
String severity, int startLine, int startColumn, int endLine, int endColumn) {
public static class Problem implements HaskellProblem {

public final String decl;
public final String file;
public final String hint;
public final String from;
public final String to;
public final String module;
public final String[] note;
public final String severity;
public final int startLine;
public final int startColumn;
public final int endLine;
public final int endColumn;
public final boolean useJson;

public Problem(String decl, String file, String hint, String from, String to, String module, String[] note, String severity, int startLine, int startColumn, int endLine, int endColumn, boolean useJson) {
this.decl = decl;
this.file = file;
this.from = from;
this.hint = hint;
this.from = from;
this.to = to;
this.module = module;
this.note = note;
this.severity = severity;
this.startLine = startLine;
this.startColumn = startColumn;
this.endLine = endLine;
this.endColumn = endColumn;
this.to = to;
this.useJson = useJson;
}

public Problem(String decl, String file, String hint, String from, String to, String module, String[] note,
String severity, int startLine, int startColumn) {
this(decl, file, hint, from, to, module, note, severity, startLine, startColumn, -1, -1);
useJson = false;
public static Problem forFallback(String decl, String file, String hint, String from, String to, String module, String[] note, String severity, int startLine, int startColumn) {
return new Problem(decl, file, hint, from, to, module, note, severity, startLine, startColumn, -1, -1, false);
}

public String getMessage() {
Expand All @@ -263,6 +256,16 @@ protected void createAnnotation(@NotNull HaskellAnnotationHolder holder, int sta
if (ann != null) ann.registerFix(new IgnoreHLint(hint));
}

@Override
public int getStartLine() {
return startLine;
}

@Override
public int getStartColumn() {
return startColumn;
}

@Override
public void createAnnotations(@NotNull PsiFile file, @NotNull HaskellAnnotationHolder holder) {
final String text = file.getText();
Expand Down Expand Up @@ -332,8 +335,11 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Problem problem = (Problem) o;
return endLine == problem.endLine &&
return startLine == problem.startLine &&
startColumn == problem.startColumn &&
endLine == problem.endLine &&
endColumn == problem.endColumn &&
useJson == problem.useJson &&
Objects.equals(decl, problem.decl) &&
Objects.equals(file, problem.file) &&
Objects.equals(hint, problem.hint) &&
Expand All @@ -346,7 +352,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = Objects.hash(decl, file, hint, from, to, module, severity, endLine, endColumn);
int result = Objects.hash(decl, file, hint, from, to, module, severity, startLine, startColumn, endLine, endColumn, useJson);
result = 31 * result + Arrays.hashCode(note);
return result;
}
Expand All @@ -362,21 +368,21 @@ private static class HLintProblemGsonAdapter implements JsonDeserializer<Problem
@Override
public Problem deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject o = jsonElement.getAsJsonObject();
Problem p = new Problem();
p.useJson = true;
p.decl = extractStringOrSingletonArray(o, "decl");
p.file = o.get("file").getAsString();
p.hint = o.get("hint").getAsString();
p.from = o.get("from").getAsString();
p.to = o.get("to").getAsString();
p.module = extractStringOrSingletonArray(o, "module");
p.note = extractStringArray(o, "note");
p.severity = o.get("severity").getAsString();
p.startLine = o.get("startLine").getAsInt();
p.startColumn = o.get("startColumn").getAsInt();
p.endLine = o.get("endLine").getAsInt();
p.endColumn = o.get("endColumn").getAsInt();
return p;
return new Problem(
extractStringOrSingletonArray(o, "decl"),
o.get("file").getAsString(),
o.get("hint").getAsString(),
o.get("from").getAsString(),
o.get("to").getAsString(),
extractStringOrSingletonArray(o, "module"),
extractStringArray(o, "note"),
o.get("severity").getAsString(),
o.get("startLine").getAsInt(),
o.get("startColumn").getAsInt(),
o.get("endLine").getAsInt(),
o.get("endColumn").getAsInt(),
true
);
}

private String extractStringOrSingletonArray(JsonObject o, String field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class HLintParserTest extends UsefulTestCase with AssertMixin {
4,
8,
5,
25
25,
true
)
}
}

0 comments on commit 5e6502c

Please sign in to comment.