Skip to content

Commit c9148c1

Browse files
committed
WIP function arguments
1 parent cee5693 commit c9148c1

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

crates/ra_assists/src/handlers/add_function.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use ra_syntax::{
55

66
use crate::{Assist, AssistCtx, AssistId};
77
use ast::{CallExpr, Expr};
8+
use hir::ModuleDef;
89
use ra_fmt::leading_indent;
910

1011
// Assist: add_function
@@ -72,7 +73,7 @@ fn generate_fn(ctx: &AssistCtx, call: &CallExpr) -> Option<(String, TextUnit, Te
7273
let fn_generics = fn_generics(&call)?;
7374
fn_buf.push_str(&fn_generics);
7475

75-
let fn_args = fn_args()?;
76+
let fn_args = fn_args(ctx, &call)?;
7677
fn_buf.push_str(&fn_args);
7778

7879
fn_buf.push_str(" {\n");
@@ -99,10 +100,34 @@ fn fn_generics(_call: &CallExpr) -> Option<String> {
99100
Some("".into())
100101
}
101102

102-
fn fn_args() -> Option<String> {
103+
fn fn_args(ctx: &AssistCtx, _call: &CallExpr) -> Option<String> {
104+
let arg_list = _call.syntax().descendants().find_map(ast::ArgList::cast)?;
105+
for arg in arg_list.args() {
106+
match &arg {
107+
Expr::CallExpr(call_expr) => {
108+
arg_for_call(ctx, call_expr);
109+
}
110+
_ => {}
111+
}
112+
dbg!(&arg);
113+
}
103114
Some("()".into())
104115
}
105116

117+
fn arg_for_call(ctx: &AssistCtx, call_expr: &CallExpr) -> Option<String> {
118+
let path = call_expr.syntax().descendants().find_map(ast::Path::cast)?;
119+
let type_string = match ctx.sema.resolve_path(&path)? {
120+
hir::PathResolution::Def(ModuleDef::Function(f)) => {
121+
let _ret_type = dbg!(f.ret_type(ctx.sema.db));
122+
// TODO: find out how to print this
123+
String::new()
124+
}
125+
_ => return None,
126+
};
127+
128+
Some(format!("arg: {}", type_string))
129+
}
130+
106131
/// Returns the position inside the current mod or file
107132
/// directly after the current block
108133
/// We want to write the generated function directly after

crates/ra_hir/src/code_model.rs

+4
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ impl Function {
610610
db.function_data(self.id).params.clone()
611611
}
612612

613+
pub fn ret_type(self, db: &dyn HirDatabase) -> TypeRef {
614+
db.function_data(self.id).ret_type.clone()
615+
}
616+
613617
pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
614618
let _p = profile("Function::diagnostics");
615619
let infer = db.infer(self.id.into());

0 commit comments

Comments
 (0)