diff options
Diffstat (limited to 'crates/ide/src/display.rs')
-rw-r--r-- | crates/ide/src/display.rs | 81 |
1 files changed, 1 insertions, 80 deletions
diff --git a/crates/ide/src/display.rs b/crates/ide/src/display.rs index 2484dbbf1..0650915c5 100644 --- a/crates/ide/src/display.rs +++ b/crates/ide/src/display.rs | |||
@@ -4,87 +4,8 @@ | |||
4 | mod navigation_target; | 4 | mod navigation_target; |
5 | mod short_label; | 5 | mod short_label; |
6 | 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; | 7 | pub use navigation_target::NavigationTarget; |
16 | pub(crate) use navigation_target::{ToNav, TryToNav}; | 8 | pub(crate) use navigation_target::{ToNav, TryToNav}; |
17 | pub(crate) use short_label::ShortLabel; | 9 | pub(crate) use short_label::ShortLabel; |
18 | 10 | ||
19 | pub(crate) fn function_declaration(node: &ast::Fn) -> String { | 11 | pub(crate) use syntax::display::{function_declaration, macro_label}; |
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 | let params: Vec<String> = param_list | ||
45 | .self_param() | ||
46 | .into_iter() | ||
47 | .map(|self_param| self_param.to_string()) | ||
48 | .chain(param_list.params().map(|param| param.to_string())) | ||
49 | .collect(); | ||
50 | // Useful to inline parameters | ||
51 | format_to!(buf, "({})", params.join(", ")); | ||
52 | } | ||
53 | if let Some(ret_type) = node.ret_type() { | ||
54 | if ret_type.ty().is_some() { | ||
55 | format_to!(buf, " {}", ret_type); | ||
56 | } | ||
57 | } | ||
58 | if let Some(where_clause) = node.where_clause() { | ||
59 | format_to!(buf, "\n{}", where_clause); | ||
60 | } | ||
61 | buf | ||
62 | } | ||
63 | |||
64 | pub(crate) fn const_label(node: &ast::Const) -> String { | ||
65 | let label: String = node | ||
66 | .syntax() | ||
67 | .children_with_tokens() | ||
68 | .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR)) | ||
69 | .map(|node| node.to_string()) | ||
70 | .collect(); | ||
71 | |||
72 | label.trim().to_owned() | ||
73 | } | ||
74 | |||
75 | pub(crate) fn type_label(node: &ast::TypeAlias) -> String { | ||
76 | let label: String = node | ||
77 | .syntax() | ||
78 | .children_with_tokens() | ||
79 | .filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR)) | ||
80 | .map(|node| node.to_string()) | ||
81 | .collect(); | ||
82 | |||
83 | label.trim().to_owned() | ||
84 | } | ||
85 | |||
86 | pub(crate) fn macro_label(node: &ast::MacroCall) -> String { | ||
87 | let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default(); | ||
88 | let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" }; | ||
89 | format!("{}macro_rules! {}", vis, name) | ||
90 | } | ||