Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not offer inline hints for parameters that already named #76532

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading