Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calculation of margin width. #12

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions RelativeLineNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,23 @@ private void DrawLineNumbers()

string notFoundTxt = "~ ";

// The amount of digits in the amount of lines is used to calculate the width of the line
// numbers. For documents that have less than 9999 lines, this is static, which prevents the
// margin's width from being adjusted while typing (adding a tenth line would otherwise
// cause the margin's width to change, making the document 'jump' sideways).
int documentLineCount = _textView.TextSnapshot.LineCount;
int maxDigits = CalculateNumDigits(Math.Max(documentLineCount, 1000));

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}", relLineNumber == notFoundVal ? notFoundTxt : Math.Abs(relLineNumber).ToString());
tb.Text = relLineNumber == notFoundVal ? notFoundTxt : Math.Abs(relLineNumber).ToString();
// Padding the text causes it to be right-aligned. Setting the TextAlignment property does
// not properly do this.
tb.Text = tb.Text.PadLeft(maxDigits);
tb.FontFamily = _fontFamily;
tb.FontSize = _fontEmSize;
tb.Foreground = fgBrush;
Expand All @@ -182,12 +192,19 @@ private void DrawLineNumbers()
_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();
string sample = new string('-', maxDigits);
this.Width = GetMarginWidth(new Typeface(_fontFamily.Source), _fontEmSize, sample) + 2 * _labelOffsetX;
}

private static int CalculateNumDigits(int num)
{
if (num == 0)
{
return 1;
}
return (int)Math.Floor(Math.Log10(Math.Abs(num))) + 1;
}

private int GetLineNumber(int index)
{
int position = _textView.TextViewLines[index].Start.Position;
Expand Down