Skip to content

Suggest name when naming incomplete tuple element#80671

Merged
CyrusNajmabadi merged 9 commits intomainfrom
copilot/add-suggested-name-for-tuples
Oct 13, 2025
Merged

Suggest name when naming incomplete tuple element#80671
CyrusNajmabadi merged 9 commits intomainfrom
copilot/add-suggested-name-for-tuples

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 12, 2025

Fixes #22342

Summary

This PR adds support for suggesting names when typing incomplete tuple elements in method bodies. Previously, name suggestions only worked when the tuple syntax was fully formed.

Problem

When typing an incomplete tuple like this:

void M()
{
    (List<Person> 
}

The IDE would not suggest "people" as a name for the tuple element, even though it worked correctly for complete tuples:

void M()
{
    (List<Person> , int i)  // ✅ Suggests "people" for first element
}

Root Cause

The C# parser creates different syntax nodes depending on how complete the tuple is:

  • Complete tuple: (List<Person> , int i)TupleExpressionSyntax (handled by IsTupleLiteralElement)
  • Incomplete tuple: (List<Person> ParenthesizedExpressionSyntax (not handled)
  • Tuple in parameter: void M((List<Person> )TupleTypeSyntax (handled by IsTupleTypeElement)

Solution

Added a new method IsIncompleteParenthesizedTuple to DeclarationNameInfo.cs that detects when the user is typing an incomplete tuple by:

  1. Checking if the cursor is inside a ParenthesizedExpressionSyntax
  2. Verifying the expression inside the parentheses resolves to a type symbol
  3. Returning the appropriate NameDeclarationInfo for name generation

The implementation follows the same pattern as existing tuple handling methods and is placed appropriately in the check chain.

Testing

Added ten test cases covering:

  • Generic types: (List<Person> $$ → suggests "people"
  • Predefined types: (int $$ → suggests "i"
  • Qualified names: (System.Action $$ → suggests "action"
  • Fully qualified generic types: (System.Collections.Generic.List<Person> $$ → suggests "people"
  • Array types: (Person[] $$ → suggests "people"
  • Variants with closing paren present for all of the above

All builds successful with no regressions expected.

Fixes #75347

Original prompt

This section details on the original issue you should resolve

<issue_title>Suggest name when naming tuple element</issue_title>
<issue_description>### Version Used

Compiler: 4.12.0-3.24470.4 (d799b05)
.NET: 9.0.100-rc.1.24452.12
Visual Studio Code: 1.93.1, 38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40, x64
C# Extension: v2.49.25
C# Dev Kit Extension: v1.11.11

Background and Motivation

Many symbols today get suggested names depending on the context, like method parameters. This is currently not the case for tuples (specifically value tuples).

Proposed Feature

Provide a Suggested name code action for value tuple names.
</issue_description>

<agent_instructions>@copilot This is likely in DeclarationNameRecommender. But potentially needs work in NameDeclarationInfo.GetDeclarationInfoAsync to determine what is going on.

Note: this functionality does already work when the IDE knows for sure it is a tuple. like (List<Person> $$, int i). It is when the tuple is beginning to be written where there is a problem. This may take dedicated checking of the syntax shape here which may not actually have a TupleSyntax in it.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@CyrusNajmabadi This needs more info. Can you give an example showing what code you have, and name you think should be suggested? @CyrusNajmabadi Thanks. Updating title @CyrusNajmabadi Works when we know it is a tuple: Image

Fixes #75347


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Add suggested name code action for value tuples Suggest name when naming incomplete tuple element Oct 12, 2025
Copilot AI requested a review from CyrusNajmabadi October 12, 2025 15:01
(System.Action $$
}
}
""", "action");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Create versions of all these tests as well where there is a close paren after the $$.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
{
void Do()
{
(List<Person> $$)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add variants with System.Collections.Generic.List<Person> and Person[]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test variants for fully qualified generic types and array types in commit d2a3d56.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review October 12, 2025 15:20
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 12, 2025 15:20
@CyrusNajmabadi CyrusNajmabadi merged commit e6c1929 into main Oct 13, 2025
25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/add-suggested-name-for-tuples branch October 13, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest name when naming tuple element Suggested names not provided for tuple elements in generic type arguments

3 participants