-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Complete assoc. items on type parameters #4162
Complete assoc. items on type parameters #4162
Conversation
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.
Yeah, we shouldn't expose those types, I've got some suggestions how to avoid that.
crates/ra_hir/src/lib.rs
Outdated
@@ -68,8 +68,9 @@ pub use hir_def::{ | |||
nameres::ModuleSource, | |||
path::{ModPath, Path, PathKind}, | |||
type_ref::Mutability, | |||
AssocItemId, |
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.
yeah, we don't really want to expose that
crates/ra_hir/src/semantics.rs
Outdated
@@ -40,6 +41,35 @@ pub enum PathResolution { | |||
AssocItem(AssocItem), | |||
} | |||
|
|||
impl PathResolution { | |||
pub fn in_type_ns(self) -> Option<TypeNs> { |
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.
hmm I wouldn't really want to have this public -- PathResolution
is a 'simplified' type for the facade that loses the namespace information, so trying to infer the namespace again might be lossy. It'll be good enough for this purpose, but might lead to problems otherwise. (Apart from the fact that we don't want to expose TypeNs
.)
crates/ra_hir_ty/src/lower.rs
Outdated
@@ -694,6 +673,56 @@ pub fn callable_item_sig(db: &dyn HirDatabase, def: CallableDef) -> PolyFnSig { | |||
} | |||
} | |||
|
|||
pub fn associated_items<R>( |
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.
we're really only interested in associated types here, so why not make it only iterate those, and pass TypeAliasId
s?
also how about calling this iterate_associated_type_shorthand_candidates
or something like that... though that's admittedly a bit long 🤔
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.
something like that... though that's admittedly a bit long thinking
something something when writing a command line compiler, you get tyctx
, when writing an IDE, you get iterate_associated_type_shorthand_candidates
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.
😄 I did think about adding, "... but we have completion"
associated_items(ctx.db, typens, |_, _, assoc_id| { | ||
match assoc_id { | ||
AssocItemId::ConstId(c) => acc.add_const(ctx, c.into()), | ||
AssocItemId::FunctionId(f) => acc.add_function(ctx, f.into(), 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.
I don't think we should try to handle associated consts or functions with this, that's bound to get edge cases wrong. Those should be done through the normal Type
methods as above.
@@ -91,8 +93,21 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | |||
} | |||
} | |||
} | |||
Some(res @ PathResolution::TypeParam(_)) | Some(res @ PathResolution::SelfType(_)) => { | |||
if let Some(typens) = res.in_type_ns() { | |||
associated_items(ctx.db, typens, |_, _, assoc_id| { |
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.
API-wise, maybe we should just put a wrapper for associated_items
on PathResolution
. Then we don't need a public in_type_ns
method. Also the wrapper can turn the TypeAliasId
s into TypeAlias
es, which is the proper HIR facade wrapper. Finally, I think we don't even need to match here -- we can just unconditionally call that method on res
, and it'll return items when it's the right kind of resolution.
Thanks for the review, I've pushed a new version that's much cleaner. |
crates/ra_hir/src/semantics.rs
Outdated
PathResolution::Def(ModuleDef::Function(_)) => None, | ||
PathResolution::Def(ModuleDef::Module(_)) => None, | ||
PathResolution::Def(ModuleDef::Static(_)) => None, | ||
PathResolution::Def(ModuleDef::Trait(_)) => 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.
Super minor, but I'd probably do a nested match
and a bunch of |
here
crates/ra_hir/src/semantics.rs
Outdated
db: &dyn HirDatabase, | ||
mut cb: impl FnMut(TypeAlias) -> Option<R>, | ||
) -> Option<R> { | ||
if let Some(res) = self.clone().in_type_ns() { |
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.
?
let scope = ctx.scope(); | ||
let context_module = scope.module(); | ||
|
||
let res = if let Some(res) = scope.resolve_hir_path(&path) { |
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.
LGTM otherwise. bors d+ |
✌️ jonas-schievink can now approve this pull request. To approve and merge a pull request, simply reply with |
bors r+ |
Build succeeded: |
This is fairly messy and seems to leak a lot through the
ra_hir
abstraction (TypeNs
,AssocItemId
, ...), so I'd be glad for any advise for how to improve this.