aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/display.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/display.rs')
-rw-r--r--crates/ra_ide/src/display.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs
new file mode 100644
index 000000000..30617412a
--- /dev/null
+++ b/crates/ra_ide/src/display.rs
@@ -0,0 +1,84 @@
1//! This module contains utilities for turning SyntaxNodes and HIR types
2//! into types that may be used to render in a UI.
3
4mod function_signature;
5mod navigation_target;
6mod structure;
7mod short_label;
8
9use ra_syntax::{
10 ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
11 SyntaxKind::{ATTR, COMMENT},
12};
13
14pub use function_signature::FunctionSignature;
15pub use navigation_target::NavigationTarget;
16pub use structure::{file_structure, StructureNode};
17
18pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol, ToNav};
19pub(crate) use short_label::ShortLabel;
20
21pub(crate) fn function_label(node: &ast::FnDef) -> String {
22 FunctionSignature::from(node).to_string()
23}
24
25pub(crate) fn const_label(node: &ast::ConstDef) -> String {
26 let label: String = node
27 .syntax()
28 .children_with_tokens()
29 .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
30 .map(|node| node.to_string())
31 .collect();
32
33 label.trim().to_owned()
34}
35
36pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String {
37 let label: String = node
38 .syntax()
39 .children_with_tokens()
40 .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
41 .map(|node| node.to_string())
42 .collect();
43
44 label.trim().to_owned()
45}
46
47pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> {
48 let mut res = vec![];
49 if let Some(type_params) = node.type_param_list() {
50 res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string()));
51 res.extend(type_params.type_params().map(|p| p.syntax().text().to_string()));
52 }
53 res
54}
55
56pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> {
57 let mut res = vec![];
58 if let Some(clause) = node.where_clause() {
59 res.extend(clause.predicates().map(|p| p.syntax().text().to_string()));
60 }
61 res
62}
63
64pub(crate) fn macro_label(node: &ast::MacroCall) -> String {
65 let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default();
66 let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" };
67 format!("{}macro_rules! {}", vis, name)
68}
69
70pub(crate) fn rust_code_markup<CODE: AsRef<str>>(val: CODE) -> String {
71 rust_code_markup_with_doc::<_, &str>(val, None)
72}
73
74pub(crate) fn rust_code_markup_with_doc<CODE, DOC>(val: CODE, doc: Option<DOC>) -> String
75where
76 CODE: AsRef<str>,
77 DOC: AsRef<str>,
78{
79 if let Some(doc) = doc {
80 format!("```rust\n{}\n```\n\n{}", val.as_ref(), doc.as_ref())
81 } else {
82 format!("```rust\n{}\n```", val.as_ref())
83 }
84}