diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-29 13:48:26 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-29 13:48:26 +0000 |
commit | de16f94ada933cfd394ddab34c31410cf05268f1 (patch) | |
tree | f27b83617f56cb96f91b3a7ba37a5a5f41524606 /crates/ra_ide_api/src/display | |
parent | e38cdf6e56d963525fcc656b80965e7114756496 (diff) | |
parent | b915bf2d05e3edf7e23e595b2b95bdcdaa0907fd (diff) |
Merge #2103
2103: Expand signature help r=matklad a=kjeremy
Signature help using call syntax with tuple structs and enum variants
Fixes #2102.
Co-authored-by: Jeremy Kolb <[email protected]>
Co-authored-by: kjeremy <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/display')
-rw-r--r-- | crates/ra_ide_api/src/display/function_signature.rs | 90 |
1 files changed, 88 insertions, 2 deletions
diff --git a/crates/ra_ide_api/src/display/function_signature.rs b/crates/ra_ide_api/src/display/function_signature.rs index 43f022ccd..e21f8378d 100644 --- a/crates/ra_ide_api/src/display/function_signature.rs +++ b/crates/ra_ide_api/src/display/function_signature.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use std::fmt::{self, Display}; | 3 | use std::fmt::{self, Display}; |
4 | 4 | ||
5 | use hir::{Docs, Documentation, HasSource}; | 5 | use hir::{Docs, Documentation, HasSource, HirDisplay}; |
6 | use join_to_string::join; | 6 | use join_to_string::join; |
7 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; | 7 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; |
8 | use std::convert::From; | 8 | use std::convert::From; |
@@ -12,9 +12,17 @@ use crate::{ | |||
12 | display::{generic_parameters, where_predicates}, | 12 | display::{generic_parameters, where_predicates}, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | #[derive(Debug)] | ||
16 | pub enum CallableKind { | ||
17 | Function, | ||
18 | StructConstructor, | ||
19 | VariantConstructor, | ||
20 | } | ||
21 | |||
15 | /// Contains information about a function signature | 22 | /// Contains information about a function signature |
16 | #[derive(Debug)] | 23 | #[derive(Debug)] |
17 | pub struct FunctionSignature { | 24 | pub struct FunctionSignature { |
25 | pub kind: CallableKind, | ||
18 | /// Optional visibility | 26 | /// Optional visibility |
19 | pub visibility: Option<String>, | 27 | pub visibility: Option<String>, |
20 | /// Name of the function | 28 | /// Name of the function |
@@ -42,6 +50,79 @@ impl FunctionSignature { | |||
42 | let ast_node = function.source(db).ast; | 50 | let ast_node = function.source(db).ast; |
43 | FunctionSignature::from(&ast_node).with_doc_opt(doc) | 51 | FunctionSignature::from(&ast_node).with_doc_opt(doc) |
44 | } | 52 | } |
53 | |||
54 | pub(crate) fn from_struct(db: &db::RootDatabase, st: hir::Struct) -> Option<Self> { | ||
55 | let node: ast::StructDef = st.source(db).ast; | ||
56 | match node.kind() { | ||
57 | ast::StructKind::Named(_) => return None, | ||
58 | _ => (), | ||
59 | }; | ||
60 | |||
61 | let params = st | ||
62 | .fields(db) | ||
63 | .into_iter() | ||
64 | .map(|field: hir::StructField| { | ||
65 | let ty = field.ty(db); | ||
66 | format!("{}", ty.display(db)) | ||
67 | }) | ||
68 | .collect(); | ||
69 | |||
70 | Some( | ||
71 | FunctionSignature { | ||
72 | kind: CallableKind::StructConstructor, | ||
73 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | ||
74 | name: node.name().map(|n| n.text().to_string()), | ||
75 | ret_type: node.name().map(|n| n.text().to_string()), | ||
76 | parameters: params, | ||
77 | generic_parameters: generic_parameters(&node), | ||
78 | where_predicates: where_predicates(&node), | ||
79 | doc: None, | ||
80 | } | ||
81 | .with_doc_opt(st.docs(db)), | ||
82 | ) | ||
83 | } | ||
84 | |||
85 | pub(crate) fn from_enum_variant( | ||
86 | db: &db::RootDatabase, | ||
87 | variant: hir::EnumVariant, | ||
88 | ) -> Option<Self> { | ||
89 | let node: ast::EnumVariant = variant.source(db).ast; | ||
90 | match node.kind() { | ||
91 | ast::StructKind::Named(_) | ast::StructKind::Unit => return None, | ||
92 | _ => (), | ||
93 | }; | ||
94 | |||
95 | let parent_name = match variant.parent_enum(db).name(db) { | ||
96 | Some(name) => name.to_string(), | ||
97 | None => "missing".into(), | ||
98 | }; | ||
99 | |||
100 | let name = format!("{}::{}", parent_name, variant.name(db).unwrap()); | ||
101 | |||
102 | let params = variant | ||
103 | .fields(db) | ||
104 | .into_iter() | ||
105 | .map(|field: hir::StructField| { | ||
106 | let name = field.name(db); | ||
107 | let ty = field.ty(db); | ||
108 | format!("{}: {}", name, ty.display(db)) | ||
109 | }) | ||
110 | .collect(); | ||
111 | |||
112 | Some( | ||
113 | FunctionSignature { | ||
114 | kind: CallableKind::VariantConstructor, | ||
115 | visibility: None, | ||
116 | name: Some(name), | ||
117 | ret_type: None, | ||
118 | parameters: params, | ||
119 | generic_parameters: vec![], | ||
120 | where_predicates: vec![], | ||
121 | doc: None, | ||
122 | } | ||
123 | .with_doc_opt(variant.docs(db)), | ||
124 | ) | ||
125 | } | ||
45 | } | 126 | } |
46 | 127 | ||
47 | impl From<&'_ ast::FnDef> for FunctionSignature { | 128 | impl From<&'_ ast::FnDef> for FunctionSignature { |
@@ -59,6 +140,7 @@ impl From<&'_ ast::FnDef> for FunctionSignature { | |||
59 | } | 140 | } |
60 | 141 | ||
61 | FunctionSignature { | 142 | FunctionSignature { |
143 | kind: CallableKind::Function, | ||
62 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | 144 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), |
63 | name: node.name().map(|n| n.text().to_string()), | 145 | name: node.name().map(|n| n.text().to_string()), |
64 | ret_type: node | 146 | ret_type: node |
@@ -81,7 +163,11 @@ impl Display for FunctionSignature { | |||
81 | } | 163 | } |
82 | 164 | ||
83 | if let Some(name) = &self.name { | 165 | if let Some(name) = &self.name { |
84 | write!(f, "fn {}", name)?; | 166 | match self.kind { |
167 | CallableKind::Function => write!(f, "fn {}", name)?, | ||
168 | CallableKind::StructConstructor => write!(f, "struct {}", name)?, | ||
169 | CallableKind::VariantConstructor => write!(f, "{}", name)?, | ||
170 | } | ||
85 | } | 171 | } |
86 | 172 | ||
87 | if !self.generic_parameters.is_empty() { | 173 | if !self.generic_parameters.is_empty() { |