Skip to content

Commit 1d49ae5

Browse files
committed
update line number when text change
1 parent b9df4cc commit 1d49ae5

File tree

4 files changed

+46
-109
lines changed

4 files changed

+46
-109
lines changed

app/src/main/java/com/duy/editor/theme/EditorThemeFragment.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.duy.editor.theme;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
4-
import com.duy.ide.editor.core.text.SpannableStringBuilder;
55
import android.os.Bundle;
66
import android.support.annotation.NonNull;
77
import android.support.annotation.Nullable;
@@ -25,6 +25,7 @@
2525
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView;
2626

2727
import org.gjt.sp.jedit.Catalog;
28+
import org.gjt.sp.jedit.Mode;
2829

2930
import java.util.ArrayList;
3031
import java.util.Collections;
@@ -69,6 +70,7 @@ public static class EditorThemeAdapter extends RecyclerView.Adapter<EditorThemeA
6970
private final ArrayList<EditorTheme> mEditorThemes;
7071
private Context mContext;
7172
private OnThemeSelectListener onThemeSelectListener;
73+
private Mode mLanguage = Catalog.getModeByName("C++");
7274

7375
public EditorThemeAdapter(Context context) {
7476
mContext = context;
@@ -104,13 +106,14 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
104106
Highlighter highlighter = new Highlighter();
105107
editorView.setTheme(editorTheme);
106108

107-
buffer.setMode(Catalog.getModeByName("C++"), mContext);
108-
editorView.setText(new SpannableStringBuilder());
109+
buffer.setMode(mLanguage, mContext);
110+
editorView.setText("");
109111
editorView.getText().insert(0, getSampleData());
110112

111113
buffer.setEditable(editorView.getText());
112114
buffer.insert(0, getSampleData());
113115

116+
@SuppressLint("UseSparseArrays")
114117
HashMap<Integer, ArrayList<? extends CharacterStyle>> colorsMap = new HashMap<>();
115118
int lineCount = buffer.getLineManager().getLineCount();
116119
highlighter.highlight(buffer, editorTheme, colorsMap, editorView.getText(), 0, lineCount - 1);
@@ -134,7 +137,7 @@ public int getItemCount() {
134137
return mEditorThemes.size();
135138
}
136139

137-
private CharSequence getSampleData() {
140+
private String getSampleData() {
138141
return "// C++ Program to Find Largest Element of an Array\n" +
139142
"\n" +
140143
"// This program takes n number of element from user (where, n is specified by user) and stores data in an array. Then, this program displays the largest element of that array using loops.\n" +

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424
android:id="@+id/btn_select"
2525
android:layout_width="wrap_content"
2626
android:layout_height="wrap_content"
27+
android:typeface="monospace"
2728
app:srcCompat="@drawable/baseline_check_24"
2829
android:text="Select" />
2930

3031
</LinearLayout>
3132

32-
<com.duy.ide.editor.core.widget.EditAreaView
33+
<com.duy.ide.editor.view.EditAreaView2
3334
android:id="@+id/editor_view"
3435
android:layout_width="match_parent"
3536
android:layout_height="wrap_content">
3637

37-
</com.duy.ide.editor.core.widget.EditAreaView>
38+
</com.duy.ide.editor.view.EditAreaView2>
3839
</LinearLayout>
3940

libeditor/src/main/java/com/duy/ide/editor/core/text/SpannableStringBuilder.java

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
package com.duy.ide.editor.core.text;
2020

2121

22-
import com.duy.ide.editor.core.graphics.CanvasCompat;
23-
import com.duy.ide.editor.core.text.method.Touch;
24-
import com.duy.ide.editor.core.util.ArrayUtils;
25-
import com.duy.ide.editor.core.util.EmptyArray;
26-
import com.duy.ide.editor.core.util.GrowingArrayUtils;
27-
import com.duy.ide.editor.core.widget.BaseEditorView;
2822
import android.graphics.Canvas;
2923
import android.graphics.Paint;
3024
import android.text.Editable;
@@ -46,7 +40,13 @@
4640
import android.text.style.URLSpan;
4741
import android.util.Log;
4842

43+
import com.duy.ide.editor.core.graphics.CanvasCompat;
44+
import com.duy.ide.editor.core.text.method.Touch;
4945
import com.duy.ide.editor.core.text.style.ErrorSpan;
46+
import com.duy.ide.editor.core.util.ArrayUtils;
47+
import com.duy.ide.editor.core.util.EmptyArray;
48+
import com.duy.ide.editor.core.util.GrowingArrayUtils;
49+
import com.duy.ide.editor.core.widget.BaseEditorView;
5050

5151
import java.lang.reflect.Array;
5252

@@ -552,86 +552,6 @@ public SpannableStringBuilder replace(int start, int end, CharSequence tb) {
552552
return replace(start, end, tb, 0, tb.length());
553553
}
554554

555-
/*
556-
private boolean isprint(char c) { // XXX
557-
if (c >= ' ' && c <= '~')
558-
return true;
559-
else
560-
return false;
561-
}
562-
563-
private static final int startFlag(int flag) {
564-
return (flag >> 4) & 0x0F;
565-
}
566-
567-
private static final int endFlag(int flag) {
568-
return flag & 0x0F;
569-
}
570-
571-
public void dump() { // XXX
572-
for (int i = 0; i < mGapStart; i++) {
573-
System.out.print('|');
574-
System.out.print(' ');
575-
System.out.print(isprint(mText[i]) ? mText[i] : '.');
576-
System.out.print(' ');
577-
}
578-
579-
for (int i = mGapStart; i < mGapStart + mGapLength; i++) {
580-
System.out.print('|');
581-
System.out.print('(');
582-
System.out.print(isprint(mText[i]) ? mText[i] : '.');
583-
System.out.print(')');
584-
}
585-
586-
for (int i = mGapStart + mGapLength; i < mText.length; i++) {
587-
System.out.print('|');
588-
System.out.print(' ');
589-
System.out.print(isprint(mText[i]) ? mText[i] : '.');
590-
System.out.print(' ');
591-
}
592-
593-
System.out.print('\n');
594-
595-
for (int i = 0; i < mText.length + 1; i++) {
596-
int found = 0;
597-
int wfound = 0;
598-
599-
for (int j = 0; j < mSpanCount; j++) {
600-
if (mSpanStarts[j] == i) {
601-
found = 1;
602-
wfound = j;
603-
break;
604-
}
605-
606-
if (mSpanEnds[j] == i) {
607-
found = 2;
608-
wfound = j;
609-
break;
610-
}
611-
}
612-
613-
if (found == 1) {
614-
if (startFlag(mSpanFlags[wfound]) == MARK)
615-
System.out.print("( ");
616-
if (startFlag(mSpanFlags[wfound]) == PARAGRAPH)
617-
System.out.print("< ");
618-
else
619-
System.out.print("[ ");
620-
} else if (found == 2) {
621-
if (endFlag(mSpanFlags[wfound]) == POINT)
622-
System.out.print(") ");
623-
if (endFlag(mSpanFlags[wfound]) == PARAGRAPH)
624-
System.out.print("> ");
625-
else
626-
System.out.print("] ");
627-
} else {
628-
System.out.print(" ");
629-
}
630-
}
631-
632-
System.out.print("\n");
633-
}
634-
*/
635555

636556
private CharSequence autoIndent(int start) {
637557
if (start < 0 || start >= mText.length)

libeditor/src/main/java/com/duy/ide/editor/view/EditAreaView2.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
import android.content.Context;
2121
import android.content.SharedPreferences;
22-
import com.duy.ide.editor.core.text.LayoutContext;
23-
import com.duy.ide.editor.core.text.SpannableStringBuilder;
24-
import com.duy.ide.editor.core.widget.model.EditorIndex;
2522
import android.graphics.Canvas;
2623
import android.graphics.Paint;
24+
import android.graphics.Typeface;
2725
import android.support.annotation.NonNull;
2826
import android.support.annotation.Nullable;
2927
import android.support.v7.widget.AppCompatEditText;
@@ -34,6 +32,9 @@
3432
import android.view.inputmethod.EditorInfo;
3533

3634
import com.duy.common.DLog;
35+
import com.duy.ide.editor.core.text.LayoutContext;
36+
import com.duy.ide.editor.core.text.SpannableStringBuilder;
37+
import com.duy.ide.editor.core.widget.model.EditorIndex;
3738
import com.duy.ide.editor.text.LineManager;
3839
import com.duy.ide.editor.text.TextLineNumber;
3940
import com.duy.ide.editor.theme.model.EditorTheme;
@@ -59,6 +60,10 @@ public class EditAreaView2 extends AppCompatEditText implements IEditAreaView, S
5960
* Store last line count has been calculated
6061
*/
6162
private int mPreLineCount;
63+
/**
64+
* We can not update line count when layout is null, lazy init
65+
*/
66+
private boolean mNeedUpdateLineNumber = false;
6267
/**
6368
* Line manager will be calculate real lines and virtual lines
6469
*/
@@ -102,6 +107,7 @@ private void init(Context context) {
102107
mLayoutContext.setGutterBackgroundPaint(new Paint(getPaint()));
103108

104109
setInitLineNumber(1);
110+
setTypeface(Typeface.MONOSPACE);
105111
setTextSize(getTextSize());
106112
setTheme(mPreferences.getEditorTheme());
107113

@@ -115,20 +121,10 @@ private void init(Context context) {
115121
onSharedPreferenceChanged(null, Preferences.KEY_AUTO_CAPITALIZE);
116122
}
117123

118-
@Override
119-
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
120-
super.onTextChanged(text, start, lengthBefore, lengthAfter);
121-
updateLineNumberCount(start);
122-
}
123-
124-
125124
@Override
126125
public void setText(CharSequence text, BufferType type) {
127-
if (text instanceof SpannableStringBuilder) {
128-
super.setText(text, type);
129-
} else {
130-
super.setText(new SpannableStringBuilder(text), type);
131-
}
126+
super.setText(text, type);
127+
mNeedUpdateLineNumber = true;
132128
}
133129

134130
@Override
@@ -157,6 +153,7 @@ public EditorTheme getEditorTheme() {
157153
protected void onDraw(Canvas canvas) {
158154
super.onDraw(canvas);
159155

156+
160157
drawLineNumber(canvas);
161158
}
162159

@@ -265,14 +262,15 @@ public int getLineForOffset(int offset) {
265262
/**
266263
* Calculate padding line number padding
267264
*/
265+
@Override
268266
public void setInitLineNumber(int lineNumber) {
269267
if (!mLayoutContext.preferences.isShowLineNumber()) {
270268
//invalidate
271269
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom());
272270
return;
273271
}
274272
mLayoutContext.setLineNumber(lineNumber);
275-
updateGutterSize();
273+
updateLineNumberCount(0);
276274
}
277275

278276

@@ -281,6 +279,11 @@ private void drawLineNumber(Canvas canvas) {
281279
return;
282280
}
283281

282+
if (mNeedUpdateLineNumber) {
283+
updateLineNumberCount(0);
284+
mNeedUpdateLineNumber = false;
285+
}
286+
284287
//calculate position
285288
mLineManager.calculateLinePositionForDraw();
286289

@@ -362,6 +365,12 @@ public void setTextSize(int unit, float size) {
362365
updateTabChar();
363366
}
364367

368+
@Override
369+
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
370+
super.onTextChanged(text, start, lengthBefore, lengthAfter);
371+
setNeedUpdateLineNumber(true);
372+
}
373+
365374
/**
366375
* Update tab width
367376
*/
@@ -370,7 +379,7 @@ private void updateTabChar() {
370379
float spaceWidth = getPaint().measureText(" ");
371380
float tabWidth = spaceWidth * (mPreferences == null ? 4 : mPreferences.getTabSize());
372381
try {
373-
Field tabIncrement = ReflectionUtil.getField(Layout.class, "TAB_INCREMENT", true);
382+
Field tabIncrement = ReflectionUtil.getField(getLayout().getClass(), "TAB_INCREMENT", true);
374383
ReflectionUtil.setFinalStatic(tabIncrement, (int) tabWidth);
375384
postInvalidate();
376385
} catch (Exception e) {
@@ -402,4 +411,8 @@ private void updateGutterSize() {
402411
setPadding(newPaddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
403412
}
404413

414+
public void setNeedUpdateLineNumber(boolean needUpdateLineNumber) {
415+
mNeedUpdateLineNumber = needUpdateLineNumber;
416+
mPreLineCount = -1;
417+
}
405418
}

0 commit comments

Comments
 (0)