2019-04-08 13:04:58 +00:00
|
|
|
//! This module contains utilities for turning SyntaxNodes and HIR types
|
2019-04-09 13:08:24 +00:00
|
|
|
//! into types that may be used to render in a UI.
|
2019-04-08 13:07:50 +00:00
|
|
|
|
2019-04-08 13:34:59 +00:00
|
|
|
mod function_signature;
|
2019-04-08 13:07:50 +00:00
|
|
|
mod navigation_target;
|
|
|
|
mod structure;
|
2019-06-09 19:28:53 +00:00
|
|
|
mod short_label;
|
2019-04-08 13:07:50 +00:00
|
|
|
|
2020-03-28 10:20:34 +00:00
|
|
|
use std::fmt::Display;
|
2020-03-16 00:35:59 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_syntax::{
|
2019-09-10 05:32:47 +00:00
|
|
|
ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
|
2019-07-04 20:05:17 +00:00
|
|
|
SyntaxKind::{ATTR, COMMENT},
|
|
|
|
};
|
2020-03-28 10:20:34 +00:00
|
|
|
use stdx::format_to;
|
2019-04-08 07:25:35 +00:00
|
|
|
|
2019-04-08 13:34:59 +00:00
|
|
|
pub use function_signature::FunctionSignature;
|
2019-07-04 20:05:17 +00:00
|
|
|
pub use navigation_target::NavigationTarget;
|
|
|
|
pub use structure::{file_structure, StructureNode};
|
2019-04-08 09:20:28 +00:00
|
|
|
|
2020-02-22 15:57:29 +00:00
|
|
|
pub(crate) use navigation_target::{ToNav, TryToNav};
|
2019-06-09 19:28:53 +00:00
|
|
|
pub(crate) use short_label::ShortLabel;
|
2019-06-09 15:59:59 +00:00
|
|
|
|
2019-04-08 08:44:23 +00:00
|
|
|
pub(crate) fn function_label(node: &ast::FnDef) -> String {
|
|
|
|
FunctionSignature::from(node).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn type_label(node: &ast::TypeAliasDef) -> 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()
|
|
|
|
}
|
|
|
|
|
2019-04-08 07:25:35 +00:00
|
|
|
pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> {
|
|
|
|
let mut res = vec![];
|
|
|
|
if let Some(type_params) = node.type_param_list() {
|
|
|
|
res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string()));
|
|
|
|
res.extend(type_params.type_params().map(|p| p.syntax().text().to_string()));
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> {
|
|
|
|
let mut res = vec![];
|
|
|
|
if let Some(clause) = node.where_clause() {
|
|
|
|
res.extend(clause.predicates().map(|p| p.syntax().text().to_string()));
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2019-04-09 11:43:11 +00:00
|
|
|
|
2019-09-10 05:32:47 +00: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)
|
|
|
|
}
|
|
|
|
|
2020-03-16 00:35:59 +00:00
|
|
|
pub(crate) fn rust_code_markup(code: &impl Display) -> String {
|
|
|
|
rust_code_markup_with_doc(code, None, None)
|
2019-04-09 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 00:35:59 +00:00
|
|
|
pub(crate) fn rust_code_markup_with_doc(
|
|
|
|
code: &impl Display,
|
|
|
|
doc: Option<&str>,
|
|
|
|
mod_path: Option<&str>,
|
|
|
|
) -> String {
|
2020-05-22 18:10:37 +00:00
|
|
|
let mut buf = String::new();
|
2020-03-16 00:35:59 +00:00
|
|
|
|
|
|
|
if let Some(mod_path) = mod_path {
|
|
|
|
if !mod_path.is_empty() {
|
2020-05-22 18:10:37 +00:00
|
|
|
format_to!(buf, "{}\n___\n\n", mod_path);
|
2020-03-16 00:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 18:10:37 +00:00
|
|
|
format_to!(buf, "```rust\n{}\n```", code);
|
2020-03-16 00:35:59 +00:00
|
|
|
|
2019-04-09 11:43:11 +00:00
|
|
|
if let Some(doc) = doc {
|
2020-03-28 10:20:34 +00:00
|
|
|
format_to!(buf, "\n\n{}", doc);
|
2019-04-09 11:43:11 +00:00
|
|
|
}
|
2020-03-16 00:35:59 +00:00
|
|
|
|
2020-03-28 10:20:34 +00:00
|
|
|
buf
|
2019-04-09 11:43:11 +00:00
|
|
|
}
|