-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add ide-assist; Generate AsRef impl from Borrow #20065
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
base: master
Are you sure you want to change the base?
Conversation
use syntax::ast::edit_in_place::Indent; | ||
use syntax::ast::make; | ||
use syntax::ast::{self, AstNode, HasName}; | ||
use syntax::ted; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is a new assist and relatively simple, could this be implemented with new SyntaxEditor
infra?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
"Borrow" => ("AsRef", "as_ref"), | ||
"BorrowMut" => ("AsMut", "as_mut"), | ||
_ => return None, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be better semantically like:
rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs
Lines 35 to 44 in 5b852da
let impl_ = ctx.find_node_at_offset::<ast::Impl>()?; | |
let src_type = impl_.self_ty()?; | |
let ast_trait = impl_.trait_()?; | |
let module = ctx.sema.scope(impl_.syntax())?.module(); | |
let trait_ = resolve_target_trait(&ctx.sema, &impl_)?; | |
if trait_ != FamousDefs(&ctx.sema, module.krate()).core_convert_Into()? { | |
return None; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using strings can be easily implemented without adding new methods to FamousDefs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's easier but might be incorrect, like if there are other traits with the same names.
Generate
AsRef
implement fromBorrow
.->