aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/imp.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-05 14:33:31 +0000
committerAleksey Kladov <[email protected]>2019-01-05 14:33:31 +0000
commit9f44d4c56d51fdae1ff073df261b8c897b27c824 (patch)
treef2b248616c5d31d5d4e48c6fe5969cbd7594b477 /crates/ra_analysis/src/imp.rs
parent3ad0037f907778d20ce6cfd9bf676a467b5734ad (diff)
fold doc_comment into hover
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r--crates/ra_analysis/src/imp.rs104
1 files changed, 2 insertions, 102 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index eae73c2c4..1e9129c4f 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -8,11 +8,11 @@ use hir::{
8use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; 8use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase};
9use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity}; 9use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity};
10use ra_syntax::{ 10use ra_syntax::{
11 algo::{find_covering_node, visit::{visitor, Visitor}}, 11 algo::find_covering_node,
12 ast::{self, ArgListOwner, Expr, FnDef, NameOwner}, 12 ast::{self, ArgListOwner, Expr, FnDef, NameOwner},
13 AstNode, SourceFileNode, 13 AstNode, SourceFileNode,
14 SyntaxKind::*, 14 SyntaxKind::*,
15 SyntaxNode, SyntaxNodeRef, TextRange, TextUnit, 15 SyntaxNodeRef, TextRange, TextUnit,
16}; 16};
17 17
18use crate::{ 18use crate::{
@@ -256,18 +256,6 @@ impl db::RootDatabase {
256 Ok(Some((binding, descr))) 256 Ok(Some((binding, descr)))
257 } 257 }
258 } 258 }
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 259
272 pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { 260 pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
273 let syntax = self.source_file(file_id); 261 let syntax = self.source_file(file_id);
@@ -506,91 +494,3 @@ impl<'a> FnCallNode<'a> {
506 } 494 }
507 } 495 }
508} 496}
509
510impl 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}