diff options
-rw-r--r-- | crates/ra_hir/src/nameres/lower.rs | 13 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ptr.rs | 34 |
3 files changed, 40 insertions, 9 deletions
diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index ab6f3a9bc..921ba3c98 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs | |||
@@ -1,10 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | SyntaxKind, AstNode, SourceFile, TreeArc, SyntaxNodePtr, | 4 | SyntaxKind, AstNode, SourceFile, TreeArc, AstPtr, |
5 | ast::{self, ModuleItemOwner}, | 5 | ast::{self, ModuleItemOwner}, |
6 | }; | 6 | }; |
7 | use ra_db::{SourceRootId}; | 7 | use ra_db::SourceRootId; |
8 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; | 8 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
@@ -72,13 +72,12 @@ pub struct LoweredModule { | |||
72 | 72 | ||
73 | #[derive(Debug, Default, PartialEq, Eq)] | 73 | #[derive(Debug, Default, PartialEq, Eq)] |
74 | pub struct ImportSourceMap { | 74 | pub struct ImportSourceMap { |
75 | map: ArenaMap<ImportId, SyntaxNodePtr>, | 75 | map: ArenaMap<ImportId, AstPtr<ast::PathSegment>>, |
76 | } | 76 | } |
77 | 77 | ||
78 | impl ImportSourceMap { | 78 | impl ImportSourceMap { |
79 | fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { | 79 | fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { |
80 | self.map | 80 | self.map.insert(import, AstPtr::new(segment)) |
81 | .insert(import, SyntaxNodePtr::new(segment.syntax())) | ||
82 | } | 81 | } |
83 | 82 | ||
84 | pub fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> { | 83 | pub fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> { |
@@ -87,9 +86,7 @@ impl ImportSourceMap { | |||
87 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), | 86 | ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), |
88 | }; | 87 | }; |
89 | 88 | ||
90 | ast::PathSegment::cast(self.map[import].to_node(file)) | 89 | self.map[import].to_node(file).to_owned() |
91 | .unwrap() | ||
92 | .to_owned() | ||
93 | } | 90 | } |
94 | } | 91 | } |
95 | 92 | ||
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 97b196118..104f32851 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -43,7 +43,7 @@ pub use crate::{ | |||
43 | lexer::{tokenize, Token}, | 43 | lexer::{tokenize, Token}, |
44 | syntax_kinds::SyntaxKind, | 44 | syntax_kinds::SyntaxKind, |
45 | yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc}, | 45 | yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc}, |
46 | ptr::SyntaxNodePtr, | 46 | ptr::{SyntaxNodePtr, AstPtr}, |
47 | }; | 47 | }; |
48 | 48 | ||
49 | use ra_text_edit::AtomTextEdit; | 49 | use ra_text_edit::AtomTextEdit; |
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index e8c40e5d3..b50cd8a52 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::marker::PhantomData; | ||
2 | |||
1 | use crate::{ | 3 | use crate::{ |
2 | AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, | 4 | AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, |
3 | algo::generate, | 5 | algo::generate, |
@@ -37,6 +39,38 @@ impl SyntaxNodePtr { | |||
37 | } | 39 | } |
38 | } | 40 | } |
39 | 41 | ||
42 | /// Like `SyntaxNodePtr`, but remembers the type of node | ||
43 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
44 | pub struct AstPtr<N: AstNode> { | ||
45 | ptr: SyntaxNodePtr, | ||
46 | _ty: PhantomData<N>, | ||
47 | } | ||
48 | |||
49 | impl<N: AstNode> Copy for AstPtr<N> {} | ||
50 | impl<N: AstNode> Clone for AstPtr<N> { | ||
51 | fn clone(&self) -> AstPtr<N> { | ||
52 | *self | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl<N: AstNode> AstPtr<N> { | ||
57 | pub fn new(node: &N) -> AstPtr<N> { | ||
58 | AstPtr { | ||
59 | ptr: SyntaxNodePtr::new(node.syntax()), | ||
60 | _ty: PhantomData, | ||
61 | } | ||
62 | } | ||
63 | |||
64 | pub fn to_node(self, source_file: &SourceFile) -> &N { | ||
65 | let syntax_node = self.ptr.to_node(source_file); | ||
66 | N::cast(syntax_node).unwrap() | ||
67 | } | ||
68 | |||
69 | pub fn syntax_node_ptr(self) -> SyntaxNodePtr { | ||
70 | self.ptr | ||
71 | } | ||
72 | } | ||
73 | |||
40 | #[test] | 74 | #[test] |
41 | fn test_local_syntax_ptr() { | 75 | fn test_local_syntax_ptr() { |
42 | use crate::{ast, AstNode}; | 76 | use crate::{ast, AstNode}; |