migrate ra_db to new rowan

This commit is contained in:
Aleksey Kladov 2019-01-07 17:00:39 +03:00
parent b88775af7f
commit fe53b28250
3 changed files with 20 additions and 11 deletions

View file

@ -8,7 +8,7 @@ pub mod mock;
use std::sync::Arc; use std::sync::Arc;
use ra_editor::LineIndex; use ra_editor::LineIndex;
use ra_syntax::{TextUnit, TextRange, SourceFileNode}; use ra_syntax::{TextUnit, TextRange, SourceFile, TreePtr};
pub use crate::{ pub use crate::{
cancelation::{Canceled, Cancelable}, cancelation::{Canceled, Cancelable},
@ -47,7 +47,7 @@ pub trait BaseDatabase: salsa::Database {
salsa::query_group! { salsa::query_group! {
pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase {
fn source_file(file_id: FileId) -> SourceFileNode { fn source_file(file_id: FileId) -> TreePtr<SourceFile> {
type SourceFileQuery; type SourceFileQuery;
} }
fn file_lines(file_id: FileId) -> Arc<LineIndex> { fn file_lines(file_id: FileId) -> Arc<LineIndex> {
@ -56,9 +56,9 @@ salsa::query_group! {
} }
} }
fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreePtr<SourceFile> {
let text = db.file_text(file_id); let text = db.file_text(file_id);
SourceFileNode::parse(&*text) SourceFile::parse(&*text)
} }
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> { fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id); let text = db.file_text(file_id);

View file

@ -1,4 +1,4 @@
use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange}; use ra_syntax::{AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, TreePtr};
/// A pointer to a syntax node inside a file. /// A pointer to a syntax node inside a file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -8,18 +8,18 @@ pub struct LocalSyntaxPtr {
} }
impl LocalSyntaxPtr { impl LocalSyntaxPtr {
pub fn new(node: SyntaxNodeRef) -> LocalSyntaxPtr { pub fn new(node: &SyntaxNode) -> LocalSyntaxPtr {
LocalSyntaxPtr { LocalSyntaxPtr {
range: node.range(), range: node.range(),
kind: node.kind(), kind: node.kind(),
} }
} }
pub fn resolve(self, file: &SourceFileNode) -> SyntaxNode { pub fn resolve(self, file: &SourceFile) -> TreePtr<SyntaxNode> {
let mut curr = file.syntax(); let mut curr = file.syntax();
loop { loop {
if curr.range() == self.range && curr.kind() == self.kind { if curr.range() == self.range && curr.kind() == self.kind {
return curr.owned(); return curr.to_owned();
} }
curr = curr curr = curr
.children() .children()
@ -40,7 +40,7 @@ impl LocalSyntaxPtr {
#[test] #[test]
fn test_local_syntax_ptr() { fn test_local_syntax_ptr() {
use ra_syntax::{ast, AstNode}; use ra_syntax::{ast, AstNode};
let file = SourceFileNode::parse("struct Foo { f: u32, }"); let file = SourceFile::parse("struct Foo { f: u32, }");
let field = file let field = file
.syntax() .syntax()
.descendants() .descendants()
@ -48,5 +48,5 @@ fn test_local_syntax_ptr() {
.unwrap(); .unwrap();
let ptr = LocalSyntaxPtr::new(field.syntax()); let ptr = LocalSyntaxPtr::new(field.syntax());
let field_syntax = ptr.resolve(&file); let field_syntax = ptr.resolve(&file);
assert_eq!(field.syntax(), field_syntax); assert_eq!(field.syntax(), &*field_syntax);
} }

View file

@ -20,7 +20,7 @@ impl Types for RaTypes {
pub type GreenNode = rowan::GreenNode<RaTypes>; pub type GreenNode = rowan::GreenNode<RaTypes>;
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash)]
pub struct TreePtr<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>>( pub struct TreePtr<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>>(
pub(crate) rowan::TreePtr<RaTypes, T>, pub(crate) rowan::TreePtr<RaTypes, T>,
); );
@ -47,6 +47,15 @@ where
} }
} }
impl<T> Clone for TreePtr<T>
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
{
fn clone(&self) -> TreePtr<T> {
TreePtr(self.0.clone())
}
}
impl<T> fmt::Debug for TreePtr<T> impl<T> fmt::Debug for TreePtr<T>
where where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>, T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,