Skip to content

Commit

Permalink
Do not offer inline hints for parameters that already named (#76532)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Dec 20, 2024
2 parents a9cafab + 37d3667 commit 057bc26
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1298,5 +1298,95 @@ class A

Await VerifyParamHints(input, output)
End Function

<WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/59317")>
Public Async Function TestExistingNamedParameter1() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
static void Main(string[] args)
{
Goo({|a:|}1, 2, b: 0);
}

static void Goo(int a, int b)
{

}
}
</Document>
</Project>
</Workspace>

Dim output =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
static void Main(string[] args)
{
Goo(a: 1, 2, b: 0);
}

static void Goo(int a, int b)
{

}
}
</Document>
</Project>
</Workspace>

Await VerifyParamHints(input, output)
End Function

<WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/59317")>
Public Async Function TestExistingNamedParameter2() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
static void Main(string[] args)
{
Goo({|a:|}1, {|b:|}2, c: 0);
}

static void Goo(int a, int b)
{

}
}
</Document>
</Project>
</Workspace>

Dim output =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
class C
{
static void Main(string[] args)
{
Goo(a: 1, b: 2, c: 0);
}

static void Goo(int a, int b)
{

}
}
</Document>
</Project>
</Workspace>

Await VerifyParamHints(input, output)
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ namespace Microsoft.CodeAnalysis.CSharp.InlineHints;
/// as well as associate the adornments back to the parameter name
/// </summary>
[ExportLanguageService(typeof(IInlineParameterNameHintsService), LanguageNames.CSharp), Shared]
internal class CSharpInlineParameterNameHintsService : AbstractInlineParameterNameHintsService
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpInlineParameterNameHintsService() : AbstractInlineParameterNameHintsService
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpInlineParameterNameHintsService()
{
}

protected override void AddAllParameterNameHintLocations(
SemanticModel semanticModel,
ISyntaxFactsService syntaxFacts,
Expand Down Expand Up @@ -66,12 +62,23 @@ private static void AddArguments(
BaseArgumentListSyntax argumentList,
CancellationToken cancellationToken)
{
// Ensure we don't add an inline parameter name hint using the same name already present on another argument.
using var _ = PooledHashSet<string?>.GetInstance(out var presentNames);
foreach (var argument in argumentList.Arguments)
{
if (argument is { NameColon.Name.Identifier.ValueText: string nameText })
presentNames.Add(nameText);
}

foreach (var argument in argumentList.Arguments)
{
if (argument.NameColon != null)
continue;

var parameter = argument.DetermineParameter(semanticModel, cancellationToken: cancellationToken);
if (presentNames.Contains(parameter?.Name))
continue;

buffer.Add((argument.Span.Start, argument, parameter, GetKind(argument.Expression)));
}
}
Expand Down

0 comments on commit 057bc26

Please sign in to comment.