diff options
Diffstat (limited to 'crates/ra_db/src/syntax_ptr.rs')
-rw-r--r-- | crates/ra_db/src/syntax_ptr.rs | 52 |
1 files changed, 0 insertions, 52 deletions
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 | } | ||