Search code examples
refactoringlanguage-server-protocol

How can I automatically trigger the rename flow after extracting into a variable (LSP)?


I'm implementing IDE support for a language using Language Server Protocol.

I want to trigger a rename after extracting a variable into the current scope. That is, I've implemented steps 1 to 2 of the current flow and want to know how to implement 3 and 4

  1. When the user selects an expression a yellow lightbulb shows up. Example: z = 3 + /*selection-start*/5000/*selection-end*/ lightbulb

  2. When the user selects "extract into variable" then a new variable called "placeholder" is created in the current scope and the original expression is assigned to it. Example: placeholder = 5000; z = 3 + placeholderrenaming

  3. The first instance of placeholder is highlighted and the text box for renaming pops up. When the user types "the_new_name" and presses Return then the text is: the_new_name = 5000; z = 3 + the_new_name

renamed

Is it possible to implement this flow with LSP? If so, how? I checked the LSP spec and it sounds like I'm looking for a Command, but I didn't see a built-in Command for renaming

TypeScript's language server has the behavior I'm trying to replicate (implemented around here), but TypeScript doesn't implement language server protocol, so peeking at its source didn't help me. The screenshots above are from the TypeScript plugin built into VSCode


Solution

  • It seems as if editors must call for this rename themselves. Servers can send a list of edits as a result of a code action request, however they cannot request input from the user at this time: https://github.com/microsoft/language-server-protocol/issues/1641