Skip to content

Commit 511084e

Browse files
committed
Add example for CommandManager external plugin with support for start and end comment syntax like gradle or xml comments
1 parent 05d75bf commit 511084e

8 files changed

Lines changed: 153 additions & 1 deletion

File tree

app/src/main/java/com/amrdeveloper/codeviewlibrary/MainActivity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.amrdeveloper.codeview.Code;
1919
import com.amrdeveloper.codeview.CodeView;
20+
import com.amrdeveloper.codeviewlibrary.plugin.CommentManager;
2021
import com.amrdeveloper.codeviewlibrary.syntax.ThemeName;
2122
import com.amrdeveloper.codeviewlibrary.syntax.LanguageName;
2223
import com.amrdeveloper.codeviewlibrary.syntax.LanguageManager;
@@ -30,6 +31,7 @@ public class MainActivity extends AppCompatActivity {
3031

3132
private CodeView codeView;
3233
private LanguageManager languageManager;
34+
private CommentManager commentManager;
3335

3436
private LanguageName currentLanguage = LanguageName.JAVA;
3537
private ThemeName currentTheme = ThemeName.MONOKAI;
@@ -42,6 +44,7 @@ protected void onCreate(Bundle savedInstanceState) {
4244
setContentView(R.layout.activity_main);
4345

4446
configCodeView();
47+
configCodeViewPlugins();
4548
}
4649

4750
private void configCodeView() {
@@ -110,6 +113,17 @@ private void configLanguageAutoIndentation() {
110113
codeView.setIndentationEnds(languageManager.getLanguageIndentationEnds(currentLanguage));
111114
}
112115

116+
private void configCodeViewPlugins() {
117+
commentManager = new CommentManager(codeView);
118+
configCommentInfo();
119+
120+
}
121+
122+
private void configCommentInfo() {
123+
commentManager.setCommentStart(languageManager.getCommentStart(currentLanguage));
124+
commentManager.setCommendEnd(languageManager.getCommentEnd(currentLanguage));
125+
}
126+
113127
@Override
114128
public boolean onCreateOptionsMenu(Menu menu) {
115129
getMenuInflater().inflate(R.menu.menu_main, menu);
@@ -124,6 +138,8 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
124138
if (menuGroupId == R.id.group_languages) changeTheEditorLanguage(menuItemId);
125139
else if (menuGroupId == R.id.group_themes) changeTheEditorTheme(menuItemId);
126140
else if (menuItemId == R.id.findMenu) launchEditorButtonSheet();
141+
else if (menuItemId == R.id.comment) commentManager.commentSelected();
142+
else if (menuItemId == R.id.un_comment) commentManager.unCommentSelected();
127143
else if (menuItemId == R.id.clearText) codeView.setText("");
128144

129145
return super.onOptionsItemSelected(item);
@@ -139,6 +155,7 @@ private void changeTheEditorLanguage(int languageId) {
139155
languageManager.applyTheme(currentLanguage, currentTheme);
140156
configLanguageAutoComplete();
141157
configLanguageAutoIndentation();
158+
configCommentInfo();
142159
}
143160
}
144161

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.amrdeveloper.codeviewlibrary.plugin;
2+
3+
import android.text.Editable;
4+
import android.widget.EditText;
5+
6+
public class CommentManager {
7+
8+
private final EditText textView;
9+
private final Editable editable;
10+
11+
private String commentStart;
12+
private int commentStartLength;
13+
14+
private String commentEnd;
15+
private int commendEndLength;
16+
17+
public CommentManager(EditText textView) {
18+
this.textView = textView;
19+
this.editable = textView.getText();
20+
this.commentStart = "";
21+
this.commentStartLength = 0;
22+
this.commentEnd = "";
23+
this.commendEndLength = 0;
24+
}
25+
26+
public CommentManager(EditText textView, String commentStart, String commentEnd) {
27+
this.textView = textView;
28+
this.editable = textView.getText();
29+
this.commentStart = commentStart;
30+
this.commentStartLength = commentStart.length();
31+
this.commentEnd = commentEnd;
32+
this.commendEndLength = commentEnd.length();
33+
}
34+
35+
public void commentSelected() {
36+
int start = textView.getSelectionStart();
37+
int end = textView.getSelectionEnd();
38+
if (start != end) {
39+
String[] lines = editable.subSequence(start, end).toString().split("\n");
40+
StringBuilder builder = new StringBuilder();
41+
int len = lines.length;
42+
for (int i = 0; i < len ; i++) {
43+
String line = lines[i];
44+
if (!line.startsWith(commentStart)) builder.append(commentStart);
45+
builder.append(line);
46+
if (!line.endsWith(commentEnd)) builder.append(commentEnd);
47+
if (i != len - 1) builder.append("\n");
48+
}
49+
editable.replace(start, end, builder);
50+
}
51+
}
52+
53+
public void unCommentSelected() {
54+
int start = textView.getSelectionStart();
55+
int end = textView.getSelectionEnd();
56+
if (start != end) {
57+
String[] lines = editable.subSequence(start, end).toString().split("\n");
58+
StringBuilder builder = new StringBuilder();
59+
int len = lines.length;
60+
for (int i = 0; i < len ; i++) {
61+
String line = lines[i];
62+
if (line.startsWith(commentStart) && line.endsWith(commentEnd))
63+
builder.append(line.substring(commentStartLength, line.length() - commendEndLength));
64+
else builder.append(line);
65+
if (i != len - 1) builder.append("\n");
66+
}
67+
editable.replace(start, end, builder);
68+
}
69+
}
70+
71+
public void setCommentStart(String comment) {
72+
this.commentStart = comment;
73+
this.commentStartLength = comment.length();
74+
}
75+
76+
public void setCommendEnd(String comment) {
77+
this.commentEnd = comment;
78+
this.commendEndLength = comment.length();
79+
}
80+
}

app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,12 @@ public static Set<Character> getIndentationEnds() {
159159
characterSet.add('}');
160160
return characterSet;
161161
}
162+
163+
public static String getCommentStart() {
164+
return "//";
165+
}
166+
167+
public static String getCommentEnd() {
168+
return "";
169+
}
162170
}

app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,12 @@ public static Set<Character> getIndentationEnds() {
169169
characterSet.add('}');
170170
return characterSet;
171171
}
172+
173+
public static String getCommentStart() {
174+
return "//";
175+
}
176+
177+
public static String getCommentEnd() {
178+
return "";
179+
}
172180
}

app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ public Set<Character> getLanguageIndentationEnds(LanguageName language) {
7373
}
7474
}
7575

76+
public String getCommentStart(LanguageName language) {
77+
switch (language) {
78+
case JAVA: return JavaLanguage.getCommentStart();
79+
case PYTHON: return PythonLanguage.getCommentStart();
80+
case GO_LANG: return GoLanguage.getCommentStart();
81+
default: return "";
82+
}
83+
}
84+
85+
public String getCommentEnd(LanguageName language) {
86+
switch (language) {
87+
case JAVA: return JavaLanguage.getCommentEnd();
88+
case PYTHON: return PythonLanguage.getCommentEnd();
89+
case GO_LANG: return GoLanguage.getCommentEnd();
90+
default: return "";
91+
}
92+
}
93+
7694
private void applyMonokaiTheme(LanguageName language) {
7795
switch (language) {
7896
case JAVA:

app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonLanguage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,12 @@ public static Set<Character> getIndentationStarts() {
157157
public static Set<Character> getIndentationEnds() {
158158
return new HashSet<>();
159159
}
160+
161+
public static String getCommentStart() {
162+
return "#";
163+
}
164+
165+
public static String getCommentEnd() {
166+
return "";
167+
}
160168
}

app/src/main/res/menu/menu_main.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,25 @@
4545
</menu>
4646
</item>
4747

48+
4849
<item
4950
android:id="@+id/findMenu"
5051
android:icon="@drawable/ic_find_in_page"
5152
android:orderInCategory="3"
5253
android:title="@string/find_and_replace" />
5354

5455
<item
55-
android:id="@+id/clearText"
56+
android:id="@+id/comment"
5657
android:orderInCategory="4"
58+
android:title="@string/comment" />
59+
60+
<item
61+
android:id="@+id/un_comment"
62+
android:orderInCategory="5"
63+
android:title="@string/uncomment" />
64+
65+
<item
66+
android:id="@+id/clearText"
67+
android:orderInCategory="6"
5768
android:title="@string/clear_text" />
5869
</menu>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<string name="replacement">Replacement</string>
1010
<string name="replace_all">Replace all</string>
1111
<string name="clear_text">Clear Text</string>
12+
<string name="comment">Comment</string>
13+
<string name="uncomment">UnComment</string>
1214

1315
<!-- Programming Languages -->
1416
<string name="languages">Languages</string>

0 commit comments

Comments
 (0)