-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Rename type import to alias #15858
Comments
Maybe could impl within a |
Sorry, I'm not sure what your response means @Young-Flash. Do you mean the feature exists already and I could implement/use it by using this |
If it really doesn't exist yet, I would propose one could just use the normal rename LSP command (e.g. |
👋 We have the infra for this so it should be easy to do. Do you want to do it @Young-Flash or should I do it? |
Awesome, great news! |
@fritzrehde Sorry for the unclearness, it's the latter. That feature hasn't implement for now. |
@alibektas I could have a try. It's okay if @fritzrehde feel like to implement this personally, I think R-A Community will happy to help new contributor to make their first contribution, there are some related PR which could refers to |
Sure I will be posting some instructions in a couple of hours that will help with the implementation. |
IMPORTANT : Implementing this as an assist has a serious shortcoming, so it'd be better if we'd implemented it as a special case of renaming. See comments below where the problem is discussed. So let's talk about how this issue can be solved. I will mostly omit the assist-related part. For this you will have numerous examples in the Renaming in rust-analyzer is handled by two crates : rust-analyzer/crates/ide-db/src/source_change.rs Lines 19 to 23 in c1c9e10
Two things immediately draw our attention about
Feel free to ask any questions you have! |
I actually had the idea in my mind that trigger a rename on an import should introduce an alias as the initial issue here outlines as well. Though we could probably have both, that rename behavior and an assist (for discoverability). |
👋 I'm interested in implementing this, however I some questions regarding how it will work for end-users (specially after reading #15656). The way that I particularly would like to implement this would be: if use std::io::Result;
fn main() -> Result<()> {
Ok(())
} then If I try to rename use std::io::Result as IoResult;
fn main() -> IoResult<()> {
Ok(())
} This would work fine, but some users might not want this behavior. I think we could use cc @alibektas, because you were the one that implemented As a side note, I don't have a problem having an |
👋 I know the day would soon come where I would say to myself, I'd better made that bool an enum, I just wasn't expecting it to happen to soon 😄. But jokes aside I think we should avoid changing the said config. I don't think that we should be using type aliasing as if it were a fallback for renaming. I think it's justified to have a "special" renaming function around import statements. So what I am trying to say is that renaming on an import should always produce type aliases and I am not too sure about what I am going to say now but I don't think that there is a party that wouldn't welcome this. Why don't you implement this feature first only as an assist but instead of having the actual function that does the heavy work implemented in |
I imagine that you would have that reaction when I realized that the feature was implemented 2 weaks ago 🤣
Ok sounds good, however I have a pretty big question:
like: use std::io::Result; // <- assist "generate_type_alias"
fn main() -> Result<()> {
Ok(())
} to use std::io::Result as TypeAlias;
fn main() -> TypeAlias<()> {
Ok(())
} If the answer is no (you can provide values), how is this done? I checked similar, assists and they seem to just hard-code the names. If the answer is yes, then this is problematic, because renaming aliases is currently unsupported. As a side note, an code action for this wouldn't be necessary if you were able to rename aliases, because you could create an alias with the same name of the qualified type (e.g Maybe I'm overthinking this too much? Another alternative that I see is: // User wants to rename `Result` to `IoResult`.
use std::io::Result;
fn main() -> Result<()> {
Ok(())
} So they change it like so: use std::io::Result as IoResult;
fn main() -> Result<()> {
Ok(())
} Then a supposedly (apply_alias?) gonna find all the usages then apply those changes automatically? |
So the answer is sth in between the two. User can provide a value directly iff
So as an example an editor like VSCode does support snippets but another like Helix (IIRC) may not, which brings us to why you asked this question in the first place. You have absolutely a fair argument , once the user invokes the assist and edits alias's name one way or another, all the defs that refer to this by its previous name will now be invalid. And no we don't wait for user to edit to exit the assist routine : Once we created the text The only way making this possible is if client supports multi cursor snippets which basically means putting cursor in front of every occurence of the aliased definition which is ofc ridiculous and I don't think that even VSCode supports this. |
To wrap things up : If you still want to do renaming you are most welcomed to do so. It may be a little more challenging than implementing an assist but it is a greenfield with regards to the |
Oh, I completely missed that, it was also used on extract_function...
Weirdly enough, I installed a vscode to test if it would prompt me for a name (extract_function), but it just created the default identifier (fun_name)? As as side note, on vscode
Yeah I had the suspicion that there was something wrong regarding this. This is my first my time contributing to RA so I wasn't really sure if I was missing something.
So if I understand correctly, we keep the option ( |
It won't prompt, it will rather put the cursor on fn main() -> () {
let i : i32 = abc(3);
()
}
I didn't understand what you meant by handler's example.
This is a little vague but I am sure you mean what I meant but just to be clear. If we are looking at a renaming in a non-local create and the config has Feel free to contact me if you have any questions. |
Ok now I get it; thanks for the example.
The example here, didn't work with vscode: struct S {
field: (u8, u8, u8),
} If I select from "(u8, u8, u8)", then select "Extract type as alias", I get
Ok so:
right?
Will do; I kinda have an idea how to implement it, it just kinda confusing what would be the behavior that we want to go with 😅. |
(nit, you guys are talking about import aliases, not type aliases, those are different things) Implementing the rename idea is nicer UX wise until then. You just need to check if the rename happens on a name inside a use tree and insert an alias and adjust the search scope for usage appropriately to just the module (there is a bit more to that in terms of other modules using that import but that is something that can be fixed as a next step). |
I went ahead and implemented the solution that Veykril suggested above. I got pretty far with it (I can even rename items in other modules!), but I have problem when I need to pick the references that needs to be renamed. With the current implementation, all references, qualified and non-qualified, are being renamed, so: pub mod foo { pub struct Foo; }
use foo::Foo$0;
fn main() {
let _: Foo;
let _: foo::Foo;
} If renamed with pub mod foo { pub struct Foo; }
use foo::Foo as Bar;
fn main() {
let _: Bar;
let _: foo::Bar; // <- wrong, should have continue being "foo::Foo".
} (funny enough, I also ran on this problem when changing How should I approach this? Can I "configure" usages to be more "smart" about which references it needs to pick? Or I need to manually check if the provided reference has any Thanks in advance. |
If I have
use Type;
imported, I would like to be able to refactor that touse Type as CustomName;
(in VS Code). Then, I would like all instances that previously usedType
in that file to now useCustomName
.Is this already possible in VS Code somehow?
The text was updated successfully, but these errors were encountered: