Remove hover::type_of

This commit is contained in:
Edwin Cheng 2020-02-28 11:37:22 +08:00
parent e0c6e106d9
commit 5f30dd6f72
2 changed files with 24 additions and 79 deletions

View File

@ -6,7 +6,6 @@
RootDatabase,
};
use ra_syntax::{
algo::find_covering_element,
ast::{self, DocCommentsOwner},
match_ast, AstNode,
SyntaxKind::*,
@ -16,7 +15,7 @@
use crate::{
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
references::classify_name_ref,
FilePosition, FileRange, RangeInfo,
FilePosition, RangeInfo,
};
/// Contains the results when hovering over an item
@ -174,23 +173,25 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
.ancestors()
.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.
// (e.g expanding a builtin macro). So we give up here.
if node.kind() == MACRO_CALL {
return None;
}
// FIXME: Currently `hover::typeof` do not work inside
// macro expansion such that if the hover range is pointing to
// a string literal, the following type_of will return None.
// See also `test_hover_through_literal_string_in_macro`
let frange = sema.original_range(&node);
res.extend(type_of(db, frange).map(rust_code_markup));
if res.is_empty() {
return None;
}
let range = node.text_range();
let ty = match_ast! {
match node {
ast::MacroCall(_it) => {
// 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;
},
ast::Expr(it) => {
sema.type_of_expr(&it)
},
ast::Pat(it) => {
sema.type_of_pat(&it)
},
_ => None,
}
}?;
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))
}
@ -206,33 +207,12 @@ fn priority(n: &SyntaxToken) -> usize {
}
}
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)]
mod tests {
use ra_db::FileLoader;
use ra_syntax::TextRange;
use crate::mock_analysis::{
analysis_and_position, single_file_with_position, single_file_with_range,
};
use crate::mock_analysis::{analysis_and_position, single_file_with_position};
fn trim_markup(s: &str) -> &str {
s.trim_start_matches("```rust\n").trim_end_matches("\n```")
@ -524,37 +504,6 @@ fn hover_for_param_edge() {
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]
fn test_hover_infer_associated_method_result() {
let (analysis, position) = single_file_with_position(
@ -791,9 +740,7 @@ fn foo(bar:u32) {
#[test]
fn test_hover_through_literal_string_in_macro() {
// FIXME: Currently `hover::type_of` do not work inside
// macro expansion
check_hover_no_result(
let hover_on = check_hover_result(
r#"
//- /lib.rs
macro_rules! arr {
@ -804,7 +751,10 @@ fn foo() {
let _ = arr!("Tr<|>acks", &mastered_for_itunes);
}
"#,
&["&str"],
);
assert_eq!(hover_on, "\"Tracks\"");
}
#[test]

View File

@ -455,11 +455,6 @@ pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
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
/// name.
pub fn rename(