aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJeremy Kolb <[email protected]>2019-10-28 01:26:12 +0000
committerJeremy Kolb <[email protected]>2019-10-28 12:32:22 +0000
commit49e89772f63e10ebeb3c8720bd0b0ef8244f6c4a (patch)
tree692c35449952ec6fc3a92fb0f7fbece9ba3f5ec9 /crates
parent55d4b06a53246c144be900877e6ac03237d6f8b4 (diff)
Preliminary enum variant support
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/call_info.rs32
-rw-r--r--crates/ra_ide_api/src/display/function_signature.rs37
2 files changed, 67 insertions, 2 deletions
diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs
index 29ae2f552..e6bdaae6a 100644
--- a/crates/ra_ide_api/src/call_info.rs
+++ b/crates/ra_ide_api/src/call_info.rs
@@ -29,7 +29,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
29 (CallInfo::with_fn(db, it), it.data(db).has_self_param()) 29 (CallInfo::with_fn(db, it), it.data(db).has_self_param())
30 } 30 }
31 hir::CallableDef::Struct(it) => (CallInfo::with_struct(db, it), false), 31 hir::CallableDef::Struct(it) => (CallInfo::with_struct(db, it), false),
32 hir::CallableDef::EnumVariant(_it) => return None, 32 hir::CallableDef::EnumVariant(it) => (CallInfo::with_enum_variant(db, it), false),
33 } 33 }
34 } 34 }
35 FnCallNode::MethodCallExpr(expr) => { 35 FnCallNode::MethodCallExpr(expr) => {
@@ -129,6 +129,12 @@ impl CallInfo {
129 CallInfo { signature, active_parameter: None } 129 CallInfo { signature, active_parameter: None }
130 } 130 }
131 131
132 fn with_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Self {
133 let signature = FunctionSignature::from_enum_variant(db, variant);
134
135 CallInfo { signature, active_parameter: None }
136 }
137
132 fn parameters(&self) -> &[String] { 138 fn parameters(&self) -> &[String] {
133 &self.signature.parameters 139 &self.signature.parameters
134 } 140 }
@@ -485,4 +491,28 @@ fn main() {
485 assert_eq!(info.doc().map(|it| it.into()), Some("A cool tuple struct".to_string())); 491 assert_eq!(info.doc().map(|it| it.into()), Some("A cool tuple struct".to_string()));
486 assert_eq!(info.active_parameter, Some(1)); 492 assert_eq!(info.active_parameter, Some(1));
487 } 493 }
494
495 #[test]
496 fn works_for_enum_variants() {
497 let info = call_info(
498 r#"
499enum E {
500 /// A Variant
501 A(i32),
502 /// Another
503 B,
504 /// And C
505 C(a: i32, b: i32)
506}
507
508fn main() {
509 let a = E::A(<|>);
510}
511 "#,
512 );
513
514 assert_eq!(info.label(), "E::A(0: i32)");
515 assert_eq!(info.doc().map(|it| it.into()), Some("A Variant".to_string()));
516 assert_eq!(info.active_parameter, Some(0));
517 }
488} 518}
diff --git a/crates/ra_ide_api/src/display/function_signature.rs b/crates/ra_ide_api/src/display/function_signature.rs
index 6555f8619..6b169b3ae 100644
--- a/crates/ra_ide_api/src/display/function_signature.rs
+++ b/crates/ra_ide_api/src/display/function_signature.rs
@@ -16,6 +16,7 @@ use crate::{
16pub enum SigKind { 16pub enum SigKind {
17 Function, 17 Function,
18 Struct, 18 Struct,
19 EnumVariant,
19} 20}
20 21
21/// Contains information about a function signature 22/// Contains information about a function signature
@@ -70,13 +71,46 @@ impl FunctionSignature {
70 visibility: node.visibility().map(|n| n.syntax().text().to_string()), 71 visibility: node.visibility().map(|n| n.syntax().text().to_string()),
71 name: node.name().map(|n| n.text().to_string()), 72 name: node.name().map(|n| n.text().to_string()),
72 ret_type: node.name().map(|n| n.text().to_string()), 73 ret_type: node.name().map(|n| n.text().to_string()),
73 parameters: /*param_list(node)*/ params, 74 parameters: params,
74 generic_parameters: generic_parameters(&node), 75 generic_parameters: generic_parameters(&node),
75 where_predicates: where_predicates(&node), 76 where_predicates: where_predicates(&node),
76 doc: None, 77 doc: None,
77 } 78 }
78 .with_doc_opt(doc) 79 .with_doc_opt(doc)
79 } 80 }
81
82 pub(crate) fn from_enum_variant(db: &db::RootDatabase, variant: hir::EnumVariant) -> Self {
83 let doc = variant.docs(db);
84
85 let parent_name = match variant.parent_enum(db).name(db) {
86 Some(name) => name.to_string(),
87 None => "missing".into(),
88 };
89
90 let name = format!("{}::{}", parent_name, variant.name(db).unwrap());
91
92 let params = variant
93 .fields(db)
94 .into_iter()
95 .map(|field: hir::StructField| {
96 let name = field.name(db);
97 let ty = field.ty(db);
98 format!("{}: {}", name, ty.display(db))
99 })
100 .collect();
101
102 FunctionSignature {
103 kind: SigKind::EnumVariant,
104 visibility: None,
105 name: Some(name),
106 ret_type: None,
107 parameters: params,
108 generic_parameters: vec![],
109 where_predicates: vec![],
110 doc: None,
111 }
112 .with_doc_opt(doc)
113 }
80} 114}
81 115
82impl From<&'_ ast::FnDef> for FunctionSignature { 116impl From<&'_ ast::FnDef> for FunctionSignature {
@@ -120,6 +154,7 @@ impl Display for FunctionSignature {
120 match self.kind { 154 match self.kind {
121 SigKind::Function => write!(f, "fn {}", name)?, 155 SigKind::Function => write!(f, "fn {}", name)?,
122 SigKind::Struct => write!(f, "struct {}", name)?, 156 SigKind::Struct => write!(f, "struct {}", name)?,
157 SigKind::EnumVariant => write!(f, "{}", name)?,
123 } 158 }
124 } 159 }
125 160