mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Merge #5403
5403: Simplify r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
2a14c43d5b
@ -7,7 +7,15 @@ use ra_syntax::{
|
||||
};
|
||||
use test_utils::mark;
|
||||
|
||||
use crate::{CallInfo, FilePosition, FunctionSignature};
|
||||
use crate::{FilePosition, FunctionSignature};
|
||||
|
||||
/// Contains information about a call site. Specifically the
|
||||
/// `FunctionSignature`and current parameter.
|
||||
#[derive(Debug)]
|
||||
pub struct CallInfo {
|
||||
pub signature: FunctionSignature,
|
||||
pub active_parameter: Option<usize>,
|
||||
}
|
||||
|
||||
/// Computes parameter information for the given call expression.
|
||||
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
|
||||
// Find the calling expression and it's NameRef
|
||||
let calling_node = FnCallNode::with_node(&token.parent())?;
|
||||
|
||||
let (mut call_info, has_self) = match &calling_node {
|
||||
let signature = match &calling_node {
|
||||
FnCallNode::CallExpr(call) => {
|
||||
//FIXME: Type::as_callable is broken
|
||||
let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?;
|
||||
match callable_def {
|
||||
hir::CallableDef::FunctionId(it) => {
|
||||
let fn_def = it.into();
|
||||
(CallInfo::with_fn(sema.db, fn_def), fn_def.has_self_param(sema.db))
|
||||
FunctionSignature::from_hir(sema.db, fn_def)
|
||||
}
|
||||
hir::CallableDef::StructId(it) => {
|
||||
(CallInfo::with_struct(sema.db, it.into())?, false)
|
||||
FunctionSignature::from_struct(sema.db, it.into())?
|
||||
}
|
||||
hir::CallableDef::EnumVariantId(it) => {
|
||||
(CallInfo::with_enum_variant(sema.db, it.into())?, false)
|
||||
FunctionSignature::from_enum_variant(sema.db, it.into())?
|
||||
}
|
||||
}
|
||||
}
|
||||
FnCallNode::MethodCallExpr(method_call) => {
|
||||
let function = sema.resolve_method_call(&method_call)?;
|
||||
(CallInfo::with_fn(sema.db, function), function.has_self_param(sema.db))
|
||||
FunctionSignature::from_hir(sema.db, function)
|
||||
}
|
||||
FnCallNode::MacroCallExpr(macro_call) => {
|
||||
let macro_def = sema.resolve_macro_call(¯o_call)?;
|
||||
(CallInfo::with_macro(sema.db, macro_def)?, false)
|
||||
FunctionSignature::from_macro(sema.db, macro_def)?
|
||||
}
|
||||
};
|
||||
|
||||
// If we have a calling expression let's find which argument we are on
|
||||
let num_params = call_info.parameters().len();
|
||||
let num_params = signature.parameters.len();
|
||||
|
||||
match num_params {
|
||||
0 => (),
|
||||
1 => {
|
||||
if !has_self {
|
||||
call_info.active_parameter = Some(0);
|
||||
}
|
||||
}
|
||||
let active_parameter = match num_params {
|
||||
0 => None,
|
||||
1 if signature.has_self_param => None,
|
||||
1 => Some(0),
|
||||
_ => {
|
||||
if let Some(arg_list) = calling_node.arg_list() {
|
||||
// Number of arguments specified at the call site
|
||||
@ -99,16 +104,18 @@ fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Op
|
||||
);
|
||||
|
||||
// If we are in a method account for `self`
|
||||
if has_self {
|
||||
if signature.has_self_param {
|
||||
param += 1;
|
||||
}
|
||||
|
||||
call_info.active_parameter = Some(param);
|
||||
Some(param)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Some(call_info)
|
||||
Some(CallInfo { signature, active_parameter })
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -181,34 +188,6 @@ impl CallInfo {
|
||||
let res = ActiveParameter { ty, name };
|
||||
Some(res)
|
||||
}
|
||||
|
||||
fn with_fn(db: &RootDatabase, function: hir::Function) -> Self {
|
||||
let signature = FunctionSignature::from_hir(db, function);
|
||||
|
||||
CallInfo { signature, active_parameter: None }
|
||||
}
|
||||
|
||||
fn with_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
|
||||
let signature = FunctionSignature::from_struct(db, st)?;
|
||||
|
||||
Some(CallInfo { signature, active_parameter: None })
|
||||
}
|
||||
|
||||
fn with_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
|
||||
let signature = FunctionSignature::from_enum_variant(db, variant)?;
|
||||
|
||||
Some(CallInfo { signature, active_parameter: None })
|
||||
}
|
||||
|
||||
fn with_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
|
||||
let signature = FunctionSignature::from_macro(db, macro_def)?;
|
||||
|
||||
Some(CallInfo { signature, active_parameter: None })
|
||||
}
|
||||
|
||||
fn parameters(&self) -> &[String] {
|
||||
&self.signature.parameters
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -228,7 +207,8 @@ mod tests {
|
||||
Some(docs) => format!("{}\n------\n", docs.as_str()),
|
||||
};
|
||||
let params = call_info
|
||||
.parameters()
|
||||
.signature
|
||||
.parameters
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, param)| {
|
||||
|
@ -61,15 +61,11 @@ pub struct FunctionQualifier {
|
||||
}
|
||||
|
||||
impl FunctionSignature {
|
||||
pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
|
||||
self.doc = doc;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn from_hir(db: &RootDatabase, function: hir::Function) -> Self {
|
||||
let doc = function.docs(db);
|
||||
let ast_node = function.source(db).value;
|
||||
FunctionSignature::from(&ast_node).with_doc_opt(doc)
|
||||
let mut res = FunctionSignature::from(&ast_node);
|
||||
res.doc = function.docs(db);
|
||||
res
|
||||
}
|
||||
|
||||
pub(crate) fn from_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
|
||||
@ -93,24 +89,21 @@ impl FunctionSignature {
|
||||
params.push(raw_param);
|
||||
}
|
||||
|
||||
Some(
|
||||
FunctionSignature {
|
||||
kind: CallableKind::StructConstructor,
|
||||
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
|
||||
// Do we need `const`?
|
||||
qualifier: Default::default(),
|
||||
name: node.name().map(|n| n.text().to_string()),
|
||||
ret_type: node.name().map(|n| n.text().to_string()),
|
||||
parameters: params,
|
||||
parameter_names: vec![],
|
||||
parameter_types,
|
||||
generic_parameters: generic_parameters(&node),
|
||||
where_predicates: where_predicates(&node),
|
||||
doc: None,
|
||||
has_self_param: false,
|
||||
}
|
||||
.with_doc_opt(st.docs(db)),
|
||||
)
|
||||
Some(FunctionSignature {
|
||||
kind: CallableKind::StructConstructor,
|
||||
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
|
||||
// Do we need `const`?
|
||||
qualifier: Default::default(),
|
||||
name: node.name().map(|n| n.text().to_string()),
|
||||
ret_type: node.name().map(|n| n.text().to_string()),
|
||||
parameters: params,
|
||||
parameter_names: vec![],
|
||||
parameter_types,
|
||||
generic_parameters: generic_parameters(&node),
|
||||
where_predicates: where_predicates(&node),
|
||||
doc: st.docs(db),
|
||||
has_self_param: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn from_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
|
||||
@ -140,24 +133,21 @@ impl FunctionSignature {
|
||||
params.push(format!("{}: {}", name, ty.display(db)));
|
||||
}
|
||||
|
||||
Some(
|
||||
FunctionSignature {
|
||||
kind: CallableKind::VariantConstructor,
|
||||
visibility: None,
|
||||
// Do we need `const`?
|
||||
qualifier: Default::default(),
|
||||
name: Some(name),
|
||||
ret_type: None,
|
||||
parameters: params,
|
||||
parameter_names: vec![],
|
||||
parameter_types,
|
||||
generic_parameters: vec![],
|
||||
where_predicates: vec![],
|
||||
doc: None,
|
||||
has_self_param: false,
|
||||
}
|
||||
.with_doc_opt(variant.docs(db)),
|
||||
)
|
||||
Some(FunctionSignature {
|
||||
kind: CallableKind::VariantConstructor,
|
||||
visibility: None,
|
||||
// Do we need `const`?
|
||||
qualifier: Default::default(),
|
||||
name: Some(name),
|
||||
ret_type: None,
|
||||
parameters: params,
|
||||
parameter_names: vec![],
|
||||
parameter_types,
|
||||
generic_parameters: vec![],
|
||||
where_predicates: vec![],
|
||||
doc: variant.docs(db),
|
||||
has_self_param: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn from_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
|
||||
@ -165,23 +155,20 @@ impl FunctionSignature {
|
||||
|
||||
let params = vec![];
|
||||
|
||||
Some(
|
||||
FunctionSignature {
|
||||
kind: CallableKind::Macro,
|
||||
visibility: None,
|
||||
qualifier: Default::default(),
|
||||
name: node.name().map(|n| n.text().to_string()),
|
||||
ret_type: None,
|
||||
parameters: params,
|
||||
parameter_names: vec![],
|
||||
parameter_types: vec![],
|
||||
generic_parameters: vec![],
|
||||
where_predicates: vec![],
|
||||
doc: None,
|
||||
has_self_param: false,
|
||||
}
|
||||
.with_doc_opt(macro_def.docs(db)),
|
||||
)
|
||||
Some(FunctionSignature {
|
||||
kind: CallableKind::Macro,
|
||||
visibility: None,
|
||||
qualifier: Default::default(),
|
||||
name: node.name().map(|n| n.text().to_string()),
|
||||
ret_type: None,
|
||||
parameters: params,
|
||||
parameter_names: vec![],
|
||||
parameter_types: vec![],
|
||||
generic_parameters: vec![],
|
||||
where_predicates: vec![],
|
||||
doc: macro_def.docs(db),
|
||||
has_self_param: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,7 @@ use crate::display::ToNav;
|
||||
|
||||
pub use crate::{
|
||||
call_hierarchy::CallItem,
|
||||
call_info::CallInfo,
|
||||
completion::{
|
||||
CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat,
|
||||
},
|
||||
@ -131,14 +132,6 @@ impl<T> RangeInfo<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Contains information about a call site. Specifically the
|
||||
/// `FunctionSignature`and current parameter.
|
||||
#[derive(Debug)]
|
||||
pub struct CallInfo {
|
||||
pub signature: FunctionSignature,
|
||||
pub active_parameter: Option<usize>,
|
||||
}
|
||||
|
||||
/// `AnalysisHost` stores the current state of the world.
|
||||
#[derive(Debug)]
|
||||
pub struct AnalysisHost {
|
||||
|
Loading…
Reference in New Issue
Block a user