Skip to content

Commit d0d452c

Browse files
committed
Supported format source feature
1 parent fe98c36 commit d0d452c

File tree

34 files changed

+133
-28
lines changed

34 files changed

+133
-28
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.duy.ide.suggestion.ISuggestion;
2828
import com.jecelyin.common.utils.UIUtils;
2929
import com.jecelyin.editor.v2.common.Command;
30-
import com.jecelyin.editor.v2.editor.EditorDelegate;
30+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
3131
import com.jecelyin.editor.v2.manager.TabManager;
3232
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
3333

@@ -74,7 +74,7 @@ public void onDiagnosticClick(View view, Diagnostic diagnostic) {
7474
mActivity.getString(R.string.message_non_project_file, source.getPath()));
7575
return;
7676
}
77-
EditorDelegate editorDelegate = moveToEditor(source, null);
77+
IEditorDelegate editorDelegate = moveToEditor(source, null);
7878
if (editorDelegate != null) {
7979
Command command = new Command(Command.CommandEnum.GOTO_INDEX);
8080
command.args.putInt("line", (int) diagnostic.getLineNumber());
@@ -88,15 +88,15 @@ public void onDiagnosticClick(View view, Diagnostic diagnostic) {
8888
@Override
8989
public void onSuggestionClick(Diagnostic diagnostic, ISuggestion suggestion) {
9090
File source = suggestion.getSourceFile();
91-
EditorDelegate delegate = moveToEditor(source, mHashCode.get(source));
91+
IEditorDelegate delegate = moveToEditor(source, mHashCode.get(source));
9292

9393
if (delegate != null) {
9494
int start = delegate.getEditText()
9595
.getCursorIndex(suggestion.getLineStart(), suggestion.getColStart()).offset;
9696
int end = delegate.getEditText()
9797
.getCursorIndex(suggestion.getLineEnd(), suggestion.getColEnd()).offset;
9898
if (start >= 0 && start <= end) {
99-
delegate.getEditableText().replace(start, end, suggestion.getMessage());
99+
delegate.getEditText().getEditableText().replace(start, end, suggestion.getMessage());
100100
delegate.getEditText().setSelection(start + suggestion.getMessage().length());
101101
}
102102
mView.remove(diagnostic);
@@ -106,11 +106,11 @@ public void onSuggestionClick(Diagnostic diagnostic, ISuggestion suggestion) {
106106
@Nullable
107107
@SuppressWarnings("ConstantConditions")
108108
@MainThread
109-
private EditorDelegate moveToEditor(File source, @Nullable byte[] md5) {
110-
Pair<Integer, EditorDelegate> pair = mTabManager.getEditorDelegate(source);
109+
private IEditorDelegate moveToEditor(File source, @Nullable byte[] md5) {
110+
Pair<Integer, IEditorDelegate> pair = mTabManager.getEditorDelegate(source);
111111
if (pair != null) {
112112
int index = pair.first;
113-
EditorDelegate editorDelegate = pair.second;
113+
IEditorDelegate editorDelegate = pair.second;
114114
if (editorDelegate.isChanged()) {
115115
return null;
116116
}
@@ -177,7 +177,7 @@ private boolean isSystemFile(File file) {
177177
* Find first error index and move cursor to it
178178
*/
179179
private void highlightErrorCurrentEditor() {
180-
EditorDelegate delegate = mTabManager.getEditorPagerAdapter().getCurrentEditorDelegate();
180+
IEditorDelegate delegate = mTabManager.getEditorPagerAdapter().getCurrentEditorDelegate();
181181
if (delegate == null) {
182182
return;
183183
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.jecelyin.editor.v2.dialog.WrapCharDialog;
6666
import com.jecelyin.editor.v2.editor.Document;
6767
import com.jecelyin.editor.v2.editor.EditorDelegate;
68+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
6869
import com.jecelyin.editor.v2.editor.task.SaveAllTask;
6970
import com.jecelyin.editor.v2.manager.MenuManager;
7071
import com.jecelyin.editor.v2.manager.RecentFilesManager;
@@ -537,7 +538,7 @@ private void showSoftInput() {
537538
}
538539

539540
public void doCommandForAllEditor(Command command) {
540-
for (EditorDelegate editorDelegate : mTabManager.getEditorPagerAdapter().getAllEditor()) {
541+
for (IEditorDelegate editorDelegate : mTabManager.getEditorPagerAdapter().getAllEditor()) {
541542
editorDelegate.doCommand(command);
542543
}
543544
}

editor/src/main/java/com/duy/ide/editor/pager/EditorFragmentPagerAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.jecelyin.editor.v2.common.TabCloseListener;
2626
import com.jecelyin.editor.v2.editor.EditorDelegate;
2727
import com.jecelyin.editor.v2.editor.EditorFragment;
28+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
2829
import com.nakama.arraypageradapter.ArrayFragmentStatePagerAdapter;
2930

3031
import java.io.File;
@@ -115,8 +116,8 @@ public void removeEditor(final int position, final TabCloseListener listener) {
115116
}
116117
}
117118

118-
public ArrayList<EditorDelegate> getAllEditor() {
119-
ArrayList<EditorDelegate> delegates = new ArrayList<>();
119+
public ArrayList<IEditorDelegate> getAllEditor() {
120+
ArrayList<IEditorDelegate> delegates = new ArrayList<>();
120121
for (int i = 0; i < getCount(); i++) {
121122
delegates.add(getEditorDelegateAt(i));
122123
}

editor/src/main/java/com/jecelyin/editor/v2/common/Command.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ public enum CommandEnum {
5959
COMMENT,
6060
INSERT_TEXT,
6161
RELOAD_WITH_ENCODING,
62-
FULL_SCREEN,
63-
THEME, REQUEST_FOCUS, DELETE, HIGHLIGHT_ERROR, CLEAR_ERROR,
62+
REQUEST_FOCUS,
63+
HIGHLIGHT_ERROR,
64+
CLEAR_ERROR,
6465
SHARE_CODE,
66+
FORMAT_SOURCE,
6567
}
6668
}

editor/src/main/java/com/jecelyin/editor/v2/editor/EditorDelegate.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.support.annotation.Nullable;
3232
import android.support.v7.app.AlertDialog;
3333
import android.text.Editable;
34+
import android.text.SpannableStringBuilder;
3435
import android.text.Spanned;
3536
import android.text.TextUtils;
3637
import android.text.TextWatcher;
@@ -39,7 +40,9 @@
3940
import android.view.ActionMode;
4041
import android.view.Menu;
4142
import android.view.MenuItem;
43+
import android.widget.Toast;
4244

45+
import com.duy.astyle.AStyleInterface;
4346
import com.duy.common.ShareUtil;
4447
import com.duy.ide.editor.SimpleEditorActivity;
4548
import com.duy.ide.editor.editor.R;
@@ -181,7 +184,6 @@ public void onDestroy() {
181184
mEditText.removeTextChangedListener(this);
182185
}
183186

184-
185187
public CharSequence getSelectedText() {
186188
return mEditText.hasSelection() ? mEditText.getEditableText().subSequence(mEditText.getSelectionStart(), mEditText.getSelectionEnd()) : "";
187189
}
@@ -236,6 +238,7 @@ public void onSaveFailed(Exception e) {
236238
/**
237239
* Write current content of editor to file
238240
*/
241+
@Override
239242
public void saveCurrentFile() throws Exception {
240243
if (mDocument.isChanged()) {
241244
mDocument.writeToFile(mDocument.getFile(), mDocument.getEncoding());
@@ -245,6 +248,7 @@ public void saveCurrentFile() throws Exception {
245248
/**
246249
* Write out content of editor to file in background thread
247250
*/
251+
@Override
248252
public void saveInBackground() {
249253
if (mDocument.isChanged()) {
250254
saveInBackground(mDocument.getFile(), mDocument.getEncoding());
@@ -253,18 +257,19 @@ public void saveInBackground() {
253257
}
254258
}
255259

256-
257260
public void addHighlight(int start, int end) {
258261
mEditText.getText().setSpan(new BackgroundColorSpan(findResultsKeywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
259262
mEditText.setSelection(end, end);
260263
}
261264

262265
public int getCursorOffset() {
263-
if (mEditText == null)
266+
if (mEditText == null) {
264267
return -1;
268+
}
265269
return mEditText.getSelectionEnd();
266270
}
267271

272+
@Override
268273
public void doCommand(Command command) {
269274
if (mEditText == null)
270275
return;
@@ -393,6 +398,50 @@ public void doCommand(Command command) {
393398
case SHARE_CODE:
394399
shareCurrentContent();
395400
break;
401+
case FORMAT_SOURCE:
402+
formatSource();
403+
break;
404+
}
405+
}
406+
407+
/**
408+
* Format current source
409+
* Supported: c++, c, java,
410+
*/
411+
private void formatSource() {
412+
String currMode = mDocument.getModeName();
413+
String mode;
414+
String style = "gnu";
415+
if (currMode.contentEquals(Catalog.getModeByName("C++").getName())) {
416+
mode = "c";
417+
} else if (currMode.contentEquals(Catalog.getModeByName("C").getName())) {
418+
mode = "c";
419+
} else if (currMode.contentEquals(Catalog.getModeByName("Objective-C").getName())) {
420+
mode = "c";
421+
} else if (currMode.contentEquals(Catalog.getModeByName("C#").getName())) {
422+
mode = "cs";
423+
} else if (currMode.contentEquals(Catalog.getModeByName("Java").getName())) {
424+
mode = "java";
425+
style = "java";
426+
} else {
427+
Toast.makeText(mContext, R.string.unsupported_format_source, Toast.LENGTH_SHORT).show();
428+
return;
429+
}
430+
431+
String options = "--mode=" + mode;
432+
if (!style.isEmpty()) {
433+
options += " --style=" + style;
434+
}
435+
try {
436+
AStyleInterface astyle = new AStyleInterface();
437+
String source = mEditText.getText().toString();
438+
int oldSelection = mEditText.getSelectionStart();
439+
String formatted = astyle.formatSource(source, options);
440+
mEditText.setText(new SpannableStringBuilder(formatted));
441+
mEditText.setSelection(oldSelection);
442+
Toast.makeText(mContext, R.string.formated_source, Toast.LENGTH_SHORT).show();
443+
} catch (Exception e) {
444+
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
396445
}
397446
}
398447

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package com.jecelyin.editor.v2.editor;
22

3+
import android.core.widget.EditAreaView;
4+
import android.support.annotation.Nullable;
5+
6+
import com.jecelyin.editor.v2.common.Command;
7+
38
public interface IEditorDelegate {
9+
//MARK: File interface
10+
11+
void saveCurrentFile() throws Exception;
12+
13+
void saveInBackground();
14+
15+
//MARK: View interface
16+
17+
EditAreaView getEditText();
18+
19+
int getCursorOffset();
20+
21+
22+
//MAKR: Content interface
23+
24+
@Nullable
425
Document getDocument();
26+
27+
void onDocumentChanged();
28+
29+
boolean isChanged();
30+
31+
String getPath();
32+
33+
String getEncoding();
34+
35+
36+
void doCommand(Command command);
37+
538
}

editor/src/main/java/com/jecelyin/editor/v2/editor/task/SaveAllTask.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.duy.ide.editor.SimpleEditorActivity;
2323
import com.duy.ide.editor.pager.EditorFragmentPagerAdapter;
2424
import com.duy.ide.filemanager.SaveListener;
25-
import com.jecelyin.editor.v2.editor.EditorDelegate;
25+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
2626
import com.jecelyin.editor.v2.manager.TabManager;
2727

2828
/**
@@ -50,7 +50,7 @@ protected void onPreExecute() {
5050
protected Boolean doInBackground(Void... voids) {
5151
TabManager tabManager = editorActivity.getTabManager();
5252
EditorFragmentPagerAdapter editorPagerAdapter = tabManager.getEditorPagerAdapter();
53-
for (EditorDelegate editorDelegate : editorPagerAdapter.getAllEditor()) {
53+
for (IEditorDelegate editorDelegate : editorPagerAdapter.getAllEditor()) {
5454
try {
5555
editorDelegate.saveCurrentFile();
5656
} catch (Exception e) {
@@ -65,7 +65,7 @@ protected Boolean doInBackground(Void... voids) {
6565
protected void onPostExecute(Boolean aBoolean) {
6666
super.onPostExecute(aBoolean);
6767
EditorFragmentPagerAdapter editorPagerAdapter = editorActivity.getTabManager().getEditorPagerAdapter();
68-
for (EditorDelegate editorDelegate : editorPagerAdapter.getAllEditor()) {
68+
for (IEditorDelegate editorDelegate : editorPagerAdapter.getAllEditor()) {
6969
editorDelegate.onDocumentChanged();
7070
}
7171
if (aBoolean) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.jecelyin.editor.v2.dialog.SaveConfirmDialog;
3636
import com.jecelyin.editor.v2.editor.Document;
3737
import com.jecelyin.editor.v2.editor.EditorDelegate;
38+
import com.jecelyin.editor.v2.editor.IEditorDelegate;
3839
import com.jecelyin.editor.v2.editor.task.SaveAllTask;
3940
import com.jecelyin.editor.v2.utils.DBHelper;
4041
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
@@ -220,8 +221,8 @@ public boolean onDestroy() {
220221
Preferences.getInstance(mActivity).setLastTab(getCurrentTab());
221222
}
222223
ArrayList<File> needSaveFiles = new ArrayList<>();
223-
ArrayList<EditorDelegate> allEditor = mEditorFragmentPagerAdapter.getAllEditor();
224-
for (EditorDelegate editorDelegate : allEditor) {
224+
ArrayList<IEditorDelegate> allEditor = mEditorFragmentPagerAdapter.getAllEditor();
225+
for (IEditorDelegate editorDelegate : allEditor) {
225226
String path = editorDelegate.getPath();
226227
String encoding = editorDelegate.getEncoding();
227228
int offset = editorDelegate.getCursorOffset();
@@ -292,12 +293,12 @@ public EditorFragmentPagerAdapter getEditorPagerAdapter() {
292293
* @return first is index, second is {@link EditorDelegate}, null if not found
293294
*/
294295
@Nullable
295-
public Pair<Integer, EditorDelegate> getEditorDelegate(File file) {
296+
public Pair<Integer, IEditorDelegate> getEditorDelegate(File file) {
296297
String path = file.getPath();
297-
ArrayList<EditorDelegate> allEditor = mEditorFragmentPagerAdapter.getAllEditor();
298+
ArrayList<IEditorDelegate> allEditor = mEditorFragmentPagerAdapter.getAllEditor();
298299
for (int i = 0, allEditorSize = allEditor.size(); i < allEditorSize; i++) {
299-
EditorDelegate editorDelegate = allEditor.get(i);
300-
if (editorDelegate.getPath().equals(path)) {
300+
IEditorDelegate editorDelegate = allEditor.get(i);
301+
if (editorDelegate.getDocument().getFile().equals(path)) {
301302
return new Pair<>(i, editorDelegate);
302303
}
303304
}

editor/src/main/java/com/jecelyin/editor/v2/widget/menu/MenuFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import java.util.List;
2929
import java.util.Map;
3030

31-
import static com.jecelyin.editor.v2.common.Command.CommandEnum.CURSOR_BACK;
3231
import static com.jecelyin.editor.v2.common.Command.CommandEnum.CONVERT_WRAP_CHAR;
32+
import static com.jecelyin.editor.v2.common.Command.CommandEnum.CURSOR_BACK;
33+
import static com.jecelyin.editor.v2.common.Command.CommandEnum.CURSOR_FORWARD;
3334
import static com.jecelyin.editor.v2.common.Command.CommandEnum.DOC_INFO;
3435
import static com.jecelyin.editor.v2.common.Command.CommandEnum.FIND;
35-
import static com.jecelyin.editor.v2.common.Command.CommandEnum.CURSOR_FORWARD;
36+
import static com.jecelyin.editor.v2.common.Command.CommandEnum.FORMAT_SOURCE;
3637
import static com.jecelyin.editor.v2.common.Command.CommandEnum.GOTO_END;
3738
import static com.jecelyin.editor.v2.common.Command.CommandEnum.GOTO_INDEX;
3839
import static com.jecelyin.editor.v2.common.Command.CommandEnum.GOTO_TOP;
@@ -128,6 +129,7 @@ private void initAllMenuItem() {
128129
menuItemInfos.add(new MenuItemInfo(MenuGroup.EDIT, R.id.action_cursor_back, CURSOR_BACK, R.drawable.ic_arrow_back_white_24dp, R.string.cursor_back));
129130
menuItemInfos.add(new MenuItemInfo(MenuGroup.EDIT, R.id.action_cursor_forward, CURSOR_FORWARD, R.drawable.ic_arrow_forward_white_24dp, R.string.cursor_forward));
130131
menuItemInfos.add(new MenuItemInfo(MenuGroup.EDIT, R.id.action_share_code, SHARE_CODE, R.drawable.baseline_share_24, R.string.share_code));
132+
menuItemInfos.add(new MenuItemInfo(MenuGroup.EDIT, R.id.action_format_source, FORMAT_SOURCE, R.drawable.baseline_format_indent_increase_24, R.string.format_source));
131133

132134
menuItemInfos.add(new MenuItemInfo(MenuGroup.VIEW, R.id.m_info, DOC_INFO, R.drawable.ic_info_white_24dp, R.string.document_info));
133135
menuItemInfos.add(new MenuItemInfo(MenuGroup.VIEW, R.id.action_encoding, NONE, R.drawable.m_encoding, R.string.encoding));

0 commit comments

Comments
 (0)