Skip to content

Commit 6d563c3

Browse files
authored
Fix NRE when invoking completion in empty document (#10610)
* Fix NRE when invoking completion in empty document In empty document completion context will have RazorDocumentSyntax as Owner. RazorDoucmentSyntax has null as the parent, which makes sense. However compiler thinks we can't have a null here, while obviously we do. I'm adding a null check and a test for this case. We would probably further discuss the implications here. I tried to track down how we get null in a non-nullable field, but it's a bit confusing since the class gets generated. * Move RazorDocumentSyntax special case inside switch per CR suggestion * Fixing build
1 parent 24c99ef commit 6d563c3

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Completion/TagHelperCompletionProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ public ImmutableArray<RazorCompletionItem> GetCompletionItems(RazorCompletionCon
4848
if (owner is null)
4949
{
5050
Debug.Fail("Owner should never be null.");
51-
return ImmutableArray<RazorCompletionItem>.Empty;
51+
return [];
5252
}
5353

5454
owner = owner switch
5555
{
5656
// This provider is trying to find the nearest Start or End tag. Most of the time, that's a level up, but if the index the user is typing at
5757
// is a token of a start or end tag directly, we already have the node we want.
5858
MarkupStartTagSyntax or MarkupEndTagSyntax or MarkupTagHelperStartTagSyntax or MarkupTagHelperEndTagSyntax or MarkupTagHelperAttributeSyntax => owner,
59+
// Invoking completion in an empty file will give us RazorDocumentSyntax which always has null parent
60+
RazorDocumentSyntax => owner,
5961
// Either the parent is a context we can handle, or it's not and we shouldn't show completions.
6062
_ => owner.Parent
6163
};
@@ -118,7 +120,7 @@ static bool InOrAtEndOfAttribute(SyntaxNode attributeSyntax, int absoluteIndex)
118120
}
119121

120122
// Invalid location for TagHelper completions.
121-
return ImmutableArray<RazorCompletionItem>.Empty;
123+
return [];
122124
}
123125

124126
private ImmutableArray<RazorCompletionItem> GetAttributeCompletions(

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/TagHelperCompletionProviderTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ public void GetCompletionAt_AtEmptyTagName_ReturnsCompletions()
8989
completion => Assert.Equal("test2", completion.InsertText));
9090
}
9191

92+
[Fact]
93+
public void GetCompletionAt_InEmptyDocument_ReturnsEmptyCompletionArray()
94+
{
95+
// Arrange
96+
var service = new TagHelperCompletionProvider(RazorTagHelperCompletionService, TestRazorLSPOptionsMonitor.Create());
97+
var context = CreateRazorCompletionContext(
98+
"$$",
99+
isRazorFile: true,
100+
tagHelpers: DefaultTagHelpers);
101+
102+
// Act
103+
var completions = service.GetCompletionItems(context);
104+
105+
// Assert
106+
Assert.Empty(completions);
107+
}
108+
92109
[Fact]
93110
public void GetCompletionAt_OutsideOfTagName_DoesNotReturnCompletions()
94111
{

0 commit comments

Comments
 (0)