2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-03-28 10:01:25 +00:00
|
|
|
use std::{
|
|
|
|
convert::From,
|
|
|
|
fmt::{self, Display},
|
|
|
|
};
|
2019-06-11 14:54:51 +00:00
|
|
|
|
2019-10-27 23:12:21 +00:00
|
|
|
use hir::{Docs, Documentation, HasSource, HirDisplay};
|
2020-02-06 11:52:32 +00:00
|
|
|
use ra_ide_db::RootDatabase;
|
2019-04-08 13:34:59 +00:00
|
|
|
use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
|
2020-03-28 10:01:25 +00:00
|
|
|
use stdx::SepBy;
|
2019-06-11 14:54:51 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
use crate::display::{generic_parameters, where_predicates};
|
2019-04-08 13:34:59 +00:00
|
|
|
|
2019-10-28 00:11:02 +00:00
|
|
|
#[derive(Debug)]
|
2019-10-29 13:46:55 +00:00
|
|
|
pub enum CallableKind {
|
2019-10-28 00:11:02 +00:00
|
|
|
Function,
|
2019-10-29 13:46:55 +00:00
|
|
|
StructConstructor,
|
|
|
|
VariantConstructor,
|
2019-10-29 16:16:55 +00:00
|
|
|
Macro,
|
2019-10-28 00:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:34:59 +00:00
|
|
|
/// Contains information about a function signature
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FunctionSignature {
|
2019-10-29 13:46:55 +00:00
|
|
|
pub kind: CallableKind,
|
2019-04-08 13:34:59 +00:00
|
|
|
/// Optional visibility
|
|
|
|
pub visibility: Option<String>,
|
|
|
|
/// Name of the function
|
|
|
|
pub name: Option<String>,
|
|
|
|
/// Documentation for the function
|
|
|
|
pub doc: Option<Documentation>,
|
|
|
|
/// Generic parameters
|
|
|
|
pub generic_parameters: Vec<String>,
|
|
|
|
/// Parameters of the function
|
|
|
|
pub parameters: Vec<String>,
|
2020-01-14 17:02:01 +00:00
|
|
|
/// Parameter names of the function
|
|
|
|
pub parameter_names: Vec<String>,
|
2019-04-08 13:34:59 +00:00
|
|
|
/// Optional return type
|
|
|
|
pub ret_type: Option<String>,
|
|
|
|
/// Where predicates
|
|
|
|
pub where_predicates: Vec<String>,
|
2020-02-23 09:49:53 +00:00
|
|
|
/// Self param presence
|
|
|
|
pub has_self_param: bool,
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionSignature {
|
|
|
|
pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
|
|
|
|
self.doc = doc;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
pub(crate) fn from_hir(db: &RootDatabase, function: hir::Function) -> Self {
|
2019-04-08 13:34:59 +00:00
|
|
|
let doc = function.docs(db);
|
2019-11-20 06:40:36 +00:00
|
|
|
let ast_node = function.source(db).value;
|
2019-07-19 09:56:47 +00:00
|
|
|
FunctionSignature::from(&ast_node).with_doc_opt(doc)
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
2019-10-27 23:12:21 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
pub(crate) fn from_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
|
2019-11-20 06:40:36 +00:00
|
|
|
let node: ast::StructDef = st.source(db).value;
|
2020-02-18 13:32:19 +00:00
|
|
|
if let ast::StructKind::Record(_) = node.kind() {
|
|
|
|
return None;
|
2019-10-28 14:48:40 +00:00
|
|
|
};
|
2019-10-27 23:12:21 +00:00
|
|
|
|
|
|
|
let params = st
|
|
|
|
.fields(db)
|
|
|
|
.into_iter()
|
|
|
|
.map(|field: hir::StructField| {
|
Fix completion of HashMap::new
The `ty` function in code_model returned the type with placeholders for type
parameters. That's nice for printing, but not good for completion, because
placeholders won't unify with anything else: So the type we got for `HashMap`
was `HashMap<K, V, T>`, which doesn't unify with `HashMap<?, ?, RandomState>`,
so the `new` method wasn't shown.
Now we instead return `HashMap<{unknown}, {unknown}, {unknown}>`, which does
unify with the impl type. Maybe we should just expose this properly as variables
though, i.e. we'd return something like `exists<type, type, type> HashMap<?0,
?1, ?2>` (in Chalk notation). It'll make the API more complicated, but harder to
misuse. (And it would handle cases like `type TypeAlias<T> = HashMap<T, T>` more
correctly.)
2020-03-13 10:45:58 +00:00
|
|
|
let ty = field.signature_ty(db);
|
2019-10-28 14:48:40 +00:00
|
|
|
format!("{}", ty.display(db))
|
2019-10-27 23:12:21 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-10-28 14:48:40 +00:00
|
|
|
Some(
|
|
|
|
FunctionSignature {
|
2019-10-29 13:46:55 +00:00
|
|
|
kind: CallableKind::StructConstructor,
|
2019-10-28 14:48:40 +00:00
|
|
|
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
|
|
|
|
name: node.name().map(|n| n.text().to_string()),
|
|
|
|
ret_type: node.name().map(|n| n.text().to_string()),
|
|
|
|
parameters: params,
|
2020-01-14 17:02:01 +00:00
|
|
|
parameter_names: vec![],
|
2019-10-28 14:48:40 +00:00
|
|
|
generic_parameters: generic_parameters(&node),
|
|
|
|
where_predicates: where_predicates(&node),
|
|
|
|
doc: None,
|
2020-02-23 09:49:53 +00:00
|
|
|
has_self_param: false,
|
2019-10-28 14:48:40 +00:00
|
|
|
}
|
|
|
|
.with_doc_opt(st.docs(db)),
|
|
|
|
)
|
2019-10-27 23:12:21 +00:00
|
|
|
}
|
2019-10-28 01:26:12 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
pub(crate) fn from_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
|
2019-11-20 06:40:36 +00:00
|
|
|
let node: ast::EnumVariant = variant.source(db).value;
|
2019-10-28 14:48:40 +00:00
|
|
|
match node.kind() {
|
2019-11-22 18:52:06 +00:00
|
|
|
ast::StructKind::Record(_) | ast::StructKind::Unit => return None,
|
2019-10-28 14:48:40 +00:00
|
|
|
_ => (),
|
|
|
|
};
|
2019-10-28 01:26:12 +00:00
|
|
|
|
2019-11-27 20:22:20 +00:00
|
|
|
let parent_name = variant.parent_enum(db).name(db).to_string();
|
2019-10-28 01:26:12 +00:00
|
|
|
|
2019-11-27 20:22:20 +00:00
|
|
|
let name = format!("{}::{}", parent_name, variant.name(db));
|
2019-10-28 01:26:12 +00:00
|
|
|
|
|
|
|
let params = variant
|
|
|
|
.fields(db)
|
|
|
|
.into_iter()
|
|
|
|
.map(|field: hir::StructField| {
|
|
|
|
let name = field.name(db);
|
Fix completion of HashMap::new
The `ty` function in code_model returned the type with placeholders for type
parameters. That's nice for printing, but not good for completion, because
placeholders won't unify with anything else: So the type we got for `HashMap`
was `HashMap<K, V, T>`, which doesn't unify with `HashMap<?, ?, RandomState>`,
so the `new` method wasn't shown.
Now we instead return `HashMap<{unknown}, {unknown}, {unknown}>`, which does
unify with the impl type. Maybe we should just expose this properly as variables
though, i.e. we'd return something like `exists<type, type, type> HashMap<?0,
?1, ?2>` (in Chalk notation). It'll make the API more complicated, but harder to
misuse. (And it would handle cases like `type TypeAlias<T> = HashMap<T, T>` more
correctly.)
2020-03-13 10:45:58 +00:00
|
|
|
let ty = field.signature_ty(db);
|
2019-10-28 01:26:12 +00:00
|
|
|
format!("{}: {}", name, ty.display(db))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-10-28 14:48:40 +00:00
|
|
|
Some(
|
|
|
|
FunctionSignature {
|
2019-10-29 13:46:55 +00:00
|
|
|
kind: CallableKind::VariantConstructor,
|
2019-10-28 14:48:40 +00:00
|
|
|
visibility: None,
|
|
|
|
name: Some(name),
|
|
|
|
ret_type: None,
|
|
|
|
parameters: params,
|
2020-01-14 17:02:01 +00:00
|
|
|
parameter_names: vec![],
|
2019-10-28 14:48:40 +00:00
|
|
|
generic_parameters: vec![],
|
|
|
|
where_predicates: vec![],
|
|
|
|
doc: None,
|
2020-02-23 09:49:53 +00:00
|
|
|
has_self_param: false,
|
2019-10-28 14:48:40 +00:00
|
|
|
}
|
|
|
|
.with_doc_opt(variant.docs(db)),
|
|
|
|
)
|
2019-10-28 01:26:12 +00:00
|
|
|
}
|
2019-10-29 16:16:55 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
pub(crate) fn from_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
|
2019-11-20 06:40:36 +00:00
|
|
|
let node: ast::MacroCall = macro_def.source(db).value;
|
2019-10-29 16:16:55 +00:00
|
|
|
|
|
|
|
let params = vec![];
|
|
|
|
|
|
|
|
Some(
|
|
|
|
FunctionSignature {
|
|
|
|
kind: CallableKind::Macro,
|
|
|
|
visibility: None,
|
|
|
|
name: node.name().map(|n| n.text().to_string()),
|
|
|
|
ret_type: None,
|
|
|
|
parameters: params,
|
2020-01-14 17:02:01 +00:00
|
|
|
parameter_names: vec![],
|
2019-10-29 16:16:55 +00:00
|
|
|
generic_parameters: vec![],
|
|
|
|
where_predicates: vec![],
|
|
|
|
doc: None,
|
2020-02-23 09:49:53 +00:00
|
|
|
has_self_param: false,
|
2019-10-29 16:16:55 +00:00
|
|
|
}
|
|
|
|
.with_doc_opt(macro_def.docs(db)),
|
|
|
|
)
|
|
|
|
}
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&'_ ast::FnDef> for FunctionSignature {
|
|
|
|
fn from(node: &ast::FnDef) -> FunctionSignature {
|
2020-02-23 09:49:53 +00:00
|
|
|
fn param_list(node: &ast::FnDef) -> (bool, Vec<String>) {
|
2019-04-08 13:34:59 +00:00
|
|
|
let mut res = vec![];
|
2020-02-23 09:49:53 +00:00
|
|
|
let mut has_self_param = false;
|
2019-04-08 13:34:59 +00:00
|
|
|
if let Some(param_list) = node.param_list() {
|
|
|
|
if let Some(self_param) = param_list.self_param() {
|
2020-02-23 09:49:53 +00:00
|
|
|
has_self_param = true;
|
2019-04-08 13:34:59 +00:00
|
|
|
res.push(self_param.syntax().text().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
res.extend(param_list.params().map(|param| param.syntax().text().to_string()));
|
|
|
|
}
|
2020-02-23 09:49:53 +00:00
|
|
|
(has_self_param, res)
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:02:01 +00:00
|
|
|
fn param_name_list(node: &ast::FnDef) -> Vec<String> {
|
|
|
|
let mut res = vec![];
|
|
|
|
if let Some(param_list) = node.param_list() {
|
|
|
|
if let Some(self_param) = param_list.self_param() {
|
|
|
|
res.push(self_param.syntax().text().to_string())
|
|
|
|
}
|
|
|
|
|
2020-01-18 12:40:32 +00:00
|
|
|
res.extend(
|
|
|
|
param_list
|
|
|
|
.params()
|
|
|
|
.map(|param| {
|
|
|
|
Some(
|
|
|
|
param
|
|
|
|
.pat()?
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::Name::cast)?
|
|
|
|
.text()
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|param| param.unwrap_or_default()),
|
|
|
|
);
|
2020-01-14 17:02:01 +00:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:49:53 +00:00
|
|
|
let (has_self_param, parameters) = param_list(node);
|
|
|
|
|
2019-04-08 13:34:59 +00:00
|
|
|
FunctionSignature {
|
2019-10-29 13:46:55 +00:00
|
|
|
kind: CallableKind::Function,
|
2019-04-08 13:34:59 +00:00
|
|
|
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
|
|
|
|
name: node.name().map(|n| n.text().to_string()),
|
|
|
|
ret_type: node
|
|
|
|
.ret_type()
|
|
|
|
.and_then(|r| r.type_ref())
|
|
|
|
.map(|n| n.syntax().text().to_string()),
|
2020-02-23 09:49:53 +00:00
|
|
|
parameters,
|
2020-01-14 17:02:01 +00:00
|
|
|
parameter_names: param_name_list(node),
|
2019-04-08 13:34:59 +00:00
|
|
|
generic_parameters: generic_parameters(node),
|
|
|
|
where_predicates: where_predicates(node),
|
|
|
|
// docs are processed separately
|
|
|
|
doc: None,
|
2020-02-23 09:49:53 +00:00
|
|
|
has_self_param,
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for FunctionSignature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
if let Some(t) = &self.visibility {
|
|
|
|
write!(f, "{} ", t)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(name) = &self.name {
|
2019-10-28 00:11:02 +00:00
|
|
|
match self.kind {
|
2019-10-29 13:46:55 +00:00
|
|
|
CallableKind::Function => write!(f, "fn {}", name)?,
|
|
|
|
CallableKind::StructConstructor => write!(f, "struct {}", name)?,
|
|
|
|
CallableKind::VariantConstructor => write!(f, "{}", name)?,
|
2019-10-29 16:16:55 +00:00
|
|
|
CallableKind::Macro => write!(f, "{}!", name)?,
|
2019-10-28 00:11:02 +00:00
|
|
|
}
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.generic_parameters.is_empty() {
|
2020-03-28 10:01:25 +00:00
|
|
|
write!(f, "{}", self.generic_parameters.iter().sep_by(", ").surround_with("<", ">"))?;
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 10:01:25 +00:00
|
|
|
write!(f, "{}", self.parameters.iter().sep_by(", ").surround_with("(", ")"))?;
|
2019-04-08 13:34:59 +00:00
|
|
|
|
|
|
|
if let Some(t) = &self.ret_type {
|
|
|
|
write!(f, " -> {}", t)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.where_predicates.is_empty() {
|
2020-03-28 10:01:25 +00:00
|
|
|
write!(f, "\nwhere {}", self.where_predicates.iter().sep_by(",\n "))?;
|
2019-04-08 13:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|