From 7b901f86cd1d0198994e5a2ab7eea18f444dd148 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 17:37:10 +0300 Subject: move SyntaxPtr to ra_syntax --- .../ra_hir/src/code_model_impl/function/scope.rs | 13 +++--- crates/ra_hir/src/expr.rs | 50 +++++++++++----------- crates/ra_hir/src/macros.rs | 13 +++--- crates/ra_hir/src/nameres/lower.rs | 10 ++--- crates/ra_hir/src/ty/tests.rs | 2 +- 5 files changed, 44 insertions(+), 44 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model_impl/function/scope.rs b/crates/ra_hir/src/code_model_impl/function/scope.rs index 3a7d53a93..c5d1de5eb 100644 --- a/crates/ra_hir/src/code_model_impl/function/scope.rs +++ b/crates/ra_hir/src/code_model_impl/function/scope.rs @@ -3,12 +3,11 @@ use std::sync::Arc; use rustc_hash::{FxHashMap, FxHashSet}; use ra_syntax::{ - AstNode, SyntaxNode, TextUnit, TextRange, + AstNode, SyntaxNode, TextUnit, TextRange, SyntaxNodePtr, algo::generate, ast, }; use ra_arena::{Arena, RawId, impl_arena_id}; -use ra_db::LocalSyntaxPtr; use crate::{Name, AsName, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping}}; @@ -126,7 +125,7 @@ pub struct ScopesWithSyntaxMapping { #[derive(Debug, Clone, PartialEq, Eq)] pub struct ScopeEntryWithSyntax { name: Name, - ptr: LocalSyntaxPtr, + ptr: SyntaxNodePtr, } impl ScopeEntryWithSyntax { @@ -134,7 +133,7 @@ impl ScopeEntryWithSyntax { &self.name } - pub fn ptr(&self) -> LocalSyntaxPtr { + pub fn ptr(&self) -> SyntaxNodePtr { self.ptr } } @@ -169,7 +168,7 @@ impl ScopesWithSyntaxMapping { // XXX: during completion, cursor might be outside of any particular // expression. Try to figure out the correct scope... - fn adjust(&self, ptr: LocalSyntaxPtr, original_scope: ScopeId, offset: TextUnit) -> ScopeId { + fn adjust(&self, ptr: SyntaxNodePtr, original_scope: ScopeId, offset: TextUnit) -> ScopeId { let r = ptr.range(); let child_scopes = self .scopes @@ -212,7 +211,7 @@ impl ScopesWithSyntaxMapping { pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); - let name_ptr = LocalSyntaxPtr::new(pat.syntax()); + let name_ptr = SyntaxNodePtr::new(pat.syntax()); fn_def .syntax() .descendants() @@ -230,7 +229,7 @@ impl ScopesWithSyntaxMapping { fn scope_for(&self, node: &SyntaxNode) -> Option { node.ancestors() - .map(LocalSyntaxPtr::new) + .map(SyntaxNodePtr::new) .filter_map(|ptr| self.syntax_mapping.syntax_expr(ptr)) .find_map(|it| self.scopes.scope_for(it)) } diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 51dae8e25..1a3821692 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -4,8 +4,10 @@ use std::sync::Arc; use rustc_hash::FxHashMap; use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; -use ra_db::LocalSyntaxPtr; -use ra_syntax::ast::{self, AstNode, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor}; +use ra_syntax::{ + SyntaxNodePtr, AstNode, + ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor} +}; use crate::{Path, type_ref::{Mutability, TypeRef}, Name, HirDatabase, DefId, Def, name::AsName}; use crate::ty::primitive::{UintTy, UncertainIntTy, UncertainFloatTy}; @@ -38,10 +40,10 @@ pub struct Body { #[derive(Debug, Eq, PartialEq)] pub struct BodySyntaxMapping { body: Arc, - expr_syntax_mapping: FxHashMap, - expr_syntax_mapping_back: ArenaMap, - pat_syntax_mapping: FxHashMap, - pat_syntax_mapping_back: ArenaMap, + expr_syntax_mapping: FxHashMap, + expr_syntax_mapping_back: ArenaMap, + pat_syntax_mapping: FxHashMap, + pat_syntax_mapping_back: ArenaMap, } impl Body { @@ -71,31 +73,31 @@ impl Index for Body { } impl BodySyntaxMapping { - pub fn expr_syntax(&self, expr: ExprId) -> Option { + pub fn expr_syntax(&self, expr: ExprId) -> Option { self.expr_syntax_mapping_back.get(expr).cloned() } - pub fn syntax_expr(&self, ptr: LocalSyntaxPtr) -> Option { + pub fn syntax_expr(&self, ptr: SyntaxNodePtr) -> Option { self.expr_syntax_mapping.get(&ptr).cloned() } pub fn node_expr(&self, node: &ast::Expr) -> Option { self.expr_syntax_mapping - .get(&LocalSyntaxPtr::new(node.syntax())) + .get(&SyntaxNodePtr::new(node.syntax())) .cloned() } - pub fn pat_syntax(&self, pat: PatId) -> Option { + pub fn pat_syntax(&self, pat: PatId) -> Option { self.pat_syntax_mapping_back.get(pat).cloned() } - pub fn syntax_pat(&self, ptr: LocalSyntaxPtr) -> Option { + pub fn syntax_pat(&self, ptr: SyntaxNodePtr) -> Option { self.pat_syntax_mapping.get(&ptr).cloned() } pub fn node_pat(&self, node: &ast::Pat) -> Option { self.pat_syntax_mapping - .get(&LocalSyntaxPtr::new(node.syntax())) + .get(&SyntaxNodePtr::new(node.syntax())) .cloned() } @@ -440,10 +442,10 @@ pub(crate) fn body_hir(db: &impl HirDatabase, def_id: DefId) -> Arc { struct ExprCollector { exprs: Arena, pats: Arena, - expr_syntax_mapping: FxHashMap, - expr_syntax_mapping_back: ArenaMap, - pat_syntax_mapping: FxHashMap, - pat_syntax_mapping_back: ArenaMap, + expr_syntax_mapping: FxHashMap, + expr_syntax_mapping_back: ArenaMap, + pat_syntax_mapping: FxHashMap, + pat_syntax_mapping_back: ArenaMap, } impl ExprCollector { @@ -458,14 +460,14 @@ impl ExprCollector { } } - fn alloc_expr(&mut self, expr: Expr, syntax_ptr: LocalSyntaxPtr) -> ExprId { + fn alloc_expr(&mut self, expr: Expr, syntax_ptr: SyntaxNodePtr) -> ExprId { let id = self.exprs.alloc(expr); self.expr_syntax_mapping.insert(syntax_ptr, id); self.expr_syntax_mapping_back.insert(id, syntax_ptr); id } - fn alloc_pat(&mut self, pat: Pat, syntax_ptr: LocalSyntaxPtr) -> PatId { + fn alloc_pat(&mut self, pat: Pat, syntax_ptr: SyntaxNodePtr) -> PatId { let id = self.pats.alloc(pat); self.pat_syntax_mapping.insert(syntax_ptr, id); self.pat_syntax_mapping_back.insert(id, syntax_ptr); @@ -481,7 +483,7 @@ impl ExprCollector { } fn collect_expr(&mut self, expr: &ast::Expr) -> ExprId { - let syntax_ptr = LocalSyntaxPtr::new(expr.syntax()); + let syntax_ptr = SyntaxNodePtr::new(expr.syntax()); match expr.kind() { ast::ExprKind::IfExpr(e) => { if let Some(pat) = e.condition().and_then(|c| c.pat()) { @@ -643,9 +645,9 @@ impl ExprCollector { // field shorthand let id = self.exprs.alloc(Expr::Path(Path::from_name_ref(nr))); self.expr_syntax_mapping - .insert(LocalSyntaxPtr::new(nr.syntax()), id); + .insert(SyntaxNodePtr::new(nr.syntax()), id); self.expr_syntax_mapping_back - .insert(id, LocalSyntaxPtr::new(nr.syntax())); + .insert(id, SyntaxNodePtr::new(nr.syntax())); id } else { self.exprs.alloc(Expr::Missing) @@ -806,7 +808,7 @@ impl ExprCollector { let tail = block.expr().map(|e| self.collect_expr(e)); self.alloc_expr( Expr::Block { statements, tail }, - LocalSyntaxPtr::new(block.syntax()), + SyntaxNodePtr::new(block.syntax()), ) } @@ -883,7 +885,7 @@ impl ExprCollector { // TODO: implement ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, }; - let syntax_ptr = LocalSyntaxPtr::new(pat.syntax()); + let syntax_ptr = SyntaxNodePtr::new(pat.syntax()); self.alloc_pat(pattern, syntax_ptr) } @@ -919,7 +921,7 @@ pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping { let mut params = Vec::new(); if let Some(self_param) = param_list.self_param() { - let self_param = LocalSyntaxPtr::new( + let self_param = SyntaxNodePtr::new( self_param .self_kw() .expect("self param without self keyword") diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index 220bee94e..7ca34d434 100644 --- a/crates/ra_hir/src/macros.rs +++ b/crates/ra_hir/src/macros.rs @@ -9,9 +9,8 @@ /// those yet, so all macros are string based at the moment! use std::sync::Arc; -use ra_db::LocalSyntaxPtr; use ra_syntax::{ - TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreeArc, + TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreeArc, SyntaxNodePtr, ast::{self, NameOwner}, }; @@ -80,7 +79,7 @@ impl MacroDef { let file = SourceFile::parse(&text); let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?; let match_arg = match_expr.expr()?; - let ptr = LocalSyntaxPtr::new(match_arg.syntax()); + let ptr = SyntaxNodePtr::new(match_arg.syntax()); let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text)); let ranges_map = vec![(src_range, match_arg.syntax().range())]; let res = MacroExpansion { @@ -94,7 +93,7 @@ impl MacroDef { let text = format!(r"fn dummy() {{ {}; }}", input.text); let file = SourceFile::parse(&text); let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?; - let ptr = LocalSyntaxPtr::new(array_expr.syntax()); + let ptr = SyntaxNodePtr::new(array_expr.syntax()); let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text)); let ranges_map = vec![(src_range, array_expr.syntax().range())]; let res = MacroExpansion { @@ -119,7 +118,7 @@ impl MacroDef { let file = SourceFile::parse(&text); let trait_def = file.syntax().descendants().find_map(ast::TraitDef::cast)?; let name = trait_def.name()?; - let ptr = LocalSyntaxPtr::new(trait_def.syntax()); + let ptr = SyntaxNodePtr::new(trait_def.syntax()); let ranges_map = vec![(src_range, name.syntax().range())]; let res = MacroExpansion { text, @@ -146,7 +145,7 @@ pub struct MacroExpansion { /// Implementation detail: internally, a macro is expanded to the whole file, /// even if it is an expression. This `ptr` selects the actual expansion from /// the expanded file. - ptr: LocalSyntaxPtr, + ptr: SyntaxNodePtr, } impl MacroExpansion { @@ -157,7 +156,7 @@ impl MacroExpansion { } pub fn syntax(&self) -> TreeArc { - self.ptr.resolve(&self.file()) + self.ptr.to_node(&self.file()).to_owned() } /// Maps range in the source code to the range in the expanded code. pub fn map_range_forward(&self, src_range: TextRange) -> Option { diff --git a/crates/ra_hir/src/nameres/lower.rs b/crates/ra_hir/src/nameres/lower.rs index 52448644c..ab6f3a9bc 100644 --- a/crates/ra_hir/src/nameres/lower.rs +++ b/crates/ra_hir/src/nameres/lower.rs @@ -1,10 +1,10 @@ use std::sync::Arc; use ra_syntax::{ - SyntaxKind, AstNode, SourceFile, TreeArc, + SyntaxKind, AstNode, SourceFile, TreeArc, SyntaxNodePtr, ast::{self, ModuleItemOwner}, }; -use ra_db::{SourceRootId, LocalSyntaxPtr}; +use ra_db::{SourceRootId}; use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; use crate::{ @@ -72,13 +72,13 @@ pub struct LoweredModule { #[derive(Debug, Default, PartialEq, Eq)] pub struct ImportSourceMap { - map: ArenaMap, + map: ArenaMap, } impl ImportSourceMap { fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { self.map - .insert(import, LocalSyntaxPtr::new(segment.syntax())) + .insert(import, SyntaxNodePtr::new(segment.syntax())) } pub fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc { @@ -87,7 +87,7 @@ impl ImportSourceMap { ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), }; - ast::PathSegment::cast(&self.map[import].resolve(file)) + ast::PathSegment::cast(self.map[import].to_node(file)) .unwrap() .to_owned() } diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 9bf1fd3b2..92c74cf00 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -562,7 +562,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.resolve(&source_file); + let node = syntax_ptr.to_node(&source_file); write!( acc, "{} '{}': {}\n", -- cgit v1.2.3