Skip to content

Commit 02a2aca

Browse files
authored
feat: mark current line in editor (#51)
Use WindowsText color to mark the line for the caret. Define the resource color LineNumberCurrent.
1 parent 2b68790 commit 02a2aca

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Project/Resources/Mode.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<xsd:element name="CaretLine" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
4848

4949
<xsd:element name="LineNumbers" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
50+
<xsd:element name="LineNumberSelected" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
5051

5152
<xsd:element name="FoldLine" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
5253
<xsd:element name="FoldMarker" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />

Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static DefaultHighlightingStrategy()
6565
["CaretMarker"] = new HighlightColor(nameof(SystemColors.MenuBar), bold: false, italic: false),
6666
["CaretLine"] = new HighlightBackground(nameof(SystemColors.ControlLight), nameof(SystemColors.Window), bold: false, italic: false),
6767
["LineNumbers"] = new HighlightBackground(nameof(SystemColors.GrayText), nameof(SystemColors.Window), bold: false, italic: false),
68+
["LineNumberSelected"] = new HighlightBackground(nameof(SystemColors.WindowText), nameof(SystemColors.Window), bold: true, italic: false),
6869
["FoldLine"] = new HighlightColor(nameof(SystemColors.ControlDark), bold: false, italic: false),
6970
["FoldMarker"] = new HighlightColor(nameof(SystemColors.WindowText), nameof(SystemColors.Window), bold: false, italic: false),
7071
["SelectedFoldLine"] = new HighlightColor(nameof(SystemColors.WindowText), bold: false, italic: false),

Project/Src/Gui/AbstractMargin.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace ICSharpCode.TextEditor
2121
/// </summary>
2222
public abstract class AbstractMargin
2323
{
24+
private int _caretLine = -1;
25+
2426
protected Rectangle drawingPosition = new Rectangle(x: 0, y: 0, width: 0, height: 0);
2527

2628
protected TextArea textArea;
@@ -42,6 +44,8 @@ public Rectangle DrawingPosition
4244

4345
public ITextEditorProperties TextEditorProperties => textArea.Document.TextEditorProperties;
4446

47+
public bool MarkSelectedLine { get; set; } = true;
48+
4549
public virtual Cursor Cursor { get; set; } = Cursors.Default;
4650

4751
public virtual int Width => -1;
@@ -63,6 +67,17 @@ public virtual void HandleMouseLeave(EventArgs e)
6367
MouseLeave?.Invoke(this, e);
6468
}
6569

70+
public virtual void SelectedLineChanged(int line)
71+
{
72+
if (!IsVisible || _caretLine == line || !MarkSelectedLine)
73+
{
74+
return;
75+
}
76+
77+
_caretLine = line;
78+
textArea.Invalidate();
79+
}
80+
6681
public virtual void Paint(Graphics g, Rectangle rect)
6782
{
6883
Painted?.Invoke(this, g, rect);

Project/Src/Gui/GutterMargin.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ public override void Paint(Graphics g, Rectangle rect)
5757
return;
5858

5959
var lineNumberPainterColor = textArea.Document.HighlightingStrategy.GetColorFor("LineNumbers");
60+
var lineNumberCurrentPainterColor = textArea.Document.HighlightingStrategy.GetColorFor("LineNumberSelected");
6061
var fontHeight = textArea.TextView.FontHeight;
6162
var fillBrush = textArea.Enabled
6263
? BrushRegistry.GetBrush(lineNumberPainterColor.BackgroundColor)
6364
: SystemBrushes.InactiveBorder;
6465
var drawBrush = BrushRegistry.GetBrush(lineNumberPainterColor.Color);
66+
Brush currentLineBrush = BrushRegistry.GetBrush(lineNumberCurrentPainterColor.Color);
6567

6668
for (var y = 0; y < (drawingPosition.Height + textArea.TextView.VisibleLineDrawingRemainder)/fontHeight + 1; ++y)
6769
{
@@ -71,12 +73,17 @@ public override void Paint(Graphics g, Rectangle rect)
7173
{
7274
g.FillRectangle(fillBrush, backgroundRectangle);
7375
var curLine = textArea.Document.GetFirstLogicalLine(textArea.Document.GetVisibleLine(textArea.TextView.FirstVisibleLine) + y);
76+
bool isCurrentLine = curLine == textArea.Caret.Line && MarkSelectedLine;
77+
Brush lineBrush = isCurrentLine ? currentLineBrush : drawBrush;
78+
Font font = isCurrentLine
79+
? lineNumberCurrentPainterColor.GetFont(TextEditorProperties.FontContainer)
80+
: lineNumberPainterColor.GetFont(TextEditorProperties.FontContainer);
7481

7582
if (curLine < textArea.Document.TotalNumberOfLines)
7683
g.DrawString(
7784
(curLine + 1).ToString(),
78-
lineNumberPainterColor.GetFont(TextEditorProperties.FontContainer),
79-
drawBrush,
85+
font,
86+
lineBrush,
8087
backgroundRectangle,
8188
numberStringFormat);
8289
}

0 commit comments

Comments
 (0)