diff options
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/traits.rs | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs new file mode 100644 index 000000000..85fe6d5e1 --- /dev/null +++ b/crates/ra_syntax/src/ast/traits.rs | |||
@@ -0,0 +1,148 @@ | |||
1 | use itertools::Itertools; | ||
2 | |||
3 | use crate::{ | ||
4 | syntax_node::{SyntaxNodeChildren, SyntaxElementChildren}, | ||
5 | ast::{self, child_opt, children, AstNode, AstChildren}, | ||
6 | }; | ||
7 | |||
8 | pub trait TypeAscriptionOwner: AstNode { | ||
9 | fn ascribed_type(&self) -> Option<&ast::TypeRef> { | ||
10 | child_opt(self) | ||
11 | } | ||
12 | } | ||
13 | |||
14 | pub trait NameOwner: AstNode { | ||
15 | fn name(&self) -> Option<&ast::Name> { | ||
16 | child_opt(self) | ||
17 | } | ||
18 | } | ||
19 | |||
20 | pub trait VisibilityOwner: AstNode { | ||
21 | fn visibility(&self) -> Option<&ast::Visibility> { | ||
22 | child_opt(self) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | pub trait LoopBodyOwner: AstNode { | ||
27 | fn loop_body(&self) -> Option<&ast::Block> { | ||
28 | child_opt(self) | ||
29 | } | ||
30 | } | ||
31 | |||
32 | pub trait ArgListOwner: AstNode { | ||
33 | fn arg_list(&self) -> Option<&ast::ArgList> { | ||
34 | child_opt(self) | ||
35 | } | ||
36 | } | ||
37 | |||
38 | pub trait FnDefOwner: AstNode { | ||
39 | fn functions(&self) -> AstChildren<ast::FnDef> { | ||
40 | children(self) | ||
41 | } | ||
42 | } | ||
43 | |||
44 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
45 | pub enum ItemOrMacro<'a> { | ||
46 | Item(&'a ast::ModuleItem), | ||
47 | Macro(&'a ast::MacroCall), | ||
48 | } | ||
49 | |||
50 | pub trait ModuleItemOwner: AstNode { | ||
51 | fn items(&self) -> AstChildren<ast::ModuleItem> { | ||
52 | children(self) | ||
53 | } | ||
54 | fn items_with_macros(&self) -> ItemOrMacroIter { | ||
55 | ItemOrMacroIter(self.syntax().children()) | ||
56 | } | ||
57 | } | ||
58 | |||
59 | #[derive(Debug)] | ||
60 | pub struct ItemOrMacroIter<'a>(SyntaxNodeChildren<'a>); | ||
61 | |||
62 | impl<'a> Iterator for ItemOrMacroIter<'a> { | ||
63 | type Item = ItemOrMacro<'a>; | ||
64 | fn next(&mut self) -> Option<ItemOrMacro<'a>> { | ||
65 | loop { | ||
66 | let n = self.0.next()?; | ||
67 | if let Some(item) = ast::ModuleItem::cast(n) { | ||
68 | return Some(ItemOrMacro::Item(item)); | ||
69 | } | ||
70 | if let Some(call) = ast::MacroCall::cast(n) { | ||
71 | return Some(ItemOrMacro::Macro(call)); | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | pub trait TypeParamsOwner: AstNode { | ||
78 | fn type_param_list(&self) -> Option<&ast::TypeParamList> { | ||
79 | child_opt(self) | ||
80 | } | ||
81 | |||
82 | fn where_clause(&self) -> Option<&ast::WhereClause> { | ||
83 | child_opt(self) | ||
84 | } | ||
85 | } | ||
86 | |||
87 | pub trait TypeBoundsOwner: AstNode { | ||
88 | fn type_bound_list(&self) -> Option<&ast::TypeBoundList> { | ||
89 | child_opt(self) | ||
90 | } | ||
91 | } | ||
92 | |||
93 | pub trait AttrsOwner: AstNode { | ||
94 | fn attrs(&self) -> AstChildren<ast::Attr> { | ||
95 | children(self) | ||
96 | } | ||
97 | fn has_atom_attr(&self, atom: &str) -> bool { | ||
98 | self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) | ||
99 | } | ||
100 | } | ||
101 | |||
102 | pub trait DocCommentsOwner: AstNode { | ||
103 | fn doc_comments(&self) -> CommentIter { | ||
104 | CommentIter { iter: self.syntax().children_with_tokens() } | ||
105 | } | ||
106 | |||
107 | /// Returns the textual content of a doc comment block as a single string. | ||
108 | /// That is, strips leading `///` (+ optional 1 character of whitespace) | ||
109 | /// and joins lines. | ||
110 | fn doc_comment_text(&self) -> Option<std::string::String> { | ||
111 | let docs = self | ||
112 | .doc_comments() | ||
113 | .filter(|comment| comment.is_doc_comment()) | ||
114 | .map(|comment| { | ||
115 | let prefix_len = comment.prefix().len(); | ||
116 | |||
117 | let line = comment.text().as_str(); | ||
118 | |||
119 | // Determine if the prefix or prefix + 1 char is stripped | ||
120 | let pos = | ||
121 | if line.chars().nth(prefix_len).map(|c| c.is_whitespace()).unwrap_or(false) { | ||
122 | prefix_len + 1 | ||
123 | } else { | ||
124 | prefix_len | ||
125 | }; | ||
126 | |||
127 | line[pos..].to_owned() | ||
128 | }) | ||
129 | .join("\n"); | ||
130 | |||
131 | if docs.is_empty() { | ||
132 | None | ||
133 | } else { | ||
134 | Some(docs) | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | pub struct CommentIter<'a> { | ||
140 | iter: SyntaxElementChildren<'a>, | ||
141 | } | ||
142 | |||
143 | impl<'a> Iterator for CommentIter<'a> { | ||
144 | type Item = ast::Comment<'a>; | ||
145 | fn next(&mut self) -> Option<ast::Comment<'a>> { | ||
146 | self.iter.by_ref().find_map(|el| el.as_token().and_then(ast::Comment::cast)) | ||
147 | } | ||
148 | } | ||