diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 3 | ||||
-rw-r--r-- | crates/syntax/src/display.rs | 83 | ||||
-rw-r--r-- | crates/syntax/src/lib.rs | 1 |
3 files changed, 87 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 74dbdfaf7..5b06cb767 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -172,6 +172,9 @@ pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { | |||
172 | pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr { | 172 | pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr { |
173 | expr_from_text(&format!("{}.{}{}", receiver, method, arg_list)) | 173 | expr_from_text(&format!("{}.{}{}", receiver, method, arg_list)) |
174 | } | 174 | } |
175 | pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { | ||
176 | expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) }) | ||
177 | } | ||
175 | fn expr_from_text(text: &str) -> ast::Expr { | 178 | fn expr_from_text(text: &str) -> ast::Expr { |
176 | ast_from_text(&format!("const C: () = {};", text)) | 179 | ast_from_text(&format!("const C: () = {};", text)) |
177 | } | 180 | } |
diff --git a/crates/syntax/src/display.rs b/crates/syntax/src/display.rs new file mode 100644 index 000000000..8d2c7eae4 --- /dev/null +++ b/crates/syntax/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 | use crate::{ | ||
5 | ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}, | ||
6 | SyntaxKind::{ATTR, COMMENT}, | ||
7 | }; | ||
8 | |||
9 | use ast::VisibilityOwner; | ||
10 | use stdx::format_to; | ||
11 | |||
12 | pub fn function_declaration(node: &ast::Fn) -> String { | ||
13 | let mut buf = String::new(); | ||
14 | if let Some(vis) = node.visibility() { | ||
15 | format_to!(buf, "{} ", vis); | ||
16 | } | ||
17 | if node.async_token().is_some() { | ||
18 | format_to!(buf, "async "); | ||
19 | } | ||
20 | if node.const_token().is_some() { | ||
21 | format_to!(buf, "const "); | ||
22 | } | ||
23 | if node.unsafe_token().is_some() { | ||
24 | format_to!(buf, "unsafe "); | ||
25 | } | ||
26 | if let Some(abi) = node.abi() { | ||
27 | // Keyword `extern` is included in the string. | ||
28 | format_to!(buf, "{} ", abi); | ||
29 | } | ||
30 | if let Some(name) = node.name() { | ||
31 | format_to!(buf, "fn {}", name) | ||
32 | } | ||
33 | if let Some(type_params) = node.generic_param_list() { | ||
34 | format_to!(buf, "{}", type_params); | ||
35 | } | ||
36 | if let Some(param_list) = node.param_list() { | ||
37 | let params: Vec<String> = param_list | ||
38 | .self_param() | ||
39 | .into_iter() | ||
40 | .map(|self_param| self_param.to_string()) | ||
41 | .chain(param_list.params().map(|param| param.to_string())) | ||
42 | .collect(); | ||
43 | // Useful to inline parameters | ||
44 | format_to!(buf, "({})", params.join(", ")); | ||
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 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 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 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 | } | ||
diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 7f8da66af..849a1cdd6 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs | |||
@@ -32,6 +32,7 @@ mod ptr; | |||
32 | #[cfg(test)] | 32 | #[cfg(test)] |
33 | mod tests; | 33 | mod tests; |
34 | 34 | ||
35 | pub mod display; | ||
35 | pub mod algo; | 36 | pub mod algo; |
36 | pub mod ast; | 37 | pub mod ast; |
37 | #[doc(hidden)] | 38 | #[doc(hidden)] |