From 070a9802246123c79709271c9ba6fddd6f888813 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Tue, 22 Jan 2019 10:54:50 -0500 Subject: Add function signature to CompletionItem detail --- crates/ra_hir/src/code_model_api.rs | 22 ++++++++++++++++++++++ .../ra_ide_api/src/completion/completion_item.rs | 4 ++++ .../completion_item__bindings_from_for.snap | 6 ++++-- .../completion_item__bindings_from_if_let.snap | 6 ++++-- .../completion_item__bindings_from_let.snap | 6 ++++-- ...ont_render_function_parens_if_already_call.snap | 10 +++++++--- ...m__dont_render_function_parens_in_use_item.snap | 6 ++++-- ...__dont_show_both_completions_for_shadowing.snap | 6 ++++-- ...n_item__inserts_parens_for_function_calls1.snap | 10 +++++++--- ...n_item__inserts_parens_for_function_calls2.snap | 10 +++++++--- .../completion_item__method_completion.snap | 6 ++++-- .../snapshots/completion_item__module_items.snap | 6 ++++-- ...etion_item__module_items_in_nested_modules.snap | 6 ++++-- .../snapshots/completion_item__return_type.snap | 4 +++- ...on_item__struct_field_completion_autoderef.snap | 6 ++++-- ...pletion_item__struct_field_completion_self.snap | 6 ++++-- 16 files changed, 90 insertions(+), 30 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 9ae620efd..21ca36265 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -366,6 +366,28 @@ impl Function { Some(comments) } } + + pub fn label(&self, db: &impl HirDatabase) -> Option { + let def_loc = self.def_id.loc(db); + let syntax = db.file_item(def_loc.source_item_id); + let node = ast::FnDef::cast(&syntax).expect("fn def should point to FnDef node"); + + let label: String = if let Some(body) = node.body() { + let body_range = body.syntax().range(); + let label: String = node + .syntax() + .children() + .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body + .filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments + .map(|node| node.text().to_string()) + .collect(); + label + } else { + node.syntax().text().to_string() + }; + + Some(label.trim().to_owned()) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index d70c36889..b720a1382 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -263,6 +263,10 @@ impl Builder { self.documentation = Some(docs); } + if let Some(label) = function.label(ctx.db) { + self.detail = Some(label); + } + self.kind = Some(CompletionItemKind::Function); self } diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_for.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_for.snap index 3d8d5af78..eb806ad62 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_for.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_for.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.858540400+00:00" +created: "2019-01-22T15:38:19.841148300+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -25,7 +25,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn quux()" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap index 7ed1d5ccb..4bb28c7e8 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.860535200+00:00" +created: "2019-01-22T15:38:19.863089100+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -39,7 +39,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn quux()" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap index 95581122d..0579d00ec 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.871506600+00:00" +created: "2019-01-22T15:38:19.881041600+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -39,7 +39,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn quux(x: i32)" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call.snap index 18d8b3fb3..091bf581e 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.797704900+00:00" +created: "2019-01-22T15:38:19.788294+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn main()" + ), documentation: None, lookup: None, insert_text: None, @@ -25,7 +27,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn frobnicate()" + ), documentation: None, lookup: None, insert_text: None, diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_in_use_item.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_in_use_item.snap index 8bf7bf06c..3bc3f5f38 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_in_use_item.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_in_use_item.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.801693300+00:00" +created: "2019-01-22T15:38:19.815217100+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "pub fn foo()" + ), documentation: None, lookup: None, insert_text: None, diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap index 5844254e1..3e88d8c8e 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.912397100+00:00" +created: "2019-01-22T15:38:19.914951100+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -25,7 +25,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn foo() ->" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls1.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls1.snap index 732a87382..b30a4c9e9 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls1.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls1.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.932343200+00:00" +created: "2019-01-22T15:38:19.919937+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn no_args()" + ), documentation: None, lookup: None, insert_text: Some( @@ -27,7 +29,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn main()" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls2.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls2.snap index 2431b3276..c524f3587 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls2.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__inserts_parens_for_function_calls2.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.995180+00:00" +created: "2019-01-22T15:38:19.996733+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn main()" + ), documentation: None, lookup: None, insert_text: Some( @@ -27,7 +29,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn with_args(x: i32, y: String)" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap index c294de5cb..ffeac0bf4 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__method_completion.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.546394900+00:00" +created: "2019-01-22T15:38:19.541947400+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Method ), - detail: None, + detail: Some( + "fn the_method(&self)" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items.snap index 032516ab4..df9677ba3 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.874497400+00:00" +created: "2019-01-22T15:38:19.896999400+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn quux()" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items_in_nested_modules.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items_in_nested_modules.snap index b33babaf0..f4b38feb0 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items_in_nested_modules.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__module_items_in_nested_modules.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.875495300+00:00" +created: "2019-01-22T15:38:19.896002200+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn quux()" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap index 24dbe6bb0..e6819f231 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__return_type.snap @@ -25,7 +25,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Function ), - detail: None, + detail: Some( + "fn x() ->" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap index 9ee668b35..5ef140b28 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_autoderef.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.552379600+00:00" +created: "2019-01-22T15:38:19.541947400+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -27,7 +27,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Method ), - detail: None, + detail: Some( + "fn foo(&self)" + ), documentation: None, lookup: None, insert_text: Some( diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap index b9d70ea3f..b7903c5bc 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-22T14:45:00.552379600+00:00" +created: "2019-01-22T15:38:19.541947400+00:00" creator: insta@0.4.0 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -27,7 +27,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( Method ), - detail: None, + detail: Some( + "fn foo(self)" + ), documentation: None, lookup: None, insert_text: Some( -- cgit v1.2.3 From a3472f8fe16a359f9ea7a12e7b4920894921421e Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Tue, 22 Jan 2019 18:20:40 -0500 Subject: Move label from hir to ide_api --- crates/ra_hir/src/code_model_api.rs | 22 ------------------ .../ra_ide_api/src/completion/completion_item.rs | 27 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 21ca36265..9ae620efd 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -366,28 +366,6 @@ impl Function { Some(comments) } } - - pub fn label(&self, db: &impl HirDatabase) -> Option { - let def_loc = self.def_id.loc(db); - let syntax = db.file_item(def_loc.source_item_id); - let node = ast::FnDef::cast(&syntax).expect("fn def should point to FnDef node"); - - let label: String = if let Some(body) = node.body() { - let body_range = body.syntax().range(); - let label: String = node - .syntax() - .children() - .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body - .filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments - .map(|node| node.text().to_string()) - .collect(); - label - } else { - node.syntax().text().to_string() - }; - - Some(label.trim().to_owned()) - } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index b720a1382..680fd8d1b 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,7 +1,10 @@ use hir::PerNs; use crate::completion::completion_context::CompletionContext; -use ra_syntax::TextRange; +use ra_syntax::{ + ast::{self, AstNode}, + TextRange +}; use ra_text_edit::TextEdit; /// `CompletionItem` describes a single completion variant in the editor pop-up. @@ -263,7 +266,7 @@ impl Builder { self.documentation = Some(docs); } - if let Some(label) = function.label(ctx.db) { + if let Some(label) = function_label(ctx, function) { self.detail = Some(label); } @@ -303,6 +306,26 @@ impl Into> for Completions { } } +fn function_label(ctx: &CompletionContext, function: hir::Function) -> Option { + let node = function.source(ctx.db).1; + + let label: String = if let Some(body) = node.body() { + let body_range = body.syntax().range(); + let label: String = node + .syntax() + .children() + .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body + .filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments + .map(|node| node.text().to_string()) + .collect(); + label + } else { + node.syntax().text().to_string() + }; + + Some(label.trim().to_owned()) +} + #[cfg(test)] pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) { use crate::mock_analysis::{single_file_with_position, analysis_and_position}; -- cgit v1.2.3