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

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

Closed
DanTup opened this issue May 29, 2019 · 11 comments
Closed
Milestone

Comments

@DanTup
Copy link
Contributor

DanTup commented May 29, 2019

#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.

@dbaeumer is there something I've overlooked - can we already do this today?

@mickaelistria
Copy link

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.

@DanTup
Copy link
Contributor Author

DanTup commented Jun 5, 2019

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

@mickaelistria
Copy link

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?

@fbricon
Copy link
Contributor

fbricon commented Jun 5, 2019

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.

@mickaelistria
Copy link

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.

@DanTup
Copy link
Contributor Author

DanTup commented Jun 12, 2019

@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).

@rockbruno
Copy link

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

@dbaeumer
Copy link
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.

@dbaeumer dbaeumer added this to the Backlog milestone Oct 31, 2019
@hokein
Copy link

hokein commented Mar 12, 2020

any progress on this?

@dbaeumer
Copy link
Member

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

@dbaeumer dbaeumer modified the milestones: Backlog, 3.18 Dec 11, 2023
@dbaeumer
Copy link
Member

dbaeumer commented Jul 8, 2024

Snippers made it into code actions and workspace edits for 3.18

@dbaeumer dbaeumer closed this as completed Jul 8, 2024
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

6 participants