mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Fully get rid of SyntaxNodePtr::range
This commit is contained in:
parent
29bc218fba
commit
27dd0086ea
3 changed files with 40 additions and 24 deletions
|
@ -18,16 +18,19 @@ use hir_def::{
|
||||||
nameres::CrateDefMap,
|
nameres::CrateDefMap,
|
||||||
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
||||||
};
|
};
|
||||||
use hir_expand::InFile;
|
use hir_expand::{db::AstDatabase, InFile};
|
||||||
use insta::assert_snapshot;
|
use insta::assert_snapshot;
|
||||||
use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
|
use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo,
|
algo,
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
|
SyntaxNode,
|
||||||
};
|
};
|
||||||
use stdx::format_to;
|
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
|
// These tests compare the inference results for all expressions in a file
|
||||||
// against snapshots of the expected results using insta. Use cargo-insta to
|
// 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<InferenceResult>,
|
let mut infer_def = |inference_result: Arc<InferenceResult>,
|
||||||
body_source_map: Arc<BodySourceMap>| {
|
body_source_map: Arc<BodySourceMap>| {
|
||||||
let mut types = Vec::new();
|
let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
|
||||||
let mut mismatches = Vec::new();
|
let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
|
||||||
|
|
||||||
for (pat, ty) in inference_result.type_of_pat.iter() {
|
for (pat, ty) in inference_result.type_of_pat.iter() {
|
||||||
let syntax_ptr = match body_source_map.pat_syntax(pat) {
|
let syntax_ptr = match body_source_map.pat_syntax(pat) {
|
||||||
Ok(sp) => {
|
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,
|
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() {
|
for (expr, ty) in inference_result.type_of_expr.iter() {
|
||||||
let syntax_ptr = match body_source_map.expr_syntax(expr) {
|
let node = match body_source_map.expr_syntax(expr) {
|
||||||
Ok(sp) => sp.map(|ast| ast.syntax_node_ptr()),
|
Ok(sp) => {
|
||||||
|
let root = db.parse_or_expand(sp.file_id).unwrap();
|
||||||
|
sp.map(|ptr| ptr.to_node(&root).syntax().clone())
|
||||||
|
}
|
||||||
Err(SyntheticSyntax) => continue,
|
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) {
|
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
|
||||||
mismatches.push((syntax_ptr, mismatch));
|
mismatches.push((node, mismatch));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort ranges for consistency
|
// sort ranges for consistency
|
||||||
types.sort_by_key(|(src_ptr, _)| {
|
types.sort_by_key(|(node, _)| {
|
||||||
(src_ptr.value.range().start(), src_ptr.value.range().end())
|
let range = node.value.text_range();
|
||||||
|
(range.start(), range.end())
|
||||||
});
|
});
|
||||||
for (src_ptr, ty) in &types {
|
for (node, 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.value.clone()) {
|
||||||
|
|
||||||
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
|
|
||||||
(self_param.self_token().unwrap().text_range(), "self".to_string())
|
(self_param.self_token().unwrap().text_range(), "self".to_string())
|
||||||
} else {
|
} 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!(
|
format_to!(
|
||||||
buf,
|
buf,
|
||||||
"{}{} '{}': {}\n",
|
"{}{} '{}': {}\n",
|
||||||
|
@ -114,11 +125,12 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if include_mismatches {
|
if include_mismatches {
|
||||||
mismatches.sort_by_key(|(src_ptr, _)| {
|
mismatches.sort_by_key(|(node, _)| {
|
||||||
(src_ptr.value.range().start(), src_ptr.value.range().end())
|
let range = node.value.text_range();
|
||||||
|
(range.start(), range.end())
|
||||||
});
|
});
|
||||||
for (src_ptr, mismatch) in &mismatches {
|
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 { "" };
|
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
|
||||||
format_to!(
|
format_to!(
|
||||||
buf,
|
buf,
|
||||||
|
|
|
@ -30,9 +30,9 @@ impl SyntaxNodePtr {
|
||||||
.unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
|
.unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn range(&self) -> TextRange {
|
// pub fn range(&self) -> TextRange {
|
||||||
self.range
|
// self.range
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> {
|
pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> {
|
||||||
if !N::can_cast(self.kind) {
|
if !N::can_cast(self.kind) {
|
||||||
|
|
|
@ -162,9 +162,13 @@ pub fn analysis_stats(
|
||||||
let (_, sm) = db.body_with_source_map(f_id.into());
|
let (_, sm) = db.body_with_source_map(f_id.into());
|
||||||
let src = sm.expr_syntax(expr_id);
|
let src = sm.expr_syntax(expr_id);
|
||||||
if let Ok(src) = src {
|
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 original_file = src.file_id.original_file(db);
|
||||||
let line_index = host.analysis().file_line_index(original_file).unwrap();
|
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) = (
|
let (start, end) = (
|
||||||
line_index.line_col(text_range.start()),
|
line_index.line_col(text_range.start()),
|
||||||
line_index.line_col(text_range.end()),
|
line_index.line_col(text_range.end()),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue