diff options
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 118 |
1 files changed, 2 insertions, 116 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index eae73c2c4..e2871451c 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -8,11 +8,10 @@ use hir::{ | |||
8 | use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; | 8 | use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; |
9 | use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity}; | 9 | use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity}; |
10 | use ra_syntax::{ | 10 | use ra_syntax::{ |
11 | algo::{find_covering_node, visit::{visitor, Visitor}}, | 11 | ast::{self, ArgListOwner, Expr, NameOwner}, |
12 | ast::{self, ArgListOwner, Expr, FnDef, NameOwner}, | ||
13 | AstNode, SourceFileNode, | 12 | AstNode, SourceFileNode, |
14 | SyntaxKind::*, | 13 | SyntaxKind::*, |
15 | SyntaxNode, SyntaxNodeRef, TextRange, TextUnit, | 14 | SyntaxNodeRef, TextRange, TextUnit, |
16 | }; | 15 | }; |
17 | 16 | ||
18 | use crate::{ | 17 | use crate::{ |
@@ -256,18 +255,6 @@ impl db::RootDatabase { | |||
256 | Ok(Some((binding, descr))) | 255 | Ok(Some((binding, descr))) |
257 | } | 256 | } |
258 | } | 257 | } |
259 | pub(crate) fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> { | ||
260 | let result = match (nav.description(self), nav.docs(self)) { | ||
261 | (Some(desc), Some(docs)) => { | ||
262 | Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs) | ||
263 | } | ||
264 | (Some(desc), None) => Some("```rust\n".to_string() + &*desc + "\n```"), | ||
265 | (None, Some(docs)) => Some(docs), | ||
266 | _ => None, | ||
267 | }; | ||
268 | |||
269 | Ok(result) | ||
270 | } | ||
271 | 258 | ||
272 | pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { | 259 | pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { |
273 | let syntax = self.source_file(file_id); | 260 | let syntax = self.source_file(file_id); |
@@ -410,19 +397,6 @@ impl db::RootDatabase { | |||
410 | Ok(None) | 397 | Ok(None) |
411 | } | 398 | } |
412 | 399 | ||
413 | pub(crate) fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> { | ||
414 | let file = self.source_file(frange.file_id); | ||
415 | let syntax = file.syntax(); | ||
416 | let node = find_covering_node(syntax, frange.range); | ||
417 | let parent_fn = ctry!(node.ancestors().find_map(FnDef::cast)); | ||
418 | let function = ctry!(source_binder::function_from_source( | ||
419 | self, | ||
420 | frange.file_id, | ||
421 | parent_fn | ||
422 | )?); | ||
423 | let infer = function.infer(self)?; | ||
424 | Ok(infer.type_of_node(node).map(|t| t.to_string())) | ||
425 | } | ||
426 | pub(crate) fn rename( | 400 | pub(crate) fn rename( |
427 | &self, | 401 | &self, |
428 | position: FilePosition, | 402 | position: FilePosition, |
@@ -506,91 +480,3 @@ impl<'a> FnCallNode<'a> { | |||
506 | } | 480 | } |
507 | } | 481 | } |
508 | } | 482 | } |
509 | |||
510 | impl NavigationTarget { | ||
511 | fn node(&self, db: &db::RootDatabase) -> Option<SyntaxNode> { | ||
512 | let source_file = db.source_file(self.file_id); | ||
513 | let source_file = source_file.syntax(); | ||
514 | let node = source_file | ||
515 | .descendants() | ||
516 | .find(|node| node.kind() == self.kind && node.range() == self.range)? | ||
517 | .owned(); | ||
518 | Some(node) | ||
519 | } | ||
520 | |||
521 | fn docs(&self, db: &db::RootDatabase) -> Option<String> { | ||
522 | let node = self.node(db)?; | ||
523 | let node = node.borrowed(); | ||
524 | fn doc_comments<'a, N: ast::DocCommentsOwner<'a>>(node: N) -> Option<String> { | ||
525 | let comments = node.doc_comment_text(); | ||
526 | if comments.is_empty() { | ||
527 | None | ||
528 | } else { | ||
529 | Some(comments) | ||
530 | } | ||
531 | } | ||
532 | |||
533 | visitor() | ||
534 | .visit(doc_comments::<ast::FnDef>) | ||
535 | .visit(doc_comments::<ast::StructDef>) | ||
536 | .visit(doc_comments::<ast::EnumDef>) | ||
537 | .visit(doc_comments::<ast::TraitDef>) | ||
538 | .visit(doc_comments::<ast::Module>) | ||
539 | .visit(doc_comments::<ast::TypeDef>) | ||
540 | .visit(doc_comments::<ast::ConstDef>) | ||
541 | .visit(doc_comments::<ast::StaticDef>) | ||
542 | .accept(node)? | ||
543 | } | ||
544 | |||
545 | /// Get a description of this node. | ||
546 | /// | ||
547 | /// e.g. `struct Name`, `enum Name`, `fn Name` | ||
548 | fn description(&self, db: &db::RootDatabase) -> Option<String> { | ||
549 | // TODO: After type inference is done, add type information to improve the output | ||
550 | let node = self.node(db)?; | ||
551 | let node = node.borrowed(); | ||
552 | // TODO: Refactor to be have less repetition | ||
553 | visitor() | ||
554 | .visit(|node: ast::FnDef| { | ||
555 | let mut string = "fn ".to_string(); | ||
556 | node.name()?.syntax().text().push_to(&mut string); | ||
557 | Some(string) | ||
558 | }) | ||
559 | .visit(|node: ast::StructDef| { | ||
560 | let mut string = "struct ".to_string(); | ||
561 | node.name()?.syntax().text().push_to(&mut string); | ||
562 | Some(string) | ||
563 | }) | ||
564 | .visit(|node: ast::EnumDef| { | ||
565 | let mut string = "enum ".to_string(); | ||
566 | node.name()?.syntax().text().push_to(&mut string); | ||
567 | Some(string) | ||
568 | }) | ||
569 | .visit(|node: ast::TraitDef| { | ||
570 | let mut string = "trait ".to_string(); | ||
571 | node.name()?.syntax().text().push_to(&mut string); | ||
572 | Some(string) | ||
573 | }) | ||
574 | .visit(|node: ast::Module| { | ||
575 | let mut string = "mod ".to_string(); | ||
576 | node.name()?.syntax().text().push_to(&mut string); | ||
577 | Some(string) | ||
578 | }) | ||
579 | .visit(|node: ast::TypeDef| { | ||
580 | let mut string = "type ".to_string(); | ||
581 | node.name()?.syntax().text().push_to(&mut string); | ||
582 | Some(string) | ||
583 | }) | ||
584 | .visit(|node: ast::ConstDef| { | ||
585 | let mut string = "const ".to_string(); | ||
586 | node.name()?.syntax().text().push_to(&mut string); | ||
587 | Some(string) | ||
588 | }) | ||
589 | .visit(|node: ast::StaticDef| { | ||
590 | let mut string = "static ".to_string(); | ||
591 | node.name()?.syntax().text().push_to(&mut string); | ||
592 | Some(string) | ||
593 | }) | ||
594 | .accept(node)? | ||
595 | } | ||
596 | } | ||