2019-04-08 08:04:58 -05:00
|
|
|
//! This module contains utilities for turning SyntaxNodes and HIR types
|
2019-04-09 08:08:24 -05:00
|
|
|
//! into types that may be used to render in a UI.
|
2019-04-08 08:07:50 -05:00
|
|
|
|
|
|
|
mod navigation_target;
|
2019-06-09 14:28:53 -05:00
|
|
|
mod short_label;
|
2019-04-08 08:07:50 -05:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_syntax::{
|
2020-07-30 08:36:21 -05:00
|
|
|
ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner},
|
2019-07-04 15:05:17 -05:00
|
|
|
SyntaxKind::{ATTR, COMMENT},
|
|
|
|
};
|
2019-04-08 02:25:35 -05:00
|
|
|
|
2020-07-16 14:32:20 -05:00
|
|
|
use ast::VisibilityOwner;
|
|
|
|
use stdx::format_to;
|
2020-07-16 11:13:43 -05:00
|
|
|
|
2020-07-16 14:33:11 -05:00
|
|
|
pub use navigation_target::NavigationTarget;
|
|
|
|
pub(crate) use navigation_target::{ToNav, TryToNav};
|
|
|
|
pub(crate) use short_label::ShortLabel;
|
|
|
|
|
2020-07-30 07:51:08 -05:00
|
|
|
pub(crate) fn function_declaration(node: &ast::Fn) -> String {
|
2020-07-16 14:32:20 -05:00
|
|
|
let mut buf = String::new();
|
|
|
|
if let Some(vis) = node.visibility() {
|
|
|
|
format_to!(buf, "{} ", vis);
|
|
|
|
}
|
|
|
|
if node.async_token().is_some() {
|
|
|
|
format_to!(buf, "async ");
|
|
|
|
}
|
|
|
|
if node.const_token().is_some() {
|
|
|
|
format_to!(buf, "const ");
|
|
|
|
}
|
|
|
|
if node.unsafe_token().is_some() {
|
|
|
|
format_to!(buf, "unsafe ");
|
|
|
|
}
|
|
|
|
if let Some(abi) = node.abi() {
|
|
|
|
// Keyword `extern` is included in the string.
|
|
|
|
format_to!(buf, "{} ", abi);
|
|
|
|
}
|
|
|
|
if let Some(name) = node.name() {
|
|
|
|
format_to!(buf, "fn {}", name)
|
|
|
|
}
|
2020-07-30 08:36:21 -05:00
|
|
|
if let Some(type_params) = node.generic_param_list() {
|
2020-07-16 14:32:20 -05:00
|
|
|
format_to!(buf, "{}", type_params);
|
|
|
|
}
|
|
|
|
if let Some(param_list) = node.param_list() {
|
|
|
|
format_to!(buf, "{}", param_list);
|
|
|
|
}
|
|
|
|
if let Some(ret_type) = node.ret_type() {
|
|
|
|
if ret_type.type_ref().is_some() {
|
|
|
|
format_to!(buf, " {}", ret_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(where_clause) = node.where_clause() {
|
|
|
|
format_to!(buf, "\n{}", where_clause);
|
|
|
|
}
|
|
|
|
buf
|
2019-04-08 03:44:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn const_label(node: &ast::ConstDef) -> String {
|
|
|
|
let label: String = node
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
|
|
|
|
.map(|node| node.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
label.trim().to_owned()
|
|
|
|
}
|
|
|
|
|
2020-07-30 08:25:46 -05:00
|
|
|
pub(crate) fn type_label(node: &ast::TypeAlias) -> String {
|
2019-04-08 03:44:23 -05:00
|
|
|
let label: String = node
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
|
|
|
|
.map(|node| node.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
label.trim().to_owned()
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:32:47 -05:00
|
|
|
pub(crate) fn macro_label(node: &ast::MacroCall) -> String {
|
|
|
|
let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default();
|
|
|
|
let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" };
|
|
|
|
format!("{}macro_rules! {}", vis, name)
|
|
|
|
}
|