migrate ra_analysis to new rowan

This commit is contained in:
Aleksey Kladov 2019-01-08 11:47:28 +03:00
parent da0b348ae9
commit 3ffd5dd2a6
12 changed files with 67 additions and 78 deletions

View file

@ -39,9 +39,9 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
.add_to(acc)
});
fn process<'a, N: ast::FnDefOwner<'a>>(
node: N,
params: &mut FxHashMap<String, (u32, ast::Param<'a>)>,
fn process<'a, N: ast::FnDefOwner>(
node: &'a N,
params: &mut FxHashMap<String, (u32, &'a ast::Param)>,
) {
node.functions()
.filter_map(|it| it.param_list())

View file

@ -2,7 +2,7 @@ use ra_syntax::{
algo::visit::{visitor, Visitor},
AstNode,
ast::{self, LoopBodyOwner},
SyntaxKind::*, SyntaxNodeRef,
SyntaxKind::*, SyntaxNode,
};
use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind};
@ -76,7 +76,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
acc.add_all(complete_return(fn_def, ctx.can_be_stmt));
}
fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool {
fn is_in_loop_body(leaf: &SyntaxNode) -> bool {
for node in leaf.ancestors() {
if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR {
break;
@ -95,7 +95,7 @@ fn is_in_loop_body(leaf: SyntaxNodeRef) -> bool {
false
}
fn complete_return(fn_def: ast::FnDef, can_be_stmt: bool) -> Option<CompletionItem> {
fn complete_return(fn_def: &ast::FnDef, can_be_stmt: bool) -> Option<CompletionItem> {
let snip = match (can_be_stmt, fn_def.ret_type().is_some()) {
(true, true) => "return $0;",
(true, false) => "return;",

View file

@ -1,13 +1,9 @@
use ra_editor::find_node_at_offset;
use ra_text_edit::AtomTextEdit;
use ra_syntax::{
algo::{find_leaf_at_offset, find_covering_node},
AstNode, SyntaxNode, SourceFile, TextUnit, TextRange,
ast,
AstNode,
SyntaxNodeRef,
SourceFileNode,
TextUnit,
TextRange,
algo::{find_leaf_at_offset, find_covering_node},
SyntaxKind::*,
};
use hir::source_binder;
@ -20,11 +16,11 @@ use crate::{db, FilePosition, Cancelable};
pub(super) struct CompletionContext<'a> {
pub(super) db: &'a db::RootDatabase,
pub(super) offset: TextUnit,
pub(super) leaf: SyntaxNodeRef<'a>,
pub(super) leaf: &'a SyntaxNode,
pub(super) module: Option<hir::Module>,
pub(super) function: Option<hir::Function>,
pub(super) function_syntax: Option<ast::FnDef<'a>>,
pub(super) use_item_syntax: Option<ast::UseItem<'a>>,
pub(super) function_syntax: Option<&'a ast::FnDef>,
pub(super) use_item_syntax: Option<&'a ast::UseItem>,
pub(super) is_param: bool,
/// A single-indent path, like `foo`.
pub(super) is_trivial_path: bool,
@ -36,7 +32,7 @@ pub(super) struct CompletionContext<'a> {
/// Something is typed at the "top" level, in module or impl/trait.
pub(super) is_new_item: bool,
/// The receiver if this is a field or method access, i.e. writing something.<|>
pub(super) dot_receiver: Option<ast::Expr<'a>>,
pub(super) dot_receiver: Option<&'a ast::Expr>,
/// If this is a method call in particular, i.e. the () are already there.
pub(super) is_method_call: bool,
}
@ -44,7 +40,7 @@ pub(super) struct CompletionContext<'a> {
impl<'a> CompletionContext<'a> {
pub(super) fn new(
db: &'a db::RootDatabase,
original_file: &'a SourceFileNode,
original_file: &'a SourceFile,
position: FilePosition,
) -> Cancelable<Option<CompletionContext<'a>>> {
let module = source_binder::module_from_position(db, position)?;
@ -71,7 +67,7 @@ impl<'a> CompletionContext<'a> {
Ok(Some(ctx))
}
fn fill(&mut self, original_file: &'a SourceFileNode, offset: TextUnit) {
fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) {
// Insert a fake ident to get a valid parse tree. We will use this file
// to determine context, though the original_file will be used for
// actual completion.
@ -100,7 +96,7 @@ impl<'a> CompletionContext<'a> {
}
}
}
fn classify_name_ref(&mut self, original_file: &'a SourceFileNode, name_ref: ast::NameRef) {
fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) {
let name_range = name_ref.syntax().range();
let top_node = name_ref
.syntax()
@ -197,15 +193,12 @@ impl<'a> CompletionContext<'a> {
}
}
fn find_node_with_range<'a, N: AstNode<'a>>(
syntax: SyntaxNodeRef<'a>,
range: TextRange,
) -> Option<N> {
fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<&N> {
let node = find_covering_node(syntax, range);
node.ancestors().find_map(N::cast)
}
fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool {
fn is_node<N: AstNode>(node: &SyntaxNode) -> bool {
match node.ancestors().filter_map(N::cast).next() {
None => false,
Some(n) => n.syntax().range() == node.range(),

View file

@ -1,6 +1,6 @@
use ra_db::SyntaxDatabase;
use ra_syntax::{
SyntaxNodeRef, AstNode, SourceFileNode,
SyntaxNode, AstNode, SourceFile,
ast, algo::find_covering_node,
};
@ -19,18 +19,18 @@ pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRang
fn extend_selection_in_macro(
_db: &RootDatabase,
source_file: &SourceFileNode,
source_file: &SourceFile,
frange: FileRange,
) -> Option<TextRange> {
let macro_call = find_macro_call(source_file.syntax(), frange.range)?;
let (off, exp) = hir::MacroDef::ast_expand(macro_call)?;
let dst_range = exp.map_range_forward(frange.range - off)?;
let dst_range = ra_editor::extend_selection(exp.syntax().borrowed(), dst_range)?;
let dst_range = ra_editor::extend_selection(&exp.syntax(), dst_range)?;
let src_range = exp.map_range_back(dst_range)? + off;
Some(src_range)
}
fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> {
fn find_macro_call(node: &SyntaxNode, range: TextRange) -> Option<&ast::MacroCall> {
find_covering_node(node, range)
.ancestors()
.find_map(ast::MacroCall::cast)

View file

@ -23,7 +23,7 @@ pub(crate) fn goto_defenition(
pub(crate) fn reference_defenition(
db: &RootDatabase,
file_id: FileId,
name_ref: ast::NameRef,
name_ref: &ast::NameRef,
) -> Cancelable<Vec<NavigationTarget>> {
if let Some(fn_descr) =
hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax())?
@ -53,7 +53,7 @@ pub(crate) fn reference_defenition(
fn name_defenition(
db: &RootDatabase,
file_id: FileId,
name: ast::Name,
name: &ast::Name,
) -> Cancelable<Option<Vec<NavigationTarget>>> {
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
if module.has_semi() {

View file

@ -1,7 +1,7 @@
use ra_db::{Cancelable, SyntaxDatabase};
use ra_editor::find_node_at_offset;
use ra_syntax::{
AstNode, SyntaxNode,
AstNode, SyntaxNode, TreePtr,
ast::{self, NameOwner},
algo::{find_covering_node, find_leaf_at_offset, visit::{visitor, Visitor}},
};
@ -88,20 +88,19 @@ fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Cancelable<Option<S
}
impl NavigationTarget {
fn node(&self, db: &RootDatabase) -> Option<SyntaxNode> {
fn node(&self, db: &RootDatabase) -> Option<TreePtr<SyntaxNode>> {
let source_file = db.source_file(self.file_id);
let source_file = source_file.syntax();
let node = source_file
.descendants()
.find(|node| node.kind() == self.kind && node.range() == self.range)?
.owned();
.to_owned();
Some(node)
}
fn docs(&self, db: &RootDatabase) -> Option<String> {
let node = self.node(db)?;
let node = node.borrowed();
fn doc_comments<'a, N: ast::DocCommentsOwner<'a>>(node: N) -> Option<String> {
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
let comments = node.doc_comment_text();
if comments.is_empty() {
None
@ -119,7 +118,7 @@ impl NavigationTarget {
.visit(doc_comments::<ast::TypeDef>)
.visit(doc_comments::<ast::ConstDef>)
.visit(doc_comments::<ast::StaticDef>)
.accept(node)?
.accept(&node)?
}
/// Get a description of this node.
@ -128,50 +127,49 @@ impl NavigationTarget {
fn description(&self, db: &RootDatabase) -> Option<String> {
// TODO: After type inference is done, add type information to improve the output
let node = self.node(db)?;
let node = node.borrowed();
// TODO: Refactor to be have less repetition
visitor()
.visit(|node: ast::FnDef| {
.visit(|node: &ast::FnDef| {
let mut string = "fn ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::StructDef| {
.visit(|node: &ast::StructDef| {
let mut string = "struct ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::EnumDef| {
.visit(|node: &ast::EnumDef| {
let mut string = "enum ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::TraitDef| {
.visit(|node: &ast::TraitDef| {
let mut string = "trait ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::Module| {
.visit(|node: &ast::Module| {
let mut string = "mod ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::TypeDef| {
.visit(|node: &ast::TypeDef| {
let mut string = "type ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::ConstDef| {
.visit(|node: &ast::ConstDef| {
let mut string = "const ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.visit(|node: ast::StaticDef| {
.visit(|node: &ast::StaticDef| {
let mut string = "static ".to_string();
node.name()?.syntax().text().push_to(&mut string);
Some(string)
})
.accept(node)?
.accept(&node)?
}
}

View file

@ -8,10 +8,9 @@ use hir::{
use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase};
use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity};
use ra_syntax::{
ast::{self, ArgListOwner, Expr, NameOwner},
AstNode, SourceFileNode,
SyntaxNode, TextRange, TextUnit, AstNode, SourceFile,
ast::{self, ArgListOwner, NameOwner},
SyntaxKind::*,
SyntaxNodeRef, TextRange, TextUnit,
};
use crate::{
@ -113,7 +112,6 @@ impl db::RootDatabase {
None => return Ok(Vec::new()),
Some(it) => it,
};
let ast_module = ast_module.borrowed();
let name = ast_module.name().unwrap();
Ok(vec![NavigationTarget {
file_id,
@ -163,9 +161,9 @@ impl db::RootDatabase {
fn find_binding<'a>(
db: &db::RootDatabase,
source_file: &'a SourceFileNode,
source_file: &'a SourceFile,
position: FilePosition,
) -> Cancelable<Option<(ast::BindPat<'a>, hir::Function)>> {
) -> Cancelable<Option<(&'a ast::BindPat, hir::Function)>> {
let syntax = source_file.syntax();
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
let descr = ctry!(source_binder::function_from_child_node(
@ -281,7 +279,7 @@ impl db::RootDatabase {
if symbol.ptr.kind() == FN_DEF {
let fn_file = self.source_file(symbol.file_id);
let fn_def = symbol.ptr.resolve(&fn_file);
let fn_def = ast::FnDef::cast(fn_def.borrowed()).unwrap();
let fn_def = ast::FnDef::cast(&fn_def).unwrap();
let descr = ctry!(source_binder::function_from_source(
self,
symbol.file_id,
@ -352,7 +350,7 @@ impl db::RootDatabase {
.collect::<Vec<_>>();
Ok(res)
}
pub(crate) fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<FileSymbol>> {
pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Cancelable<Vec<FileSymbol>> {
let name = name_ref.text();
let mut query = Query::new(name.to_string());
query.exact();
@ -379,12 +377,12 @@ impl SourceChange {
}
enum FnCallNode<'a> {
CallExpr(ast::CallExpr<'a>),
MethodCallExpr(ast::MethodCallExpr<'a>),
CallExpr(&'a ast::CallExpr),
MethodCallExpr(&'a ast::MethodCallExpr),
}
impl<'a> FnCallNode<'a> {
pub fn with_node(syntax: SyntaxNodeRef, offset: TextUnit) -> Option<FnCallNode> {
pub fn with_node(syntax: &'a SyntaxNode, offset: TextUnit) -> Option<FnCallNode<'a>> {
if let Some(expr) = find_node_at_offset::<ast::CallExpr>(syntax, offset) {
return Some(FnCallNode::CallExpr(expr));
}
@ -394,10 +392,10 @@ impl<'a> FnCallNode<'a> {
None
}
pub fn name_ref(&self) -> Option<ast::NameRef> {
pub fn name_ref(&self) -> Option<&'a ast::NameRef> {
match *self {
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()?.kind() {
ast::ExprKind::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
_ => return None,
}),
@ -409,7 +407,7 @@ impl<'a> FnCallNode<'a> {
}
}
pub fn arg_list(&self) -> Option<ast::ArgList> {
pub fn arg_list(&self) -> Option<&'a ast::ArgList> {
match *self {
FnCallNode::CallExpr(expr) => expr.arg_list(),
FnCallNode::MethodCallExpr(expr) => expr.arg_list(),

View file

@ -26,7 +26,7 @@ mod syntax_highlighting;
use std::{fmt, sync::Arc};
use ra_syntax::{SmolStr, SourceFileNode, SyntaxKind, TextRange, TextUnit};
use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit};
use ra_text_edit::TextEdit;
use rayon::prelude::*;
use relative_path::RelativePathBuf;
@ -308,7 +308,7 @@ impl Analysis {
self.db.file_text(file_id)
}
/// Gets the syntax tree of the file.
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
pub fn file_syntax(&self, file_id: FileId) -> TreePtr<SourceFile> {
self.db.source_file(file_id).clone()
}
/// Gets the file's `LineIndex`: data structure to convert between absolute
@ -322,7 +322,7 @@ impl Analysis {
}
/// Returns position of the mathcing brace (all types of braces are
/// supported).
pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
pub fn matching_brace(&self, file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
ra_editor::matching_brace(file, offset)
}
/// Returns a syntax tree represented as `String`, for debug purposes.
@ -469,7 +469,7 @@ impl LibraryData {
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
) -> LibraryData {
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
let file = SourceFileNode::parse(text);
let file = SourceFile::parse(text);
(*file_id, file)
}));
let mut root_change = RootChange::default();

View file

@ -1,7 +1,7 @@
use itertools::Itertools;
use ra_syntax::{
TextRange, SyntaxNode,
ast::{self, AstNode, NameOwner, ModuleItemOwner},
TextRange, SyntaxNodeRef,
};
use ra_db::{Cancelable, SyntaxDatabase};
@ -30,7 +30,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Ru
Ok(res)
}
fn runnable(db: &RootDatabase, file_id: FileId, item: SyntaxNodeRef) -> Option<Runnable> {
fn runnable(db: &RootDatabase, file_id: FileId, item: &SyntaxNode) -> Option<Runnable> {
if let Some(fn_def) = ast::FnDef::cast(item) {
runnable_fn(fn_def)
} else if let Some(m) = ast::Module::cast(item) {
@ -40,7 +40,7 @@ fn runnable(db: &RootDatabase, file_id: FileId, item: SyntaxNodeRef) -> Option<R
}
}
fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> {
fn runnable_fn(fn_def: &ast::FnDef) -> Option<Runnable> {
let name = fn_def.name()?.text();
let kind = if name == "main" {
RunnableKind::Bin
@ -57,12 +57,12 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> {
})
}
fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option<Runnable> {
fn runnable_mod(db: &RootDatabase, file_id: FileId, module: &ast::Module) -> Option<Runnable> {
let has_test_function = module
.item_list()?
.items()
.filter_map(|it| match it {
ast::ModuleItem::FnDef(it) => Some(it),
.filter_map(|it| match it.kind() {
ast::ModuleItemKind::FnDef(it) => Some(it),
_ => None,
})
.any(|f| f.has_atom_attr("test"));

View file

@ -27,7 +27,7 @@ use std::{
use fst::{self, Streamer};
use ra_syntax::{
SyntaxNodeRef, SourceFileNode, SmolStr,
SyntaxNode, SourceFile, SmolStr, TreePtr, AstNode,
algo::{visit::{visitor, Visitor}, find_covering_node},
SyntaxKind::{self, *},
ast::{self, NameOwner},
@ -141,7 +141,7 @@ impl SymbolIndex {
}
pub(crate) fn for_files(
files: impl ParallelIterator<Item = (FileId, SourceFileNode)>,
files: impl ParallelIterator<Item = (FileId, TreePtr<SourceFile>)>,
) -> SymbolIndex {
let symbols = files
.flat_map(|(file_id, file)| {
@ -203,8 +203,8 @@ pub(crate) struct FileSymbol {
pub(crate) ptr: LocalSyntaxPtr,
}
fn to_symbol(node: SyntaxNodeRef) -> Option<(SmolStr, LocalSyntaxPtr)> {
fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<(SmolStr, LocalSyntaxPtr)> {
fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> {
fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> {
let name = node.name()?.text();
let ptr = LocalSyntaxPtr::new(node.syntax());
Some((name, ptr))

View file

@ -16,7 +16,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Hi
.filter_map(ast::MacroCall::cast)
{
if let Some((off, exp)) = hir::MacroDef::ast_expand(macro_call) {
let mapped_ranges = ra_editor::highlight(exp.syntax().borrowed())
let mapped_ranges = ra_editor::highlight(&exp.syntax())
.into_iter()
.filter_map(|r| {
let mapped_range = exp.map_range_back(r.range)?;

View file

@ -77,7 +77,7 @@ impl BodySyntaxMapping {
pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option<ExprId> {
self.expr_syntax_mapping.get(&ptr).cloned()
}
pub fn node_expr(&self, node: ast::Expr) -> Option<ExprId> {
pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> {
self.expr_syntax_mapping
.get(&LocalSyntaxPtr::new(node.syntax()))
.cloned()
@ -88,7 +88,7 @@ impl BodySyntaxMapping {
pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option<PatId> {
self.pat_syntax_mapping.get(&ptr).cloned()
}
pub fn node_pat(&self, node: ast::Pat) -> Option<PatId> {
pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> {
self.pat_syntax_mapping
.get(&LocalSyntaxPtr::new(node.syntax()))
.cloned()