Skip to content

Commit

Permalink
Fix completion in an empty document (#11344)
Browse files Browse the repository at this point in the history
Fixes #10613
Fixes #10614
  • Loading branch information
davidwengier authored Jan 2, 2025
2 parents c1e03c6 + 0474a35 commit c068d47
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -47,6 +48,17 @@ public static DocumentPositionInfo GetPositionInfo(
int hostDocumentIndex)
{
var sourceText = codeDocument.Source.Text;

if (sourceText.Length == 0)
{
Debug.Assert(hostDocumentIndex == 0);

// Special case for empty documents, to just force Html. When there is no content, then there are no source mappings,
// so the map call below fails, and we would default to Razor. This is fine for most cases, but empty documents are a
// special case where Html provides much better results when users first start typing.
return new DocumentPositionInfo(RazorLanguageKind.Html, new Position(0, 0), hostDocumentIndex);
}

var position = sourceText.GetPosition(hostDocumentIndex);

var languageKind = codeDocument.GetLanguageKind(hostDocumentIndex, rightAssociative: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,59 @@ The end.
snippetLabels: ["snippet1", "snippet2"]);
}

[Fact]
public async Task HtmlSnippetsCompletion_EmptyDocument()
{
await VerifyCompletionListAsync(
input: """
$$
""",
completionContext: new RoslynVSInternalCompletionContext()
{
InvokeKind = RoslynVSInternalCompletionInvokeKind.Explicit,
TriggerCharacter = null,
TriggerKind = RoslynCompletionTriggerKind.Invoked
},
expectedItemLabels: ["snippet1", "snippet2"],
snippetLabels: ["snippet1", "snippet2"]);
}

[Fact]
public async Task HtmlSnippetsCompletion_WhitespaceOnlyDocument1()
{
await VerifyCompletionListAsync(
input: """
$$
""",
completionContext: new RoslynVSInternalCompletionContext()
{
InvokeKind = RoslynVSInternalCompletionInvokeKind.Explicit,
TriggerCharacter = null,
TriggerKind = RoslynCompletionTriggerKind.Invoked
},
expectedItemLabels: ["snippet1", "snippet2"],
snippetLabels: ["snippet1", "snippet2"]);
}

[Fact]
public async Task HtmlSnippetsCompletion_WhitespaceOnlyDocument2()
{
await VerifyCompletionListAsync(
input: """
$$
""",
completionContext: new RoslynVSInternalCompletionContext()
{
InvokeKind = RoslynVSInternalCompletionInvokeKind.Explicit,
TriggerCharacter = null,
TriggerKind = RoslynCompletionTriggerKind.Invoked
},
expectedItemLabels: ["snippet1", "snippet2"],
snippetLabels: ["snippet1", "snippet2"]);
}

[Fact]
public async Task HtmlSnippetsCompletion_NotInStartTag()
{
Expand Down

0 comments on commit c068d47

Please sign in to comment.