};
let receiver_ty = infer_result[expr].clone();
if !ctx.is_call {
- complete_fields(acc, ctx, receiver_ty)?;
+ complete_fields(acc, ctx, receiver_ty.clone())?;
}
+ complete_methods(acc, ctx, receiver_ty)?;
Ok(())
}
Ok(())
}
+fn complete_methods(
+ acc: &mut Completions,
+ ctx: &CompletionContext,
+ receiver: Ty,
+) -> Cancelable<()> {
+ receiver.iterate_methods(ctx.db, |func| {
+ let sig = func.signature(ctx.db);
+ if sig.has_self_arg() {
+ CompletionItem::new(CompletionKind::Reference, sig.name().to_string())
+ .from_function(ctx, func)
+ .kind(CompletionItemKind::Method)
+ .add_to(acc);
+ }
+ Ok(None::<()>)
+ })?;
+ Ok(())
+}
+
#[cfg(test)]
mod tests {
use crate::completion::*;
}
}
",
- r#"the_field "(u32,)""#,
+ r#"the_field "(u32,)"
+ foo "foo($0)""#,
);
}
}
}
",
- r#"the_field "(u32, i32)""#,
+ r#"the_field "(u32, i32)"
+ foo "foo($0)""#,
);
}
r#""#,
);
}
+
+ #[test]
+ fn test_method_completion() {
+ check_ref_completion(
+ r"
+ struct A {}
+ impl A {
+ fn the_method(&self) {}
+ }
+ fn foo(a: A) {
+ a.<|>
+ }
+ ",
+ r#"the_method "the_method($0)""#,
+ );
+ }
+
+ #[test]
+ fn test_no_non_self_method() {
+ check_ref_completion(
+ r"
+ struct A {}
+ impl A {
+ fn the_method() {}
+ }
+ fn foo(a: A) {
+ a.<|>
+ }
+ ",
+ r#""#,
+ );
+ }
}
Const,
Trait,
TypeAlias,
+ Method,
}
#[derive(Debug, PartialEq, Eq)]
self
}
- fn from_function(mut self, ctx: &CompletionContext, function: hir::Function) -> Builder {
+ pub(super) fn from_function(
+ mut self,
+ ctx: &CompletionContext,
+ function: hir::Function,
+ ) -> Builder {
// If not an import, add parenthesis automatically.
if ctx.use_item_syntax.is_none() && !ctx.is_call {
if function.signature(ctx.db).args().is_empty() {
CompletionItemKind::TypeAlias => Struct,
CompletionItemKind::Const => Constant,
CompletionItemKind::Static => Value,
+ CompletionItemKind::Method => Method,
}
}
}