Skip to content

Commit 837c1fa

Browse files
committed
Improve match when get editor delegate for a file
1 parent 3fe3d8f commit 837c1fa

File tree

9 files changed

+79
-37
lines changed

9 files changed

+79
-37
lines changed

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/CompilerFactory.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
3636
String filePath = file.getAbsolutePath();
3737
String fileName = file.getName();
3838

39-
CompilerFactory.CompileType compilerType = CompileType.NONE;
39+
CompileType compilerType = CompileType.NONE;
4040
if (Catalog.getModeByName("C++").acceptFile(filePath, fileName)) {
41-
compilerType = CompilerFactory.CompileType.G_PLUS_PLUS;
41+
compilerType = CompileType.G_PLUS_PLUS;
4242

4343
} else if (Catalog.getModeByName("C").acceptFile(filePath, fileName)) {
44-
compilerType = CompilerFactory.CompileType.GCC;
44+
compilerType = CompileType.GCC;
45+
} else if (Catalog.getModeByName("Makefile").acceptFile(filePath, fileName)){
46+
47+
compilerType = CompileType.MAKE;
4548
}
4649

4750

@@ -52,6 +55,9 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
5255
case GCC:
5356
return new GCCCompiler(context, nativeActivity, new CompileSetting(context));
5457

58+
case MAKE:
59+
return new MakeCompiler(context);
60+
5561
default:
5662
return null;
5763
}
@@ -62,6 +68,6 @@ public static ICompiler getCompilerForFile(Context context, File[] sourceFiles,
6268
*/
6369

6470
public enum CompileType {
65-
GCC, G_PLUS_PLUS, NONE
71+
GCC, G_PLUS_PLUS, MAKE, NONE
6672
}
6773
}

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/CompilerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class CompilerImpl implements ICompiler {
4141
}
4242

4343
@NonNull
44-
private CommandResult execCommand(@NonNull Context context, @NonNull String workingDir, @NonNull String cmd) {
44+
protected CommandResult execCommand(@NonNull Context context, @NonNull String workingDir, @NonNull String cmd) {
4545
return Shell.exec(context, workingDir, cmd);
4646
}
4747

app/src/main/java/com/duy/ccppcompiler/compiler/compilers/MakeCompiler.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,29 @@
1616

1717
package com.duy.ccppcompiler.compiler.compilers;
1818

19+
import android.content.Context;
20+
21+
import java.io.File;
22+
1923
/**
2024
* Created by Duy on 18-May-18.
2125
*/
2226

23-
public class MakeCompiler {
27+
public class MakeCompiler extends CompilerImpl {
28+
29+
private static final String MARK_PROGRAM = "make";
30+
31+
MakeCompiler(Context context) {
32+
super(context);
33+
}
34+
35+
@Override
36+
protected String buildArgs(File[] sourceFiles) {
37+
return "run";
38+
}
39+
40+
@Override
41+
protected String getCompilerProgram() {
42+
return MARK_PROGRAM;
43+
}
2444
}

editor.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CFLAGS = -Os -Wall -DANDROID -I${EXTERNAL_STORAGE}/CCPlusPlusNIDE/SDL/include
1414

1515
LDFLAGS = -shared ${EXTERNAL_STORAGE}/CCPlusPlusNIDE/SDL/lib/SDL_android_main.o -L${EXTERNAL_STORAGE}/CCPlusPlusNIDE/SDL/lib -lSDL2 -lGLESv1_CM -llog -lm
1616

17-
SDLRUN = am start $(shell am 2>&1| grep -q '\-\-user' && echo '--user 0') -n com.duy.c.cpp.compiler.sdlplugin/.sdlpluginActivity -e sdlmain
17+
SDLRUN = am start --user 0 -n com.duy.c.cpp.compiler.sdlplugin/.sdlpluginActivity -e sdlmain
1818

1919
OBJS = $(TARGET).o
2020

editor/src/main/java/com/duy/ide/Diagnostic.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import com.duy.ide.suggestion.ISuggestion;
2424

25-
import java.io.File;
26-
2725
/**
2826
* Created by Duy on 28-Apr-18.
2927
*/
@@ -44,7 +42,7 @@ public interface Diagnostic extends Parcelable {
4442
* diagnostic.
4543
*/
4644
@Nullable
47-
File getSourceFile();
45+
String getSourceFile();
4846

4947
/**
5048
* Gets a character offset from the beginning of the source object

editor/src/main/java/com/duy/ide/DiagnosticPresenter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ public void onDiagnosticClick(View view, Diagnostic diagnostic) {
6565
if (DLog.DEBUG) {
6666
DLog.d(TAG, "onDiagnosticClick() called diagnostic = [" + diagnostic + "]");
6767
}
68-
File source = diagnostic.getSourceFile();
69-
70-
if (source != null) {
68+
if (diagnostic.getSourceFile() != null) {
69+
File source = new File(diagnostic.getSourceFile());
7170
if (isSystemFile(source)) {
7271
UIUtils.alert(mActivity,
7372
mActivity.getString(R.string.title_non_project_file),
@@ -130,8 +129,10 @@ private IEditorDelegate moveToEditor(File source, @Nullable byte[] md5) {
130129
editorDelegate.doCommand(new Command(Command.CommandEnum.REQUEST_FOCUS));
131130
return editorDelegate;
132131
} else {
133-
mTabManager.newTab(source);
134-
return moveToEditor(source, md5);
132+
if (mTabManager.newTab(source)) {
133+
return moveToEditor(source, md5);
134+
}
135+
return null;
135136
}
136137
}
137138

@@ -188,11 +189,13 @@ private void highlightErrorCurrentEditor() {
188189
boolean firstIndex = false;
189190

190191
for (Diagnostic diagnostic : mDiagnostics) {
191-
File sourceFile = diagnostic.getSourceFile();
192-
if (sourceFile == null) {
192+
if (diagnostic.getSourceFile() == null) {
193193
continue;
194194
}
195-
if (sourceFile.equals(delegate.getDocument().getFile())) {
195+
File sourceFile = new File(diagnostic.getSourceFile());
196+
File editFile = delegate.getDocument().getFile();
197+
if (sourceFile.equals(editFile)
198+
|| diagnostic.getSourceFile().equals(editFile.getName())) {
196199
Command command = new Command(Command.CommandEnum.HIGHLIGHT_ERROR);
197200
int lineNumber = (int) diagnostic.getLineNumber();
198201
int columnNumber = (int) diagnostic.getColumnNumber();

editor/src/main/java/com/duy/ide/model/SimpleDiagnostic.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import com.duy.ide.Kind;
2626
import com.duy.ide.suggestion.ISuggestion;
2727

28-
import java.io.File;
29-
3028
/**
3129
* Created by Duy on 28-Apr-18.
3230
*/
@@ -91,12 +89,8 @@ public Kind getKind() {
9189

9290
@Nullable
9391
@Override
94-
public File getSourceFile() {
95-
File file = new File(filePath);
96-
if (file.exists()){
97-
return file;
98-
}
99-
return null;
92+
public String getSourceFile() {
93+
return filePath;
10094
}
10195

10296
@Override

editor/src/main/java/com/duy/ide/ui/DiagnosticsAdapter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
5454
holder.txtLineCol.setText("");
5555
}
5656

57-
File source = diagnostic.getSourceFile();
58-
if (source != null) {
59-
holder.txtFile.setText(source.getName());
57+
String path = diagnostic.getSourceFile();
58+
if (path != null) {
59+
holder.txtFile.setText(new File(path).getName());
6060
} else {
6161
holder.txtFile.setText("");
6262
}
63-
setIcon(holder, diagnostic);
63+
6464

6565
if (diagnostic.getMessage(mContext).isEmpty()) {
6666
holder.txtMessage.setVisibility(View.GONE);
67-
}else {
67+
} else {
6868
holder.txtMessage.setVisibility(View.VISIBLE);
6969
holder.txtMessage.setText(diagnostic.getMessage(mContext));
7070
}

editor/src/main/java/com/jecelyin/editor/v2/manager/TabManager.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,18 @@ public void onChanged() {
129129
// TODO: 25-Apr-18 show layout create new file
130130
}
131131

132-
public void newTab(File file) {
133-
newTab(file, 0, "UTF-8");
132+
public boolean newTab(File file) {
133+
return newTab(file, 0, "UTF-8");
134134
}
135135

136+
/**
137+
* @return true if new tab added success, otherwise return fable
138+
*/
136139
public boolean newTab(File file, int offset, String encoding) {
140+
if (!file.exists() || !file.canRead() || !file.canWrite()) {
141+
return false;
142+
}
143+
137144
int count = mEditorFragmentPagerAdapter.getCount();
138145
for (int i = 0; i < count; i++) {
139146
EditorPageDescriptor descriptor = mEditorFragmentPagerAdapter.getItem(i);
@@ -296,11 +303,25 @@ public EditorFragmentPagerAdapter getEditorPagerAdapter() {
296303
public Pair<Integer, IEditorDelegate> getEditorDelegate(File file) {
297304
ArrayList<IEditorDelegate> allEditor = mEditorFragmentPagerAdapter.getAllEditor();
298305
for (int i = 0, allEditorSize = allEditor.size(); i < allEditorSize; i++) {
299-
IEditorDelegate editorDelegate = allEditor.get(i);
300-
if (editorDelegate.getDocument().getFile().equals(file)) {
301-
return new Pair<>(i, editorDelegate);
306+
IEditorDelegate delegate = allEditor.get(i);
307+
File editFile = delegate.getDocument().getFile();
308+
if (editFile.equals(file)) {
309+
return new Pair<>(i, delegate);
310+
}
311+
}
312+
313+
//file editor with name only, if has more editor, return null
314+
Pair<Integer, IEditorDelegate> result = null;
315+
for (int i = 0, allEditorSize = allEditor.size(); i < allEditorSize; i++) {
316+
IEditorDelegate delegate = allEditor.get(i);
317+
File editFile = delegate.getDocument().getFile();
318+
if (editFile.getName().equals(file.getPath())) {
319+
if (result != null) {
320+
return null;
321+
}
322+
result = new Pair<>(i, delegate);
302323
}
303324
}
304-
return null;
325+
return result;
305326
}
306327
}

0 commit comments

Comments
 (0)