aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/lib.rs')
-rw-r--r--crates/ra_ide_api/src/lib.rs64
1 files changed, 63 insertions, 1 deletions
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index f505959ce..65d21d899 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -33,7 +33,8 @@ mod syntax_highlighting;
33 33
34use std::{fmt, sync::Arc}; 34use std::{fmt, sync::Arc};
35 35
36use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit}; 36use hir::{Def, ModuleSource, Name};
37use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, SyntaxNode, TextRange, TextUnit, AstNode};
37use ra_text_edit::TextEdit; 38use ra_text_edit::TextEdit;
38use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr, BaseDatabase}; 39use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr, BaseDatabase};
39use rayon::prelude::*; 40use rayon::prelude::*;
@@ -268,6 +269,67 @@ impl NavigationTarget {
268 } 269 }
269 } 270 }
270 271
272 fn from_syntax(name: Option<Name>, file_id: FileId, node: &SyntaxNode) -> NavigationTarget {
273 NavigationTarget {
274 file_id,
275 name: name.map(|n| n.to_string().into()).unwrap_or("".into()),
276 kind: node.kind(),
277 range: node.range(),
278 ptr: Some(LocalSyntaxPtr::new(node)),
279 }
280 }
281 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
282 fn from_def(db: &db::RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
283 Ok(match def {
284 Def::Struct(s) => {
285 let (file_id, node) = s.source(db)?;
286 Some(NavigationTarget::from_syntax(
287 s.name(db)?,
288 file_id.original_file(db),
289 node.syntax(),
290 ))
291 }
292 Def::Enum(e) => {
293 let (file_id, node) = e.source(db)?;
294 Some(NavigationTarget::from_syntax(
295 e.name(db)?,
296 file_id.original_file(db),
297 node.syntax(),
298 ))
299 }
300 Def::EnumVariant(ev) => {
301 let (file_id, node) = ev.source(db)?;
302 Some(NavigationTarget::from_syntax(
303 ev.name(db)?,
304 file_id.original_file(db),
305 node.syntax(),
306 ))
307 }
308 Def::Function(f) => {
309 let (file_id, node) = f.source(db)?;
310 let name = f.signature(db).name().clone();
311 Some(NavigationTarget::from_syntax(
312 Some(name),
313 file_id.original_file(db),
314 node.syntax(),
315 ))
316 }
317 Def::Module(m) => {
318 let (file_id, source) = m.definition_source(db)?;
319 let name = m.name(db)?;
320 match source {
321 ModuleSource::SourceFile(node) => {
322 Some(NavigationTarget::from_syntax(name, file_id, node.syntax()))
323 }
324 ModuleSource::Module(node) => {
325 Some(NavigationTarget::from_syntax(name, file_id, node.syntax()))
326 }
327 }
328 }
329 Def::Item => None,
330 })
331 }
332
271 pub fn name(&self) -> &SmolStr { 333 pub fn name(&self) -> &SmolStr {
272 &self.name 334 &self.name
273 } 335 }