Skip to content

Commit d9b0252

Browse files
committed
Fix it supported
1 parent 9c35049 commit d9b0252

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed

app/src/main/java/com/duy/ccppcompiler/compiler/CompileManager.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
* Copyright 2018 Mr Duy
34
*
@@ -31,6 +32,7 @@
3132
import com.jecelyin.editor.v2.ui.widget.menu.MenuDef;
3233

3334
import java.io.File;
35+
import java.util.ArrayList;
3436

3537
/**
3638
* Created by Duy on 25-Apr-18.
@@ -88,8 +90,17 @@ public void onCompileFailed(ShellResult shellResult) {
8890
DiagnosticsCollector diagnosticsCollector = new DiagnosticsCollector<>();
8991
OutputParser parser = new OutputParser(diagnosticsCollector);
9092
parser.parse(shellResult.getMessage());
91-
mDiagnosticPresenter.setDiagnostics(diagnosticsCollector.getDiagnostics());
93+
ArrayList diagnostics = diagnosticsCollector.getDiagnostics();
94+
mDiagnosticPresenter.setDiagnostics(diagnostics);
9295
mDiagnosticPresenter.showView();
96+
97+
debug(diagnostics);
98+
}
99+
}
100+
101+
private void debug(ArrayList diagnostics) {
102+
for (Object diagnostic : diagnostics) {
103+
System.out.println(diagnostic);
93104
}
94105
}
95106

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/OutputParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ public class OutputParser {
4343
"(.*)" /*Message*/);
4444

4545
//fix-it:"/storage/emulated/0/examples/simple/bit_print.c":{6:7-6:11}:"printf"
46-
public static final Pattern FIX_IT_PATTERN = Pattern.compile(Pattern.quote(
46+
public static final Pattern FIX_IT_PATTERN = Pattern.compile(
4747
"(fix-it):" +/*prefix*/
4848
"(.*):" +/*File path*/
49-
"\\{([0-9]+):([0-9]+)-([0-9]+):([0-9]+)}:" + /*Index (line:col)-(line:col)*/
50-
"\"(.*)\"" /*Message*/
51-
));
49+
"\\{([0-9]+):([0-9]+)-([0-9]+):([0-9]+)\\}:" + /*Index (line:col)-(line:col)*/
50+
Pattern.quote("\"") + "(.*)" + Pattern.quote("\"")/*Message*/);
5251

5352
private DiagnosticsCollector diagnosticsCollector;
5453

@@ -76,6 +75,7 @@ public void parse(String inputData) {
7675
@SuppressWarnings("unchecked")
7776
private void processLine(@NonNull String line, @Nullable String nextLine) {
7877
try {
78+
line = line.trim();
7979
Matcher matcher = DIAGNOSTICS_PATTERN.matcher(line);
8080
Diagnostic diagnostic;
8181
if (matcher.find()) {
@@ -96,6 +96,7 @@ private void processLine(@NonNull String line, @Nullable String nextLine) {
9696
if (nextLine == null) {
9797
return;
9898
}
99+
nextLine = nextLine.trim();
99100

100101
matcher = FIX_IT_PATTERN.matcher(nextLine);
101102
if (matcher.find()) {

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/model/SimpleDiagnostic.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,6 @@ public long getStartPosition() {
104104
return NOPOS;
105105
}
106106

107-
@Override
108-
public String toString() {
109-
return "SimpleDiagnostic{" +
110-
"kind=" + kind +
111-
", filePath='" + filePath + '\'' +
112-
", line=" + line +
113-
", col=" + col +
114-
", message='" + message + '\'' +
115-
'}';
116-
}
117-
118107
@Override
119108
public long getEndPosition() {
120109
return NOPOS;
@@ -143,14 +132,26 @@ public String getMessage(Context context) {
143132
@Nullable
144133
@Override
145134
public ISuggestion getSuggestion() {
146-
return null;
135+
return suggestion;
147136
}
148137

149138
@Override
150139
public void setSuggestion(ISuggestion suggestion) {
151140
this.suggestion = suggestion;
152141
}
153142

143+
@Override
144+
public String toString() {
145+
return "SimpleDiagnostic{" +
146+
"kind=" + kind +
147+
", filePath='" + filePath + '\'' +
148+
", line=" + line +
149+
", col=" + col +
150+
", message='" + message + '\'' +
151+
", suggestion=" + suggestion +
152+
'}';
153+
}
154+
154155
@Override
155156
public boolean equals(Object o) {
156157
if (this == o) return true;
@@ -163,7 +164,8 @@ public boolean equals(Object o) {
163164
if (getKind() != that.getKind()) return false;
164165
if (filePath != null ? !filePath.equals(that.filePath) : that.filePath != null)
165166
return false;
166-
return message != null ? message.equals(that.message) : that.message == null;
167+
if (message != null ? !message.equals(that.message) : that.message != null) return false;
168+
return getSuggestion() != null ? getSuggestion().equals(that.getSuggestion()) : that.getSuggestion() == null;
167169
}
168170

169171
@Override
@@ -173,7 +175,7 @@ public int hashCode() {
173175
result = 31 * result + line;
174176
result = 31 * result + col;
175177
result = 31 * result + (message != null ? message.hashCode() : 0);
178+
result = 31 * result + (getSuggestion() != null ? getSuggestion().hashCode() : 0);
176179
return result;
177180
}
178-
179181
}

app/src/main/java/com/duy/ccppcompiler/compiler/diagnostic/suggestion/DiagnosticSuggestion.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,45 @@ protected DiagnosticSuggestion(Parcel in) {
8282
suggestion = in.readString();
8383
}
8484

85+
@Override
86+
public String toString() {
87+
return "DiagnosticSuggestion{" +
88+
"filePath='" + filePath + '\'' +
89+
", lineStart=" + lineStart +
90+
", colStart=" + colStart +
91+
", lineEnd=" + lineEnd +
92+
", colEnd=" + colEnd +
93+
", suggestion='" + suggestion + '\'' +
94+
'}';
95+
}
96+
97+
@Override
98+
public boolean equals(Object o) {
99+
if (this == o) return true;
100+
if (!(o instanceof DiagnosticSuggestion)) return false;
101+
102+
DiagnosticSuggestion that = (DiagnosticSuggestion) o;
103+
104+
if (getLineStart() != that.getLineStart()) return false;
105+
if (getColStart() != that.getColStart()) return false;
106+
if (getLineEnd() != that.getLineEnd()) return false;
107+
if (getColEnd() != that.getColEnd()) return false;
108+
if (getFilePath() != null ? !getFilePath().equals(that.getFilePath()) : that.getFilePath() != null)
109+
return false;
110+
return getSuggestion() != null ? getSuggestion().equals(that.getSuggestion()) : that.getSuggestion() == null;
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
int result = getFilePath() != null ? getFilePath().hashCode() : 0;
116+
result = 31 * result + getLineStart();
117+
result = 31 * result + getColStart();
118+
result = 31 * result + getLineEnd();
119+
result = 31 * result + getColEnd();
120+
result = 31 * result + (getSuggestion() != null ? getSuggestion().hashCode() : 0);
121+
return result;
122+
}
123+
85124
@Override
86125
public int describeContents() {
87126
return 0;

app/src/main/res/layout/list_item_diagnostic.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
android:id="@+id/txt_file"
4242
android:layout_width="wrap_content"
4343
android:layout_height="wrap_content"
44-
4544
android:layout_alignParentTop="true"
4645
android:layout_toEndOf="@+id/txt_line_col"
4746
android:layout_toRightOf="@+id/txt_line_col"

app/src/test/java/com/duy/ccppcompiler/compiler/diagnostic/parser/OutputParserTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import junit.framework.TestCase;
2424

2525
import java.util.ArrayList;
26+
import java.util.regex.Matcher;
2627

2728
/**
2829
* Created by Duy on 28-Apr-18.
@@ -63,12 +64,17 @@ public void testParse2() throws Exception {
6364
public void testFixIt() {
6465
DiagnosticsCollector<Diagnostic> diagnosticsCollector = new DiagnosticsCollector<>();
6566
OutputParser outputParser = new OutputParser(diagnosticsCollector);
66-
outputParser.parse("fix-it:\"/storage/emulated/0/examples/simple/bit_print.c\":{13:7-13:23}:\"temporaryVariable\"");
67+
outputParser.parse("/storage/emulated/0/examples/simple/bit_print.c:6:7: warning: implicit declaration of function 'pinf'; did you mean 'printf'?\n" +
68+
" fix-it:\"/storage/emulated/0/examples/simple/bit_print.c\":{6:7-6:11}:\"printf\"");
6769
ArrayList<Diagnostic<? extends Diagnostic>> diagnostics = diagnosticsCollector.getDiagnostics();
6870
for (Diagnostic<? extends Diagnostic> diagnostic : diagnostics) {
6971
System.out.println(diagnostic);
7072
}
7173
assertEquals(diagnostics.size(), 1);
7274
}
7375

76+
public void testFixIt2() {
77+
Matcher matcher = OutputParser.FIX_IT_PATTERN.matcher("fix-it:\"/storage/emulated/0/examples/simple/bit_print.c\":{6:7-6:11}:\"printf\"");
78+
assertTrue(matcher.find());
79+
}
7480
}

0 commit comments

Comments
 (0)