Skip to content

Commit 5aff820

Browse files
committed
support insert tab when press tab key
1 parent 0300d73 commit 5aff820

File tree

9 files changed

+80
-63
lines changed

9 files changed

+80
-63
lines changed

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252
import com.duy.common.purchase.Premium;
5353
import com.duy.file.explorer.FileExplorerActivity;
5454
import com.duy.ide.code.api.CodeFormatProvider;
55+
import com.duy.ide.code.api.SuggestItem;
56+
import com.duy.ide.code.api.SuggestionProvider;
5557
import com.duy.ide.core.api.IdeActivity;
5658
import com.duy.ide.diagnostic.DiagnosticContract;
5759
import com.duy.ide.diagnostic.parser.PatternAwareOutputParser;
5860
import com.duy.ide.editor.EditorDelegate;
5961
import com.duy.ide.editor.IEditorDelegate;
62+
import com.duy.ide.editor.internal.suggestion.DefaultSuggestItem;
63+
import com.duy.ide.editor.internal.suggestion.Editor;
6064
import com.jecelyin.common.utils.UIUtils;
6165
import com.jecelyin.editor.v2.Preferences;
6266
import com.jecelyin.editor.v2.widget.menu.MenuDef;
@@ -65,6 +69,7 @@
6569
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
6670

6771
import java.io.File;
72+
import java.util.ArrayList;
6873
import java.util.regex.Pattern;
6974

7075
import jackpal.androidterm.TermPreferencesActivity;
@@ -130,22 +135,22 @@ public void onEditorViewCreated(@NonNull IEditorDelegate editorDelegate) {
130135
}
131136
}
132137

133-
// editorDelegate.setSuggestionProvider(new SuggestionProvider() {
134-
// @Override
135-
// public ArrayList<SuggestItem> getSuggestions(Editor editor) {
136-
// try {
137-
// Thread.sleep(100);
138-
// } catch (InterruptedException e) {
139-
// e.printStackTrace();
140-
// }
141-
// ArrayList<SuggestItem> items = new ArrayList<>();
142-
// int size = (int) (Math.random() * 10);
143-
// for (int i = 0; i <= size; i++) {
144-
// items.add(new DefaultSuggestItem("toString", "", "int", "s", 0));
145-
// }
146-
// return items;
147-
// }
148-
// });
138+
editorDelegate.setSuggestionProvider(new SuggestionProvider() {
139+
@Override
140+
public ArrayList<SuggestItem> getSuggestions(Editor editor) {
141+
try {
142+
Thread.sleep(100);
143+
} catch (InterruptedException e) {
144+
e.printStackTrace();
145+
}
146+
ArrayList<SuggestItem> items = new ArrayList<>();
147+
int size = (int) (Math.random() * 10);
148+
for (int i = 0; i <= size; i++) {
149+
items.add(new DefaultSuggestItem("toString", "", "int", "s", 0));
150+
}
151+
return items;
152+
}
153+
});
149154

150155
}
151156

lib-n-ide/build.gradle

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.regex.Pattern
2+
13
apply plugin: 'com.android.library'
24

35
android {
@@ -112,22 +114,22 @@ task copyReleaseToJavaNIDE << {
112114
println "rename success"
113115

114116
//now modify setting.gradle
115-
// println "modify settings.gradle"
116-
// File settingsFile = new File(javaIdeDir, "settings.gradle");
117-
// String content = fileToString(settingsFile);
118-
// //comment it
119-
// content = content.replaceAll("include\\s+':${Pattern.quote(oldModuleName)}'", "//include ':${newModuleName}'");
120-
// FileOutputStream output = new FileOutputStream(settingsFile);
121-
// output.write(content.getBytes());
122-
// output.close();
123-
//
124-
// //now modify build.gradle
125-
// println "modify build.gradle"
126-
// File gradleFile = new File(moduleDir, "build.gradle")
127-
// output = new FileOutputStream(gradleFile);
128-
// output.write(("configurations.maybeCreate(\"default\")\n" +
129-
// "artifacts.add(\"default\", file('${newModuleName}.aar'))").getBytes());
130-
// output.close();
117+
println "modify settings.gradle"
118+
File settingsFile = new File(javaIdeDir, "settings.gradle");
119+
String content = fileToString(settingsFile);
120+
//comment it
121+
content = content.replaceAll("include\\s+':${Pattern.quote(oldModuleName)}'", "//include ':${newModuleName}'");
122+
FileOutputStream output = new FileOutputStream(settingsFile);
123+
output.write(content.getBytes());
124+
output.close();
125+
126+
//now modify build.gradle
127+
println "modify build.gradle"
128+
File gradleFile = new File(moduleDir, "build.gradle")
129+
output = new FileOutputStream(gradleFile);
130+
output.write(("configurations.maybeCreate(\"default\")\n" +
131+
"artifacts.add(\"default\", file('${newModuleName}.aar'))").getBytes());
132+
output.close();
131133
//
132134
// File appGradleFile = new File(javaIdeDir, "app/build.gradle");
133135
// if (appGradleFile.exists()) {

lib-n-ide/src/main/java/com/duy/ide/code/api/SuggestItem.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.duy.ide.code.api;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
36
import com.duy.ide.editor.view.IEditAreaView;
47

58
/**
@@ -14,23 +17,21 @@ public interface SuggestItem {
1417
/**
1518
* Display name
1619
*/
20+
@Nullable
1721
String getName();
1822

1923
/**
2024
* Display description
2125
*/
26+
@Nullable
2227
String getDescription();
2328

2429
/**
2530
* Display type
2631
*/
32+
@Nullable
2733
String getReturnType();
2834

29-
/**
30-
* @return the text will be insert then user click suggestion item
31-
*/
32-
String getInsertText();
33-
3435
int getSuggestionPriority();
3536

3637
/**
@@ -45,5 +46,5 @@ public interface SuggestItem {
4546
/**
4647
* @param editAreaView - editor you will modify for this suggestion
4748
*/
48-
void onSelectThis(IEditAreaView editAreaView);
49+
void onSelectThis(@NonNull IEditAreaView editAreaView);
4950
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ private void postGetSuggestion() {
547547
SuggestionEditor view = (SuggestionEditor) this.mEditText;
548548
if (view != null && view.hasFocus() && !view.hasSelection()) {
549549
mHandler.removeCallbacks(mGetSuggestion);
550-
mHandler.postDelayed(mGetSuggestion, 100);
550+
mHandler.postDelayed(mGetSuggestion, 50);
551551
}
552552
}
553553

lib-n-ide/src/main/java/com/duy/ide/editor/internal/suggestion/DefaultSuggestItem.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.duy.ide.editor.internal.suggestion;
22

3+
import android.support.annotation.NonNull;
4+
35
import com.duy.ide.editor.view.IEditAreaView;
46

57
public class DefaultSuggestItem implements com.duy.ide.code.api.SuggestItem {
@@ -38,11 +40,6 @@ public String getReturnType() {
3840
return returnType;
3941
}
4042

41-
@Override
42-
public String getInsertText() {
43-
return insertText;
44-
}
45-
4643
@Override
4744
public int getSuggestionPriority() {
4845
return priority;
@@ -54,7 +51,7 @@ public char getTypeHeader() {
5451
}
5552

5653
@Override
57-
public void onSelectThis(IEditAreaView editAreaView) {
54+
public void onSelectThis(@NonNull IEditAreaView editAreaView) {
5855

5956
}
6057
}

lib-n-ide/src/main/java/com/duy/ide/editor/view/EditActionSupportEditor.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public void insert(@NonNull CharSequence text) {
103103
int selectionStart = getSelectionStart();
104104
selectionStart = Math.max(0, selectionStart);
105105
getText().insert(selectionStart, text);
106+
setSelection(selectionStart + text.length());
106107
}
107108

108109
/**
@@ -237,20 +238,37 @@ public void scrollToLine(int virtualLine) {
237238

238239
@Override
239240
public boolean onKeyDown(int keyCode, KeyEvent event) {
240-
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
241-
if (mPreferences.isUseVolumeToMove()) {
242-
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
241+
switch (keyCode) {
242+
case KeyEvent.KEYCODE_VOLUME_DOWN:
243+
if (mPreferences.isUseVolumeToMove()) {
243244
if (getSelectionStart() > 0) {
244245
setSelection(getSelectionStart() - 1);
245246
return true;
246247
}
247-
} else {
248+
}
249+
break;
250+
case KeyEvent.KEYCODE_VOLUME_UP:
251+
if (mPreferences.isUseVolumeToMove()) {
248252
if (getSelectionEnd() < length()) {
249253
setSelection(getSelectionEnd() + 1);
250254
return true;
251255
}
252256
}
253-
}
257+
break;
258+
case KeyEvent.KEYCODE_TAB:
259+
if (mPreferences.isInsertSpaceForTab()) {
260+
int tabSize = mPreferences.getTabSize();
261+
StringBuilder tab = new StringBuilder();
262+
for (int i = 0; i < tabSize; i++) {
263+
tab.append(" ");
264+
}
265+
insert(tab);
266+
} else {
267+
insert("\t");
268+
}
269+
return true;
270+
default:
271+
break;
254272
}
255273
return super.onKeyDown(keyCode, event);
256274
}

lib-n-ide/src/main/java/com/duy/ide/editor/view/SuggestionEditor.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,19 +428,12 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
428428
// from the drop down as its content
429429
case KeyEvent.KEYCODE_ENTER:
430430
case KeyEvent.KEYCODE_DPAD_CENTER:
431-
case KeyEvent.KEYCODE_TAB:
432431
if (event.hasNoModifiers()) {
433432
performCompletion();
434433
}
435434
return true;
436435
}
437436
}
438-
439-
if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) {
440-
performCompletion();
441-
return true;
442-
}
443-
444437
return super.onKeyUp(keyCode, event);
445438
}
446439

@@ -475,9 +468,6 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
475468
if (mPopup.onKeyDown(keyCode, event)) {
476469
return true;
477470
}
478-
if (isPopupShowing() && keyCode == KeyEvent.KEYCODE_TAB && event.hasNoModifiers()) {
479-
return true;
480-
}
481471
boolean handled = super.onKeyDown(keyCode, event);
482472

483473
if (handled && isPopupShowing()) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public class Preferences implements SharedPreferences.OnSharedPreferenceChangeLi
6464
, "&", "?", "!", "@", "^", "+", "*", "-", "_", "`", "\\t", "\\n"});
6565
public static final String KEY_AUTO_PAIR = "pref_auto_pair";
6666
private static final String KEY_TOUCH_TO_ADJUST_TEXT_SIZE = "pref_touch_to_adjust_text_size";
67-
private static final String KEY_INSERT_SPACE_FOR_TAB = "pref_insert_space_for_tab";
6867
private static final String KEY_HIGHLIGHT_FILE_SIZE_LIMIT = "pref_highlight_file_size_limit";
6968
private static final String KEY_REMEMBER_LAST_OPENED_FILES = "pref_remember_last_opened_files";
7069
private static final String KEY_TOOLBAR_ICONS = "pref_toolbar_icons";
@@ -100,7 +99,7 @@ public Preferences(Context context) {
10099
map.put(KEY_AUTO_INDENT, true);
101100
map.put(KEY_AUTO_PAIR, true);
102101

103-
map.put(KEY_INSERT_SPACE_FOR_TAB, true);
102+
map.put(context.getString(R.string.pref_insert_space_for_tab), true);
104103
map.put(KEY_TAB_SIZE, 4);
105104
map.put(KEY_SYMBOL, VALUE_SYMBOL);
106105
map.put(KEY_AUTO_CAPITALIZE, false);
@@ -393,6 +392,10 @@ public boolean isUseAutoComplete() {
393392
return getBoolean(context.getString(R.string.pref_auto_complete), true);
394393
}
395394

395+
public boolean isInsertSpaceForTab() {
396+
return getBoolean(context.getString(R.string.pref_insert_space_for_tab), true);
397+
}
398+
396399
@IntDef({SCREEN_ORIENTATION_AUTO, SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT})
397400
public @interface ScreenOrientation {
398401
}

lib-n-ide/src/main/res-file-expoler/layout/list_item_suggest_default.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
android:ellipsize="middle"
4343
android:gravity="center|start"
4444
android:padding="2dp"
45-
4645
android:textColor="?android:textColorPrimary"
46+
android:textSize="@dimen/smallest_text_size"
4747
android:typeface="monospace"
4848
tools:text="GetResultCode" />
4949

@@ -54,6 +54,7 @@
5454
android:gravity="center|end"
5555
android:maxEms="5"
5656
android:padding="2dp"
57+
android:textSize="@dimen/smallest_text_size"
5758
android:typeface="monospace"
5859
tools:text="String" />
5960

0 commit comments

Comments
 (0)