mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Remove hover::type_of
This commit is contained in:
parent
e0c6e106d9
commit
5f30dd6f72
2 changed files with 24 additions and 79 deletions
|
@ -6,7 +6,6 @@ use ra_ide_db::{
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::find_covering_element,
|
|
||||||
ast::{self, DocCommentsOwner},
|
ast::{self, DocCommentsOwner},
|
||||||
match_ast, AstNode,
|
match_ast, AstNode,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
|
@ -16,7 +15,7 @@ use ra_syntax::{
|
||||||
use crate::{
|
use crate::{
|
||||||
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
|
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
|
||||||
references::classify_name_ref,
|
references::classify_name_ref,
|
||||||
FilePosition, FileRange, RangeInfo,
|
FilePosition, RangeInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Contains the results when hovering over an item
|
/// Contains the results when hovering over an item
|
||||||
|
@ -174,23 +173,25 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
.ancestors()
|
.ancestors()
|
||||||
.find(|n| ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some())?;
|
.find(|n| ast::Expr::cast(n.clone()).is_some() || ast::Pat::cast(n.clone()).is_some())?;
|
||||||
|
|
||||||
// if this node is a MACRO_CALL, it means that `descend_into_macros` is failed to resolve.
|
let ty = match_ast! {
|
||||||
// (e.g expanding a builtin macro). So we give up here.
|
match node {
|
||||||
if node.kind() == MACRO_CALL {
|
ast::MacroCall(_it) => {
|
||||||
return None;
|
// if this node is a MACRO_CALL, it means that `descend_into_macros` is failed to resolve.
|
||||||
}
|
// (e.g expanding a builtin macro). So we give up here.
|
||||||
|
return None;
|
||||||
// FIXME: Currently `hover::typeof` do not work inside
|
},
|
||||||
// macro expansion such that if the hover range is pointing to
|
ast::Expr(it) => {
|
||||||
// a string literal, the following type_of will return None.
|
sema.type_of_expr(&it)
|
||||||
// See also `test_hover_through_literal_string_in_macro`
|
},
|
||||||
let frange = sema.original_range(&node);
|
ast::Pat(it) => {
|
||||||
res.extend(type_of(db, frange).map(rust_code_markup));
|
sema.type_of_pat(&it)
|
||||||
if res.is_empty() {
|
},
|
||||||
return None;
|
_ => None,
|
||||||
}
|
}
|
||||||
let range = node.text_range();
|
}?;
|
||||||
|
|
||||||
|
res.extend(Some(rust_code_markup(ty.display_truncated(db, None).to_string())));
|
||||||
|
let range = sema.original_range(&node).range;
|
||||||
Some(RangeInfo::new(range, res))
|
Some(RangeInfo::new(range, res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,33 +207,12 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
|
|
||||||
let sema = Semantics::new(db);
|
|
||||||
let source_file = sema.parse(frange.file_id);
|
|
||||||
let leaf_node = find_covering_element(source_file.syntax(), frange.range);
|
|
||||||
// if we picked identifier, expand to pattern/expression
|
|
||||||
let node = leaf_node
|
|
||||||
.ancestors()
|
|
||||||
.take_while(|it| it.text_range() == leaf_node.text_range())
|
|
||||||
.find(|it| ast::Expr::cast(it.clone()).is_some() || ast::Pat::cast(it.clone()).is_some())?;
|
|
||||||
let ty = if let Some(ty) = ast::Expr::cast(node.clone()).and_then(|e| sema.type_of_expr(&e)) {
|
|
||||||
ty
|
|
||||||
} else if let Some(ty) = ast::Pat::cast(node).and_then(|p| sema.type_of_pat(&p)) {
|
|
||||||
ty
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
};
|
|
||||||
Some(ty.display_truncated(db, None).to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ra_db::FileLoader;
|
use ra_db::FileLoader;
|
||||||
use ra_syntax::TextRange;
|
use ra_syntax::TextRange;
|
||||||
|
|
||||||
use crate::mock_analysis::{
|
use crate::mock_analysis::{analysis_and_position, single_file_with_position};
|
||||||
analysis_and_position, single_file_with_position, single_file_with_range,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn trim_markup(s: &str) -> &str {
|
fn trim_markup(s: &str) -> &str {
|
||||||
s.trim_start_matches("```rust\n").trim_end_matches("\n```")
|
s.trim_start_matches("```rust\n").trim_end_matches("\n```")
|
||||||
|
@ -524,37 +504,6 @@ fn func(foo: i32) { if true { <|>foo; }; }
|
||||||
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
|
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_type_of_for_function() {
|
|
||||||
let (analysis, range) = single_file_with_range(
|
|
||||||
"
|
|
||||||
pub fn foo() -> u32 { 1 };
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let foo_test = <|>foo()<|>;
|
|
||||||
}
|
|
||||||
",
|
|
||||||
);
|
|
||||||
|
|
||||||
let type_name = analysis.type_of(range).unwrap().unwrap();
|
|
||||||
assert_eq!("u32", &type_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_type_of_for_expr() {
|
|
||||||
let (analysis, range) = single_file_with_range(
|
|
||||||
"
|
|
||||||
fn main() {
|
|
||||||
let foo: usize = 1;
|
|
||||||
let bar = <|>1 + foo<|>;
|
|
||||||
}
|
|
||||||
",
|
|
||||||
);
|
|
||||||
|
|
||||||
let type_name = analysis.type_of(range).unwrap().unwrap();
|
|
||||||
assert_eq!("usize", &type_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hover_infer_associated_method_result() {
|
fn test_hover_infer_associated_method_result() {
|
||||||
let (analysis, position) = single_file_with_position(
|
let (analysis, position) = single_file_with_position(
|
||||||
|
@ -791,9 +740,7 @@ fn func(foo: i32) { if true { <|>foo; }; }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hover_through_literal_string_in_macro() {
|
fn test_hover_through_literal_string_in_macro() {
|
||||||
// FIXME: Currently `hover::type_of` do not work inside
|
let hover_on = check_hover_result(
|
||||||
// macro expansion
|
|
||||||
check_hover_no_result(
|
|
||||||
r#"
|
r#"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
macro_rules! arr {
|
macro_rules! arr {
|
||||||
|
@ -804,7 +751,10 @@ fn func(foo: i32) { if true { <|>foo; }; }
|
||||||
let _ = arr!("Tr<|>acks", &mastered_for_itunes);
|
let _ = arr!("Tr<|>acks", &mastered_for_itunes);
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
&["&str"],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_eq!(hover_on, "\"Tracks\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -455,11 +455,6 @@ impl Analysis {
|
||||||
self.with_db(|db| diagnostics::diagnostics(db, file_id))
|
self.with_db(|db| diagnostics::diagnostics(db, file_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the type of the expression at the given position.
|
|
||||||
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
|
|
||||||
self.with_db(|db| hover::type_of(db, frange))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the edit required to rename reference at the position to the new
|
/// Returns the edit required to rename reference at the position to the new
|
||||||
/// name.
|
/// name.
|
||||||
pub fn rename(
|
pub fn rename(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue