Skip to content

Commit 4344edb

Browse files
committed
draw tab
1 parent 9491d18 commit 4344edb

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2018 Duy Tran Le
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.duy.ide.editor.text.style;
19+
20+
import android.graphics.Canvas;
21+
import android.graphics.Paint;
22+
import android.support.annotation.NonNull;
23+
import android.support.annotation.Nullable;
24+
import android.text.style.ReplacementSpan;
25+
26+
import com.duy.ide.editor.text.LayoutContext;
27+
28+
public class TabSpan extends ReplacementSpan {
29+
private final int whiteSpaceColor;
30+
private final int tabWidth;
31+
private boolean isShowWhiteSpace;
32+
33+
public TabSpan(LayoutContext layoutContext, int tabWidth) {
34+
this.whiteSpaceColor = layoutContext.whiteSpaceColor;
35+
this.isShowWhiteSpace = layoutContext.isShowWhiteSpace;
36+
this.tabWidth = tabWidth;
37+
}
38+
39+
@Override
40+
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
41+
return tabWidth;
42+
}
43+
44+
@Override
45+
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
46+
if (isShowWhiteSpace) {
47+
paint.set(paint);
48+
paint.setColor(whiteSpaceColor);
49+
paint.setStrokeWidth(2f);
50+
paint.setStyle(Paint.Style.FILL);
51+
canvas.drawLine(x, y, x + tabWidth - tabWidth / 10f, y, paint);
52+
}
53+
}
54+
}

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
import android.graphics.Typeface;
2525
import android.support.annotation.NonNull;
2626
import android.support.annotation.Nullable;
27+
import android.text.Editable;
2728
import android.text.InputFilter;
2829
import android.text.InputType;
2930
import android.text.Layout;
31+
import android.text.Spannable;
3032
import android.text.Spanned;
3133
import android.text.TextPaint;
34+
import android.text.TextWatcher;
3235
import android.util.AttributeSet;
3336
import android.util.TypedValue;
3437
import android.view.inputmethod.EditorInfo;
@@ -37,6 +40,7 @@
3740
import com.duy.ide.editor.text.LayoutContext;
3841
import com.duy.ide.editor.text.LineManager;
3942
import com.duy.ide.editor.text.TextLineNumber;
43+
import com.duy.ide.editor.text.style.TabSpan;
4044
import com.duy.ide.editor.theme.model.EditorTheme;
4145
import com.jecelyin.common.utils.ReflectionUtil;
4246
import com.jecelyin.common.utils.SysUtils;
@@ -46,7 +50,8 @@
4650
import java.util.List;
4751

4852
public abstract class HighlightEditorView extends android.support.v7.widget.AppCompatEditText
49-
implements IEditAreaView, SharedPreferences.OnSharedPreferenceChangeListener {
53+
implements IEditAreaView, SharedPreferences.OnSharedPreferenceChangeListener,
54+
TextWatcher {
5055
private static final String TAG = "EditAreaView2";
5156
private final LayoutContext mLayoutContext = new LayoutContext();
5257
protected Preferences mPreferences;
@@ -68,6 +73,7 @@ public abstract class HighlightEditorView extends android.support.v7.widget.AppC
6873
*/
6974
private boolean mNeedUpdateLineNumber = false;
7075
private boolean mIsAutoIndent = true;
76+
private int mTabWidth = 3;
7177

7278
public HighlightEditorView(Context context) {
7379
super(context);
@@ -123,6 +129,7 @@ private void init(Context context) {
123129
onSharedPreferenceChanged(null, Preferences.KEY_AUTO_CAPITALIZE);
124130

125131
initAutoIndent();
132+
addTextChangedListener(this);
126133
}
127134

128135
private void initAutoIndent() {
@@ -213,7 +220,6 @@ private void setCursorColor(int caretColor) {
213220
}
214221
}
215222

216-
217223
@Override
218224
public void scrollToLine(int line) {
219225

@@ -241,7 +247,6 @@ public void setInitLineNumber(int lineNumber) {
241247
updateLineNumberCount(0);
242248
}
243249

244-
245250
private void drawLineNumber(Canvas canvas) {
246251
if (!mLayoutContext.getPreferences().isShowLineNumber()) {
247252
return;
@@ -321,7 +326,6 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
321326
}
322327
}
323328

324-
325329
@Override
326330
public void setTextSize(int unit, float size) {
327331
super.setTextSize(unit, size);
@@ -330,9 +334,30 @@ public void setTextSize(int unit, float size) {
330334
}
331335

332336
@Override
333-
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
334-
super.onTextChanged(text, start, lengthBefore, lengthAfter);
337+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
338+
339+
}
340+
341+
@Override
342+
public void afterTextChanged(Editable s) {
343+
344+
}
345+
346+
@Override
347+
public void onTextChanged(CharSequence s, int start, int before, int count) {
335348
updateLineNumberCount(start);
349+
if (s instanceof Spannable) {
350+
applyTabWidth((Spannable) s, start, start + count - 1);
351+
}
352+
}
353+
354+
public void applyTabWidth(Spannable text, int start, int end) {
355+
for (int index = start; index <= end; index++) {
356+
if (text.charAt(index) == '\t') {
357+
text.setSpan(new TabSpan(getLayoutContext(), mTabWidth),
358+
index, index + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
359+
}
360+
}
336361
}
337362

338363
/**
@@ -342,6 +367,7 @@ private void updateTabChar() {
342367
if (DLog.DEBUG) DLog.d(TAG, "updateTabChar() called");
343368
float spaceWidth = getPaint().measureText(" ");
344369
float tabWidth = spaceWidth * (mPreferences == null ? 4 : mPreferences.getTabSize());
370+
mTabWidth = (int) tabWidth;
345371
try {
346372
Field tabIncrement = ReflectionUtil.getField(Layout.class,
347373
"TAB_INCREMENT", true);

0 commit comments

Comments
 (0)