From 27dd0086ea0642cc36e2a388dcfd7e5f6a466ac5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Apr 2020 21:23:36 +0200 Subject: Fully get rid of SyntaxNodePtr::range --- crates/ra_hir_ty/src/tests.rs | 52 ++++++++++++++++---------- crates/ra_syntax/src/ptr.rs | 6 +-- crates/rust-analyzer/src/cli/analysis_stats.rs | 6 ++- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 81fc0f63a..846005baa 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -18,16 +18,19 @@ use hir_def::{ nameres::CrateDefMap, AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, }; -use hir_expand::InFile; +use hir_expand::{db::AstDatabase, InFile}; use insta::assert_snapshot; use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase}; use ra_syntax::{ algo, ast::{self, AstNode}, + SyntaxNode, }; use stdx::format_to; -use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult}; +use crate::{ + db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty, +}; // These tests compare the inference results for all expressions in a file // against snapshots of the expected results using insta. Use cargo-insta to @@ -67,13 +70,19 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { let mut infer_def = |inference_result: Arc, body_source_map: Arc| { - let mut types = Vec::new(); - let mut mismatches = Vec::new(); + let mut types: Vec<(InFile, &Ty)> = Vec::new(); + let mut mismatches: Vec<(InFile, &TypeMismatch)> = Vec::new(); for (pat, ty) in inference_result.type_of_pat.iter() { let syntax_ptr = match body_source_map.pat_syntax(pat) { Ok(sp) => { - sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr())) + let root = db.parse_or_expand(sp.file_id).unwrap(); + sp.map(|ptr| { + ptr.either( + |it| it.to_node(&root).syntax().clone(), + |it| it.to_node(&root).syntax().clone(), + ) + }) } Err(SyntheticSyntax) => continue, }; @@ -81,29 +90,31 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { } for (expr, ty) in inference_result.type_of_expr.iter() { - let syntax_ptr = match body_source_map.expr_syntax(expr) { - Ok(sp) => sp.map(|ast| ast.syntax_node_ptr()), + let node = match body_source_map.expr_syntax(expr) { + Ok(sp) => { + let root = db.parse_or_expand(sp.file_id).unwrap(); + sp.map(|ptr| ptr.to_node(&root).syntax().clone()) + } Err(SyntheticSyntax) => continue, }; - types.push((syntax_ptr.clone(), ty)); + types.push((node.clone(), ty)); if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) { - mismatches.push((syntax_ptr, mismatch)); + mismatches.push((node, mismatch)); } } // sort ranges for consistency - types.sort_by_key(|(src_ptr, _)| { - (src_ptr.value.range().start(), src_ptr.value.range().end()) + types.sort_by_key(|(node, _)| { + let range = node.value.text_range(); + (range.start(), range.end()) }); - for (src_ptr, ty) in &types { - let node = src_ptr.value.to_node(&src_ptr.file_syntax(&db)); - - let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) { + for (node, ty) in &types { + let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) { (self_param.self_token().unwrap().text_range(), "self".to_string()) } else { - (src_ptr.value.range(), node.text().to_string().replace("\n", " ")) + (node.value.text_range(), node.value.text().to_string().replace("\n", " ")) }; - let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" }; + let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" }; format_to!( buf, "{}{} '{}': {}\n", @@ -114,11 +125,12 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { ); } if include_mismatches { - mismatches.sort_by_key(|(src_ptr, _)| { - (src_ptr.value.range().start(), src_ptr.value.range().end()) + mismatches.sort_by_key(|(node, _)| { + let range = node.value.text_range(); + (range.start(), range.end()) }); for (src_ptr, mismatch) in &mismatches { - let range = src_ptr.value.range(); + let range = src_ptr.value.text_range(); let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" }; format_to!( buf, diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index 3be648c2a..8d9812d24 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs @@ -30,9 +30,9 @@ impl SyntaxNodePtr { .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) } - pub fn range(&self) -> TextRange { - self.range - } + // pub fn range(&self) -> TextRange { + // self.range + // } pub fn cast(self) -> Option> { if !N::can_cast(self.kind) { diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index d442cbd63..9fa7dad71 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -162,9 +162,13 @@ pub fn analysis_stats( let (_, sm) = db.body_with_source_map(f_id.into()); let src = sm.expr_syntax(expr_id); if let Ok(src) = src { + let node = { + let root = db.parse_or_expand(src.file_id).unwrap(); + src.value.to_node(&root) + }; let original_file = src.file_id.original_file(db); let line_index = host.analysis().file_line_index(original_file).unwrap(); - let text_range = src.value.syntax_node_ptr().range(); + let text_range = node.syntax().text_range(); let (start, end) = ( line_index.line_col(text_range.start()), line_index.line_col(text_range.end()), -- cgit v1.2.3