Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! This module contains utilities for rendering syntax nodes into a string representing their signature.

use crate::ast::{self, HasAttrs, HasGenericParams, HasName};

use ast::HasVisibility;
use stdx::format_to;

pub fn function_declaration(node: &ast::Fn) -> String {
    let mut buf = String::new();
    if let Some(vis) = node.visibility() {
        format_to!(buf, "{} ", vis);
    }
    if node.async_token().is_some() {
        format_to!(buf, "async ");
    }
    if node.const_token().is_some() {
        format_to!(buf, "const ");
    }
    if node.unsafe_token().is_some() {
        format_to!(buf, "unsafe ");
    }
    if let Some(abi) = node.abi() {
        // Keyword `extern` is included in the string.
        format_to!(buf, "{} ", abi);
    }
    if let Some(name) = node.name() {
        format_to!(buf, "fn {}", name);
    }
    if let Some(type_params) = node.generic_param_list() {
        format_to!(buf, "{}", type_params);
    }
    if let Some(param_list) = node.param_list() {
        let params: Vec<String> = param_list
            .self_param()
            .into_iter()
            .map(|self_param| self_param.to_string())
            .chain(param_list.params().map(|param| param.to_string()))
            .collect();
        // Useful to inline parameters
        format_to!(buf, "({})", params.join(", "));
    }
    if let Some(ret_type) = node.ret_type() {
        if ret_type.ty().is_some() {
            format_to!(buf, " {}", ret_type);
        }
    }
    if let Some(where_clause) = node.where_clause() {
        format_to!(buf, "\n{}", where_clause);
    }
    buf
}

pub fn const_label(node: &ast::Const) -> String {
    let mut s = String::new();
    if let Some(vis) = node.visibility() {
        format_to!(s, "{} ", vis);
    }
    format_to!(s, "const ");
    if let Some(name) = node.name() {
        format_to!(s, "{}", name);
    } else {
        format_to!(s, "?");
    }
    format_to!(s, ": ");
    if let Some(ty) = node.ty() {
        format_to!(s, "{}", ty);
    } else {
        format_to!(s, "?");
    }
    format_to!(s, ";");
    s
}

pub fn type_label(node: &ast::TypeAlias) -> String {
    let mut s = String::new();
    if let Some(vis) = node.visibility() {
        format_to!(s, "{} ", vis);
    }
    format_to!(s, "type ");
    if let Some(name) = node.name() {
        format_to!(s, "{}", name);
    } else {
        format_to!(s, "?");
    }
    format_to!(s, ";");
    s
}

pub fn macro_label(node: &ast::Macro) -> String {
    let name = node.name();
    let mut s = String::new();
    match node {
        ast::Macro::MacroRules(node) => {
            let vis = if node.has_atom_attr("macro_export") { "#[macro_export] " } else { "" };
            format_to!(s, "{}macro_rules!", vis);
        }
        ast::Macro::MacroDef(node) => {
            if let Some(vis) = node.visibility() {
                format_to!(s, "{} ", vis);
            }
            format_to!(s, "macro");
        }
    }
    if let Some(name) = name {
        format_to!(s, " {}", name);
    }
    s
}

pub fn fn_as_proc_macro_label(node: &ast::Fn) -> String {
    let name = node.name();
    let mut s = String::new();
    if let Some(vis) = node.visibility() {
        format_to!(s, "{} ", vis);
    }
    format_to!(s, "macro");
    if let Some(name) = name {
        format_to!(s, " {}", name);
    }
    s
}