Skip to content

Commit 2a14c43

Browse files
bors[bot]matklad
andauthored
Merge #5403
5403: Simplify r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents c3ce891 + 1d6cf33 commit 2a14c43

File tree

3 files changed

+76
-116
lines changed

3 files changed

+76
-116
lines changed

crates/ra_ide/src/call_info.rs

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ use ra_syntax::{
77
};
88
use test_utils::mark;
99

10-
use crate::{CallInfo, FilePosition, FunctionSignature};
10+
use crate::{FilePosition, FunctionSignature};
11+
12+
/// Contains information about a call site. Specifically the
13+
/// `FunctionSignature`and current parameter.
14+
#[derive(Debug)]
15+
pub struct CallInfo {
16+
pub signature: FunctionSignature,
17+
pub active_parameter: Option<usize>,
18+
}
1119

1220
/// Computes parameter information for the given call expression.
1321
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
@@ -40,43 +48,40 @@ fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Op
4048
// Find the calling expression and it's NameRef
4149
let calling_node = FnCallNode::with_node(&token.parent())?;
4250

43-
let (mut call_info, has_self) = match &calling_node {
51+
let signature = match &calling_node {
4452
FnCallNode::CallExpr(call) => {
4553
//FIXME: Type::as_callable is broken
4654
let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?;
4755
match callable_def {
4856
hir::CallableDef::FunctionId(it) => {
4957
let fn_def = it.into();
50-
(CallInfo::with_fn(sema.db, fn_def), fn_def.has_self_param(sema.db))
58+
FunctionSignature::from_hir(sema.db, fn_def)
5159
}
5260
hir::CallableDef::StructId(it) => {
53-
(CallInfo::with_struct(sema.db, it.into())?, false)
61+
FunctionSignature::from_struct(sema.db, it.into())?
5462
}
5563
hir::CallableDef::EnumVariantId(it) => {
56-
(CallInfo::with_enum_variant(sema.db, it.into())?, false)
64+
FunctionSignature::from_enum_variant(sema.db, it.into())?
5765
}
5866
}
5967
}
6068
FnCallNode::MethodCallExpr(method_call) => {
6169
let function = sema.resolve_method_call(&method_call)?;
62-
(CallInfo::with_fn(sema.db, function), function.has_self_param(sema.db))
70+
FunctionSignature::from_hir(sema.db, function)
6371
}
6472
FnCallNode::MacroCallExpr(macro_call) => {
6573
let macro_def = sema.resolve_macro_call(&macro_call)?;
66-
(CallInfo::with_macro(sema.db, macro_def)?, false)
74+
FunctionSignature::from_macro(sema.db, macro_def)?
6775
}
6876
};
6977

7078
// If we have a calling expression let's find which argument we are on
71-
let num_params = call_info.parameters().len();
79+
let num_params = signature.parameters.len();
7280

73-
match num_params {
74-
0 => (),
75-
1 => {
76-
if !has_self {
77-
call_info.active_parameter = Some(0);
78-
}
79-
}
81+
let active_parameter = match num_params {
82+
0 => None,
83+
1 if signature.has_self_param => None,
84+
1 => Some(0),
8085
_ => {
8186
if let Some(arg_list) = calling_node.arg_list() {
8287
// Number of arguments specified at the call site
@@ -99,16 +104,18 @@ fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Op
99104
);
100105

101106
// If we are in a method account for `self`
102-
if has_self {
107+
if signature.has_self_param {
103108
param += 1;
104109
}
105110

106-
call_info.active_parameter = Some(param);
111+
Some(param)
112+
} else {
113+
None
107114
}
108115
}
109-
}
116+
};
110117

111-
Some(call_info)
118+
Some(CallInfo { signature, active_parameter })
112119
}
113120

114121
#[derive(Debug)]
@@ -181,34 +188,6 @@ impl CallInfo {
181188
let res = ActiveParameter { ty, name };
182189
Some(res)
183190
}
184-
185-
fn with_fn(db: &RootDatabase, function: hir::Function) -> Self {
186-
let signature = FunctionSignature::from_hir(db, function);
187-
188-
CallInfo { signature, active_parameter: None }
189-
}
190-
191-
fn with_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
192-
let signature = FunctionSignature::from_struct(db, st)?;
193-
194-
Some(CallInfo { signature, active_parameter: None })
195-
}
196-
197-
fn with_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
198-
let signature = FunctionSignature::from_enum_variant(db, variant)?;
199-
200-
Some(CallInfo { signature, active_parameter: None })
201-
}
202-
203-
fn with_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
204-
let signature = FunctionSignature::from_macro(db, macro_def)?;
205-
206-
Some(CallInfo { signature, active_parameter: None })
207-
}
208-
209-
fn parameters(&self) -> &[String] {
210-
&self.signature.parameters
211-
}
212191
}
213192

214193
#[cfg(test)]
@@ -228,7 +207,8 @@ mod tests {
228207
Some(docs) => format!("{}\n------\n", docs.as_str()),
229208
};
230209
let params = call_info
231-
.parameters()
210+
.signature
211+
.parameters
232212
.iter()
233213
.enumerate()
234214
.map(|(i, param)| {

crates/ra_ide/src/display/function_signature.rs

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,11 @@ pub struct FunctionQualifier {
6161
}
6262

6363
impl FunctionSignature {
64-
pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
65-
self.doc = doc;
66-
self
67-
}
68-
6964
pub(crate) fn from_hir(db: &RootDatabase, function: hir::Function) -> Self {
70-
let doc = function.docs(db);
7165
let ast_node = function.source(db).value;
72-
FunctionSignature::from(&ast_node).with_doc_opt(doc)
66+
let mut res = FunctionSignature::from(&ast_node);
67+
res.doc = function.docs(db);
68+
res
7369
}
7470

7571
pub(crate) fn from_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
@@ -93,24 +89,21 @@ impl FunctionSignature {
9389
params.push(raw_param);
9490
}
9591

96-
Some(
97-
FunctionSignature {
98-
kind: CallableKind::StructConstructor,
99-
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
100-
// Do we need `const`?
101-
qualifier: Default::default(),
102-
name: node.name().map(|n| n.text().to_string()),
103-
ret_type: node.name().map(|n| n.text().to_string()),
104-
parameters: params,
105-
parameter_names: vec![],
106-
parameter_types,
107-
generic_parameters: generic_parameters(&node),
108-
where_predicates: where_predicates(&node),
109-
doc: None,
110-
has_self_param: false,
111-
}
112-
.with_doc_opt(st.docs(db)),
113-
)
92+
Some(FunctionSignature {
93+
kind: CallableKind::StructConstructor,
94+
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
95+
// Do we need `const`?
96+
qualifier: Default::default(),
97+
name: node.name().map(|n| n.text().to_string()),
98+
ret_type: node.name().map(|n| n.text().to_string()),
99+
parameters: params,
100+
parameter_names: vec![],
101+
parameter_types,
102+
generic_parameters: generic_parameters(&node),
103+
where_predicates: where_predicates(&node),
104+
doc: st.docs(db),
105+
has_self_param: false,
106+
})
114107
}
115108

116109
pub(crate) fn from_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
@@ -140,48 +133,42 @@ impl FunctionSignature {
140133
params.push(format!("{}: {}", name, ty.display(db)));
141134
}
142135

143-
Some(
144-
FunctionSignature {
145-
kind: CallableKind::VariantConstructor,
146-
visibility: None,
147-
// Do we need `const`?
148-
qualifier: Default::default(),
149-
name: Some(name),
150-
ret_type: None,
151-
parameters: params,
152-
parameter_names: vec![],
153-
parameter_types,
154-
generic_parameters: vec![],
155-
where_predicates: vec![],
156-
doc: None,
157-
has_self_param: false,
158-
}
159-
.with_doc_opt(variant.docs(db)),
160-
)
136+
Some(FunctionSignature {
137+
kind: CallableKind::VariantConstructor,
138+
visibility: None,
139+
// Do we need `const`?
140+
qualifier: Default::default(),
141+
name: Some(name),
142+
ret_type: None,
143+
parameters: params,
144+
parameter_names: vec![],
145+
parameter_types,
146+
generic_parameters: vec![],
147+
where_predicates: vec![],
148+
doc: variant.docs(db),
149+
has_self_param: false,
150+
})
161151
}
162152

163153
pub(crate) fn from_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
164154
let node: ast::MacroCall = macro_def.source(db).value;
165155

166156
let params = vec![];
167157

168-
Some(
169-
FunctionSignature {
170-
kind: CallableKind::Macro,
171-
visibility: None,
172-
qualifier: Default::default(),
173-
name: node.name().map(|n| n.text().to_string()),
174-
ret_type: None,
175-
parameters: params,
176-
parameter_names: vec![],
177-
parameter_types: vec![],
178-
generic_parameters: vec![],
179-
where_predicates: vec![],
180-
doc: None,
181-
has_self_param: false,
182-
}
183-
.with_doc_opt(macro_def.docs(db)),
184-
)
158+
Some(FunctionSignature {
159+
kind: CallableKind::Macro,
160+
visibility: None,
161+
qualifier: Default::default(),
162+
name: node.name().map(|n| n.text().to_string()),
163+
ret_type: None,
164+
parameters: params,
165+
parameter_names: vec![],
166+
parameter_types: vec![],
167+
generic_parameters: vec![],
168+
where_predicates: vec![],
169+
doc: macro_def.docs(db),
170+
has_self_param: false,
171+
})
185172
}
186173
}
187174

crates/ra_ide/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use crate::display::ToNav;
6060

6161
pub use crate::{
6262
call_hierarchy::CallItem,
63+
call_info::CallInfo,
6364
completion::{
6465
CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat,
6566
},
@@ -131,14 +132,6 @@ impl<T> RangeInfo<T> {
131132
}
132133
}
133134

134-
/// Contains information about a call site. Specifically the
135-
/// `FunctionSignature`and current parameter.
136-
#[derive(Debug)]
137-
pub struct CallInfo {
138-
pub signature: FunctionSignature,
139-
pub active_parameter: Option<usize>,
140-
}
141-
142135
/// `AnalysisHost` stores the current state of the world.
143136
#[derive(Debug)]
144137
pub struct AnalysisHost {

0 commit comments

Comments
 (0)