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

Automatically open rename dialog after extracting into a function #49

Open
FichteFoll opened this issue Dec 13, 2021 · 7 comments
Open

Comments

@FichteFoll
Copy link

When using a code action to extract code into a function, 9 out of 10 times I need to rename the created function. LSP-rust-analyzer could make that much easier for me if it always opened the rename dialog after an extraction action, where I can then simply press esc to close it if I don't need it. I would even argue it makes sense to have this as the new default and not even make it configurable.

IntelliJ also has this behavior.

@rchl
Copy link
Member

rchl commented Dec 13, 2021

Note that ideally the actual server (https://github.com/rust-analyzer/rust-analyzer) should support that in the first place since it would have to send us a notification with a position of the new function symbol after refactoring. Otherwise I think we would be guessing where it is.

@rchl
Copy link
Member

rchl commented Dec 13, 2021

I've checked how it works and I suppose it might not be too hacky to just find the new function symbol if it's always called fun_name and it's always within the current selection.

Although then another issue is that there is no API for hooking into when specific code action is applied. So a custom notification from the server would still be nice.

@rwols
Copy link
Member

rwols commented Dec 13, 2021

There is a protocol extension that rust-analyzer defined where the text edits can contain snippet syntax. Perhaps it uses that?

@rchl
Copy link
Member

rchl commented Dec 13, 2021

Indeed it does make a difference here.

If I set:

	"experimental_capabilities": {
		"snippetTextEdit": true,
	}

in LSP-rust-analyzer.sublime-settings I get a response like this:

{
  "edit": {
    "documentChanges": [
      {
        "edits": [
          {
            "insertTextFormat": 2,
            "newText": "self.fun_name(idx);",
            "range": {
              "end": {
                "character": 65,
                "line": 17
              },
              "start": {
                "character": 8,
                "line": 17
              }
            }
          },
          {
            "insertTextFormat": 2,
            "newText": "\n\n    fn $0fun_name(&mut self, idx: usize) {\n        self.v.resize_with((idx + 1).max(self.v.len()), || None);\n    }",
            "range": {
              "end": {
                "character": 5,
                "line": 19
              },
              "start": {
                "character": 5,
                "line": 19
              }
            }
          }
        ],
        "textDocument": {
          "uri": "file:///Users/rafal/workspace/github/rust-analyzer/lib/arena/src/map.rs",
          "version": 6
        }
      }
    ]
  },
  "kind": "refactor.extract",
  "title": "Extract into function"
}

Though that still looks wrong since there are two edits and the first one doesn't have a placeholder so I'm not sure how an editor is supposed to handle that.

@rchl
Copy link
Member

rchl commented Dec 14, 2021

When applying such code action, the editor should insert snippet, with tab stops and placeholder. At the moment, rust-analyzer guarantees that only a single edit will have InsertTextFormat.Snippet.

This doesn't seem to be the case @matklad

https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/lsp-extensions.md

@matklad
Copy link

matklad commented Dec 14, 2021

Huh, indeed -- in the above, we indeed have only snippet, but we wrongly set InsertTextFormat.Snippet. everywhere.

@rchl
Copy link
Member

rchl commented Dec 14, 2021

That's half of the problem though. There are also 2 edits. If the intention is that the user is able to easily change the function's name then that will work only partially - the user will only be able to edit the name in the function definition but not the place where it's called.

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

No branches or pull requests

4 participants