diff options
author | Aleksey Kladov <[email protected]> | 2019-01-23 14:37:10 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-23 14:37:10 +0000 |
commit | 7b901f86cd1d0198994e5a2ab7eea18f444dd148 (patch) | |
tree | dfe8364efeeaca6f8f32e1d922fb615119b8012b /crates/ra_db | |
parent | 81fcfc55d247bfe6090741f2e4ae9aa89947bf32 (diff) |
move SyntaxPtr to ra_syntax
Diffstat (limited to 'crates/ra_db')
-rw-r--r-- | crates/ra_db/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_db/src/syntax_ptr.rs | 52 |
2 files changed, 0 insertions, 54 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index dbeb9ec71..32d7e09b9 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | //! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api. | 1 | //! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api. |
2 | mod cancellation; | 2 | mod cancellation; |
3 | mod syntax_ptr; | ||
4 | mod input; | 3 | mod input; |
5 | mod loc2id; | 4 | mod loc2id; |
6 | pub mod mock; | 5 | pub mod mock; |
@@ -12,7 +11,6 @@ use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; | |||
12 | pub use ::salsa as salsa; | 11 | pub use ::salsa as salsa; |
13 | pub use crate::{ | 12 | pub use crate::{ |
14 | cancellation::Canceled, | 13 | cancellation::Canceled, |
15 | syntax_ptr::LocalSyntaxPtr, | ||
16 | input::{ | 14 | input::{ |
17 | FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, | 15 | FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, |
18 | FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, | 16 | FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, |
diff --git a/crates/ra_db/src/syntax_ptr.rs b/crates/ra_db/src/syntax_ptr.rs deleted file mode 100644 index 5270826da..000000000 --- a/crates/ra_db/src/syntax_ptr.rs +++ /dev/null | |||
@@ -1,52 +0,0 @@ | |||
1 | use ra_syntax::{AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, TreeArc}; | ||
2 | |||
3 | /// A pointer to a syntax node inside a file. | ||
4 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
5 | pub struct LocalSyntaxPtr { | ||
6 | range: TextRange, | ||
7 | kind: SyntaxKind, | ||
8 | } | ||
9 | |||
10 | impl LocalSyntaxPtr { | ||
11 | pub fn new(node: &SyntaxNode) -> LocalSyntaxPtr { | ||
12 | LocalSyntaxPtr { | ||
13 | range: node.range(), | ||
14 | kind: node.kind(), | ||
15 | } | ||
16 | } | ||
17 | |||
18 | pub fn resolve(self, file: &SourceFile) -> TreeArc<SyntaxNode> { | ||
19 | let mut curr = file.syntax(); | ||
20 | loop { | ||
21 | if curr.range() == self.range && curr.kind() == self.kind { | ||
22 | return curr.to_owned(); | ||
23 | } | ||
24 | curr = curr | ||
25 | .children() | ||
26 | .find(|it| self.range.is_subrange(&it.range())) | ||
27 | .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) | ||
28 | } | ||
29 | } | ||
30 | |||
31 | pub fn range(self) -> TextRange { | ||
32 | self.range | ||
33 | } | ||
34 | |||
35 | pub fn kind(self) -> SyntaxKind { | ||
36 | self.kind | ||
37 | } | ||
38 | } | ||
39 | |||
40 | #[test] | ||
41 | fn test_local_syntax_ptr() { | ||
42 | use ra_syntax::{ast, AstNode}; | ||
43 | let file = SourceFile::parse("struct Foo { f: u32, }"); | ||
44 | let field = file | ||
45 | .syntax() | ||
46 | .descendants() | ||
47 | .find_map(ast::NamedFieldDef::cast) | ||
48 | .unwrap(); | ||
49 | let ptr = LocalSyntaxPtr::new(field.syntax()); | ||
50 | let field_syntax = ptr.resolve(&file); | ||
51 | assert_eq!(field.syntax(), &*field_syntax); | ||
52 | } | ||