Skip to content

Introduce Local should look for naming patterns in ForEachStatementSyntax.Expression #56

Closed

Description

@rchande
class Program
{
    static void Main(string[] args)
    {
        foreach (var num in GetNumbers())
        {

        }
    }

    static IEnumerable<int> GetNumbers()
    {
        return new[] { 1, 2, 3 };
    }
}

Highlight "GetNumbers()" in the foreach statement and use Ctrl+. to get the light bulb. Invoke "Introduce Local". Observe that the new local is named "enumerable".

Since we have code to detect known patterns like "GetBlah", it would be nice if we could notice that the Expression of the ForEachStatementSyntax was an InvocationExpression and try to recognize that pattern.

Activity

  1. changed the title [-]Introduce Local should look for naming patterns in ForeachStatementSyntax.Enumerable[/-] [+]Introduce Local should look for naming patterns in ForEachStatementSyntax.Expression[/+] on Jan 22, 2015
  2. added
    help wanted
    on Jan 30, 2015
  3. added this to the milestone on Jan 30, 2015
  4. giggio commented on Feb 1, 2015

    @giggio

    I would like to work on this issue, if possible. A few questions:
    What code do you have to detect patterns like "GetBlah"? Where is it?
    Is this for C# only?
    Any pointer on where to start? I saw a lot of infrastructure on IntroduceVariable. Is that it?

  5. jmarolf commented on Feb 1, 2015

    @jmarolf
    Contributor

    CSharpIntroduceVariableService and VisualBasicIntroduceVariableService determine the name in IntroduceLocalAsync(C#, VB) and IntroduceFieldAsync(C#, VB), but these both use methods from AbstractIntroduceVariableService.

    These then call into the semantic facts services(C#, VB). Which in turn call GenerateNameForExpression(C#, VB). The best way to accomplish what you want is to modify the GenerateNameForExpression to have a lot more smarts.

  6. giggio commented on Feb 1, 2015

    @giggio

    Cool. Can I start working on it, then?

  7. jmarolf commented on Feb 1, 2015

    @jmarolf
    Contributor

    feel free to start working on it.

  8. rchande commented on Feb 2, 2015

    @rchande
    ContributorAuthor

    You're free to start working on it. Thanks!

  9. balajikris commented on Feb 3, 2015

    @balajikris
    Contributor

    "Is this for C# only?"

    The issue will likely repro for both C# and VB. the fix however, could be in a common file that could fix both languages.. It'll be useful to add unit tests for both languages (IntrdouceVariableTests.cs, IntrdouceVariableTests.vb) to make sure they're both addressed.. 😃

  10. removed this from the milestone on Feb 8, 2015
  11. added this to the milestone on Feb 12, 2015
  12. removed this from the milestone on Nov 23, 2015
  13. added this to the milestone on Dec 4, 2015
  14. sievajetnamdar commented on Jan 2, 2018

    @sievajetnamdar

    I managed to add this feature with the corresponding unit tests. Perhaps there are some tweaks needed for the naming in the codefix. Can I commit this to the master branch so that someone can review this? How does this work?

  15. CyrusNajmabadi commented on Jan 3, 2018

    @CyrusNajmabadi
    Contributor

    @sievajetnamdar You should open a PR using the "new pull request" button on https://github.com/dotnet/roslyn/pulls

    There, you can point at your own fork and branch and a PR will be created from that. Give the PR a good title, ensure that the code in it is what you want, and submit the PR. Then tag appropriate people (like @rchande and me) to look at it.

    We'll go through some cycles of reviews+changes on your part. And when finally approved, it can be merged in by a team member.

  16. added a commit that references this issue on Jan 3, 2018
  17. added a commit that references this issue on Nov 21, 2025
  18. added a commit that references this issue on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions