Skip to content

Commit a76fe59

Browse files
author
Hasan Badran
committed
Ability to turn on/off AutoIndent
1 parent 51427aa commit a76fe59

File tree

4 files changed

+20
-31
lines changed

4 files changed

+20
-31
lines changed

.idea/misc.xml

Lines changed: 4 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/net/cryptobrewery/syntaxviewexample/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
3535
syntax_view.setAnnotationsColor("#1932F3");
3636
//this will set special characters color like ;
3737
syntax_view.setSpecialCharsColor("#cc7832");
38+
syntax_view.setAutoIndent(true);
3839
//you can change the font
3940
//code:
4041
// Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "yourFont.ttf");

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<net.cryptobrewery.syntaxview.SyntaxView
1010
android:id="@+id/syn"
1111
android:layout_width="match_parent"
12-
android:layout_height="match_parent">
12+
android:layout_height="match_parent"/>
13+
1314

14-
</net.cryptobrewery.syntaxview.SyntaxView>
1515
</RelativeLayout>

syntax-view/src/main/java/net/cryptobrewery/syntaxview/SyntaxView.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
import android.widget.RelativeLayout;
1818
import android.widget.TextView;
1919
import android.widget.Toast;
20-
2120
import com.rengwuxian.materialedittext.MaterialEditText;
22-
2321
import java.util.Stack;
24-
import android.os.Handler;
2522
import java.util.regex.Matcher;
2623
import java.util.regex.Pattern;
2724

@@ -45,6 +42,7 @@ public class SyntaxView extends RelativeLayout {
4542
);
4643
private SyntaxHighlighter[] schemes = {keywords, numbers, special, printStatments, annotations};
4744
private TextView rows;
45+
private boolean autoIndent=false;
4846

4947
public SyntaxView(Context context) {
5048
super(context);
@@ -119,15 +117,16 @@ public void afterTextChanged(final Editable s) {
119117
//if(!deleting) and there is { at the end of the text -> auto indent
120118
//
121119

122-
char lastDiff =getLastDifference(temp2,temp1);
123-
if(oldLength < newLength && (lastDiff == ';' || lastDiff == '{' ) ){
120+
if(autoIndent) {
121+
char lastDiff = getLastDifference(temp2, temp1);
122+
if (oldLength < newLength && (lastDiff == ';' || lastDiff == '{')) {
124123

125-
int position = code.getSelectionStart();
126-
code.getText().insert(position, "\n ");
127-
position = code.getSelectionStart();
128-
code.setSelection(position );
124+
int position = code.getSelectionStart();
125+
code.getText().insert(position, "\n ");
126+
position = code.getSelectionStart();
127+
code.setSelection(position);
128+
}
129129
}
130-
131130
removeSpans(s, ForegroundColorSpan.class);
132131
for (SyntaxHighlighter scheme : schemes) {
133132
for (Matcher m = scheme.pattern.matcher(s); m.find(); ) {
@@ -366,5 +365,9 @@ public char getLastDifference(String a,String b){
366365
}
367366
return '.';
368367
}
368+
369+
public void setAutoIndent(boolean val){
370+
this.autoIndent = val;
371+
}
369372
}
370373

0 commit comments

Comments
 (0)