aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-10-18 11:09:00 +0100
committerIgor Aleksanov <[email protected]>2020-10-18 11:09:00 +0100
commit9e7c952bbddc2e6763c49f0511a295362e9893d6 (patch)
tree5b144eabe1eaf62aa1ec5b804ee6fff00f5f84e9 /crates/syntax
parent2067a410f31810f6e1941a86cdea0247c3b7d6f4 (diff)
Extract call_info and completion into separate crates
Diffstat (limited to 'crates/syntax')
-rw-r--r--crates/syntax/src/display.rs83
-rw-r--r--crates/syntax/src/lib.rs1
2 files changed, 84 insertions, 0 deletions
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
4use crate::{
5 ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner},
6 SyntaxKind::{ATTR, COMMENT},
7};
8
9use ast::VisibilityOwner;
10use stdx::format_to;
11
12pub 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
57pub 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
68pub 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
79pub 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)]
33mod tests; 33mod tests;
34 34
35pub mod display;
35pub mod algo; 36pub mod algo;
36pub mod ast; 37pub mod ast;
37#[doc(hidden)] 38#[doc(hidden)]