From ff0312fa32715ce42f134fd9f049c4df5956d042 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 13:00:56 +0200 Subject: Semantical call info --- crates/ra_ide/src/call_hierarchy.rs | 10 +- crates/ra_ide/src/call_info.rs | 332 ++++++++++++------------ crates/ra_ide/src/completion/presentation.rs | 7 +- crates/ra_ide/src/display/function_signature.rs | 21 -- crates/ra_ide/src/inlay_hints.rs | 10 +- 5 files changed, 177 insertions(+), 203 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/call_hierarchy.rs b/crates/ra_ide/src/call_hierarchy.rs index cb7e62cd6..6af251d23 100644 --- a/crates/ra_ide/src/call_hierarchy.rs +++ b/crates/ra_ide/src/call_hierarchy.rs @@ -95,9 +95,9 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio if let Some(func_target) = match &call_node { FnCallNode::CallExpr(expr) => { //FIXME: Type::as_callable is broken - let callable_def = sema.type_of_expr(&expr.expr()?)?.as_callable()?; - match callable_def { - hir::CallableDefId::FunctionId(it) => { + let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?; + match callable.kind() { + hir::CallableKind::Function(it) => { let fn_def: hir::Function = it.into(); let nav = fn_def.to_nav(db); Some(nav) @@ -109,10 +109,6 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio let function = sema.resolve_method_call(&expr)?; Some(function.to_nav(db)) } - FnCallNode::MacroCallExpr(macro_call) => { - let macro_def = sema.resolve_macro_call(¯o_call)?; - Some(macro_def.to_nav(db)) - } } { Some((func_target, name_ref.syntax().text_range())) } else { diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index 1fe1c21de..a2d23b2ec 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -1,20 +1,41 @@ //! FIXME: write short doc here -use hir::Semantics; +use hir::{Docs, HirDisplay, Semantics, Type}; use ra_ide_db::RootDatabase; use ra_syntax::{ ast::{self, ArgListOwner}, - match_ast, AstNode, SyntaxNode, SyntaxToken, + match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, }; +use stdx::format_to; use test_utils::mark; -use crate::{FilePosition, FunctionSignature}; +use crate::FilePosition; /// Contains information about a call site. Specifically the /// `FunctionSignature`and current parameter. #[derive(Debug)] pub struct CallInfo { - pub signature: FunctionSignature, + pub doc: Option, + pub signature: String, pub active_parameter: Option, + parameters: Vec, +} + +impl CallInfo { + pub fn parameter_labels(&self) -> impl Iterator + '_ { + self.parameters.iter().map(move |&it| &self.signature[it]) + } + pub fn parameter_ranges(&self) -> &[TextRange] { + &self.parameters + } + fn push_param(&mut self, param: &str) { + if !self.signature.ends_with('(') { + self.signature.push_str(", "); + } + let start = TextSize::of(&self.signature); + self.signature.push_str(param); + let end = TextSize::of(&self.signature); + self.parameters.push(TextRange::new(start, end)) + } } /// Computes parameter information for the given call expression. @@ -24,105 +45,127 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option Option { - call_info(db, position)?.into_active_parameter() + let mut res = + CallInfo { doc: None, signature: String::new(), parameters: vec![], active_parameter }; + + match callable.kind() { + hir::CallableKind::Function(func) => { + res.doc = func.docs(db).map(|it| it.as_str().to_string()); + format_to!(res.signature, "fn {}", func.name(db)); + } + hir::CallableKind::TupleStruct(strukt) => { + res.doc = strukt.docs(db).map(|it| it.as_str().to_string()); + format_to!(res.signature, "struct {}", strukt.name(db)); + } + hir::CallableKind::TupleEnumVariant(variant) => { + res.doc = variant.docs(db).map(|it| it.as_str().to_string()); + format_to!( + res.signature, + "enum {}::{}", + variant.parent_enum(db).name(db), + variant.name(db) + ); + } } - pub(crate) fn at_token(sema: &Semantics, token: SyntaxToken) -> Option { - call_info_for_token(sema, token)?.into_active_parameter() + res.signature.push('('); + { + if let Some(self_param) = callable.receiver_param(db) { + format_to!(res.signature, "{}", self_param) + } + let mut buf = String::new(); + for (pat, ty) in callable.params(db) { + buf.clear(); + if let Some(pat) = pat { + format_to!(buf, "{}: ", pat); + } + format_to!(buf, "{}", ty.display(db)); + res.push_param(&buf); + } } + res.signature.push(')'); + + match callable.kind() { + hir::CallableKind::Function(_) => { + let ret_type = callable.return_type(); + if !ret_type.is_unit() { + format_to!(res.signature, " -> {}", ret_type.display(db)); + } + } + hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_) => {} + } + Some(res) } -fn call_info_for_token(sema: &Semantics, token: SyntaxToken) -> Option { +fn call_info_impl( + sema: &Semantics, + token: SyntaxToken, +) -> Option<(hir::Callable, Option)> { // Find the calling expression and it's NameRef let calling_node = FnCallNode::with_node(&token.parent())?; - let signature = match &calling_node { - FnCallNode::CallExpr(call) => { - //FIXME: Type::as_callable is broken - let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?; - match callable_def { - hir::CallableDefId::FunctionId(it) => { - let fn_def = it.into(); - FunctionSignature::from_hir(sema.db, fn_def) - } - hir::CallableDefId::StructId(it) => { - FunctionSignature::from_struct(sema.db, it.into())? - } - hir::CallableDefId::EnumVariantId(it) => { - FunctionSignature::from_enum_variant(sema.db, it.into())? - } - } - } - FnCallNode::MethodCallExpr(method_call) => { - let function = sema.resolve_method_call(&method_call)?; - FunctionSignature::from_hir(sema.db, function) - } - FnCallNode::MacroCallExpr(macro_call) => { - let macro_def = sema.resolve_macro_call(¯o_call)?; - FunctionSignature::from_macro(sema.db, macro_def)? + let callable = match &calling_node { + FnCallNode::CallExpr(call) => sema.type_of_expr(&call.expr()?)?.as_callable(sema.db)?, + FnCallNode::MethodCallExpr(call) => sema.resolve_method_call_as_callable(call)?, + }; + let active_param = if let Some(arg_list) = calling_node.arg_list() { + // Number of arguments specified at the call site + let num_args_at_callsite = arg_list.args().count(); + + let arg_list_range = arg_list.syntax().text_range(); + if !arg_list_range.contains_inclusive(token.text_range().start()) { + mark::hit!(call_info_bad_offset); + return None; } + let param = std::cmp::min( + num_args_at_callsite, + arg_list + .args() + .take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start()) + .count(), + ); + + Some(param) + } else { + None }; + Some((callable, active_param)) +} - // If we have a calling expression let's find which argument we are on - let num_params = signature.parameters.len(); - - let active_parameter = match num_params { - 0 => None, - 1 if signature.has_self_param => None, - 1 => Some(0), - _ => { - if let Some(arg_list) = calling_node.arg_list() { - // Number of arguments specified at the call site - let num_args_at_callsite = arg_list.args().count(); - - let arg_list_range = arg_list.syntax().text_range(); - if !arg_list_range.contains_inclusive(token.text_range().start()) { - mark::hit!(call_info_bad_offset); - return None; - } +#[derive(Debug)] +pub(crate) struct ActiveParameter { + pub(crate) ty: Type, + pub(crate) name: String, +} - let mut param = std::cmp::min( - num_args_at_callsite, - arg_list - .args() - .take_while(|arg| { - arg.syntax().text_range().end() <= token.text_range().start() - }) - .count(), - ); - - // If we are in a method account for `self` - if signature.has_self_param { - param += 1; - } +impl ActiveParameter { + pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option { + let sema = Semantics::new(db); + let file = sema.parse(position.file_id); + let file = file.syntax(); + let token = file.token_at_offset(position.offset).next()?; + let token = sema.descend_into_macros(token); + Self::at_token(&sema, token) + } - Some(param) - } else { - None - } - } - }; + pub(crate) fn at_token(sema: &Semantics, token: SyntaxToken) -> Option { + let (signature, active_parameter) = call_info_impl(&sema, token)?; - Some(CallInfo { signature, active_parameter }) + let idx = active_parameter?; + let mut params = signature.params(sema.db); + let (pat, ty) = params.swap_remove(idx); + let name = pat?.to_string(); + Some(ActiveParameter { ty, name }) + } } #[derive(Debug)] pub(crate) enum FnCallNode { CallExpr(ast::CallExpr), MethodCallExpr(ast::MethodCallExpr), - MacroCallExpr(ast::MacroCall), } impl FnCallNode { @@ -138,7 +181,6 @@ impl FnCallNode { } Some(FnCallNode::MethodCallExpr(it)) }, - ast::MacroCall(it) => Some(FnCallNode::MacroCallExpr(it)), _ => None, } } @@ -150,7 +192,6 @@ impl FnCallNode { match node { ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)), ast::MethodCallExpr(it) => Some(FnCallNode::MethodCallExpr(it)), - ast::MacroCall(it) => Some(FnCallNode::MacroCallExpr(it)), _ => None, } } @@ -166,8 +207,6 @@ impl FnCallNode { FnCallNode::MethodCallExpr(call_expr) => { call_expr.syntax().children().filter_map(ast::NameRef::cast).next() } - - FnCallNode::MacroCallExpr(call_expr) => call_expr.path()?.segment()?.name_ref(), } } @@ -175,21 +214,10 @@ impl FnCallNode { match self { FnCallNode::CallExpr(expr) => expr.arg_list(), FnCallNode::MethodCallExpr(expr) => expr.arg_list(), - FnCallNode::MacroCallExpr(_) => None, } } } -impl CallInfo { - fn into_active_parameter(self) -> Option { - let idx = self.active_parameter?; - let ty = self.signature.parameter_types.get(idx)?.clone(); - let name = self.signature.parameter_names.get(idx)?.clone(); - let res = ActiveParameter { ty, name }; - Some(res) - } -} - #[cfg(test)] mod tests { use expect::{expect, Expect}; @@ -202,20 +230,18 @@ mod tests { let call_info = analysis.call_info(position).unwrap(); let actual = match call_info { Some(call_info) => { - let docs = match &call_info.signature.doc { + let docs = match &call_info.doc { None => "".to_string(), Some(docs) => format!("{}\n------\n", docs.as_str()), }; let params = call_info - .signature - .parameters - .iter() + .parameter_labels() .enumerate() .map(|(i, param)| { if Some(i) == call_info.active_parameter { format!("<{}>", param) } else { - param.clone() + param.to_string() } }) .collect::>() @@ -296,10 +322,8 @@ fn foo(x: T, y: U) -> u32 fn bar() { foo(<|>3, ); } "#, expect![[r#" - fn foo(x: T, y: U) -> u32 - where T: Copy + Display, - U: Debug - (, y: U) + fn foo(x: i32, y: {unknown}) -> u32 + (, y: {unknown}) "#]], ); } @@ -312,8 +336,7 @@ fn foo() -> T where T: Copy + Display {} fn bar() { foo(<|>); } "#, expect![[r#" - fn foo() -> T - where T: Copy + Display + fn foo() -> {unknown} () "#]], ); @@ -323,11 +346,14 @@ fn bar() { foo(<|>); } fn test_fn_signature_for_impl() { check( r#" -struct F; impl F { pub fn new() { F{}} } -fn bar() {let _ : F = F::new(<|>);} +struct F; +impl F { pub fn new() { } } +fn bar() { + let _ : F = F::new(<|>); +} "#, expect![[r#" - pub fn new() + fn new() () "#]], ); @@ -346,8 +372,8 @@ fn bar() { } "#, expect![[r#" - pub fn do_it(&self) - (&self) + fn do_it(&self) + () "#]], ); } @@ -365,8 +391,8 @@ fn bar() { } "#, expect![[r#" - pub fn do_it(&self, x: i32) - (&self, ) + fn do_it(&self, x: i32) + () "#]], ); } @@ -425,7 +451,7 @@ pub fn do() { assert_eq!(6, my_crate::add_one(5)); ``` ------ - pub fn add_one(x: i32) -> i32 + fn add_one(x: i32) -> i32 () "##]], ); @@ -467,7 +493,7 @@ pub fn do_it() { assert_eq!(6, my_crate::add_one(5)); ``` ------ - pub fn add_one(x: i32) -> i32 + fn add_one(x: i32) -> i32 () "##]], ); @@ -505,8 +531,8 @@ pub fn foo(mut r: WriteHandler<()>) { By default this method stops actor's `Context`. ------ - fn finished(&mut self, ctx: &mut Self::Context) - (&mut self, ) + fn finished(&mut self, ctx: &mut {unknown}) + () "#]], ); } @@ -539,7 +565,7 @@ fn main() { "#, expect![[r#" fn bar(&self, _: u32) - (&self, <_: u32>) + (<_: u32>) "#]], ); } @@ -549,15 +575,15 @@ fn main() { check( r#" /// A cool tuple struct -struct TS(u32, i32); +struct S(u32, i32); fn main() { - let s = TS(0, <|>); + let s = S(0, <|>); } "#, expect![[r#" A cool tuple struct ------ - struct TS(u32, i32) -> TS + struct S(u32, i32) (u32, ) "#]], ); @@ -567,31 +593,18 @@ fn main() { fn generic_struct() { check( r#" -struct TS(T); +struct S(T); fn main() { - let s = TS(<|>); + let s = S(<|>); } "#, expect![[r#" - struct TS(T) -> TS - () + struct S({unknown}) + (<{unknown}>) "#]], ); } - #[test] - fn cant_call_named_structs() { - check( - r#" -struct TS { x: u32, y: i32 } -fn main() { - let s = TS(<|>); -} -"#, - expect![[""]], - ); - } - #[test] fn works_for_enum_variants() { check( @@ -612,27 +625,19 @@ fn main() { expect![[r#" A Variant ------ - E::A(0: i32) - (<0: i32>) + enum E::A(i32) + () "#]], ); } #[test] - fn cant_call_enum_records() { + fn cant_call_struct_record() { check( r#" -enum E { - /// A Variant - A(i32), - /// Another - B, - /// And C - C { a: i32, b: i32 } -} - +struct S { x: u32, y: i32 } fn main() { - let a = E::C(<|>); + let s = S(<|>); } "#, expect![[""]], @@ -640,24 +645,23 @@ fn main() { } #[test] - fn fn_signature_for_macro() { + fn cant_call_enum_record() { check( r#" -/// empty macro -macro_rules! foo { - () => {} +enum E { + /// A Variant + A(i32), + /// Another + B, + /// And C + C { a: i32, b: i32 } } -fn f() { - foo!(<|>); +fn main() { + let a = E::C(<|>); } "#, - expect![[r#" - empty macro - ------ - foo!() - () - "#]], + expect![[""]], ); } diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 64349dcb8..e6b4737aa 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -329,15 +329,10 @@ pub(crate) fn compute_score( ty: &Type, name: &str, ) -> Option { - // FIXME: this should not fall back to string equality. - let ty = &ty.display(ctx.db).to_string(); let (active_name, active_type) = if let Some(record_field) = &ctx.record_field_syntax { mark::hit!(record_field_type_match); let (struct_field, _local) = ctx.sema.resolve_record_field(record_field)?; - ( - struct_field.name(ctx.db).to_string(), - struct_field.signature_ty(ctx.db).display(ctx.db).to_string(), - ) + (struct_field.name(ctx.db).to_string(), struct_field.signature_ty(ctx.db)) } else if let Some(active_parameter) = &ctx.active_parameter { mark::hit!(active_param_type_match); (active_parameter.name.clone(), active_parameter.ty.clone()) diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index 1d39544d3..709a85f65 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -149,27 +149,6 @@ impl FunctionSignature { has_self_param: false, }) } - - pub(crate) fn from_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option { - let node: ast::MacroCall = macro_def.source(db).value; - - let params = vec![]; - - Some(FunctionSignature { - kind: CallableKind::Macro, - visibility: None, - qualifier: Default::default(), - name: node.name().map(|n| n.text().to_string()), - ret_type: None, - parameters: params, - parameter_names: vec![], - parameter_types: vec![], - generic_parameters: vec![], - where_predicates: vec![], - doc: macro_def.docs(db), - has_self_param: false, - }) - } } impl From<&'_ ast::FnDef> for FunctionSignature { diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 3cbae8a45..2e021f032 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -322,15 +322,15 @@ fn get_fn_signature(sema: &Semantics, expr: &ast::Expr) -> Option< match expr { ast::Expr::CallExpr(expr) => { // FIXME: Type::as_callable is broken for closures - let callable_def = sema.type_of_expr(&expr.expr()?)?.as_callable()?; - match callable_def { - hir::CallableDefId::FunctionId(it) => { + let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(sema.db)?; + match callable.kind() { + hir::CallableKind::Function(it) => { Some(FunctionSignature::from_hir(sema.db, it.into())) } - hir::CallableDefId::StructId(it) => { + hir::CallableKind::TupleStruct(it) => { FunctionSignature::from_struct(sema.db, it.into()) } - hir::CallableDefId::EnumVariantId(it) => { + hir::CallableKind::TupleEnumVariant(it) => { FunctionSignature::from_enum_variant(sema.db, it.into()) } } -- cgit v1.2.3 From 29832b8c3db509dea8da9164e980ccac4bccf47d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 18:07:53 +0200 Subject: Reduce visibility --- .../ra_ide/src/completion/complete_trait_impl.rs | 2 +- crates/ra_ide/src/completion/presentation.rs | 2 +- crates/ra_ide/src/display.rs | 5 ++- crates/ra_ide/src/display/function_signature.rs | 40 ++++++++++------------ crates/ra_ide/src/inlay_hints.rs | 4 +-- crates/ra_ide/src/lib.rs | 2 +- 6 files changed, 26 insertions(+), 29 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index a610fd6d1..90f5b1c25 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -43,7 +43,7 @@ use crate::{ completion::{ CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, }, - display::FunctionSignature, + display::function_signature::FunctionSignature, }; pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index e6b4737aa..e29b82017 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -11,7 +11,7 @@ use crate::{ completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, }, - display::{const_label, macro_label, type_label, FunctionSignature}, + display::{const_label, function_signature::FunctionSignature, macro_label, type_label}, CompletionScore, RootDatabase, }; diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index 70d2a2dd1..b59d4bbdf 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs @@ -1,7 +1,7 @@ //! This module contains utilities for turning SyntaxNodes and HIR types //! into types that may be used to render in a UI. -mod function_signature; +pub(crate) mod function_signature; mod navigation_target; mod structure; mod short_label; @@ -11,7 +11,6 @@ use ra_syntax::{ SyntaxKind::{ATTR, COMMENT}, }; -pub use function_signature::FunctionSignature; pub use navigation_target::NavigationTarget; pub use structure::{file_structure, StructureNode}; @@ -19,7 +18,7 @@ pub(crate) use navigation_target::{ToNav, TryToNav}; pub(crate) use short_label::ShortLabel; pub(crate) fn function_label(node: &ast::FnDef) -> String { - FunctionSignature::from(node).to_string() + function_signature::FunctionSignature::from(node).to_string() } pub(crate) fn const_label(node: &ast::ConstDef) -> String { diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index 709a85f65..9b7220d1f 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -15,49 +15,48 @@ use stdx::{split_delim, SepBy}; use crate::display::{generic_parameters, where_predicates}; #[derive(Debug)] -pub enum CallableKind { +pub(crate) enum CallableKind { Function, StructConstructor, VariantConstructor, - Macro, } /// Contains information about a function signature #[derive(Debug)] -pub struct FunctionSignature { - pub kind: CallableKind, +pub(crate) struct FunctionSignature { + pub(crate) kind: CallableKind, /// Optional visibility - pub visibility: Option, + pub(crate) visibility: Option, /// Qualifiers like `async`, `unsafe`, ... - pub qualifier: FunctionQualifier, + pub(crate) qualifier: FunctionQualifier, /// Name of the function - pub name: Option, + pub(crate) name: Option, /// Documentation for the function - pub doc: Option, + pub(crate) doc: Option, /// Generic parameters - pub generic_parameters: Vec, + pub(crate) generic_parameters: Vec, /// Parameters of the function - pub parameters: Vec, + pub(crate) parameters: Vec, /// Parameter names of the function - pub parameter_names: Vec, + pub(crate) parameter_names: Vec, /// Parameter types of the function - pub parameter_types: Vec, + pub(crate) parameter_types: Vec, /// Optional return type - pub ret_type: Option, + pub(crate) ret_type: Option, /// Where predicates - pub where_predicates: Vec, + pub(crate) where_predicates: Vec, /// Self param presence - pub has_self_param: bool, + pub(crate) has_self_param: bool, } #[derive(Debug, Default)] -pub struct FunctionQualifier { +pub(crate) struct FunctionQualifier { // `async` and `const` are mutually exclusive. Do we need to enforcing it here? - pub is_async: bool, - pub is_const: bool, - pub is_unsafe: bool, + pub(crate) is_async: bool, + pub(crate) is_const: bool, + pub(crate) is_unsafe: bool, /// The string `extern ".."` - pub extern_abi: Option, + pub(crate) extern_abi: Option, } impl FunctionSignature { @@ -277,7 +276,6 @@ impl Display for FunctionSignature { CallableKind::Function => write!(f, "fn {}", name)?, CallableKind::StructConstructor => write!(f, "struct {}", name)?, CallableKind::VariantConstructor => write!(f, "{}", name)?, - CallableKind::Macro => write!(f, "{}!", name)?, } } diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 2e021f032..ae5695f61 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -5,10 +5,10 @@ use ra_syntax::{ ast::{self, ArgListOwner, AstNode, TypeAscriptionOwner}, match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T, }; - -use crate::{FileId, FunctionSignature}; use stdx::to_lower_snake_case; +use crate::{display::function_signature::FunctionSignature, FileId}; + #[derive(Clone, Debug, PartialEq, Eq)] pub struct InlayHintsConfig { pub type_hints: bool, diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 5d1f64e19..6810c1c6a 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -65,7 +65,7 @@ pub use crate::{ CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat, }, diagnostics::Severity, - display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, + display::{file_structure, NavigationTarget, StructureNode}, expand_macro::ExpandedMacro, folding_ranges::{Fold, FoldKind}, hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult}, -- cgit v1.2.3 From a4e9681c79095d6c10a851cfefe64cf1a3570ec5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 18:13:43 +0200 Subject: Better module structure --- crates/ra_ide/src/display.rs | 6 +- crates/ra_ide/src/display/structure.rs | 441 --------------------------------- crates/ra_ide/src/file_structure.rs | 441 +++++++++++++++++++++++++++++++++ crates/ra_ide/src/lib.rs | 37 +-- 4 files changed, 463 insertions(+), 462 deletions(-) delete mode 100644 crates/ra_ide/src/display/structure.rs create mode 100644 crates/ra_ide/src/file_structure.rs (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index b59d4bbdf..1ec946369 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs @@ -3,7 +3,6 @@ pub(crate) mod function_signature; mod navigation_target; -mod structure; mod short_label; use ra_syntax::{ @@ -11,12 +10,11 @@ use ra_syntax::{ SyntaxKind::{ATTR, COMMENT}, }; -pub use navigation_target::NavigationTarget; -pub use structure::{file_structure, StructureNode}; - pub(crate) use navigation_target::{ToNav, TryToNav}; pub(crate) use short_label::ShortLabel; +pub use navigation_target::NavigationTarget; + pub(crate) fn function_label(node: &ast::FnDef) -> String { function_signature::FunctionSignature::from(node).to_string() } diff --git a/crates/ra_ide/src/display/structure.rs b/crates/ra_ide/src/display/structure.rs deleted file mode 100644 index 1f6a3febf..000000000 --- a/crates/ra_ide/src/display/structure.rs +++ /dev/null @@ -1,441 +0,0 @@ -use ra_syntax::{ - ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, - match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, WalkEvent, -}; - -#[derive(Debug, Clone)] -pub struct StructureNode { - pub parent: Option, - pub label: String, - pub navigation_range: TextRange, - pub node_range: TextRange, - pub kind: SyntaxKind, - pub detail: Option, - pub deprecated: bool, -} - -// Feature: File Structure -// -// Provides a tree of the symbols defined in the file. Can be used to -// -// * fuzzy search symbol in a file (super useful) -// * draw breadcrumbs to describe the context around the cursor -// * draw outline of the file -// -// |=== -// | Editor | Shortcut -// -// | VS Code | kbd:[Ctrl+Shift+O] -// |=== -pub fn file_structure(file: &SourceFile) -> Vec { - let mut res = Vec::new(); - let mut stack = Vec::new(); - - for event in file.syntax().preorder() { - match event { - WalkEvent::Enter(node) => { - if let Some(mut symbol) = structure_node(&node) { - symbol.parent = stack.last().copied(); - stack.push(res.len()); - res.push(symbol); - } - } - WalkEvent::Leave(node) => { - if structure_node(&node).is_some() { - stack.pop().unwrap(); - } - } - } - } - res -} - -fn structure_node(node: &SyntaxNode) -> Option { - fn decl(node: N) -> Option { - decl_with_detail(node, None) - } - - fn decl_with_ascription( - node: N, - ) -> Option { - let ty = node.ascribed_type(); - decl_with_type_ref(node, ty) - } - - fn decl_with_type_ref( - node: N, - type_ref: Option, - ) -> Option { - let detail = type_ref.map(|type_ref| { - let mut detail = String::new(); - collapse_ws(type_ref.syntax(), &mut detail); - detail - }); - decl_with_detail(node, detail) - } - - fn decl_with_detail( - node: N, - detail: Option, - ) -> Option { - let name = node.name()?; - - Some(StructureNode { - parent: None, - label: name.text().to_string(), - navigation_range: name.syntax().text_range(), - node_range: node.syntax().text_range(), - kind: node.syntax().kind(), - detail, - deprecated: node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated"), - }) - } - - fn collapse_ws(node: &SyntaxNode, output: &mut String) { - let mut can_insert_ws = false; - node.text().for_each_chunk(|chunk| { - for line in chunk.lines() { - let line = line.trim(); - if line.is_empty() { - if can_insert_ws { - output.push(' '); - can_insert_ws = false; - } - } else { - output.push_str(line); - can_insert_ws = true; - } - } - }) - } - - match_ast! { - match node { - ast::FnDef(it) => { - let mut detail = String::from("fn"); - if let Some(type_param_list) = it.type_param_list() { - collapse_ws(type_param_list.syntax(), &mut detail); - } - if let Some(param_list) = it.param_list() { - collapse_ws(param_list.syntax(), &mut detail); - } - if let Some(ret_type) = it.ret_type() { - detail.push_str(" "); - collapse_ws(ret_type.syntax(), &mut detail); - } - - decl_with_detail(it, Some(detail)) - }, - ast::StructDef(it) => decl(it), - ast::UnionDef(it) => decl(it), - ast::EnumDef(it) => decl(it), - ast::EnumVariant(it) => decl(it), - ast::TraitDef(it) => decl(it), - ast::Module(it) => decl(it), - ast::TypeAliasDef(it) => { - let ty = it.type_ref(); - decl_with_type_ref(it, ty) - }, - ast::RecordFieldDef(it) => decl_with_ascription(it), - ast::ConstDef(it) => decl_with_ascription(it), - ast::StaticDef(it) => decl_with_ascription(it), - ast::ImplDef(it) => { - let target_type = it.target_type()?; - let target_trait = it.target_trait(); - let label = match target_trait { - None => format!("impl {}", target_type.syntax().text()), - Some(t) => { - format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),) - } - }; - - let node = StructureNode { - parent: None, - label, - navigation_range: target_type.syntax().text_range(), - node_range: it.syntax().text_range(), - kind: it.syntax().kind(), - detail: None, - deprecated: false, - }; - Some(node) - }, - ast::MacroCall(it) => { - match it.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) { - Some(path_segment) if path_segment.text() == "macro_rules" - => decl(it), - _ => None, - } - }, - _ => None, - } - } -} - -#[cfg(test)] -mod tests { - use expect::{expect, Expect}; - - use super::*; - - fn check(ra_fixture: &str, expect: Expect) { - let file = SourceFile::parse(ra_fixture).ok().unwrap(); - let structure = file_structure(&file); - expect.assert_debug_eq(&structure) - } - - #[test] - fn test_file_structure() { - check( - r#" -struct Foo { - x: i32 -} - -mod m { - fn bar1() {} - fn bar2(t: T) -> T {} - fn bar3(a: A, - b: B) -> Vec< - u32 - > {} -} - -enum E { X, Y(i32) } -type T = (); -static S: i32 = 92; -const C: i32 = 92; - -impl E {} - -impl fmt::Debug for E {} - -macro_rules! mc { - () => {} -} - -#[macro_export] -macro_rules! mcexp { - () => {} -} - -/// Doc comment -macro_rules! mcexp { - () => {} -} - -#[deprecated] -fn obsolete() {} - -#[deprecated(note = "for awhile")] -fn very_obsolete() {} -"#, - expect![[r#" - [ - StructureNode { - parent: None, - label: "Foo", - navigation_range: 8..11, - node_range: 1..26, - kind: STRUCT_DEF, - detail: None, - deprecated: false, - }, - StructureNode { - parent: Some( - 0, - ), - label: "x", - navigation_range: 18..19, - node_range: 18..24, - kind: RECORD_FIELD_DEF, - detail: Some( - "i32", - ), - deprecated: false, - }, - StructureNode { - parent: None, - label: "m", - navigation_range: 32..33, - node_range: 28..158, - kind: MODULE, - detail: None, - deprecated: false, - }, - StructureNode { - parent: Some( - 2, - ), - label: "bar1", - navigation_range: 43..47, - node_range: 40..52, - kind: FN_DEF, - detail: Some( - "fn()", - ), - deprecated: false, - }, - StructureNode { - parent: Some( - 2, - ), - label: "bar2", - navigation_range: 60..64, - node_range: 57..81, - kind: FN_DEF, - detail: Some( - "fn(t: T) -> T", - ), - deprecated: false, - }, - StructureNode { - parent: Some( - 2, - ), - label: "bar3", - navigation_range: 89..93, - node_range: 86..156, - kind: FN_DEF, - detail: Some( - "fn(a: A, b: B) -> Vec< u32 >", - ), - deprecated: false, - }, - StructureNode { - parent: None, - label: "E", - navigation_range: 165..166, - node_range: 160..180, - kind: ENUM_DEF, - detail: None, - deprecated: false, - }, - StructureNode { - parent: Some( - 6, - ), - label: "X", - navigation_range: 169..170, - node_range: 169..170, - kind: ENUM_VARIANT, - detail: None, - deprecated: false, - }, - StructureNode { - parent: Some( - 6, - ), - label: "Y", - navigation_range: 172..173, - node_range: 172..178, - kind: ENUM_VARIANT, - detail: None, - deprecated: false, - }, - StructureNode { - parent: None, - label: "T", - navigation_range: 186..187, - node_range: 181..193, - kind: TYPE_ALIAS_DEF, - detail: Some( - "()", - ), - deprecated: false, - }, - StructureNode { - parent: None, - label: "S", - navigation_range: 201..202, - node_range: 194..213, - kind: STATIC_DEF, - detail: Some( - "i32", - ), - deprecated: false, - }, - StructureNode { - parent: None, - label: "C", - navigation_range: 220..221, - node_range: 214..232, - kind: CONST_DEF, - detail: Some( - "i32", - ), - deprecated: false, - }, - StructureNode { - parent: None, - label: "impl E", - navigation_range: 239..240, - node_range: 234..243, - kind: IMPL_DEF, - detail: None, - deprecated: false, - }, - StructureNode { - parent: None, - label: "impl fmt::Debug for E", - navigation_range: 265..266, - node_range: 245..269, - kind: IMPL_DEF, - detail: None, - deprecated: false, - }, - StructureNode { - parent: None, - label: "mc", - navigation_range: 284..286, - node_range: 271..303, - kind: MACRO_CALL, - detail: None, - deprecated: false, - }, - StructureNode { - parent: None, - label: "mcexp", - navigation_range: 334..339, - node_range: 305..356, - kind: MACRO_CALL, - detail: None, - deprecated: false, - }, - StructureNode { - parent: None, - label: "mcexp", - navigation_range: 387..392, - node_range: 358..409, - kind: MACRO_CALL, - detail: None, - deprecated: false, - }, - StructureNode { - parent: None, - label: "obsolete", - navigation_range: 428..436, - node_range: 411..441, - kind: FN_DEF, - detail: Some( - "fn()", - ), - deprecated: true, - }, - StructureNode { - parent: None, - label: "very_obsolete", - navigation_range: 481..494, - node_range: 443..499, - kind: FN_DEF, - detail: Some( - "fn()", - ), - deprecated: true, - }, - ] - "#]], - ); - } -} diff --git a/crates/ra_ide/src/file_structure.rs b/crates/ra_ide/src/file_structure.rs new file mode 100644 index 000000000..1f6a3febf --- /dev/null +++ b/crates/ra_ide/src/file_structure.rs @@ -0,0 +1,441 @@ +use ra_syntax::{ + ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, + match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, WalkEvent, +}; + +#[derive(Debug, Clone)] +pub struct StructureNode { + pub parent: Option, + pub label: String, + pub navigation_range: TextRange, + pub node_range: TextRange, + pub kind: SyntaxKind, + pub detail: Option, + pub deprecated: bool, +} + +// Feature: File Structure +// +// Provides a tree of the symbols defined in the file. Can be used to +// +// * fuzzy search symbol in a file (super useful) +// * draw breadcrumbs to describe the context around the cursor +// * draw outline of the file +// +// |=== +// | Editor | Shortcut +// +// | VS Code | kbd:[Ctrl+Shift+O] +// |=== +pub fn file_structure(file: &SourceFile) -> Vec { + let mut res = Vec::new(); + let mut stack = Vec::new(); + + for event in file.syntax().preorder() { + match event { + WalkEvent::Enter(node) => { + if let Some(mut symbol) = structure_node(&node) { + symbol.parent = stack.last().copied(); + stack.push(res.len()); + res.push(symbol); + } + } + WalkEvent::Leave(node) => { + if structure_node(&node).is_some() { + stack.pop().unwrap(); + } + } + } + } + res +} + +fn structure_node(node: &SyntaxNode) -> Option { + fn decl(node: N) -> Option { + decl_with_detail(node, None) + } + + fn decl_with_ascription( + node: N, + ) -> Option { + let ty = node.ascribed_type(); + decl_with_type_ref(node, ty) + } + + fn decl_with_type_ref( + node: N, + type_ref: Option, + ) -> Option { + let detail = type_ref.map(|type_ref| { + let mut detail = String::new(); + collapse_ws(type_ref.syntax(), &mut detail); + detail + }); + decl_with_detail(node, detail) + } + + fn decl_with_detail( + node: N, + detail: Option, + ) -> Option { + let name = node.name()?; + + Some(StructureNode { + parent: None, + label: name.text().to_string(), + navigation_range: name.syntax().text_range(), + node_range: node.syntax().text_range(), + kind: node.syntax().kind(), + detail, + deprecated: node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated"), + }) + } + + fn collapse_ws(node: &SyntaxNode, output: &mut String) { + let mut can_insert_ws = false; + node.text().for_each_chunk(|chunk| { + for line in chunk.lines() { + let line = line.trim(); + if line.is_empty() { + if can_insert_ws { + output.push(' '); + can_insert_ws = false; + } + } else { + output.push_str(line); + can_insert_ws = true; + } + } + }) + } + + match_ast! { + match node { + ast::FnDef(it) => { + let mut detail = String::from("fn"); + if let Some(type_param_list) = it.type_param_list() { + collapse_ws(type_param_list.syntax(), &mut detail); + } + if let Some(param_list) = it.param_list() { + collapse_ws(param_list.syntax(), &mut detail); + } + if let Some(ret_type) = it.ret_type() { + detail.push_str(" "); + collapse_ws(ret_type.syntax(), &mut detail); + } + + decl_with_detail(it, Some(detail)) + }, + ast::StructDef(it) => decl(it), + ast::UnionDef(it) => decl(it), + ast::EnumDef(it) => decl(it), + ast::EnumVariant(it) => decl(it), + ast::TraitDef(it) => decl(it), + ast::Module(it) => decl(it), + ast::TypeAliasDef(it) => { + let ty = it.type_ref(); + decl_with_type_ref(it, ty) + }, + ast::RecordFieldDef(it) => decl_with_ascription(it), + ast::ConstDef(it) => decl_with_ascription(it), + ast::StaticDef(it) => decl_with_ascription(it), + ast::ImplDef(it) => { + let target_type = it.target_type()?; + let target_trait = it.target_trait(); + let label = match target_trait { + None => format!("impl {}", target_type.syntax().text()), + Some(t) => { + format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),) + } + }; + + let node = StructureNode { + parent: None, + label, + navigation_range: target_type.syntax().text_range(), + node_range: it.syntax().text_range(), + kind: it.syntax().kind(), + detail: None, + deprecated: false, + }; + Some(node) + }, + ast::MacroCall(it) => { + match it.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) { + Some(path_segment) if path_segment.text() == "macro_rules" + => decl(it), + _ => None, + } + }, + _ => None, + } + } +} + +#[cfg(test)] +mod tests { + use expect::{expect, Expect}; + + use super::*; + + fn check(ra_fixture: &str, expect: Expect) { + let file = SourceFile::parse(ra_fixture).ok().unwrap(); + let structure = file_structure(&file); + expect.assert_debug_eq(&structure) + } + + #[test] + fn test_file_structure() { + check( + r#" +struct Foo { + x: i32 +} + +mod m { + fn bar1() {} + fn bar2(t: T) -> T {} + fn bar3(a: A, + b: B) -> Vec< + u32 + > {} +} + +enum E { X, Y(i32) } +type T = (); +static S: i32 = 92; +const C: i32 = 92; + +impl E {} + +impl fmt::Debug for E {} + +macro_rules! mc { + () => {} +} + +#[macro_export] +macro_rules! mcexp { + () => {} +} + +/// Doc comment +macro_rules! mcexp { + () => {} +} + +#[deprecated] +fn obsolete() {} + +#[deprecated(note = "for awhile")] +fn very_obsolete() {} +"#, + expect![[r#" + [ + StructureNode { + parent: None, + label: "Foo", + navigation_range: 8..11, + node_range: 1..26, + kind: STRUCT_DEF, + detail: None, + deprecated: false, + }, + StructureNode { + parent: Some( + 0, + ), + label: "x", + navigation_range: 18..19, + node_range: 18..24, + kind: RECORD_FIELD_DEF, + detail: Some( + "i32", + ), + deprecated: false, + }, + StructureNode { + parent: None, + label: "m", + navigation_range: 32..33, + node_range: 28..158, + kind: MODULE, + detail: None, + deprecated: false, + }, + StructureNode { + parent: Some( + 2, + ), + label: "bar1", + navigation_range: 43..47, + node_range: 40..52, + kind: FN_DEF, + detail: Some( + "fn()", + ), + deprecated: false, + }, + StructureNode { + parent: Some( + 2, + ), + label: "bar2", + navigation_range: 60..64, + node_range: 57..81, + kind: FN_DEF, + detail: Some( + "fn(t: T) -> T", + ), + deprecated: false, + }, + StructureNode { + parent: Some( + 2, + ), + label: "bar3", + navigation_range: 89..93, + node_range: 86..156, + kind: FN_DEF, + detail: Some( + "fn(a: A, b: B) -> Vec< u32 >", + ), + deprecated: false, + }, + StructureNode { + parent: None, + label: "E", + navigation_range: 165..166, + node_range: 160..180, + kind: ENUM_DEF, + detail: None, + deprecated: false, + }, + StructureNode { + parent: Some( + 6, + ), + label: "X", + navigation_range: 169..170, + node_range: 169..170, + kind: ENUM_VARIANT, + detail: None, + deprecated: false, + }, + StructureNode { + parent: Some( + 6, + ), + label: "Y", + navigation_range: 172..173, + node_range: 172..178, + kind: ENUM_VARIANT, + detail: None, + deprecated: false, + }, + StructureNode { + parent: None, + label: "T", + navigation_range: 186..187, + node_range: 181..193, + kind: TYPE_ALIAS_DEF, + detail: Some( + "()", + ), + deprecated: false, + }, + StructureNode { + parent: None, + label: "S", + navigation_range: 201..202, + node_range: 194..213, + kind: STATIC_DEF, + detail: Some( + "i32", + ), + deprecated: false, + }, + StructureNode { + parent: None, + label: "C", + navigation_range: 220..221, + node_range: 214..232, + kind: CONST_DEF, + detail: Some( + "i32", + ), + deprecated: false, + }, + StructureNode { + parent: None, + label: "impl E", + navigation_range: 239..240, + node_range: 234..243, + kind: IMPL_DEF, + detail: None, + deprecated: false, + }, + StructureNode { + parent: None, + label: "impl fmt::Debug for E", + navigation_range: 265..266, + node_range: 245..269, + kind: IMPL_DEF, + detail: None, + deprecated: false, + }, + StructureNode { + parent: None, + label: "mc", + navigation_range: 284..286, + node_range: 271..303, + kind: MACRO_CALL, + detail: None, + deprecated: false, + }, + StructureNode { + parent: None, + label: "mcexp", + navigation_range: 334..339, + node_range: 305..356, + kind: MACRO_CALL, + detail: None, + deprecated: false, + }, + StructureNode { + parent: None, + label: "mcexp", + navigation_range: 387..392, + node_range: 358..409, + kind: MACRO_CALL, + detail: None, + deprecated: false, + }, + StructureNode { + parent: None, + label: "obsolete", + navigation_range: 428..436, + node_range: 411..441, + kind: FN_DEF, + detail: Some( + "fn()", + ), + deprecated: true, + }, + StructureNode { + parent: None, + label: "very_obsolete", + navigation_range: 481..494, + node_range: 443..499, + kind: FN_DEF, + detail: Some( + "fn()", + ), + deprecated: true, + }, + ] + "#]], + ); + } +} diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 6810c1c6a..d3b20f371 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -19,29 +19,31 @@ pub mod mock_analysis; mod markup; mod prime_caches; -mod status; +mod display; + +mod call_hierarchy; +mod call_info; mod completion; -mod runnables; +mod diagnostics; +mod expand_macro; +mod extend_selection; +mod file_structure; +mod folding_ranges; mod goto_definition; -mod goto_type_definition; mod goto_implementation; -mod extend_selection; +mod goto_type_definition; mod hover; -mod call_hierarchy; -mod call_info; -mod syntax_highlighting; +mod inlay_hints; +mod join_lines; +mod matching_brace; mod parent_module; mod references; -mod diagnostics; +mod runnables; +mod ssr; +mod status; +mod syntax_highlighting; mod syntax_tree; -mod folding_ranges; -mod join_lines; mod typing; -mod matching_brace; -mod display; -mod inlay_hints; -mod expand_macro; -mod ssr; use std::sync::Arc; @@ -65,8 +67,9 @@ pub use crate::{ CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat, }, diagnostics::Severity, - display::{file_structure, NavigationTarget, StructureNode}, + display::NavigationTarget, expand_macro::ExpandedMacro, + file_structure::StructureNode, folding_ranges::{Fold, FoldKind}, hover::{HoverAction, HoverConfig, HoverGotoTypeData, HoverResult}, inlay_hints::{InlayHint, InlayHintsConfig, InlayKind}, @@ -323,7 +326,7 @@ impl Analysis { /// Returns a tree representation of symbols in the file. Useful to draw a /// file outline. pub fn file_structure(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| file_structure(&db.parse(file_id).tree())) + self.with_db(|db| file_structure::file_structure(&db.parse(file_id).tree())) } /// Returns a list of the places in the file where type hints can be displayed. -- cgit v1.2.3 From 6da22ed9752b239fcd4e7c75673907ceb1ac6b65 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 18:24:26 +0200 Subject: Redner self as param for call infor for assoc fn call --- crates/ra_ide/src/call_info.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index a2d23b2ec..35a8a0dc5 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs @@ -1,4 +1,5 @@ //! FIXME: write short doc here +use either::Either; use hir::{Docs, HirDisplay, Semantics, Type}; use ra_ide_db::RootDatabase; use ra_syntax::{ @@ -80,7 +81,10 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option format_to!(buf, "self: "), + Either::Right(pat) => format_to!(buf, "{}: ", pat), + } } format_to!(buf, "{}", ty.display(db)); res.push_param(&buf); @@ -383,20 +387,37 @@ fn bar() { check( r#" struct S; -impl S { pub fn do_it(&self, x: i32) {} } - -fn bar() { - let s: S = S; - s.do_it(<|>); +impl S { + fn foo(&self, x: i32) {} } + +fn main() { S.foo(<|>); } "#, expect![[r#" - fn do_it(&self, x: i32) + fn foo(&self, x: i32) () "#]], ); } + #[test] + fn test_fn_signature_for_method_with_arg_as_assoc_fn() { + check( + r#" +struct S; +impl S { + fn foo(&self, x: i32) {} +} + +fn main() { S::foo(<|>); } +"#, + expect![[r#" + fn foo(self: &S, x: i32) + (, x: i32) + "#]], + ); + } + #[test] fn test_fn_signature_with_docs_simple() { check( -- cgit v1.2.3