]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide/src/display.rs
722092de97d8380aaa1101d3359649fcbb87280f
[rust.git] / crates / ra_ide / src / display.rs
1 //! This module contains utilities for turning SyntaxNodes and HIR types
2 //! into types that may be used to render in a UI.
3
4 mod function_signature;
5 mod navigation_target;
6 mod structure;
7 mod short_label;
8
9 use std::fmt::Display;
10
11 use ra_syntax::{
12     ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
13     SyntaxKind::{ATTR, COMMENT},
14 };
15 use stdx::format_to;
16
17 pub use function_signature::FunctionSignature;
18 pub use navigation_target::NavigationTarget;
19 pub use structure::{file_structure, StructureNode};
20
21 pub(crate) use navigation_target::{ToNav, TryToNav};
22 pub(crate) use short_label::ShortLabel;
23
24 pub(crate) fn function_label(node: &ast::FnDef) -> String {
25     FunctionSignature::from(node).to_string()
26 }
27
28 pub(crate) fn const_label(node: &ast::ConstDef) -> String {
29     let label: String = node
30         .syntax()
31         .children_with_tokens()
32         .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
33         .map(|node| node.to_string())
34         .collect();
35
36     label.trim().to_owned()
37 }
38
39 pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String {
40     let label: String = node
41         .syntax()
42         .children_with_tokens()
43         .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
44         .map(|node| node.to_string())
45         .collect();
46
47     label.trim().to_owned()
48 }
49
50 pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> {
51     let mut res = vec![];
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()));
55     }
56     res
57 }
58
59 pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> {
60     let mut res = vec![];
61     if let Some(clause) = node.where_clause() {
62         res.extend(clause.predicates().map(|p| p.syntax().text().to_string()));
63     }
64     res
65 }
66
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)
71 }
72
73 pub(crate) fn rust_code_markup(code: &impl Display) -> String {
74     rust_code_markup_with_doc(code, None, None)
75 }
76
77 pub(crate) fn rust_code_markup_with_doc(
78     code: &impl Display,
79     doc: Option<&str>,
80     mod_path: Option<&str>,
81 ) -> String {
82     let mut buf = "```rust\n".to_owned();
83
84     if let Some(mod_path) = mod_path {
85         if !mod_path.is_empty() {
86             format_to!(buf, "{}\n", mod_path);
87         }
88     }
89     format_to!(buf, "{}\n```", code);
90
91     if let Some(doc) = doc {
92         format_to!(buf, "\n\n{}", doc);
93     }
94
95     buf
96 }