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

Refactoring: Move Static Members to Another Type #55127

Closed
Soreloser2 opened this issue Jul 26, 2021 · 6 comments
Closed

Refactoring: Move Static Members to Another Type #55127

Soreloser2 opened this issue Jul 26, 2021 · 6 comments

Comments

@Soreloser2
Copy link
Contributor

Soreloser2 commented Jul 26, 2021

Brief description:

Combines #33928 and #22053, allowing for users to move a static member to an already existing type or to create a new static type. Currently we will focus only on movement within the same namespace and project as the original location of the member (to simplify refactoring constraints).

Languages applicable:

Both for C# and VB

Reports on

Any static member of a class, when the cursor is selected on the declaration line, as indicated by the angled brackets <>:

C#:

public class ExampleClass
{
    <public static int DoSomething()>
    {
        return 0;
    }
}

VB:

Public Class ExampleClass
    <Public Shared Function DoSomething() As Integer>
        Return 0
    End Function
End Class

Any static member selected this way will offer a refactoring in the lightbulb dropdown (Ctrl + .) titled "Move Static Members to Another Type...", which will open a dialog for selection options.

Dialog sample (subject to change)

Initially, a generated name of a new type will be provided, and the member that the user triggered the refactoring on will be automatically selected out of a list of all static members in the current type. We will notify the user that this type does not already exist, and therefore a new type will be created.
image

The user can use the dropdown to select or simply type the name of an existing type in the current namespace or project. The user can also select other static members, or use the convenience buttons to select all static members, deselect all static members, and select members that the current selected members depend on.
image

Pressing "OK" will perform the refactoring, removing the member (and its body if there is one) and adding it to either the selected existing type, or a new type in the same namespace with the provided name in a new class.

Notes

  • We will not support the user being able to decide the name of the new file of a created type, the file name will be autogenerated. The user can rename the file after the refactoring is complete, through other tools.
  • We may support movement to different namespaces/projects in the future. Right now we will support moving within a nested namespace, but not outside of the namespace from which you triggered the refactoring.
@Soreloser2 Soreloser2 added Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings labels Jul 26, 2021
@Soreloser2 Soreloser2 self-assigned this Jul 26, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Jul 26, 2021
@Soreloser2 Soreloser2 linked a pull request Jul 27, 2021 that will close this issue
@Soreloser2
Copy link
Contributor Author

Soreloser2 commented Jul 28, 2021

Feature checklist:

  • Dialog Box
    • Member Selection
    • Destination Selection
      • Selection of a new type using a text box
      • Selection of a new or existing type using dropdown options
    • Create Options after submission
  • Implementation
    • New Type
    • Existing Type
    • Refactoring source document usages
    • Refactoring other usages
  • Testing
    • Write Cases for implementation
    • Write Cases for selection
    • Write Cases for dialog options/UX

@Soreloser2 Soreloser2 added the Epic Groups multiple user stories. Can be grouped under a theme. label Jul 28, 2021
@jinujoseph jinujoseph removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jul 28, 2021
@jinujoseph jinujoseph added this to the 17.1 milestone Jul 28, 2021
@CyraxSputnik
Copy link

CyraxSputnik commented Aug 17, 2021

Wow!!! This seems really cool!!!

I imagine is way harder to implement this using non static members, but would be not only an epic feature, should be mythic

@CyraxSputnik
Copy link

This refactoring using non-static members could create a new class passing all the related members in the constructor. Maybe ¯_(ツ)_/¯

@Soreloser2
Copy link
Contributor Author

The feature to move to a new type is scheduled for release on 17.1-preview1. A draft PR containing some code for moving to an existing type is linked directly above; feel free to take a look 😄

@Soreloser2
Copy link
Contributor Author

This refactoring using non-static members could create a new class passing all the related members in the constructor. Maybe ¯_(ツ)_/¯

Good idea, that's definitely a route to consider. I'd imagine it might be difficult to refactor other references to the moved members, as we might need to create multiple lines to construct the new class and then access the moved member.

Another option, specifically for methods, would be to convert them to extension methods, with the added first parameter being an instance of the class you're moving from. This might get a little funky with the refactoring inside the method itself, as we'd need to requalify all the member accesses to use the new instance. However, other references to the method wouldn't change, as the method invocation would stay the same.

@jmarolf jmarolf added the User Story A single user-facing feature. Can be grouped under an epic. label Dec 2, 2021
@jmarolf jmarolf added User Story A single user-facing feature. Can be grouped under an epic. and removed Epic Groups multiple user stories. Can be grouped under a theme. User Story A single user-facing feature. Can be grouped under an epic. labels Dec 10, 2021
@jmarolf jmarolf removed the User Story A single user-facing feature. Can be grouped under an epic. label Jan 6, 2022
@jinujoseph jinujoseph modified the milestones: 17.1, 17.3 Apr 27, 2022
@Soreloser2
Copy link
Contributor Author

Closing as all the checkbox points for this feature have been created and merged. Moving to existing classes should be in 17.4 with #61519

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants