Skip to content

How can a server implement "extract method" (how can it trigger rename?) #764

Closed

Description

@DanTup

#662 suggests we can implement "extract method" using existing code actions, however I can't see any mechanism for how the user would be prompted for a name, or the server could tell the client to invoke rename after the edit.

For example, let's say I have this code:

thing() {
  a();
  b();
  c();
}

I highlight the calls to b() and c() and invoke Extract Method. I end up with this:

thing() {
  a();
  this.newMethod();
}

newMethod() {
  b();
  c();
}

We really want to to either have prompted the user for newMethod, or automatically invoked rename on newMethod (which is what TypeScript does today), or - provide edits including snippet syntax.

I can't find any way to currently do this. If the solution is to use a custom command, then I think this comes back to #642, where we can't tell if the client has it implemented.

Dirk Bäumer (@dbaeumer) is there something I've overlooked - can we already do this today?

Activity

  1. mickaelistria commented on Jun 5, 2019

    @mickaelistria

    Could we imagine codeAction result having a renameRangeAfterChange which the client would be expected to use to trigger a rename?
    Maybe this could even be a list of ranges to allow renaming parametersas well.

  2. DanTup commented on Jun 5, 2019

    @DanTup
    ContributorAuthor

    That feels very specific, I think having something more general would be better (for ex. supporting snippet syntax in the returned edits?).

  3. mickaelistria commented on Jun 5, 2019

    @mickaelistria

    Yes, snippet syntax on all edits would help a lot.
    However, I think snippets only work well in the context of a single file, while some refactorings have a multi-file scope (eg Extract super/sub class). Unless snippets can be made multi-files, they seem too limited.
    While my proposal seems indeed a bit specific, I'm wondering what refactorings would need some other user interaction than chaining a rename after being applied. Do you have an example?

  4. fbricon commented on Jun 5, 2019

    @fbricon
    Contributor

    I don't think extracting a method would affect multiple files. This sounds like the sort of refactoring that's local in scope by nature, in which case, snippets would make sense (that was also the mechanism I was thinking of). But I might be wrong.

  5. mickaelistria commented on Jun 5, 2019

    @mickaelistria

    Right, my previous comment doesn't apply to "extract method", and usage of snippets in CodeActions/TextEdit still seems the best and simplest approach for this specific refactoring.

  6. DanTup commented on Jun 12, 2019

    @DanTup
    ContributorAuthor

    Dirk Bäumer (@dbaeumer) do you have any thoughts on this? I'd like to be able to migrate to LSP but I have a few remaining features that seem stuck on this. I'm unsure whether to a) implement this using my own custom commands (which means each editor will need custom code, and have to work around #642) or hold off in the hope that there might be a better solution available (or available soon).

  7. rockbruno commented on Jun 29, 2019

    @rockbruno

    I have the exact same issue as you (renaming an extracted method). Adding snippet support to edits would solve this and many other problems.

  8. dbaeumer commented on Aug 12, 2019

    @dbaeumer
    Member

    For a lengthy discussion about supporting snippets in code actions see #724.

    Even if we have this it would be something the client needs to opt in (e.g. it would be a capability).

    IMO the best way to push this forward would be to allow snippets in code actions since it resolves in the nicest user experience.

  9. hokein commented on Mar 12, 2020

    @hokein

    any progress on this?

  10. dbaeumer commented on Dec 11, 2023

    @dbaeumer
    Member

    There is work under way to add snippets to workspace edits.

  11. dbaeumer commented on Jul 8, 2024

    @dbaeumer
    Member

    Snippers made it into code actions and workspace edits for 3.18

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions