aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/diagnostics.rs4
-rw-r--r--crates/ra_hir/src/expr/validation.rs2
-rw-r--r--crates/ra_hir/src/impl_block.rs2
-rw-r--r--crates/ra_hir/src/nameres/raw.rs5
-rw-r--r--crates/ra_hir/src/source_id.rs11
-rw-r--r--crates/ra_hir/src/ty/tests.rs2
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs6
-rw-r--r--crates/ra_ide_api/src/display/navigation_target.rs2
-rw-r--r--crates/ra_ide_api/src/references.rs2
-rw-r--r--crates/ra_syntax/src/ptr.rs15
-rw-r--r--crates/ra_syntax/src/syntax_node.rs2
11 files changed, 29 insertions, 24 deletions
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs
index 61cd9d6b1..d41525779 100644
--- a/crates/ra_hir/src/diagnostics.rs
+++ b/crates/ra_hir/src/diagnostics.rs
@@ -1,6 +1,6 @@
1use std::{fmt, any::Any}; 1use std::{fmt, any::Any};
2 2
3use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode}; 3use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode, AstNode};
4use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
5 5
6use crate::{HirFileId, HirDatabase, Name}; 6use crate::{HirFileId, HirDatabase, Name};
@@ -30,7 +30,7 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
30impl dyn Diagnostic { 30impl dyn Diagnostic {
31 pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> { 31 pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
32 let source_file = db.hir_parse(self.file()); 32 let source_file = db.hir_parse(self.file());
33 self.syntax_node_ptr().to_node(&source_file).to_owned() 33 self.syntax_node_ptr().to_node(source_file.syntax()).to_owned()
34 } 34 }
35 pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { 35 pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
36 self.as_any().downcast_ref() 36 self.as_any().downcast_ref()
diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs
index fd4907313..aebed6788 100644
--- a/crates/ra_hir/src/expr/validation.rs
+++ b/crates/ra_hir/src/expr/validation.rs
@@ -76,7 +76,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
76 let source_file = db.parse(file_id.original_file(db)); 76 let source_file = db.parse(file_id.original_file(db));
77 if let Some(field_list_node) = source_map 77 if let Some(field_list_node) = source_map
78 .expr_syntax(id) 78 .expr_syntax(id)
79 .map(|ptr| ptr.to_node(&source_file)) 79 .map(|ptr| ptr.to_node(source_file.syntax()))
80 .and_then(StructLit::cast) 80 .and_then(StructLit::cast)
81 .and_then(|lit| lit.named_field_list()) 81 .and_then(|lit| lit.named_field_list())
82 { 82 {
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index b7dd775f1..51fa491c3 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -34,7 +34,7 @@ impl ImplSourceMap {
34 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), 34 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
35 }; 35 };
36 36
37 self.map[impl_id].to_node(file).to_owned() 37 self.map[impl_id].to_node(file.syntax()).to_owned()
38 } 38 }
39} 39}
40 40
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs
index 43c97a0bf..211e02068 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir/src/nameres/raw.rs
@@ -39,7 +39,10 @@ type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>
39 39
40impl ImportSourcePtr { 40impl ImportSourcePtr {
41 fn to_node(self, file: &SourceFile) -> ImportSource { 41 fn to_node(self, file: &SourceFile) -> ImportSource {
42 self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned()) 42 self.map(
43 |ptr| ptr.to_node(file.syntax()).to_owned(),
44 |ptr| ptr.to_node(file.syntax()).to_owned(),
45 )
43 } 46 }
44} 47}
45 48
diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs
index 0a8fb6d32..7a39be779 100644
--- a/crates/ra_hir/src/source_id.rs
+++ b/crates/ra_hir/src/source_id.rs
@@ -1,7 +1,7 @@
1use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}}; 1use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}};
2 2
3use ra_arena::{Arena, RawId, impl_arena_id}; 3use ra_arena::{Arena, RawId, impl_arena_id};
4use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; 4use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, AstNode, ast};
5 5
6use crate::{HirFileId, DefDatabase}; 6use crate::{HirFileId, DefDatabase};
7 7
@@ -89,7 +89,7 @@ pub struct AstIdMap {
89impl AstIdMap { 89impl AstIdMap {
90 pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> { 90 pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
91 let source_file = db.hir_parse(file_id); 91 let source_file = db.hir_parse(file_id);
92 Arc::new(AstIdMap::from_source_file(&source_file)) 92 Arc::new(AstIdMap::from_source(source_file.syntax()))
93 } 93 }
94 94
95 pub(crate) fn file_item_query( 95 pub(crate) fn file_item_query(
@@ -98,7 +98,7 @@ impl AstIdMap {
98 ast_id: ErasedFileAstId, 98 ast_id: ErasedFileAstId,
99 ) -> TreeArc<SyntaxNode> { 99 ) -> TreeArc<SyntaxNode> {
100 let source_file = db.hir_parse(file_id); 100 let source_file = db.hir_parse(file_id);
101 db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned() 101 db.ast_id_map(file_id).arena[ast_id].to_node(source_file.syntax()).to_owned()
102 } 102 }
103 103
104 pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> { 104 pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
@@ -115,13 +115,14 @@ impl AstIdMap {
115 FileAstId { raw, _ty: PhantomData } 115 FileAstId { raw, _ty: PhantomData }
116 } 116 }
117 117
118 fn from_source_file(source_file: &SourceFile) -> AstIdMap { 118 fn from_source(node: &SyntaxNode) -> AstIdMap {
119 assert!(node.parent().is_none());
119 let mut res = AstIdMap { arena: Arena::default() }; 120 let mut res = AstIdMap { arena: Arena::default() };
120 // By walking the tree in bread-first order we make sure that parents 121 // By walking the tree in bread-first order we make sure that parents
121 // get lower ids then children. That is, adding a new child does not 122 // get lower ids then children. That is, adding a new child does not
122 // change parent's id. This means that, say, adding a new function to a 123 // change parent's id. This means that, say, adding a new function to a
123 // trait does not change ids of top-level items, which helps caching. 124 // trait does not change ids of top-level items, which helps caching.
124 bfs(source_file.syntax(), |it| { 125 bfs(node, |it| {
125 if let Some(module_item) = ast::ModuleItem::cast(it) { 126 if let Some(module_item) = ast::ModuleItem::cast(it) {
126 res.alloc(module_item.syntax()); 127 res.alloc(module_item.syntax());
127 } else if let Some(macro_call) = ast::MacroCall::cast(it) { 128 } else if let Some(macro_call) = ast::MacroCall::cast(it) {
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 978cc2587..f8364203d 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -2715,7 +2715,7 @@ fn infer(content: &str) -> String {
2715 // sort ranges for consistency 2715 // sort ranges for consistency
2716 types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end())); 2716 types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end()));
2717 for (syntax_ptr, ty) in &types { 2717 for (syntax_ptr, ty) in &types {
2718 let node = syntax_ptr.to_node(&source_file); 2718 let node = syntax_ptr.to_node(source_file.syntax());
2719 let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node) { 2719 let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node) {
2720 (self_param.self_kw_token().range(), "self".to_string()) 2720 (self_param.self_kw_token().range(), "self".to_string())
2721 } else { 2721 } else {
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index 855a3ff0f..e23d178b0 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -54,7 +54,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
54 let file_id = d.file().original_file(db); 54 let file_id = d.file().original_file(db);
55 let source_file = db.parse(file_id); 55 let source_file = db.parse(file_id);
56 let syntax_node = d.syntax_node_ptr(); 56 let syntax_node = d.syntax_node_ptr();
57 let node = NamedFieldList::cast(syntax_node.to_node(&source_file)).unwrap(); 57 let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap();
58 let mut ast_editor = AstEditor::new(node); 58 let mut ast_editor = AstEditor::new(node);
59 for f in d.missed_fields.iter() { 59 for f in d.missed_fields.iter() {
60 ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f)); 60 ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f));
@@ -281,7 +281,7 @@ mod tests {
281 one: i32, 281 one: i32,
282 two: i64, 282 two: i64,
283 } 283 }
284 284
285 fn test_fn() { 285 fn test_fn() {
286 let one = 1; 286 let one = 1;
287 let s = TestStruct{ one, two: 2 }; 287 let s = TestStruct{ one, two: 2 };
@@ -298,7 +298,7 @@ mod tests {
298 one: i32, 298 one: i32,
299 two: i64, 299 two: i64,
300 } 300 }
301 301
302 fn test_fn() { 302 fn test_fn() {
303 let one = 1; 303 let one = 1;
304 let s = TestStruct{ ..a }; 304 let s = TestStruct{ ..a };
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs
index 765cf883b..7ea336c50 100644
--- a/crates/ra_ide_api/src/display/navigation_target.rs
+++ b/crates/ra_ide_api/src/display/navigation_target.rs
@@ -81,7 +81,7 @@ impl NavigationTarget {
81 ) -> NavigationTarget { 81 ) -> NavigationTarget {
82 let file = db.parse(file_id); 82 let file = db.parse(file_id);
83 let (name, full_range) = match pat { 83 let (name, full_range) = match pat {
84 Either::A(pat) => match pat.to_node(&file).kind() { 84 Either::A(pat) => match pat.to_node(file.syntax()).kind() {
85 ast::PatKind::BindPat(pat) => { 85 ast::PatKind::BindPat(pat) => {
86 return NavigationTarget::from_bind_pat(file_id, &pat) 86 return NavigationTarget::from_bind_pat(file_id, &pat)
87 } 87 }
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index 9f655d83c..d5c2b08ca 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -86,7 +86,7 @@ pub(crate) fn find_all_refs(
86 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); 86 let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
87 let resolved = analyzer.resolve_local_name(name_ref)?; 87 let resolved = analyzer.resolve_local_name(name_ref)?;
88 if let Either::A(ptr) = resolved.ptr() { 88 if let Either::A(ptr) = resolved.ptr() {
89 if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() { 89 if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file.syntax()).kind() {
90 return Some((binding, analyzer)); 90 return Some((binding, analyzer));
91 } 91 }
92 } 92 }
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index b0816b135..cee9503ca 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -3,7 +3,7 @@ use std::{
3 iter::successors, 3 iter::successors,
4}; 4};
5use crate::{ 5use crate::{
6 AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, 6 AstNode, SyntaxKind, SyntaxNode, TextRange,
7}; 7};
8 8
9/// A pointer to a syntax node inside a file. It can be used to remember a 9/// A pointer to a syntax node inside a file. It can be used to remember a
@@ -19,8 +19,9 @@ impl SyntaxNodePtr {
19 SyntaxNodePtr { range: node.range(), kind: node.kind() } 19 SyntaxNodePtr { range: node.range(), kind: node.kind() }
20 } 20 }
21 21
22 pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { 22 pub fn to_node(self, root: &SyntaxNode) -> &SyntaxNode {
23 successors(Some(source_file.syntax()), |&node| { 23 assert!(root.parent().is_none());
24 successors(Some(root), |&node| {
24 node.children().find(|it| self.range.is_subrange(&it.range())) 25 node.children().find(|it| self.range.is_subrange(&it.range()))
25 }) 26 })
26 .find(|it| it.range() == self.range && it.kind() == self.kind) 27 .find(|it| it.range() == self.range && it.kind() == self.kind)
@@ -55,8 +56,8 @@ impl<N: AstNode> AstPtr<N> {
55 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } 56 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
56 } 57 }
57 58
58 pub fn to_node(self, source_file: &SourceFile) -> &N { 59 pub fn to_node(self, root: &SyntaxNode) -> &N {
59 let syntax_node = self.raw.to_node(source_file); 60 let syntax_node = self.raw.to_node(root);
60 N::cast(syntax_node).unwrap() 61 N::cast(syntax_node).unwrap()
61 } 62 }
62 63
@@ -73,11 +74,11 @@ impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
73 74
74#[test] 75#[test]
75fn test_local_syntax_ptr() { 76fn test_local_syntax_ptr() {
76 use crate::{ast, AstNode}; 77 use crate::{ast, AstNode, SourceFile};
77 78
78 let file = SourceFile::parse("struct Foo { f: u32, }"); 79 let file = SourceFile::parse("struct Foo { f: u32, }");
79 let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap(); 80 let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap();
80 let ptr = SyntaxNodePtr::new(field.syntax()); 81 let ptr = SyntaxNodePtr::new(field.syntax());
81 let field_syntax = ptr.to_node(&file); 82 let field_syntax = ptr.to_node(file.syntax());
82 assert_eq!(field.syntax(), &*field_syntax); 83 assert_eq!(field.syntax(), &*field_syntax);
83} 84}
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index 92c15234e..80054f529 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -392,7 +392,7 @@ impl SyntaxNode {
392 // `range` private afterwards 392 // `range` private afterwards
393 let mut ptr = SyntaxNodePtr::new(self); 393 let mut ptr = SyntaxNodePtr::new(self);
394 ptr.range = TextRange::offset_len(ptr.range().start(), len); 394 ptr.range = TextRange::offset_len(ptr.range().start(), len);
395 return ptr.to_node(&file).to_owned(); 395 return ptr.to_node(file.syntax()).to_owned();
396 } 396 }
397 397
398 fn position_of_child(&self, child: SyntaxElement) -> usize { 398 fn position_of_child(&self, child: SyntaxElement) -> usize {