diff options
author | Jeremy Kolb <[email protected]> | 2019-01-14 01:25:14 +0000 |
---|---|---|
committer | Jeremy Kolb <[email protected]> | 2019-01-14 01:25:14 +0000 |
commit | da326dca6005fa23fa6f8524bde1abd3a156c0c7 (patch) | |
tree | 202df810d4688053e01771a76607ef8dd9611af3 /crates | |
parent | 2d7b4cc548a5b72c8b268a774444a59dd1ce669e (diff) |
Remove duplication
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 97 |
1 files changed, 23 insertions, 74 deletions
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 4dafefa2c..d73c5bc31 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use ra_db::{Cancelable, SyntaxDatabase}; | 1 | use ra_db::{Cancelable, SyntaxDatabase}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | AstNode, SyntaxNode, TreeArc, | 3 | AstNode, SyntaxNode, TreeArc, |
4 | ast::{self, NameOwner, VisibilityOwner}, | 4 | ast::self, |
5 | algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}}, | 5 | algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}}, |
6 | }; | 6 | }; |
7 | 7 | ||
@@ -137,80 +137,29 @@ impl NavigationTarget { | |||
137 | fn description(&self, db: &RootDatabase) -> Option<String> { | 137 | fn description(&self, db: &RootDatabase) -> Option<String> { |
138 | // TODO: After type inference is done, add type information to improve the output | 138 | // TODO: After type inference is done, add type information to improve the output |
139 | let node = self.node(db)?; | 139 | let node = self.node(db)?; |
140 | // TODO: Refactor to be have less repetition | 140 | |
141 | fn visit_node<T>(node: &T, label: &str) -> Option<String> | ||
142 | where | ||
143 | T: ast::NameOwner + ast::VisibilityOwner, | ||
144 | { | ||
145 | let mut string = node | ||
146 | .visibility() | ||
147 | .map(|v| format!("{} ", v.syntax().text())) | ||
148 | .unwrap_or_default(); | ||
149 | string.push_str(label); | ||
150 | node.name()?.syntax().text().push_to(&mut string); | ||
151 | Some(string) | ||
152 | } | ||
153 | |||
141 | visitor() | 154 | visitor() |
142 | .visit(|node: &ast::FnDef| { | 155 | .visit(|node: &ast::FnDef| visit_node(node, "fn ")) |
143 | let mut string = node | 156 | .visit(|node: &ast::StructDef| visit_node(node, "struct ")) |
144 | .visibility() | 157 | .visit(|node: &ast::EnumDef| visit_node(node, "enum ")) |
145 | .map(|v| format!("{} ", v.syntax().text())) | 158 | .visit(|node: &ast::TraitDef| visit_node(node, "trait ")) |
146 | .unwrap_or_default(); | 159 | .visit(|node: &ast::Module| visit_node(node, "mod ")) |
147 | string.push_str("fn "); | 160 | .visit(|node: &ast::TypeDef| visit_node(node, "type ")) |
148 | node.name()?.syntax().text().push_to(&mut string); | 161 | .visit(|node: &ast::ConstDef| visit_node(node, "const ")) |
149 | Some(string) | 162 | .visit(|node: &ast::StaticDef| visit_node(node, "static ")) |
150 | }) | ||
151 | .visit(|node: &ast::StructDef| { | ||
152 | let mut string = node | ||
153 | .visibility() | ||
154 | .map(|v| format!("{} ", v.syntax().text())) | ||
155 | .unwrap_or_default(); | ||
156 | string.push_str("struct "); | ||
157 | node.name()?.syntax().text().push_to(&mut string); | ||
158 | Some(string) | ||
159 | }) | ||
160 | .visit(|node: &ast::EnumDef| { | ||
161 | let mut string = node | ||
162 | .visibility() | ||
163 | .map(|v| format!("{} ", v.syntax().text())) | ||
164 | .unwrap_or_default(); | ||
165 | string.push_str("enum "); | ||
166 | node.name()?.syntax().text().push_to(&mut string); | ||
167 | Some(string) | ||
168 | }) | ||
169 | .visit(|node: &ast::TraitDef| { | ||
170 | let mut string = node | ||
171 | .visibility() | ||
172 | .map(|v| format!("{} ", v.syntax().text())) | ||
173 | .unwrap_or_default(); | ||
174 | string.push_str("trait "); | ||
175 | node.name()?.syntax().text().push_to(&mut string); | ||
176 | Some(string) | ||
177 | }) | ||
178 | .visit(|node: &ast::Module| { | ||
179 | let mut string = node | ||
180 | .visibility() | ||
181 | .map(|v| format!("{} ", v.syntax().text())) | ||
182 | .unwrap_or_default(); | ||
183 | string.push_str("mod "); | ||
184 | node.name()?.syntax().text().push_to(&mut string); | ||
185 | Some(string) | ||
186 | }) | ||
187 | .visit(|node: &ast::TypeDef| { | ||
188 | let mut string = node | ||
189 | .visibility() | ||
190 | .map(|v| format!("{} ", v.syntax().text())) | ||
191 | .unwrap_or_default(); | ||
192 | string.push_str("type "); | ||
193 | node.name()?.syntax().text().push_to(&mut string); | ||
194 | Some(string) | ||
195 | }) | ||
196 | .visit(|node: &ast::ConstDef| { | ||
197 | let mut string = node | ||
198 | .visibility() | ||
199 | .map(|v| format!("{} ", v.syntax().text())) | ||
200 | .unwrap_or_default(); | ||
201 | string.push_str("const "); | ||
202 | node.name()?.syntax().text().push_to(&mut string); | ||
203 | Some(string) | ||
204 | }) | ||
205 | .visit(|node: &ast::StaticDef| { | ||
206 | let mut string = node | ||
207 | .visibility() | ||
208 | .map(|v| format!("{} ", v.syntax().text())) | ||
209 | .unwrap_or_default(); | ||
210 | string.push_str("static "); | ||
211 | node.name()?.syntax().text().push_to(&mut string); | ||
212 | Some(string) | ||
213 | }) | ||
214 | .accept(&node)? | 163 | .accept(&node)? |
215 | } | 164 | } |
216 | } | 165 | } |