Skip to content

Commit f1133e9

Browse files
authored
Merge pull request #8 from chaitanyagurrapu/lastLine
Modify Extensions so that when the last line is empty, the 'GetLastLi…
2 parents f58a0e9 + 945ebfd commit f1133e9

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Src/EditorUtils/Extensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public static ITextSnapshotLine GetStartLine(this SnapshotSpan span)
4141
/// </summary>
4242
public static ITextSnapshotLine GetLastLine(this SnapshotSpan span)
4343
{
44+
var snapshot = span.Snapshot;
45+
var snapshotEndPoint = new SnapshotPoint(snapshot, snapshot.Length);
46+
if (snapshotEndPoint == span.End)
47+
{
48+
var line = span.End.GetContainingLine();
49+
if (line.Length == 0)
50+
{
51+
return line;
52+
}
53+
}
4454
return span.Length > 0
4555
? span.End.Subtract(1).GetContainingLine()
4656
: GetStartLine(span);

Test/EditorUtilsTest/ExtensionsTest.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using Microsoft.VisualStudio.Text.Projection;
1+
using System.Linq;
42
using Microsoft.VisualStudio.Utilities;
53
using Xunit;
64

@@ -80,5 +78,26 @@ public void TryGetPropertySafe_WrongType()
8078
string value;
8179
Assert.False(col.TryGetPropertySafe(key, out value));
8280
}
81+
82+
[Fact]
83+
public void GetLastLine_WithNonEmptyLastLine_ReturnsCorrectLastLine()
84+
{
85+
var textBuffer = CreateTextBuffer("hello","World", "Foo");
86+
var lastLine = textBuffer.GetSpan(0, textBuffer.CurrentSnapshot.Length).GetLastLine();
87+
88+
Assert.True(lastLine.LineNumber == 2);
89+
}
90+
91+
[Fact]
92+
public void GetLastLine_WithEmptyLastLine_ReturnsCorrectLastLine()
93+
{
94+
var textBuffer = CreateTextBuffer("hello","World", "");
95+
var lastLine = textBuffer.GetSpan(0, textBuffer.CurrentSnapshot.Length).GetLastLine();
96+
97+
Assert.True(lastLine.LineNumber == 2);
98+
}
99+
100+
101+
83102
}
84103
}

0 commit comments

Comments
 (0)