Skip to content

Commit

Permalink
Update relative line numbers when cursor is no longer in view
Browse files Browse the repository at this point in the history
  • Loading branch information
zklinger committed Jan 24, 2014
1 parent 44a591b commit 91af710
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
81 changes: 71 additions & 10 deletions RelativeLineNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
*********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;

namespace RelativeLineNumbers
{
Expand All @@ -44,6 +47,7 @@ class RelativeLineNumbers : Canvas, IWpfTextViewMargin //, IClassifier
private FontFamily _fontFamily = null;
private double _fontEmSize = 12.00;
private IEditorFormatMap _formatMap;
private Dictionary<int, int> _lineMap;

#endregion

Expand All @@ -56,6 +60,7 @@ public RelativeLineNumbers(IWpfTextView textView, IEditorFormatMap formatMap)
{
_textView = textView;
_formatMap = formatMap;
_lineMap = new Dictionary<int, int>();

_canvas = new Canvas();
this.Children.Add(_canvas);
Expand All @@ -65,7 +70,6 @@ public RelativeLineNumbers(IWpfTextView textView, IEditorFormatMap formatMap)
_fontFamily = _textView.FormattedLineSource.DefaultTextProperties.Typeface.FontFamily;
_fontEmSize = _textView.FormattedLineSource.DefaultTextProperties.FontRenderingEmSize;

this.Width = GetMarginWidth(new Typeface(_fontFamily.Source), _fontEmSize) + 2 * _labelOffsetX;
_textView.Caret.PositionChanged += new EventHandler<CaretPositionChangedEventArgs>(OnCaretPositionChanged);
_textView.ViewportHeightChanged += (sender, args) => DrawLineNumbers();
_textView.LayoutChanged += new EventHandler<TextViewLayoutChangedEventArgs>(OnLayoutChanged);
Expand All @@ -92,10 +96,7 @@ private void OnCaretPositionChanged (object sender, CaretPositionChangedEventArg

private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
if (e.VerticalTranslation || e.NewOrReformattedLines.Count > 1 || e.TranslatedLines.Count > 0)
{
DrawLineNumbers();
}
DrawLineNumbers();
}

#endregion
Expand All @@ -104,9 +105,53 @@ private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)

private void DrawLineNumbers()
{
int lineCount = _textView.TextViewLines.Count;
int notFoundVal = Int32.MaxValue;

List<int> rlnList = new List<int>();

// Get the index from the line collection where the cursor is currently sitting
int cursorLineIndex = _textView.TextViewLines.GetIndexOfTextLine(_textView.Caret.ContainingTextViewLine);

if (cursorLineIndex > -1)
{
_lineMap.Clear();
for (int i = 0; i < lineCount; i++)
{
int relLineNr = cursorLineIndex - i;
rlnList.Add(relLineNr);
_lineMap[GetLineNumber(i)] = relLineNr;
}
}
else
{
// Cursor is off the screen. Extrapolate relative line numbers.
for (int i = 0; i < lineCount; i++)
{
int relLineNr = 0;

// Try to get relative line number value for this line from the map.
if (!_lineMap.TryGetValue(GetLineNumber(i), out relLineNr))
{
relLineNr = notFoundVal;
}
rlnList.Add(relLineNr);
}

// Extrapolate missing relative line number values
for (int i = 0; i < lineCount; i++)
{
if (rlnList[0] != notFoundVal)
{
rlnList[i] = rlnList[0] - i;
}
else if (rlnList[rlnList.Count - 1] != notFoundVal)
{
rlnList[lineCount - 1 - i] = rlnList[lineCount - 1] + i;
}
}
}

// Clear existing text boxes
if (_canvas.Children.Count > 0)
{
Expand All @@ -119,10 +164,15 @@ private void DrawLineNumbers()
FontWeights.Bold : FontWeights.Normal;
this.Background = (SolidColorBrush)rd[EditorFormatDefinition.BackgroundBrushId];

for (int i = 0; i < _textView.TextViewLines.Count; i++)
string notFoundTxt = "~ ";

for (int i = 0; i < lineCount; i++)
{
int relLineNumber = rlnList[i];
_lineMap[GetLineNumber(i)] = relLineNumber;

TextBlock tb = new TextBlock();
tb.Text = string.Format("{0,2}", Math.Abs(cursorLineIndex - i));
tb.Text = string.Format("{0,2}", relLineNumber == notFoundVal ? notFoundTxt : Math.Abs(relLineNumber).ToString());
tb.FontFamily = _fontFamily;
tb.FontSize = _fontEmSize;
tb.Foreground = fgBrush;
Expand All @@ -131,16 +181,27 @@ private void DrawLineNumbers()
Canvas.SetTop(tb, _textView.TextViewLines[i].TextTop - _textView.ViewportTop);
_canvas.Children.Add(tb);
}

// Ajdust margin width
int maxVal = Math.Max(Math.Abs(rlnList[0]), Math.Abs(rlnList[rlnList.Count - 1]));
string sample = maxVal == notFoundVal ? notFoundTxt : maxVal.ToString();
this.Width = GetMarginWidth(new Typeface(_fontFamily.Source), _fontEmSize, sample) + 2 * _labelOffsetX;
}

private int GetLineNumber(int index)
{
int position = _textView.TextViewLines[index].Start.Position;
return _textView.TextViewLines[index].Start.Snapshot.GetLineNumberFromPosition(position) + 1;
}

#endregion

#region GetMarginWidth

private double GetMarginWidth(Typeface fontTypeFace, double fontSize)
private double GetMarginWidth(Typeface fontTypeFace, double fontSize, string txt)
{
FormattedText formattedText = new FormattedText(
"99",
txt,
System.Globalization.CultureInfo.GetCultureInfo("en-us"),
System.Windows.FlowDirection.LeftToRight,
fontTypeFace,
Expand Down
2 changes: 1 addition & 1 deletion source.extension.vsixmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Identifier Id="RelativeLineNumbers.Zoltan.Klinger.ecf8ccdf-17d2-4c5a-9131-bb6887c7b6c3">
<Name>RelativeLineNumbers</Name>
<Author>Zoltan Klinger</Author>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<Description xml:space="preserve">Displays line numbers relative to the current cursor position. Useful for ViEmu and VsVim extension users.</Description>
<Locale>1033</Locale>
<MoreInfoUrl>https://github.com/zklinger/RelativeLineNumbers</MoreInfoUrl>
Expand Down

0 comments on commit 91af710

Please sign in to comment.