Skip to content

Commit 7e6f5a9

Browse files
committed
Code analyzer
1 parent 460b546 commit 7e6f5a9

File tree

8 files changed

+116
-60
lines changed

8 files changed

+116
-60
lines changed

app/src/main/java/com/duy/ccppcompiler/compiler/analyze/CppCheckAnalyzer.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@
1818
package com.duy.ccppcompiler.compiler.analyze;
1919

2020
import android.content.Context;
21-
import android.core.widget.EditAreaView;
2221
import android.os.Handler;
2322
import android.support.annotation.NonNull;
2423
import android.text.Editable;
2524
import android.text.TextWatcher;
2625

2726
import com.duy.ccppcompiler.compiler.shell.CommandBuilder;
2827
import com.duy.ccppcompiler.compiler.shell.CommandResult;
28+
import com.duy.ccppcompiler.compiler.shell.OutputParser;
2929
import com.duy.ccppcompiler.compiler.shell.Shell;
3030
import com.duy.common.DLog;
31-
import com.jecelyin.editor.v2.editor.EditorDelegate;
32-
33-
import org.apache.commons.io.IOUtils;
31+
import com.duy.ide.diagnostic.Diagnostic;
32+
import com.duy.ide.diagnostic.DiagnosticPresenter;
33+
import com.duy.ide.diagnostic.DiagnosticsCollector;
34+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
35+
import com.jecelyin.editor.v2.io.LocalFileWriter;
3436

3537
import java.io.File;
36-
import java.io.FileOutputStream;
3738
import java.io.IOException;
39+
import java.util.ArrayList;
3840
import java.util.regex.Pattern;
3941

4042
/**
@@ -49,8 +51,9 @@ public class CppCheckAnalyzer implements ICodeAnalyser {
4951
private static final Pattern TEMPLATE_PATTERN = Pattern.compile("^(\\S+):([0-9]+):(.*)");
5052

5153
private final Context mContext;
52-
private final EditorDelegate mEditorDelegate;
54+
private final IEditorDelegate mEditorDelegate;
5355
private final android.os.Handler mHandler = new Handler();
56+
private DiagnosticPresenter mDiagnosticPresenter;
5457
private final Runnable mAnalyze = new Runnable() {
5558
@Override
5659
public void run() {
@@ -62,35 +65,47 @@ public void run() {
6265
}
6366
};
6467

65-
public CppCheckAnalyzer(@NonNull Context context, @NonNull EditorDelegate delegate) {
66-
this.mContext = context;
67-
this.mEditorDelegate = delegate;
68-
setEditor(delegate.getEditText());
68+
public CppCheckAnalyzer(@NonNull Context context, @NonNull IEditorDelegate delegate, DiagnosticPresenter diagnosticPresenter) {
69+
mContext = context;
70+
mEditorDelegate = delegate;
71+
mDiagnosticPresenter = diagnosticPresenter;
6972
}
7073

7174
private void analyzeImpl() throws IOException {
72-
File file = new File(mContext.getCacheDir(), "cppcheck/tmp/" + mEditorDelegate.getDocument().getFile().getName());
73-
FileOutputStream output = new FileOutputStream(file);
74-
IOUtils.write(mEditorDelegate.getEditableText(), output);
75-
output.close();
76-
if (DLog.DEBUG) DLog.d(TAG, "analyzeImpl: write tmp file complete");
75+
File originFile = mEditorDelegate.getDocument().getFile();
76+
File tmpFile = new File(getCppCheckTmpDir(), originFile.getName());
77+
78+
LocalFileWriter localFileWriter = new LocalFileWriter(tmpFile, "UTF-8");
79+
localFileWriter.writeToFile(mEditorDelegate.getEditText().getText());
7780

7881
CommandBuilder builder = new CommandBuilder(CPPCHECK_PROGRAM);
7982
builder.addFlags(TEMPLATE);
80-
builder.addFlags(file.getAbsolutePath());
83+
builder.addFlags(tmpFile.getAbsolutePath());
8184

8285
String cmd = builder.build();
83-
CommandResult result = Shell.exec(mContext, file.getParent(), cmd);
86+
CommandResult result = Shell.exec(mContext, tmpFile.getParent(), cmd);
8487
if (DLog.DEBUG) DLog.d(TAG, "result = " + result);
85-
}
88+
String message = result.getMessage().replace(tmpFile.getAbsolutePath(), originFile.getAbsolutePath());
8689

87-
@Override
88-
public void analyze(File[] sources) {
90+
DiagnosticsCollector diagnosticsCollector = new DiagnosticsCollector();
91+
OutputParser parser = new OutputParser(diagnosticsCollector);
92+
parser.parse(message);
8993

94+
ArrayList<Diagnostic> diagnostics = diagnosticsCollector.getDiagnostics();
95+
mDiagnosticPresenter.setDiagnostics(diagnostics);
9096
}
9197

92-
public void setEditor(EditAreaView editor) {
93-
editor.addTextChangedListener(new TextWatcher() {
98+
private File getCppCheckTmpDir() {
99+
File dir = new File(mContext.getCacheDir(), "cppcheck/tmp");
100+
if (!dir.exists()) {
101+
dir.mkdirs();
102+
}
103+
return dir;
104+
}
105+
106+
@Override
107+
public void start() {
108+
mEditorDelegate.getEditText().addTextChangedListener(new TextWatcher() {
94109

95110
@Override
96111
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

app/src/main/java/com/duy/ccppcompiler/compiler/analyze/ICodeAnalyser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616

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

19-
import java.io.File;
20-
2119
/**
2220
* Created by Duy on 25-Apr-18.
2321
*/
2422

2523
public interface ICodeAnalyser {
26-
void analyze(File[] sources);
24+
25+
void start();
2726
}

app/src/main/java/com/duy/editor/CodeEditorActivity.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import android.content.Intent;
2121
import android.core.widget.EditAreaView;
2222
import android.os.Bundle;
23-
import android.support.v4.view.ViewPager;
2423
import android.view.Menu;
2524
import android.view.MenuItem;
2625
import android.view.SubMenu;
@@ -29,6 +28,7 @@
2928
import com.duy.ccppcompiler.BuildConfig;
3029
import com.duy.ccppcompiler.R;
3130
import com.duy.ccppcompiler.compiler.CompilerSettingActivity;
31+
import com.duy.ccppcompiler.compiler.analyze.CppCheckAnalyzer;
3232
import com.duy.ccppcompiler.compiler.compilers.CompilerFactory;
3333
import com.duy.ccppcompiler.compiler.compilers.ICompiler;
3434
import com.duy.ccppcompiler.compiler.manager.CompileManager;
@@ -47,6 +47,7 @@
4747
import com.jecelyin.common.utils.UIUtils;
4848
import com.jecelyin.editor.v2.editor.Document;
4949
import com.jecelyin.editor.v2.editor.EditorDelegate;
50+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
5051
import com.jecelyin.editor.v2.widget.menu.MenuDef;
5152
import com.kobakei.ratethisapp.RateThisApp;
5253
import com.pdaxrom.cctools.BuildConstants;
@@ -76,28 +77,24 @@ protected void onCreate(Bundle savedInstanceState) {
7677
mPremiumHelper = new InAppPurchaseHelper(this);
7778
// Monitor launch times and interval from installation
7879
RateThisApp.onCreate(this);
79-
80-
initCodeAnalyzer();
8180
}
8281

83-
private void initCodeAnalyzer() {
84-
mEditorPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
85-
@Override
86-
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
87-
88-
}
89-
90-
@Override
91-
public void onPageSelected(int position) {
92-
if (DLog.DEBUG)
93-
DLog.d(TAG, "onPageSelected() called with: position = [" + position + "]");
94-
}
82+
@Override
83+
public void onEditorViewCreated(IEditorDelegate editorDelegate) {
84+
super.onEditorViewCreated(editorDelegate);
85+
File file = editorDelegate.getDocument().getFile();
86+
String fileName = file.getName().toLowerCase();
87+
if (fileName.endsWith(".cpp") || fileName.endsWith(".c")) {
88+
initCodeAnalyzer(editorDelegate);
89+
}
90+
}
9591

96-
@Override
97-
public void onPageScrollStateChanged(int state) {
9892

99-
}
100-
});
93+
private void initCodeAnalyzer(IEditorDelegate editorDelegate) {
94+
if (DLog.DEBUG)
95+
DLog.d(TAG, "initCodeAnalyzer() called with: editorDelegate = [" + editorDelegate + "]");
96+
CppCheckAnalyzer analyzer = new CppCheckAnalyzer(this, editorDelegate, mDiagnosticPresenter);
97+
analyzer.start();
10198
}
10299

103100
@Override

libeditor/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
<application android:supportsRtl="false">
2727
<activity android:name="com.duy.ide.settings.EditorSettingsActivity" />
2828
<activity android:name="com.duy.file.explorer.FileExplorerActivity" />
29+
<activity android:name="com.duy.ide.core.SimpleEditorActivity" />
2930
</application>
3031
</manifest>

libeditor/src/main/java/com/duy/ide/core/SimpleEditorActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.jecelyin.editor.v2.editor.Document;
7373
import com.jecelyin.editor.v2.editor.EditorDelegate;
7474
import com.jecelyin.editor.v2.editor.IEditorDelegate;
75+
import com.jecelyin.editor.v2.editor.IEditorStateListener;
7576
import com.jecelyin.editor.v2.editor.task.SaveAllTask;
7677
import com.jecelyin.editor.v2.manager.MenuManager;
7778
import com.jecelyin.editor.v2.manager.RecentFilesManager;
@@ -97,6 +98,7 @@
9798
* @author Jecelyin Peng <jecelyin@gmail.com>
9899
*/
99100
public class SimpleEditorActivity extends ThemeSupportActivity implements MenuItem.OnMenuItemClickListener,
101+
IEditorStateListener,
100102
SharedPreferences.OnSharedPreferenceChangeListener {
101103

102104
protected static final int RC_OPEN_FILE = 1;
@@ -739,4 +741,10 @@ public void invalidateEditMenu(Document document, EditAreaView mEditText) {
739741
setMenuStatus(R.id.action_undo, mEditText != null && mEditText.canUndo() ? MenuDef.STATUS_NORMAL : MenuDef.STATUS_DISABLED);
740742
setMenuStatus(R.id.action_redo, mEditText != null && mEditText.canRedo() ? MenuDef.STATUS_NORMAL : MenuDef.STATUS_DISABLED);
741743
}
744+
745+
746+
@Override
747+
public void onEditorViewCreated(IEditorDelegate editorDelegate) {
748+
749+
}
742750
}

libeditor/src/main/java/com/duy/ide/diagnostic/DiagnosticPresenter.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import com.duy.common.DLog;
2626
import com.duy.ide.core.SimpleEditorActivity;
27-
import com.duy.ide.editor.editor.R;
2827
import com.duy.ide.diagnostic.suggestion.ISuggestion;
28+
import com.duy.ide.editor.editor.R;
2929
import com.jecelyin.common.utils.UIUtils;
3030
import com.jecelyin.editor.v2.common.Command;
3131
import com.jecelyin.editor.v2.editor.IEditorDelegate;
@@ -185,27 +185,32 @@ private void highlightError() {
185185

186186
boolean firstIndex = false;
187187
for (Diagnostic diagnostic : mDiagnostics) {
188+
// if (diagnostic.getKind() != Kind.ERROR) {
189+
// continue;
190+
// }
188191
if (diagnostic.getSourceFile() == null) {
189192
continue;
190193
}
191194
File sourceFile = new File(diagnostic.getSourceFile());
192195
Pair<Integer, IEditorDelegate> position = mTabManager.getEditorDelegate(sourceFile);
193-
if (position != null) {
194-
final IEditorDelegate editorDelegate = position.second;
195-
Command command = new Command(Command.CommandEnum.HIGHLIGHT_ERROR);
196-
int lineNumber = (int) diagnostic.getLineNumber();
197-
int columnNumber = (int) diagnostic.getColumnNumber();
198-
199-
command.args.putInt("line", lineNumber);
200-
command.args.putInt("col", columnNumber);
201-
editorDelegate.doCommand(command);
196+
if (position == null) {
197+
continue;
198+
}
202199

203-
if (!firstIndex) {
204-
Command gotoIndexCmd = new Command(Command.CommandEnum.GOTO_INDEX);
205-
gotoIndexCmd.args.putAll(command.args);
206-
editorDelegate.doCommand(gotoIndexCmd);
207-
firstIndex = true;
208-
}
200+
final IEditorDelegate editorDelegate = position.second;
201+
Command command = new Command(Command.CommandEnum.HIGHLIGHT_ERROR);
202+
int lineNumber = (int) diagnostic.getLineNumber();
203+
int columnNumber = (int) diagnostic.getColumnNumber();
204+
205+
command.args.putInt("line", lineNumber);
206+
command.args.putInt("col", columnNumber);
207+
editorDelegate.doCommand(command);
208+
209+
if (!firstIndex) {
210+
Command gotoIndexCmd = new Command(Command.CommandEnum.GOTO_INDEX);
211+
gotoIndexCmd.args.putAll(command.args);
212+
editorDelegate.doCommand(gotoIndexCmd);
213+
firstIndex = true;
209214
}
210215
}
211216
}

libeditor/src/main/java/com/jecelyin/editor/v2/editor/EditorFragment.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
7575
return view;
7676
}
7777

78+
@Override
79+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
80+
super.onViewCreated(view, savedInstanceState);
81+
if (getActivity() instanceof IEditorStateListener) {
82+
IEditorStateListener listener = (IEditorStateListener) getActivity();
83+
listener.onEditorViewCreated(mEditorDelegate);
84+
}
85+
}
86+
7887
@Override
7988
public void onDestroyView() {
8089
if (mEditorDelegate != null) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2018 Duy Tran Le
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.jecelyin.editor.v2.editor;
19+
20+
public interface IEditorStateListener {
21+
void onEditorViewCreated(IEditorDelegate editorDelegate);
22+
}

0 commit comments

Comments
 (0)