Skip to content

Commit

Permalink
Do not offer inline hints for parameters that already named
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Dec 20, 2024
1 parent 4234893 commit 1e21020
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,7 @@ public async Task TestWorkspaceDiagnosticsWaitsForLspSolutionChanges(bool useVSD
Assert.NotEmpty(results);
}

[Theory, CombinatorialData]
[Theory(Skip = "https://github.com/dotnet/roslyn/issues/76503"), CombinatorialData]
public async Task TestWorkspaceDiagnosticsWaitsForLspTextChangesWithMultipleSources(bool useVSDiagnostics, bool mutatingLspWorkspace)
{
var markup1 =
Expand Down

0 comments on commit 1e21020

Please sign in to comment.