diff options
Diffstat (limited to 'crates/ide/src/display.rs')
-rw-r--r-- | crates/ide/src/display.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/crates/ide/src/display.rs b/crates/ide/src/display.rs new file mode 100644 index 000000000..41b5bdc49 --- /dev/null +++ b/crates/ide/src/display.rs | |||
@@ -0,0 +1,83 @@ | |||
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 navigation_target; | ||
5 | mod short_label; | ||
6 | |||
7 | use syntax::{ | ||
8 | ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}, | ||
9 | SyntaxKind::{ATTR, COMMENT}, | ||
10 | }; | ||
11 | |||
12 | use ast::VisibilityOwner; | ||
13 | use stdx::format_to; | ||
14 | |||
15 | pub use navigation_target::NavigationTarget; | ||
16 | pub(crate) use navigation_target::{ToNav, TryToNav}; | ||
17 | pub(crate) use short_label::ShortLabel; | ||
18 | |||
19 | pub(crate) fn function_declaration(node: &ast::Fn) -> String { | ||
20 | let mut buf = String::new(); | ||
21 | if let Some(vis) = node.visibility() { | ||
22 | format_to!(buf, "{} ", vis); | ||
23 | } | ||
24 | if node.async_token().is_some() { | ||
25 | format_to!(buf, "async "); | ||
26 | } | ||
27 | if node.const_token().is_some() { | ||
28 | format_to!(buf, "const "); | ||
29 | } | ||
30 | if node.unsafe_token().is_some() { | ||
31 | format_to!(buf, "unsafe "); | ||
32 | } | ||
33 | if let Some(abi) = node.abi() { | ||
34 | // Keyword `extern` is included in the string. | ||
35 | format_to!(buf, "{} ", abi); | ||
36 | } | ||
37 | if let Some(name) = node.name() { | ||
38 | format_to!(buf, "fn {}", name) | ||
39 | } | ||
40 | if let Some(type_params) = node.generic_param_list() { | ||
41 | format_to!(buf, "{}", type_params); | ||
42 | } | ||
43 | if let Some(param_list) = node.param_list() { | ||
44 | format_to!(buf, "{}", param_list); | ||
45 | } | ||
46 | if let Some(ret_type) = node.ret_type() { | ||
47 | if ret_type.ty().is_some() { | ||
48 | format_to!(buf, " {}", ret_type); | ||
49 | } | ||
50 | } | ||
51 | if let Some(where_clause) = node.where_clause() { | ||
52 | format_to!(buf, "\n{}", where_clause); | ||
53 | } | ||
54 | buf | ||
55 | } | ||
56 | |||
57 | pub(crate) fn const_label(node: &ast::Const) -> String { | ||
58 | let label: String = node | ||
59 | .syntax() | ||
60 | .children_with_tokens() | ||
61 | .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR)) | ||
62 | .map(|node| node.to_string()) | ||
63 | .collect(); | ||
64 | |||
65 | label.trim().to_owned() | ||
66 | } | ||
67 | |||
68 | pub(crate) fn type_label(node: &ast::TypeAlias) -> String { | ||
69 | let label: String = node | ||
70 | .syntax() | ||
71 | .children_with_tokens() | ||
72 | .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR)) | ||
73 | .map(|node| node.to_string()) | ||
74 | .collect(); | ||
75 | |||
76 | label.trim().to_owned() | ||
77 | } | ||
78 | |||
79 | pub(crate) fn macro_label(node: &ast::MacroCall) -> String { | ||
80 | let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default(); | ||
81 | let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" }; | ||
82 | format!("{}macro_rules! {}", vis, name) | ||
83 | } | ||