make AstId untyped

This commit is contained in:
Aleksey Kladov 2019-05-13 19:39:06 +03:00
parent 033a32f349
commit 549728bba8
11 changed files with 29 additions and 24 deletions

View File

@ -1,6 +1,6 @@
use std::{fmt, any::Any};
use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode};
use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode, AstNode};
use relative_path::RelativePathBuf;
use crate::{HirFileId, HirDatabase, Name};
@ -30,7 +30,7 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
let source_file = db.hir_parse(self.file());
self.syntax_node_ptr().to_node(&source_file).to_owned()
self.syntax_node_ptr().to_node(source_file.syntax()).to_owned()
}
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
self.as_any().downcast_ref()

View File

@ -76,7 +76,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
let source_file = db.parse(file_id.original_file(db));
if let Some(field_list_node) = source_map
.expr_syntax(id)
.map(|ptr| ptr.to_node(&source_file))
.map(|ptr| ptr.to_node(source_file.syntax()))
.and_then(StructLit::cast)
.and_then(|lit| lit.named_field_list())
{

View File

@ -34,7 +34,7 @@ impl ImplSourceMap {
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
};
self.map[impl_id].to_node(file).to_owned()
self.map[impl_id].to_node(file.syntax()).to_owned()
}
}

View File

@ -39,7 +39,10 @@ type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>
impl ImportSourcePtr {
fn to_node(self, file: &SourceFile) -> ImportSource {
self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned())
self.map(
|ptr| ptr.to_node(file.syntax()).to_owned(),
|ptr| ptr.to_node(file.syntax()).to_owned(),
)
}
}

View File

@ -1,7 +1,7 @@
use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}};
use ra_arena::{Arena, RawId, impl_arena_id};
use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, AstNode, ast};
use crate::{HirFileId, DefDatabase};
@ -89,7 +89,7 @@ pub struct AstIdMap {
impl AstIdMap {
pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
let source_file = db.hir_parse(file_id);
Arc::new(AstIdMap::from_source_file(&source_file))
Arc::new(AstIdMap::from_source(source_file.syntax()))
}
pub(crate) fn file_item_query(
@ -98,7 +98,7 @@ impl AstIdMap {
ast_id: ErasedFileAstId,
) -> TreeArc<SyntaxNode> {
let source_file = db.hir_parse(file_id);
db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned()
db.ast_id_map(file_id).arena[ast_id].to_node(source_file.syntax()).to_owned()
}
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
@ -115,13 +115,14 @@ impl AstIdMap {
FileAstId { raw, _ty: PhantomData }
}
fn from_source_file(source_file: &SourceFile) -> AstIdMap {
fn from_source(node: &SyntaxNode) -> AstIdMap {
assert!(node.parent().is_none());
let mut res = AstIdMap { arena: Arena::default() };
// By walking the tree in bread-first order we make sure that parents
// get lower ids then children. That is, adding a new child does not
// change parent's id. This means that, say, adding a new function to a
// trait does not change ids of top-level items, which helps caching.
bfs(source_file.syntax(), |it| {
bfs(node, |it| {
if let Some(module_item) = ast::ModuleItem::cast(it) {
res.alloc(module_item.syntax());
} else if let Some(macro_call) = ast::MacroCall::cast(it) {

View File

@ -2715,7 +2715,7 @@ fn infer(content: &str) -> String {
// sort ranges for consistency
types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end()));
for (syntax_ptr, ty) in &types {
let node = syntax_ptr.to_node(&source_file);
let node = syntax_ptr.to_node(source_file.syntax());
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node) {
(self_param.self_kw_token().range(), "self".to_string())
} else {

View File

@ -54,7 +54,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
let file_id = d.file().original_file(db);
let source_file = db.parse(file_id);
let syntax_node = d.syntax_node_ptr();
let node = NamedFieldList::cast(syntax_node.to_node(&source_file)).unwrap();
let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap();
let mut ast_editor = AstEditor::new(node);
for f in d.missed_fields.iter() {
ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f));

View File

@ -81,7 +81,7 @@ impl NavigationTarget {
) -> NavigationTarget {
let file = db.parse(file_id);
let (name, full_range) = match pat {
Either::A(pat) => match pat.to_node(&file).kind() {
Either::A(pat) => match pat.to_node(file.syntax()).kind() {
ast::PatKind::BindPat(pat) => {
return NavigationTarget::from_bind_pat(file_id, &pat)
}

View File

@ -86,7 +86,7 @@ pub(crate) fn find_all_refs(
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
let resolved = analyzer.resolve_local_name(name_ref)?;
if let Either::A(ptr) = resolved.ptr() {
if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() {
if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file.syntax()).kind() {
return Some((binding, analyzer));
}
}

View File

@ -3,7 +3,7 @@ use std::{
iter::successors,
};
use crate::{
AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
AstNode, SyntaxKind, SyntaxNode, TextRange,
};
/// A pointer to a syntax node inside a file. It can be used to remember a
@ -19,8 +19,9 @@ impl SyntaxNodePtr {
SyntaxNodePtr { range: node.range(), kind: node.kind() }
}
pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode {
successors(Some(source_file.syntax()), |&node| {
pub fn to_node(self, root: &SyntaxNode) -> &SyntaxNode {
assert!(root.parent().is_none());
successors(Some(root), |&node| {
node.children().find(|it| self.range.is_subrange(&it.range()))
})
.find(|it| it.range() == self.range && it.kind() == self.kind)
@ -55,8 +56,8 @@ impl<N: AstNode> AstPtr<N> {
AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
}
pub fn to_node(self, source_file: &SourceFile) -> &N {
let syntax_node = self.raw.to_node(source_file);
pub fn to_node(self, root: &SyntaxNode) -> &N {
let syntax_node = self.raw.to_node(root);
N::cast(syntax_node).unwrap()
}
@ -73,11 +74,11 @@ impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
#[test]
fn test_local_syntax_ptr() {
use crate::{ast, AstNode};
use crate::{ast, AstNode, SourceFile};
let file = SourceFile::parse("struct Foo { f: u32, }");
let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap();
let ptr = SyntaxNodePtr::new(field.syntax());
let field_syntax = ptr.to_node(&file);
let field_syntax = ptr.to_node(file.syntax());
assert_eq!(field.syntax(), &*field_syntax);
}

View File

@ -392,7 +392,7 @@ impl SyntaxNode {
// `range` private afterwards
let mut ptr = SyntaxNodePtr::new(self);
ptr.range = TextRange::offset_len(ptr.range().start(), len);
return ptr.to_node(&file).to_owned();
return ptr.to_node(file.syntax()).to_owned();
}
fn position_of_child(&self, child: SyntaxElement) -> usize {