Skip to content

Commit

Permalink
Don't show inlay hints for component attributes (#10117)
Browse files Browse the repository at this point in the history
Fixes #10088
  • Loading branch information
davidwengier authored Mar 18, 2024
2 parents 8a35a26 + 80c9518 commit 004be9d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
Expand Down Expand Up @@ -61,8 +62,15 @@ internal sealed class InlayHintService(IRazorDocumentMappingService documentMapp
foreach (var hint in inlayHints)
{
if (hint.Position.TryGetAbsoluteIndex(csharpSourceText, null, out var absoluteIndex) &&
_documentMappingService.TryMapToHostDocumentPosition(csharpDocument, absoluteIndex, out Position? hostDocumentPosition, out _))
_documentMappingService.TryMapToHostDocumentPosition(csharpDocument, absoluteIndex, out Position? hostDocumentPosition, out var hostDocumentIndex))
{
// We know this C# maps to Razor, but does it map to Razor that we like?
var node = await documentContext.GetSyntaxNodeAsync(hostDocumentIndex, cancellationToken).ConfigureAwait(false);
if (node?.FirstAncestorOrSelf<MarkupTagHelperAttributeValueSyntax>() is not null)
{
continue;
}

if (hint.TextEdits is not null)
{
hint.TextEdits = _documentMappingService.GetHostDocumentEdits(csharpDocument, hint.TextEdits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ private void M(string thisIsMyString)
""",
toolTipMap: new Dictionary<string, string>
{
{ "int", "struct System.Int32" },
{"string", "class System.String" },
{"thisIsMyString", "(parameter) string thisIsMyStr" }
{ "int", "struct System.Int32" },
{ "string", "class System.String" },
{ "thisIsMyString", "(parameter) string thisIsMyStr" }
},
output: """
Expand All @@ -62,11 +62,36 @@ private void M(string thisIsMyString)
""");

[Fact]
public Task InlayHints_ComponentAttributes()
=> VerifyInlayHintsAsync(
input: """
<div>
<InputText Value="_value" />
<InputText Value="@_value" />
<InputText Value="@(_value)" />
</div>
""",
toolTipMap: new Dictionary<string, string>
{
},
output: """
<div>
<InputText Value="_value" />
<InputText Value="@_value" />
<InputText Value="@(_value)" />
</div>
""");

private async Task VerifyInlayHintsAsync(string input, Dictionary<string, string> toolTipMap, string output)
{
TestFileMarkupParser.GetSpans(input, out input, out ImmutableDictionary<string, ImmutableArray<TextSpan>> spansDict);
var codeDocument = CreateCodeDocument(input);
var razorFilePath = "C:/path/to/file.razor";
var codeDocument = CreateCodeDocument(input, filePath: razorFilePath);

var languageServer = await CreateLanguageServerAsync(codeDocument, razorFilePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,17 @@ private class WorkspaceConfigurationHandler
values.Add(item.Section switch
{
"csharp|inlay_hints.dotnet_enable_inlay_hints_for_parameters" => "true",
"csharp|inlay_hints.dotnet_enable_inlay_hints_for_literal_parameters" => "true",
"csharp|inlay_hints.dotnet_enable_inlay_hints_for_indexer_parameters" => "true",
"csharp|inlay_hints.dotnet_enable_inlay_hints_for_object_creation_parameters" => "true",
"csharp|inlay_hints.dotnet_enable_inlay_hints_for_other_parameters" => "true",
"csharp|inlay_hints.dotnet_suppress_inlay_hints_for_parameters_that_differ_only_by_suffix" => "false",
"csharp|inlay_hints.dotnet_suppress_inlay_hints_for_parameters_that_match_method_intent" => "false",
"csharp|inlay_hints.dotnet_suppress_inlay_hints_for_parameters_that_match_argument_name" => "false",
"csharp|inlay_hints.csharp_enable_inlay_hints_for_types" => "true",
"csharp|inlay_hints.csharp_enable_inlay_hints_for_implicit_variable_types" => "true",
"csharp|inlay_hints.csharp_enable_inlay_hints_for_lambda_parameter_types" => "true",
"csharp|inlay_hints.csharp_enable_inlay_hints_for_implicit_object_creation" => "true",
_ => ""
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Razor.Test.Common.Mef;
using Microsoft.AspNetCore.Razor.Test.Common.Workspaces;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -54,7 +55,10 @@ public static async Task<CSharpTestLspServer> CreateCSharpLspServerAsync(IEnumer
var csharpFiles = files.Select(f => new CSharpFile(f.Uri, f.SourceText));

var exportProvider = TestComposition.Roslyn.ExportProviderFactory.CreateExportProvider();
var metadataReferences = await ReferenceAssemblies.Default.ResolveAsync(language: LanguageNames.CSharp, cancellationToken);
var metadataReferences = (await ReferenceAssemblies.Default.ResolveAsync(language: LanguageNames.CSharp, cancellationToken))
// ComponentBase here comes from our ComponentShim project, not the real ASP.NET libraries. It's enough for the generated C#
// in tests to at least compile better.
.Add(MetadataReference.CreateFromFile(typeof(ComponentBase).Assembly.Location));
var workspace = CreateCSharpTestWorkspace(csharpFiles, exportProvider, metadataReferences, razorSpanMappingService);
var clientCapabilities = new VSInternalClientCapabilities
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ @using BlazorApp1.Shared
@using System;
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
""",
Expand Down

0 comments on commit 004be9d

Please sign in to comment.