Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit cb557cf

Browse files
author
N. Taylor Mullen
committed
Changed GetDesiredIndentation to be resilient to our SyntaxTree which occasionally has gaps.
- Added a test to cover the scenario. #2489
1 parent fa02d34 commit cb557cf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorIndentationFactsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal class DefaultRazorIndentationFactsService : RazorIndentationFactsServic
6464
var previousLineEndIndex = GetPreviousLineEndIndex(syntaxTreeSnapshot, line);
6565
var simulatedChange = new SourceChange(previousLineEndIndex, 0, string.Empty);
6666
var owningSpan = syntaxTree.Root.LocateOwner(simulatedChange);
67-
if (owningSpan.Kind == SpanKindInternal.Code)
67+
if (owningSpan == null || owningSpan.Kind == SpanKindInternal.Code)
6868
{
6969
// Example,
7070
// @{\n

test/Microsoft.VisualStudio.Editor.Razor.Test/DefaultRazorIndentationFactsServiceTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,33 @@ public void GetIndentLevelOfLine_NoIndent()
167167
Assert.Equal(0, indentLevel);
168168
}
169169

170+
// This test verifies that we still operate on SyntaxTree's that have gaps in them. The gaps are temporary
171+
// until our work with the parser has been completed.
172+
[Fact]
173+
public void GetDesiredIndentation_ReturnsNull_IfOwningSpanDoesNotExist()
174+
{
175+
// Arrange
176+
var source = new StringTextSnapshot($@"
177+
<div>
178+
<div>
179+
</div>
180+
</div>
181+
");
182+
var syntaxTree = GetSyntaxTree(new StringTextSnapshot("something else"));
183+
var service = new DefaultRazorIndentationFactsService();
184+
185+
// Act
186+
var indentation = service.GetDesiredIndentation(
187+
syntaxTree,
188+
source,
189+
source.GetLineFromLineNumber(3),
190+
indentSize: 4,
191+
tabSize: 1);
192+
193+
// Assert
194+
Assert.Null(indentation);
195+
}
196+
170197
[Fact]
171198
public void GetDesiredIndentation_ReturnsNull_IfOwningSpanIsCode()
172199
{

0 commit comments

Comments
 (0)