1 //! This module contains utilities for turning SyntaxNodes and HIR types
2 //! into types that may be used to render in a UI.
4 mod function_signature;
12 ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
13 SyntaxKind::{ATTR, COMMENT},
17 pub use function_signature::FunctionSignature;
18 pub use navigation_target::NavigationTarget;
19 pub use structure::{file_structure, StructureNode};
21 pub(crate) use navigation_target::{ToNav, TryToNav};
22 pub(crate) use short_label::ShortLabel;
24 pub(crate) fn function_label(node: &ast::FnDef) -> String {
25 FunctionSignature::from(node).to_string()
28 pub(crate) fn const_label(node: &ast::ConstDef) -> String {
29 let label: String = node
31 .children_with_tokens()
32 .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
33 .map(|node| node.to_string())
36 label.trim().to_owned()
39 pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String {
40 let label: String = node
42 .children_with_tokens()
43 .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
44 .map(|node| node.to_string())
47 label.trim().to_owned()
50 pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> {
52 if let Some(type_params) = node.type_param_list() {
53 res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string()));
54 res.extend(type_params.type_params().map(|p| p.syntax().text().to_string()));
59 pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> {
61 if let Some(clause) = node.where_clause() {
62 res.extend(clause.predicates().map(|p| p.syntax().text().to_string()));
67 pub(crate) fn macro_label(node: &ast::MacroCall) -> String {
68 let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default();
69 let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" };
70 format!("{}macro_rules! {}", vis, name)
73 pub(crate) fn rust_code_markup(code: &impl Display) -> String {
74 rust_code_markup_with_doc(code, None, None)
77 pub(crate) fn rust_code_markup_with_doc(
80 mod_path: Option<&str>,
82 let mut buf = "```rust\n".to_owned();
84 if let Some(mod_path) = mod_path {
85 if !mod_path.is_empty() {
86 format_to!(buf, "{}\n", mod_path);
89 format_to!(buf, "{}\n```", code);
91 if let Some(doc) = doc {
92 format_to!(buf, "\n\n{}", doc);