diff options
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/call_info.rs | 7 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion.rs | 48 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display.rs | 49 |
3 files changed, 54 insertions, 50 deletions
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 1885d3da8..66a769c73 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs | |||
@@ -8,7 +8,7 @@ use ra_syntax::{ | |||
8 | }; | 8 | }; |
9 | use hir::Docs; | 9 | use hir::Docs; |
10 | 10 | ||
11 | use crate::{FilePosition, CallInfo, db::RootDatabase}; | 11 | use crate::{FilePosition, CallInfo, FunctionSignature, db::RootDatabase}; |
12 | 12 | ||
13 | /// Computes parameter information for the given call expression. | 13 | /// Computes parameter information for the given call expression. |
14 | pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { | 14 | pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { |
@@ -108,11 +108,10 @@ impl<'a> FnCallNode<'a> { | |||
108 | 108 | ||
109 | impl CallInfo { | 109 | impl CallInfo { |
110 | fn new(db: &RootDatabase, function: hir::Function, node: &ast::FnDef) -> Option<Self> { | 110 | fn new(db: &RootDatabase, function: hir::Function, node: &ast::FnDef) -> Option<Self> { |
111 | let sig = crate::completion::function_signature(node)?; | ||
112 | let doc = function.docs(db); | 111 | let doc = function.docs(db); |
113 | let sig = sig.with_doc_opt(doc); | 112 | let signature = FunctionSignature::from(node).with_doc_opt(doc); |
114 | 113 | ||
115 | Some(CallInfo { signature: sig, active_parameter: None }) | 114 | Some(CallInfo { signature, active_parameter: None }) |
116 | } | 115 | } |
117 | 116 | ||
118 | fn parameters(&self) -> &[String] { | 117 | fn parameters(&self) -> &[String] { |
diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs index d8e4410b2..71a35c689 100644 --- a/crates/ra_ide_api/src/completion.rs +++ b/crates/ra_ide_api/src/completion.rs | |||
@@ -13,7 +13,7 @@ mod complete_scope; | |||
13 | mod complete_postfix; | 13 | mod complete_postfix; |
14 | 14 | ||
15 | use ra_db::SourceDatabase; | 15 | use ra_db::SourceDatabase; |
16 | use ra_syntax::{ast::{self, AstNode, NameOwner, VisibilityOwner, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; | 16 | use ra_syntax::{ast::{self, AstNode}, SyntaxKind::{ATTR, COMMENT}}; |
17 | 17 | ||
18 | use crate::{ | 18 | use crate::{ |
19 | db, | 19 | db, |
@@ -72,52 +72,8 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti | |||
72 | Some(acc) | 72 | Some(acc) |
73 | } | 73 | } |
74 | 74 | ||
75 | pub fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> { | ||
76 | let mut res = vec![]; | ||
77 | if let Some(type_params) = node.type_param_list() { | ||
78 | res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string())); | ||
79 | res.extend(type_params.type_params().map(|p| p.syntax().text().to_string())); | ||
80 | } | ||
81 | res | ||
82 | } | ||
83 | |||
84 | pub fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> { | ||
85 | let mut res = vec![]; | ||
86 | if let Some(clause) = node.where_clause() { | ||
87 | res.extend(clause.predicates().map(|p| p.syntax().text().to_string())); | ||
88 | } | ||
89 | res | ||
90 | } | ||
91 | |||
92 | pub fn function_signature(node: &ast::FnDef) -> Option<FunctionSignature> { | ||
93 | fn param_list(node: &ast::FnDef) -> Vec<String> { | ||
94 | let mut res = vec![]; | ||
95 | if let Some(param_list) = node.param_list() { | ||
96 | if let Some(self_param) = param_list.self_param() { | ||
97 | res.push(self_param.syntax().text().to_string()) | ||
98 | } | ||
99 | |||
100 | res.extend(param_list.params().map(|param| param.syntax().text().to_string())); | ||
101 | } | ||
102 | res | ||
103 | } | ||
104 | |||
105 | let sig = FunctionSignature { | ||
106 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | ||
107 | name: node.name().map(|n| n.text().to_string()), | ||
108 | ret_type: node.ret_type().and_then(|r| r.type_ref()).map(|n| n.syntax().text().to_string()), | ||
109 | parameters: param_list(node), | ||
110 | generic_parameters: generic_parameters(node), | ||
111 | where_predicates: where_predicates(node), | ||
112 | // docs are processed separately | ||
113 | doc: None, | ||
114 | }; | ||
115 | |||
116 | Some(sig) | ||
117 | } | ||
118 | |||
119 | pub fn function_label(node: &ast::FnDef) -> Option<String> { | 75 | pub fn function_label(node: &ast::FnDef) -> Option<String> { |
120 | function_signature(node).map(|n| n.to_string()) | 76 | Some(FunctionSignature::from(node).to_string()) |
121 | } | 77 | } |
122 | 78 | ||
123 | pub fn const_label(node: &ast::ConstDef) -> String { | 79 | pub fn const_label(node: &ast::ConstDef) -> String { |
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 9d9d2097f..e01635460 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs | |||
@@ -3,6 +3,8 @@ | |||
3 | use super::*; | 3 | use super::*; |
4 | use std::fmt::{self, Display}; | 4 | use std::fmt::{self, Display}; |
5 | use join_to_string::join; | 5 | use join_to_string::join; |
6 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner, TypeParamsOwner}; | ||
7 | use std::convert::From; | ||
6 | 8 | ||
7 | /// Contains information about a function signature | 9 | /// Contains information about a function signature |
8 | #[derive(Debug)] | 10 | #[derive(Debug)] |
@@ -30,6 +32,36 @@ impl FunctionSignature { | |||
30 | } | 32 | } |
31 | } | 33 | } |
32 | 34 | ||
35 | impl From<&'_ ast::FnDef> for FunctionSignature { | ||
36 | fn from(node: &ast::FnDef) -> FunctionSignature { | ||
37 | fn param_list(node: &ast::FnDef) -> Vec<String> { | ||
38 | let mut res = vec![]; | ||
39 | if let Some(param_list) = node.param_list() { | ||
40 | if let Some(self_param) = param_list.self_param() { | ||
41 | res.push(self_param.syntax().text().to_string()) | ||
42 | } | ||
43 | |||
44 | res.extend(param_list.params().map(|param| param.syntax().text().to_string())); | ||
45 | } | ||
46 | res | ||
47 | } | ||
48 | |||
49 | FunctionSignature { | ||
50 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | ||
51 | name: node.name().map(|n| n.text().to_string()), | ||
52 | ret_type: node | ||
53 | .ret_type() | ||
54 | .and_then(|r| r.type_ref()) | ||
55 | .map(|n| n.syntax().text().to_string()), | ||
56 | parameters: param_list(node), | ||
57 | generic_parameters: generic_parameters(node), | ||
58 | where_predicates: where_predicates(node), | ||
59 | // docs are processed separately | ||
60 | doc: None, | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
33 | impl Display for FunctionSignature { | 65 | impl Display for FunctionSignature { |
34 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 66 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
35 | if let Some(t) = &self.visibility { | 67 | if let Some(t) = &self.visibility { |
@@ -61,3 +93,20 @@ impl Display for FunctionSignature { | |||
61 | Ok(()) | 93 | Ok(()) |
62 | } | 94 | } |
63 | } | 95 | } |
96 | |||
97 | pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> { | ||
98 | let mut res = vec![]; | ||
99 | if let Some(type_params) = node.type_param_list() { | ||
100 | res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string())); | ||
101 | res.extend(type_params.type_params().map(|p| p.syntax().text().to_string())); | ||
102 | } | ||
103 | res | ||
104 | } | ||
105 | |||
106 | pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> { | ||
107 | let mut res = vec![]; | ||
108 | if let Some(clause) = node.where_clause() { | ||
109 | res.extend(clause.predicates().map(|p| p.syntax().text().to_string())); | ||
110 | } | ||
111 | res | ||
112 | } | ||