Skip to content

Commit 216a6b7

Browse files
committed
cleanup and refactor
1 parent d7247fe commit 216a6b7

31 files changed

+135
-277
lines changed

app/src/main/java/com/duy/ccppcompiler/ide/editor/CppIdeActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public class CppIdeActivity extends IdeActivity {
8585
@Override
8686
protected void onCreate(Bundle savedInstanceState) {
8787
super.onCreate(savedInstanceState);
88+
89+
if (getTabManager().getTabCount() == 0){
90+
createNewFile();
91+
}
92+
8893
mPremiumHelper = new InAppPurchaseHelper(this);
8994
// Monitor launch times and interval from installation
9095
RateThisApp.onCreate(this);

lib-n-ide/src/main/java/com/duy/ide/diagnostic/DiagnosticContract.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
import com.duy.ide.diagnostic.parser.PatternAwareOutputParser;
2626
import com.duy.ide.logging.ILogger;
2727

28+
import java.io.OutputStream;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031

3132
/**
3233
* Created by Duy on 28-Apr-18.
3334
*/
34-
35+
@SuppressWarnings("unused")
3536
public class DiagnosticContract {
3637
public interface View {
3738
@WorkerThread
@@ -79,5 +80,11 @@ public interface Presenter extends ILogger {
7980
void clear();
8081

8182
void setOutputParser(@NonNull PatternAwareOutputParser... parsers);
83+
84+
@NonNull
85+
OutputStream getStandardOutput();
86+
87+
@NonNull
88+
OutputStream getErrorOutput();
8289
}
8390
}

lib-n-ide/src/main/java/com/duy/ide/diagnostic/DiagnosticPresenter.java

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import android.support.annotation.MainThread;
2323
import android.support.annotation.NonNull;
2424
import android.support.annotation.Nullable;
25-
import android.support.annotation.UiThread;
25+
import android.support.annotation.WorkerThread;
2626
import android.support.v4.util.Pair;
2727
import android.view.View;
2828

@@ -40,6 +40,7 @@
4040

4141
import java.io.File;
4242
import java.io.IOException;
43+
import java.io.OutputStream;
4344
import java.util.ArrayList;
4445
import java.util.List;
4546

@@ -56,6 +57,9 @@ public class DiagnosticPresenter implements DiagnosticContract.Presenter {
5657
private ToolOutputParser mToolOutputParser = null;
5758
private DiagnosticContract.View mView;
5859

60+
private OutputStream mStdOut;
61+
private OutputStream mStdErr;
62+
5963
public DiagnosticPresenter(@Nullable DiagnosticContract.View view,
6064
@NonNull IdeActivity activity,
6165
@NonNull TabManager tabManager,
@@ -67,6 +71,33 @@ public DiagnosticPresenter(@Nullable DiagnosticContract.View view,
6771
mView.setPresenter(this);
6872
}
6973
mHandler = handler;
74+
createIOStream();
75+
}
76+
77+
private void createIOStream() {
78+
mStdOut = new OutputStream() {
79+
@Override
80+
public void write(int b) throws IOException {
81+
write(new byte[]{(byte) b}, 0, 1);
82+
}
83+
84+
@Override
85+
public void write(@NonNull byte[] b, int off, int len) throws IOException {
86+
onNewMessage(new String(b, off, len));
87+
}
88+
};
89+
mStdErr = new OutputStream() {
90+
@Override
91+
public void write(int b) throws IOException {
92+
write(new byte[]{(byte) b}, 0, 1);
93+
}
94+
95+
@Override
96+
public void write(@NonNull byte[] b, int off, int len) throws IOException {
97+
onNewMessage(new String(b, off, len));
98+
}
99+
};
100+
70101
}
71102

72103
@SuppressWarnings("ConstantConditions")
@@ -124,7 +155,7 @@ public void hidePanel() {
124155
mActivity.mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
125156
}
126157

127-
@MainThread
158+
@SuppressLint("WrongThread")
128159
@Override
129160
public void setMessages(ArrayList<Message> messages) {
130161
show(messages);
@@ -152,29 +183,35 @@ private boolean isSystemFile(File file) {
152183
* Show error in current editor,
153184
* Find first error index and move cursor to it
154185
*/
155-
@UiThread
156-
private void highlightError(List<Message> messages) {
157-
for (Message message : messages) {
158-
if (message.getKind() != Message.Kind.ERROR) {
159-
continue;
160-
}
161-
if (message.getSourcePath() == null) {
162-
continue;
163-
}
164-
File sourceFile = new File(message.getSourcePath());
165-
Pair<Integer, IEditorDelegate> position = mTabManager.getEditorDelegate(sourceFile);
166-
if (position == null) {
167-
continue;
186+
@WorkerThread
187+
private void highlightError(final List<Message> messages) {
188+
mHandler.post(new Runnable() {
189+
@Override
190+
public void run() {
191+
for (Message message : messages) {
192+
if (message.getKind() != Message.Kind.ERROR) {
193+
continue;
194+
}
195+
if (message.getSourcePath() == null) {
196+
continue;
197+
}
198+
File sourceFile = new File(message.getSourcePath());
199+
Pair<Integer, IEditorDelegate> position = mTabManager.getEditorDelegate(sourceFile);
200+
if (position == null) {
201+
continue;
202+
}
203+
@SuppressWarnings("ConstantConditions")
204+
@NonNull final IEditorDelegate editorDelegate = position.second;
205+
Command command = new Command(Command.CommandEnum.HIGHLIGHT_ERROR);
206+
int lineNumber = message.getLineNumber();
207+
int columnNumber = message.getColumn();
208+
command.args.putInt("line", lineNumber);
209+
command.args.putInt("col", columnNumber);
210+
editorDelegate.doCommand(command);
211+
}
168212
}
169-
@SuppressWarnings("ConstantConditions")
170-
@NonNull final IEditorDelegate editorDelegate = position.second;
171-
Command command = new Command(Command.CommandEnum.HIGHLIGHT_ERROR);
172-
int lineNumber = message.getLineNumber();
173-
int columnNumber = message.getColumn();
174-
command.args.putInt("line", lineNumber);
175-
command.args.putInt("col", columnNumber);
176-
editorDelegate.doCommand(command);
177-
}
213+
});
214+
178215
}
179216

180217

@@ -189,20 +226,31 @@ public void setOutputParser(@NonNull PatternAwareOutputParser... parsers) {
189226
mToolOutputParser = new ToolOutputParser(parsers, this);
190227
}
191228

229+
@NonNull
230+
@Override
231+
public OutputStream getStandardOutput() {
232+
return mStdOut;
233+
}
234+
235+
@NonNull
236+
@Override
237+
public OutputStream getErrorOutput() {
238+
return mStdErr;
239+
}
240+
192241
@Override
193242
public void onNewMessage(String text) {
194-
mView.printMessage(text);
243+
if (mView != null) {
244+
mView.printMessage(text);
245+
}
195246
if (mToolOutputParser == null) {
196247
return;
197248
}
249+
250+
//parse output, show diagnosis
198251
final List<Message> messages = mToolOutputParser.parseToolOutput(text);
199252
show(messages);
200-
mHandler.post(new Runnable() {
201-
@Override
202-
public void run() {
203-
highlightError(messages);
204-
}
205-
});
253+
highlightError(messages);
206254
}
207255

208256
@Override

lib-n-ide/src/main/java/com/duy/ide/diagnostic/view/DiagnosticFragment.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.view.LayoutInflater;
3030
import android.view.View;
3131
import android.view.ViewGroup;
32+
import android.widget.ScrollView;
3233

3334
import com.duy.ide.diagnostic.DiagnosticClickListener;
3435
import com.duy.ide.diagnostic.DiagnosticContract;
@@ -51,17 +52,19 @@ public class DiagnosticFragment extends Fragment implements DiagnosticContract.V
5152
private DiagnosticContract.Presenter mPresenter;
5253
private DiagnosticsAdapter mAdapter;
5354
private RecyclerView mDiagnosticView;
54-
private LogView mLogView;
5555
private ViewPager mViewPager;
5656

57+
private ScrollView mLogScroller;
58+
private LogView mLogView;
59+
5760
public static DiagnosticFragment newInstance() {
5861
return new DiagnosticFragment();
5962
}
6063

6164
@Nullable
6265
@Override
6366
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
64-
return inflater.inflate(R.layout.fragment_diagnostic, container, false);
67+
return inflater.inflate(R.layout.fragment_diagnostic_default, container, false);
6568

6669
}
6770

@@ -77,6 +80,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7780
mLogView = view.findViewById(R.id.txt_log);
7881
//disable save log, avoid crash with large data
7982
mLogView.setSaveEnabled(false);
83+
mLogScroller = view.findViewById(R.id.compiler_output_container);
8084

8185
mDiagnosticView = view.findViewById(R.id.diagnostic_list_view);
8286
mDiagnosticView.setLayoutManager(new LinearLayoutManager(getContext()));
@@ -123,6 +127,7 @@ public void printMessage(final String log) {
123127
@Override
124128
public void run() {
125129
mLogView.append(log);
130+
mLogScroller.fullScroll(View.FOCUS_DOWN);
126131
////move to log view
127132
//if (mAdapter.getDiagnostics().isEmpty()) {
128133
// mViewPager.setCurrentItem(1);
@@ -137,6 +142,7 @@ public void printError(final String log) {
137142
@Override
138143
public void run() {
139144
mLogView.append(log);
145+
mLogScroller.fullScroll(View.FOCUS_DOWN);
140146

141147
//move to log view
142148
if (mAdapter.getDiagnostics().isEmpty()) {

lib-n-ide/src/main/java/com/duy/ide/diagnostic/view/DiagnosticsAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class DiagnosticsAdapter extends RecyclerView.Adapter<DiagnosticsAdapter.
5656
@NonNull
5757
@Override
5858
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
59-
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_diagnostic, parent, false);
59+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_diagnostic_default, parent, false);
6060
return new ViewHolder(view);
6161
}
6262

lib-n-ide/src/main/java/com/duy/ide/file/dialogs/DialogNewFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static DialogNewFile newInstance(@NonNull String[] fileExtensions,
6767
@Nullable
6868
@Override
6969
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
70-
View view = inflater.inflate(R.layout.dialog_new_file, container, false);
70+
View view = inflater.inflate(R.layout.dialog_new_file_default, container, false);
7171
return view;
7272
}
7373

lib-n-ide/src/main/java/com/jecelyin/editor/v2/adapter/TabAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class TabAdapter extends RecyclerView.Adapter {
3838
@NonNull
3939
@Override
4040
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
41-
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_item, parent, false));
41+
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_item_default, parent, false));
4242
}
4343

4444
@Override

lib-n-ide/src/main/java/com/jecelyin/editor/v2/dialog/FinderDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static void showReplaceDialog(EditorDelegate fragment) {
8585

8686
@Override
8787
public void show() {
88-
View view = LayoutInflater.from(context).inflate(R.layout.dialog_find_replace, null);
88+
View view = LayoutInflater.from(context).inflate(R.layout.dialog_find_replace_default, null);
8989

9090
final ViewHolder holder = new ViewHolder(view);
9191
// holder.mFindEditText.setDrawableClickListener(this);

lib-n-ide/src/main/java/com/jecelyin/editor/v2/editor/EditorDelegate.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,19 @@ public void onCreate(IEditAreaView editorView) {
182182
mEditText.addTextChangedListener(this);
183183
onDocumentChanged();
184184

185+
String fileName = mDocument.getFile().getPath().replaceAll("[^A-Za-z0-9_]", "_");
185186
SharedPreferences historyData = mContext.getSharedPreferences(
186-
mDocument.getFile().getPath(), Context.MODE_PRIVATE);
187+
fileName, Context.MODE_PRIVATE);
187188
mEditText.restoreEditHistory(historyData);
188189
}
189190

190191
public void onDestroy() {
191192
if (isChanged() && Preferences.getInstance(getContext()).isAutoSave()) {
192193
saveInBackground();
193194
}
195+
String fileName = mDocument.getFile().getPath().replaceAll("[^A-Za-z0-9_]", "_");
194196
SharedPreferences historyData = mContext.getSharedPreferences(
195-
mDocument.getFile().getPath(), Context.MODE_PRIVATE);
197+
fileName, Context.MODE_PRIVATE);
196198
mEditText.saveHistory(historyData);
197199
mEditText.removeTextChangedListener(mDocument);
198200
mEditText.removeTextChangedListener(this);
@@ -557,10 +559,10 @@ private void convertSelectedText(int id) {
557559

558560
String selectedText = getEditableText().subSequence(start, end).toString();
559561

560-
if (id == R.id.m_convert_to_uppercase) {
562+
if (id == R.id.action_convert_to_uppercase) {
561563
selectedText = selectedText.toUpperCase();
562564

563-
} else if (id == R.id.m_convert_to_lowercase) {
565+
} else if (id == R.id.action_convert_to_lowercase) {
564566
selectedText = selectedText.toLowerCase();
565567

566568
}
@@ -685,20 +687,20 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
685687
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
686688

687689
if (!readOnly) {
688-
menu.add(0, R.id.m_convert_to_uppercase, 0, R.string.convert_to_uppercase)
690+
menu.add(0, R.id.action_convert_to_uppercase, 0, R.string.convert_to_uppercase)
689691
.setIcon(R.drawable.m_uppercase)
690692
.setAlphabeticShortcut('U')
691693
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
692694

693-
menu.add(0, R.id.m_convert_to_lowercase, 0, R.string.convert_to_lowercase)
695+
menu.add(0, R.id.action_convert_to_lowercase, 0, R.string.convert_to_lowercase)
694696
.setIcon(R.drawable.m_lowercase)
695697
.setAlphabeticShortcut('L')
696698
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
697699
}
698700
}
699701

700702
if (!readOnly) {
701-
menu.add(0, R.id.m_duplication, 0, selected ? R.string.duplication_text : R.string.duplication_line)
703+
menu.add(0, R.id.action_duplicate, 0, selected ? R.string.duplication_text : R.string.duplication_line)
702704
.setIcon(R.drawable.ic_control_point_duplicate_white_24dp)
703705
.setAlphabeticShortcut('L')
704706
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
@@ -719,10 +721,10 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
719721
if (i == R.id.action_find_replace) {
720722
doCommand(new Command(Command.CommandEnum.FIND));
721723
return true;
722-
} else if (i == R.id.m_convert_to_uppercase || i == R.id.m_convert_to_lowercase) {
724+
} else if (i == R.id.action_convert_to_uppercase || i == R.id.action_convert_to_lowercase) {
723725
convertSelectedText(item.getItemId());
724726
return true;
725-
} else if (i == R.id.m_duplication) {
727+
} else if (i == R.id.action_duplicate) {
726728
mEditText.duplicateSelection();
727729
return true;
728730
}

lib-n-ide/src/main/java/com/jecelyin/editor/v2/editor/EditorFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
6969
File file = (File) arguments.getSerializable(KEY_FILE);
7070
mEditorDelegate = new EditorDelegate(file, offset, encoding);
7171
}
72-
View view = inflater.inflate(R.layout.fragment_editor, container, false);
72+
View view = inflater.inflate(R.layout.fragment_editor_default, container, false);
7373
mEditorDelegate.onCreate((IEditAreaView) view.findViewById(R.id.edit_text));
7474
return view;
7575
}

0 commit comments

Comments
 (0)