diff options
author | Aleksey Kladov <[email protected]> | 2019-06-11 14:40:49 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-06-11 16:28:51 +0100 |
commit | 36865adcb946d5567fb61d3547b78fc71df58b20 (patch) | |
tree | 4cec6a3d18de8242c334fe303717c1f2b1c42be1 | |
parent | 2a1fe26b6d354dbbd1b3843d288d56e74fa00557 (diff) |
Introduce HasSource trait
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 46 |
3 files changed, 45 insertions, 29 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 63fdca55e..765850488 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -31,6 +31,11 @@ impl<T> From<(HirFileId, T)> for Source<T> { | |||
31 | } | 31 | } |
32 | } | 32 | } |
33 | 33 | ||
34 | pub trait HasSource { | ||
35 | type Ast; | ||
36 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>; | ||
37 | } | ||
38 | |||
34 | /// hir::Crate describes a single crate. It's the main interface with which | 39 | /// hir::Crate describes a single crate. It's the main interface with which |
35 | /// a crate's dependencies interact. Mostly, it should be just a proxy for the | 40 | /// a crate's dependencies interact. Mostly, it should be just a proxy for the |
36 | /// root module. | 41 | /// root module. |
@@ -364,6 +369,13 @@ pub struct Struct { | |||
364 | pub(crate) id: StructId, | 369 | pub(crate) id: StructId, |
365 | } | 370 | } |
366 | 371 | ||
372 | impl HasSource for Struct { | ||
373 | type Ast = TreeArc<ast::StructDef>; | ||
374 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> { | ||
375 | self.id.source(db).into() | ||
376 | } | ||
377 | } | ||
378 | |||
367 | impl Struct { | 379 | impl Struct { |
368 | pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> { | 380 | pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> { |
369 | self.id.source(db).into() | 381 | self.id.source(db).into() |
@@ -422,6 +434,13 @@ pub struct Union { | |||
422 | pub(crate) id: StructId, | 434 | pub(crate) id: StructId, |
423 | } | 435 | } |
424 | 436 | ||
437 | impl HasSource for Union { | ||
438 | type Ast = TreeArc<ast::StructDef>; | ||
439 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::StructDef>> { | ||
440 | self.id.source(db).into() | ||
441 | } | ||
442 | } | ||
443 | |||
425 | impl Union { | 444 | impl Union { |
426 | pub fn source( | 445 | pub fn source( |
427 | self, | 446 | self, |
@@ -455,6 +474,13 @@ pub struct Enum { | |||
455 | pub(crate) id: EnumId, | 474 | pub(crate) id: EnumId, |
456 | } | 475 | } |
457 | 476 | ||
477 | impl HasSource for Enum { | ||
478 | type Ast = TreeArc<ast::EnumDef>; | ||
479 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> { | ||
480 | self.id.source(db).into() | ||
481 | } | ||
482 | } | ||
483 | |||
458 | impl Enum { | 484 | impl Enum { |
459 | pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> { | 485 | pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::EnumDef>> { |
460 | self.id.source(db).into() | 486 | self.id.source(db).into() |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 90e3f1275..02f5f7f40 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -81,5 +81,5 @@ pub use self::code_model::{ | |||
81 | StructField, FieldSource, | 81 | StructField, FieldSource, |
82 | Static, Const, ConstSignature, | 82 | Static, Const, ConstSignature, |
83 | Trait, TypeAlias, MacroDef, Container, | 83 | Trait, TypeAlias, MacroDef, Container, |
84 | BuiltinType, Source, | 84 | BuiltinType, Source, HasSource, |
85 | }; | 85 | }; |
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 7c68bb41c..bef5f0980 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_db::{FileId, SourceDatabase}; | 1 | use ra_db::{FileId, SourceDatabase}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, | 3 | SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, TreeArc, |
4 | SyntaxKind::{self, NAME}, | 4 | SyntaxKind::{self, NAME}, |
5 | ast::{self, DocCommentsOwner}, | 5 | ast::{self, DocCommentsOwner}, |
6 | algo::visit::{visitor, Visitor}, | 6 | algo::visit::{visitor, Visitor}, |
@@ -186,35 +186,25 @@ impl NavigationTarget { | |||
186 | } | 186 | } |
187 | } | 187 | } |
188 | 188 | ||
189 | pub(crate) fn from_def_source<A, D>(db: &RootDatabase, def: D) -> NavigationTarget | ||
190 | where | ||
191 | D: hir::HasSource<Ast = TreeArc<A>>, | ||
192 | A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, | ||
193 | { | ||
194 | let src = def.source(db); | ||
195 | NavigationTarget::from_named( | ||
196 | src.file_id.original_file(db), | ||
197 | &*src.ast, | ||
198 | src.ast.doc_comment_text(), | ||
199 | src.ast.short_label(), | ||
200 | ) | ||
201 | } | ||
202 | |||
189 | pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget { | 203 | pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget { |
190 | match adt_def { | 204 | match adt_def { |
191 | hir::AdtDef::Struct(s) => { | 205 | hir::AdtDef::Struct(it) => NavigationTarget::from_def_source(db, it), |
192 | let src = s.source(db); | 206 | hir::AdtDef::Union(it) => NavigationTarget::from_def_source(db, it), |
193 | NavigationTarget::from_named( | 207 | hir::AdtDef::Enum(it) => NavigationTarget::from_def_source(db, it), |
194 | src.file_id.original_file(db), | ||
195 | &*src.ast, | ||
196 | src.ast.doc_comment_text(), | ||
197 | src.ast.short_label(), | ||
198 | ) | ||
199 | } | ||
200 | hir::AdtDef::Union(s) => { | ||
201 | let (file_id, node) = s.source(db); | ||
202 | NavigationTarget::from_named( | ||
203 | file_id.original_file(db), | ||
204 | &*node, | ||
205 | node.doc_comment_text(), | ||
206 | node.short_label(), | ||
207 | ) | ||
208 | } | ||
209 | hir::AdtDef::Enum(s) => { | ||
210 | let src = s.source(db); | ||
211 | NavigationTarget::from_named( | ||
212 | src.file_id.original_file(db), | ||
213 | &*src.ast, | ||
214 | src.ast.doc_comment_text(), | ||
215 | src.ast.short_label(), | ||
216 | ) | ||
217 | } | ||
218 | } | 208 | } |
219 | } | 209 | } |
220 | 210 | ||