Skip to content

Commit 4aecb59

Browse files
authored
Add override completion test for VS Code (#12046)
Alex didn't get a chance to do this before he had to leave last week.
2 parents 3511216 + 9ad50fb commit 4aecb59

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Completion/Delegation/DelegatedCompletionItemResolver.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ private async Task<VSInternalCompletionItem> PostProcessCompletionItemAsync(
7878
return resolvedCompletionItem;
7979
}
8080

81-
if (resolvedCompletionItem.TextEdit is null && resolvedCompletionItem.AdditionalTextEdits is null)
82-
{
83-
// Only post-processing work we have to do is formatting text edits on resolution.
84-
return resolvedCompletionItem;
85-
}
86-
8781
var identifier = context.Identifier.TextDocumentIdentifier;
8882
if (!_documentContextFactory.TryCreate(identifier, out var documentContext))
8983
{

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Completion/Delegation/DelegatedCompletionItemResolverTest.NetFx.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void ValidateResolveParams(DelegatedCompletionItemResolveParams @params)
176176
public async Task ResolveAsync_CSharp_Resolves()
177177
{
178178
// Arrange & Act
179-
var resolvedItem = await ResolveCompletionItemAsync("@$$", itemToResolve: "typeof", DisposalToken);
179+
var resolvedItem = await ResolveCompletionItemAsync("@$$", itemToResolve: "typeof", supportsVisualStudioExtensions: true, DisposalToken);
180180

181181
// Assert
182182
Assert.NotNull(resolvedItem.Description);
@@ -197,23 +197,22 @@ Task FooAsync()
197197
""";
198198
TestFileMarkupParser.GetPosition(input, out var documentContent, out _);
199199
var originalSourceText = SourceText.From(documentContent);
200-
var expectedSourceText = SourceText.From(
201-
"""
200+
var expected = """
202201
@{
203202
async Task FooAsync()
204203
{
205204
await
206205
}
207206
}
208-
""");
207+
""";
209208

210209
// Act
211-
var resolvedItem = await ResolveCompletionItemAsync(input, itemToResolve: "await", DisposalToken);
210+
var resolvedItem = await ResolveCompletionItemAsync(input, itemToResolve: "await", supportsVisualStudioExtensions: true, DisposalToken);
212211

213212
// Assert
214213
var textChange = originalSourceText.GetTextChange(resolvedItem.TextEdit.Value.First);
215214
var actualSourceText = originalSourceText.WithChanges(textChange);
216-
Assert.True(expectedSourceText.ContentEquals(actualSourceText));
215+
AssertEx.EqualOrDiff(expected, actualSourceText.ToString());
217216
}
218217

219218
[Fact]
@@ -232,16 +231,15 @@ Task FooAsync()
232231

233232
// Admittedly the result here is not perfect, but only because our tests don't implement the full LSP editor logic. The key thing
234233
// is the addition of the using directive.
235-
var expectedSourceText = SourceText.From(
236-
"""
234+
var expected = """
237235
@using System.Text
238236
@{
239237
Task FooAsync()
240238
{
241239
String
242240
}
243241
}
244-
""");
242+
""";
245243

246244
var codeDocument = CreateCodeDocument(input.Text, filePath: "C:/path/to/file.razor");
247245
// Roslyn won't send unimported types if SupportsVisualStudioExtensions is true
@@ -275,7 +273,37 @@ Task FooAsync()
275273
var originalSourceText = SourceText.From(input.Text);
276274
var textChange = originalSourceText.GetTextChange(resolvedItem.AdditionalTextEdits.Single());
277275
var actualSourceText = originalSourceText.WithChanges(textChange);
278-
AssertEx.EqualOrDiff(expectedSourceText.ToString(), actualSourceText.ToString());
276+
AssertEx.EqualOrDiff(expected, actualSourceText.ToString());
277+
}
278+
279+
[Fact]
280+
public async Task ResolveAsync_CSharp_OverrideCompletion()
281+
{
282+
// Arrange
283+
var input =
284+
"""
285+
@code {
286+
override $$
287+
}
288+
""";
289+
TestFileMarkupParser.GetPosition(input, out var documentContent, out _);
290+
var originalSourceText = SourceText.From(documentContent);
291+
var expected = """
292+
@using System.Threading.Tasks
293+
@code {
294+
protected override Task OnInitializedAsync()
295+
{
296+
return base.OnInitializedAsync();
297+
}
298+
}
299+
""";
300+
301+
// Act
302+
var resolvedItem = await ResolveCompletionItemAsync(input, itemToResolve: "OnInitializedAsync()", supportsVisualStudioExtensions: false, DisposalToken);
303+
304+
var textChange = originalSourceText.GetTextChange((TextEdit)resolvedItem.Command.Arguments[1]);
305+
var actualSourceText = originalSourceText.WithChanges(textChange);
306+
AssertEx.EqualOrDiff(expected, actualSourceText.ToString());
279307
}
280308

281309
[Fact]
@@ -306,11 +334,11 @@ void ValidateResolveParams(DelegatedCompletionItemResolveParams @params)
306334
}
307335
}
308336

309-
private async Task<VSInternalCompletionItem> ResolveCompletionItemAsync(string content, string itemToResolve, CancellationToken cancellationToken)
337+
private async Task<VSInternalCompletionItem> ResolveCompletionItemAsync(string content, string itemToResolve, bool supportsVisualStudioExtensions, CancellationToken cancellationToken)
310338
{
311339
TestFileMarkupParser.GetPosition(content, out var documentContent, out var cursorPosition);
312340
var codeDocument = CreateCodeDocument(documentContent, filePath: "C:/path/to/file.razor");
313-
await using var csharpServer = await CreateCSharpServerAsync(codeDocument, supportsVisualStudioExtensions: true);
341+
await using var csharpServer = await CreateCSharpServerAsync(codeDocument, supportsVisualStudioExtensions);
314342

315343
var clientConnection = CreateClientConnectionForResolve(csharpServer);
316344
var documentContextFactory = new TestDocumentContextFactory("C:/path/to/file.razor", codeDocument);

0 commit comments

Comments
 (0)