简体   繁体   English

如何在提取到变量 (LSP) 后自动触发重命名流程?

[英]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.我正在使用语言服务器协议实现对一种语言的 IDE 支持。

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我想在提取一个变量到当前scope后触发重命名。也就是说,我已经实现了当前流程的步骤1到2,想知道如何实现3和4

  1. When the user selects an expression a yellow lightbulb shows up.当用户选择一个表达式时,会出现一个黄色灯泡。 Example: z = 3 + /*selection-start*/5000/*selection-end*/示例: z = 3 + /*selection-start*/5000/*selection-end*/ 灯泡

  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.当用户选择“提取到变量”时,将在当前 scope 中创建一个名为“占位符”的新变量,并将原始表达式分配给它。 Example: placeholder = 5000; z = 3 + placeholder示例: placeholder = 5000; z = 3 + placeholder placeholder = 5000; z = 3 + placeholder 重命名

  3. The first instance of placeholder is highlighted and the text box for renaming pops up. placeholder的第一个实例高亮显示,并弹出重命名文本框。 When the user types "the_new_name" and presses Return then the text is: the_new_name = 5000; z = 3 + the_new_name当用户键入“the_new_name”并按下Return时,文本为: the_new_name = 5000; z = 3 + the_new_name the_new_name = 5000; z = 3 + the_new_name

更名

Is it possible to implement this flow with LSP?是否可以使用 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我检查了 LSP 规范,听起来我正在寻找一个Command ,但我没有看到用于重命名的内置Command

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. TypeScript 的语言服务器具有我试图复制的行为(在这里实现),但是 TypeScript 没有实现语言服务器协议,所以查看它的源代码对我没有帮助。 The screenshots above are from the TypeScript plugin built into VSCode以上截图来自VSCode内置的TypeScript插件

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服务器可以根据代码操作请求发送编辑列表,但是此时它们无法请求用户输入: https://github.com/microsoft/language-server-protocol/issues/1641

LSP does not support this flow, but it can be done with a custom command. LSP 不支持此流程,但可以使用自定义命令完成。 Here's one technique:这是一种技术:

  • Include snippet text in the the WorkspaceEdit s, like "${0: placeholder}".WorkspaceEdit中包含片段文本,例如“${0: placeholder}”。 Snippet syntax is documented here .片段语法在此处记录
  • In the client, when handling code action responses, detect snippet syntax in edits and then use the IDE's APIs for triggering the rename flow.在客户端中,处理代码操作响应时,检测编辑中的片段语法,然后使用 IDE 的 API 触发重命名流程。 For VSCode, this can be done by triggering a custom command that uses VSCode APIs.对于 VSCode,这可以通过触发使用 VSCode API 的自定义命令来完成。 I couldn't find any documentation for this, so cargo-culted Rust-Analyzer's solution .我找不到这方面的任何文档,所以 cargo-culted Rust-Analyzer's solution (function command(editor: vscode.editor) {... editor.replace(range, text)... }` (函数命令(编辑器:vscode.editor){... editor.replace(范围,文本)...}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量重命名为数组的一部分? - How can I rename a variable to be part of an array? 我可以自动重构整个java项目并将大写方法参数重命名为小写吗? - Can I automatically refactor an entire java project and rename uppercase method parameters to lowercase? 在Android Studio中重构后如何重命名项目本身? - How do I rename the Project itself after refactoring in Android Studio? 如何扩展Eclipse的重命名重构,以在完成后触发另一个重构 - How to extend Eclipse's rename refactoring to trigger another refactoring after its completion 如何重命名我的c#名称空间以包含一个点 - How can I rename my c# namespace to include a dot 如何验证重构是否可以保留代码流,而不仅仅是行为? - How can I verify that refactoring preserves code flow, not just behavior? 在我使用 Eclipse 重构 class 后,mercurial 能否跟踪重命名 - Can mercurial track a rename after I've used Eclipse to refactor a class 重命名类时如何重命名变量 - How to rename variable when renaming class 将属性或公共字段移动到另一个类后如何重命名? - How do I rename properties or public fields after moving them to another class? 如何使用Visual Studio以平滑的方式更改我的项目文件和类文件? - How Can I Change the Rename my Project File and Class files in a smooth way with Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM