Skip to content

Commit ea896da

Browse files
committed
Add support for highlighting the current line number and customize the color
1 parent fbf626f commit ea896da

1 file changed

Lines changed: 63 additions & 12 deletions

File tree

codeview/src/main/java/com/amrdeveloper/codeview/CodeView.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,35 @@ public class CodeView extends AppCompatMultiAutoCompleteTextView implements Find
8282
private boolean hasErrors = false;
8383
private boolean mRemoveErrorsWhenTextChanged = true;
8484

85+
// Line number options
8586
private Rect lineNumberRect;
8687
private Paint lineNumberPaint;
8788
private boolean enableLineNumber = false;
8889
private boolean enableRelativeLineNumber = false;
8990

91+
// Highlighting current line options
92+
private Rect lineBounds;
93+
private Paint highlightLinePaint;
94+
private boolean enableHighlightCurrentLine = true;
95+
private final static int LINE_HIGHLIGHT_DEFAULT_COLOR = Color.DKGRAY;
96+
97+
// Indentations options
9098
private int currentIndentation = 0;
9199
private boolean enableAutoIndentation = false;
92100
private final Set<Character> indentationStarts = new HashSet<>();
93101
private final Set<Character> indentationEnds = new HashSet<>();
94102

103+
// Matches and tokens
95104
private int currentMatchedIndex = -1;
96105
private int matchingColor = Color.YELLOW;
97106
private CharacterStyle currentMatchedToken;
98107
private final List<Token> matchedTokens = new ArrayList<>();
99108

109+
// Auto complete and Suggestions
100110
private int maxNumberOfSuggestions = Integer.MAX_VALUE;
101111
private int autoCompleteItemHeightInDp = (int) (50 * Resources.getSystem().getDisplayMetrics().density);
102112

113+
// Auto pair complete
103114
private boolean enablePairComplete = false;
104115
private boolean enablePairCompleteCenterCursor = false;
105116
private final Map<Character, Character> mPairCompleteMap = new HashMap<>();
@@ -141,30 +152,44 @@ private void initEditorView() {
141152
lineNumberRect = new Rect();
142153
lineNumberPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
143154
lineNumberPaint.setStyle(Paint.Style.FILL);
155+
156+
lineBounds = new Rect();
157+
highlightLinePaint = new Paint();
158+
highlightLinePaint.setColor(LINE_HIGHLIGHT_DEFAULT_COLOR);
144159
}
145160

146161
@Override
147162
protected void onDraw(Canvas canvas) {
148-
if (enableLineNumber) {
163+
if (enableLineNumber || enableHighlightCurrentLine) {
149164
final Editable fullText = getText();
150165
final Layout layout = getLayout();
151166
final int lineCount = getLineCount();
152167
final int selectionStart = Selection.getSelectionStart(fullText);
153168
final int cursorLine = layout.getLineForOffset(selectionStart);
154169

155-
for (int i = 0; i < lineCount; ++i) {
156-
final int baseline = getLineBounds(i, null);
157-
if (i == 0 || fullText.charAt(layout.getLineStart(i) - 1) == '\n') {
158-
// If relative line number is enabled the number should be the absolute value of cursorLine - i)
159-
// if not it should be just current line number
160-
// Add 1 to the current line number to make it start from 1 not 0
161-
int lineNumber = (i == cursorLine || !enableRelativeLineNumber) ? i + 1 : Math.abs(cursorLine - i);
162-
canvas.drawText(" " + lineNumber, lineNumberRect.left, baseline, lineNumberPaint);
163-
}
170+
// Highlight the current line with custom color by drawing rectangle on this line
171+
if (enableHighlightCurrentLine) {
172+
getLineBounds(cursorLine, lineBounds);
173+
canvas.drawRect(lineBounds, highlightLinePaint);
164174
}
165175

166-
final int paddingLeft = 50 + (int) Math.log10(lineCount) * 10;
167-
setPadding(paddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
176+
// Draw line number or relative line number
177+
if (enableLineNumber) {
178+
for (int i = 0; i < lineCount; ++i) {
179+
final int baseline = getLineBounds(i, null);
180+
if (i == 0 || fullText.charAt(layout.getLineStart(i) - 1) == '\n') {
181+
// If relative line number is enabled the number should be the absolute value of cursorLine - i)
182+
// if not it should be just current line number
183+
// Add 1 to the current line number to make it start from 1 not 0
184+
int lineNumber = (i == cursorLine || !enableRelativeLineNumber) ? i + 1 : Math.abs(cursorLine - i);
185+
canvas.drawText(" " + lineNumber, lineNumberRect.left, baseline, lineNumberPaint);
186+
}
187+
}
188+
189+
// Calculate padding depending on current line number
190+
final int paddingLeft = 50 + (int) Math.log10(lineCount) * 10;
191+
setPadding(paddingLeft, getPaddingTop(), getPaddingRight(), getPaddingBottom());
192+
}
168193
}
169194
super.onDraw(canvas);
170195
}
@@ -576,6 +601,32 @@ public boolean isLineRelativeNumberEnabled() {
576601
return enableRelativeLineNumber;
577602
}
578603

604+
/**
605+
* Enable or disable the highlighting current line feature
606+
* @param enableHighlightCurrentLine Flag to enable or disable highlighting current line
607+
* @since 1.3.6
608+
*/
609+
public void setEnableHighlightCurrentLine(boolean enableHighlightCurrentLine) {
610+
this.enableHighlightCurrentLine = enableHighlightCurrentLine;
611+
}
612+
613+
/**
614+
* @return (@code true) if highlighting current line feature is enabled
615+
* @since 1.3.6
616+
*/
617+
public boolean isHighlightCurrentLineEnabled() {
618+
return enableHighlightCurrentLine;
619+
}
620+
621+
/**
622+
* Modify the highlight current line color
623+
* @param color The new color value
624+
* @since 1.1.0
625+
*/
626+
public void setHighlightCurrentLineColor(int color) {
627+
highlightLinePaint.setColor(color);
628+
}
629+
579630
/**
580631
* Modify the line number text color
581632
* @param color The new color value

0 commit comments

Comments
 (0)