Skip to content

Commit 9c35049

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

File tree

10 files changed

+124
-61
lines changed

10 files changed

+124
-61
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ public interface Diagnostic<S> extends Parcelable {
109109

110110
@Nullable
111111
ISuggestion getSuggestion();
112+
113+
void setSuggestion(ISuggestion suggestion);
112114
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.duy.ccppcompiler.compiler.diagnostic.model.SimpleDiagnostic;
2020
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.DiagnosticSuggestion;
21+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.ISuggestion;
2122

2223
/**
2324
* Created by Duy on 28-Apr-18.
@@ -37,7 +38,7 @@ public static Diagnostic note(String filePath, int line, int col, String message
3738
}
3839

3940
public static Diagnostic create(Kind kind, String filePath, int line, int col, String message) {
40-
return new SimpleDiagnostic(kind, filePath, line, col, message);
41+
return new SimpleDiagnostic(kind, filePath, line, col, message, null);
4142
}
4243

4344
public static Kind createType(String type) {
@@ -52,7 +53,8 @@ public static Kind createType(String type) {
5253
return Kind.OTHER;
5354
}
5455

55-
public static Diagnostic createFixIt(String filePath, int lineStart, int colStart, int lineEnd, int colEnd, String suggestion) {
56+
public static ISuggestion createSuggestion(String filePath, int lineStart, int colStart,
57+
int lineEnd, int colEnd, String suggestion) {
5658
return new DiagnosticSuggestion(filePath, lineStart, colStart, lineEnd, colEnd, suggestion);
5759
}
5860
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.support.annotation.NonNull;
2020
import android.support.annotation.Nullable;
2121

22+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.ISuggestion;
23+
2224
import java.io.LineNumberReader;
2325
import java.io.StringReader;
2426
import java.util.regex.Matcher;
@@ -41,12 +43,12 @@ public class OutputParser {
4143
"(.*)" /*Message*/);
4244

4345
//fix-it:"/storage/emulated/0/examples/simple/bit_print.c":{6:7-6:11}:"printf"
44-
public static final Pattern FIX_IT_PATTERN = Pattern.compile(
46+
public static final Pattern FIX_IT_PATTERN = Pattern.compile(Pattern.quote(
4547
"(fix-it):" +/*prefix*/
4648
"(.*):" +/*File path*/
47-
"\\{([0-9]+):([0-9]+)-([0-9]+):([0-9]+)}" + /*Index (line:col)-(line:col)*/
49+
"\\{([0-9]+):([0-9]+)-([0-9]+):([0-9]+)}:" + /*Index (line:col)-(line:col)*/
4850
"\"(.*)\"" /*Message*/
49-
);
51+
));
5052

5153
private DiagnosticsCollector diagnosticsCollector;
5254

@@ -75,31 +77,36 @@ public void parse(String inputData) {
7577
private void processLine(@NonNull String line, @Nullable String nextLine) {
7678
try {
7779
Matcher matcher = DIAGNOSTICS_PATTERN.matcher(line);
80+
Diagnostic diagnostic;
7881
if (matcher.find()) {
7982
String filePath = matcher.group(1);
8083
int lineNumber = Integer.parseInt(matcher.group(2));
8184
int colNumber = Integer.parseInt(matcher.group(3));
8285
Kind type = DiagnosticFactory.createType(matcher.group(4));
8386
String message = matcher.group(5);
8487

85-
Diagnostic diagnostic = DiagnosticFactory.create(type, filePath, lineNumber, colNumber, message);
88+
diagnostic = DiagnosticFactory.create(type, filePath, lineNumber, colNumber, message);
8689
diagnosticsCollector.report(diagnostic);
90+
} else {
91+
if (nextLine != null) {
92+
processLine(nextLine, null);
93+
}
8794
return;
8895
}
8996
if (nextLine == null) {
9097
return;
9198
}
9299

93-
matcher = FIX_IT_PATTERN.matcher(line);
100+
matcher = FIX_IT_PATTERN.matcher(nextLine);
94101
if (matcher.find()) {
95102
String filePath = matcher.group(2);
96103
int lineStart = Integer.parseInt(matcher.group(3));
97104
int colStart = Integer.parseInt(matcher.group(4));
98105
int lineEnd = Integer.parseInt(matcher.group(5));
99106
int colEnd = Integer.parseInt(matcher.group(6));
100-
String suggestion = matcher.group(7);
101-
Diagnostic diagnostic = DiagnosticFactory.createFixIt(filePath, lineStart, colStart, lineEnd, colEnd, suggestion);
102-
diagnosticsCollector.report(diagnostic);
107+
String message = matcher.group(7);
108+
ISuggestion suggestion = DiagnosticFactory.createSuggestion(filePath, lineStart, colStart, lineEnd, colEnd, message);
109+
diagnostic.setSuggestion(suggestion);
103110
} else {
104111
processLine(nextLine, null);
105112
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import android.content.Context;
2020
import android.os.Parcel;
2121
import android.os.Parcelable;
22+
import android.support.annotation.Nullable;
2223

2324
import com.duy.ccppcompiler.compiler.diagnostic.Diagnostic;
2425
import com.duy.ccppcompiler.compiler.diagnostic.Kind;
26+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.ISuggestion;
2527

2628
import java.io.File;
2729

@@ -46,13 +48,16 @@ public SimpleDiagnostic[] newArray(int size) {
4648
private final int line;
4749
private final int col;
4850
private final String message;
51+
@Nullable
52+
private ISuggestion suggestion;
4953

50-
public SimpleDiagnostic(Kind kind, String filePath, int line, int col, String message) {
54+
public SimpleDiagnostic(Kind kind, String filePath, int line, int col, String message, @Nullable ISuggestion suggestion) {
5155
this.kind = kind;
5256
this.filePath = filePath;
5357
this.line = line;
5458
this.col = col;
5559
this.message = message;
60+
this.suggestion = suggestion;
5661
}
5762

5863
protected SimpleDiagnostic(Parcel in) {
@@ -61,6 +66,7 @@ protected SimpleDiagnostic(Parcel in) {
6166
line = in.readInt();
6267
col = in.readInt();
6368
message = in.readString();
69+
suggestion = in.readParcelable(ISuggestion.class.getClassLoader());
6470
}
6571

6672
@Override
@@ -70,6 +76,7 @@ public void writeToParcel(Parcel dest, int flags) {
7076
dest.writeInt(line);
7177
dest.writeInt(col);
7278
dest.writeString(message);
79+
dest.writeParcelable(suggestion, flags);
7380
}
7481

7582
@Override
@@ -133,6 +140,17 @@ public String getMessage(Context context) {
133140
return message;
134141
}
135142

143+
@Nullable
144+
@Override
145+
public ISuggestion getSuggestion() {
146+
return null;
147+
}
148+
149+
@Override
150+
public void setSuggestion(ISuggestion suggestion) {
151+
this.suggestion = suggestion;
152+
}
153+
136154
@Override
137155
public boolean equals(Object o) {
138156
if (this == o) return true;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.duy.ccppcompiler.compiler.diagnostic.suggestion;
1818

19+
import android.os.Parcelable;
20+
1921
import java.io.File;
2022

2123
/**
2224
* Created by Duy on 29-Apr-18.
2325
*/
2426

25-
public interface ISuggestion {
27+
public interface ISuggestion extends Parcelable {
2628
File getFilePath();
2729

2830
int getLineStart();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.duy.ccppcompiler.compiler.diagnostic.suggestion;
1818

1919
/**
20+
* An empty replacement string indicates that the given range is to be removed. An empty range
21+
* (e.g. “45:3-45:3”) indicates that the string is to be inserted at the given position.
22+
* Otherwise replace
2023
* Created by Duy on 29-Apr-18.
2124
*/
2225

app/src/main/java/com/duy/ccppcompiler/diagnostic/DiagnosticAdapter.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,19 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
4040
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
4141
final Diagnostic diagnostic = mDiagnostics.get(position);
4242
if (diagnostic.getLineNumber() != Diagnostic.NOPOS) {
43-
holder.txtLine.setText(String.valueOf(diagnostic.getLineNumber()));
44-
if (diagnostic.getColumnNumber() != Diagnostic.NOPOS) {
45-
holder.txtCol.setText(String.valueOf(diagnostic.getColumnNumber()));
46-
} else {
47-
holder.txtCol.getEditableText().clear();
48-
}
43+
String text = diagnostic.getLineNumber() + ":" + diagnostic.getColumnNumber();
44+
holder.txtLineCol.setText(text);
4945
} else {
50-
holder.txtLine.getEditableText().clear();
51-
holder.txtCol.getEditableText().clear();
46+
holder.txtLineCol.getEditableText().clear();
5247
}
48+
holder.btnFixIt.setVisibility(diagnostic.getSuggestion() != null ?
49+
View.VISIBLE : View.GONE);
50+
holder.btnFixIt.setOnClickListener(new View.OnClickListener() {
51+
@Override
52+
public void onClick(View v) {
53+
mDiagnosticClickListener.onSuggestionClick(v, diagnostic.getSuggestion());
54+
}
55+
});
5356
Object source = diagnostic.getSource();
5457
if (source instanceof File) {
5558
holder.txtFile.setText(((File) source).getName());
@@ -103,15 +106,16 @@ public void setData(List<Diagnostic> diagnostics) {
103106
}
104107

105108
static class ViewHolder extends RecyclerView.ViewHolder {
106-
TextView txtLine, txtCol, txtMessage, txtFile;
109+
TextView txtLineCol, txtMessage, txtFile;
107110
View root;
111+
View btnFixIt;
108112

109113
ViewHolder(View itemView) {
110114
super(itemView);
111-
txtLine = itemView.findViewById(R.id.txt_line);
112-
txtCol = itemView.findViewById(R.id.txt_col);
115+
txtLineCol = itemView.findViewById(R.id.txt_line_col);
113116
txtMessage = itemView.findViewById(R.id.txt_message);
114117
txtFile = itemView.findViewById(R.id.txt_file);
118+
btnFixIt = itemView.findViewById(R.id.btn_fix_it);
115119
root = itemView;
116120
}
117121
}

app/src/main/java/com/duy/ccppcompiler/diagnostic/DiagnosticClickListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import android.view.View;
2020

2121
import com.duy.ccppcompiler.compiler.diagnostic.Diagnostic;
22+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.ISuggestion;
2223

2324
/**
2425
* Created by Duy on 28-Apr-18.
2526
*/
2627

2728
public interface DiagnosticClickListener {
2829
public void onDiagnosisClick(Diagnostic diagnostic, View view);
30+
31+
void onSuggestionClick(View v, ISuggestion suggestion);
2932
}

app/src/main/java/com/duy/ccppcompiler/diagnostic/DiagnosticFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import com.duy.ccppcompiler.R;
3131
import com.duy.ccppcompiler.compiler.diagnostic.Diagnostic;
32+
import com.duy.ccppcompiler.compiler.diagnostic.suggestion.ISuggestion;
3233

3334
import java.util.ArrayList;
3435
import java.util.List;
@@ -117,4 +118,9 @@ public void onDiagnosisClick(Diagnostic diagnostic, View view) {
117118
mPresenter.onDiagnosticClick(view, diagnostic);
118119
}
119120
}
121+
122+
@Override
123+
public void onSuggestionClick(View v, ISuggestion suggestion) {
124+
125+
}
120126
}

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

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,64 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
17-
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
16+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1817
xmlns:tools="http://schemas.android.com/tools"
1918
android:layout_width="match_parent"
20-
android:background="?selectableItemBackground"
2119
android:layout_height="wrap_content"
22-
android:orientation="horizontal">
20+
android:background="?selectableItemBackground"
21+
android:orientation="vertical">
2322

24-
<android.support.v7.widget.AppCompatTextView
25-
android:id="@+id/txt_line"
26-
android:layout_width="wrap_content"
27-
android:layout_height="match_parent"
28-
android:padding="4dp"
29-
android:textColor="?android:textColorPrimary"
30-
android:typeface="monospace"
31-
tools:text="1" />
32-
33-
<android.support.v7.widget.AppCompatTextView
34-
android:id="@+id/txt_col"
35-
android:layout_width="wrap_content"
36-
android:layout_height="match_parent"
37-
android:padding="4dp"
23+
<RelativeLayout
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:orientation="horizontal">
3827

39-
android:textColor="?android:textColorPrimary"
40-
android:typeface="monospace"
41-
tools:text="1" />
28+
<android.support.v7.widget.AppCompatTextView
29+
android:id="@+id/txt_line_col"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_alignParentLeft="true"
33+
android:layout_alignParentStart="true"
34+
android:layout_alignParentTop="true"
35+
android:padding="4dp"
36+
android:textColor="?android:textColorPrimary"
37+
android:typeface="monospace"
38+
tools:text="12:22" />
4239

43-
<android.support.v7.widget.AppCompatTextView
44-
android:id="@+id/txt_file"
45-
android:layout_width="wrap_content"
46-
android:layout_height="match_parent"
47-
android:padding="4dp"
48-
android:textColor="?android:textColorPrimary"
49-
android:typeface="monospace"
50-
tools:text="main.c" />
51-
52-
<android.support.v7.widget.AppCompatTextView
53-
android:id="@+id/txt_message"
40+
<android.support.v7.widget.AppCompatTextView
41+
android:id="@+id/txt_file"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
45+
android:layout_alignParentTop="true"
46+
android:layout_toEndOf="@+id/txt_line_col"
47+
android:layout_toRightOf="@+id/txt_line_col"
48+
android:padding="4dp"
49+
android:textColor="?android:textColorPrimary"
50+
android:typeface="monospace"
51+
tools:text="main.c" />
52+
53+
<android.support.v7.widget.AppCompatTextView
54+
android:id="@+id/txt_message"
55+
android:layout_width="match_parent"
56+
android:layout_height="wrap_content"
57+
58+
android:layout_alignParentTop="true"
59+
android:layout_toEndOf="@+id/txt_file"
60+
android:layout_toRightOf="@+id/txt_file"
61+
android:padding="4dp"
62+
android:textColor="?android:textColorPrimary"
63+
android:typeface="monospace"
64+
tools:text="Long message Long message Long message Long messageLong messageLong messageLong messageLong messageLong message" />
65+
66+
</RelativeLayout>
67+
68+
<android.support.v7.widget.AppCompatButton
69+
android:id="@+id/btn_fix_it"
5470
android:layout_width="wrap_content"
55-
android:layout_height="match_parent"
56-
android:padding="4dp"
57-
android:textColor="?android:textColorPrimary"
58-
android:typeface="monospace"
59-
tools:text="Sample message" />
60-
</TableRow>
71+
android:layout_height="wrap_content"
72+
android:layout_gravity="right"
73+
android:text="Fit it"
74+
android:textAllCaps="false" />
75+
76+
</LinearLayout>

0 commit comments

Comments
 (0)