From 5f30dd6f72e239474ee425e466711c40b78972f2 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 28 Feb 2020 11:37:22 +0800 Subject: [PATCH] Remove hover::type_of --- crates/ra_ide/src/hover.rs | 98 ++++++++++---------------------------- crates/ra_ide/src/lib.rs | 5 -- 2 files changed, 24 insertions(+), 79 deletions(-) diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index dfa0f1d973c..06753edb244 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -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 { + // 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 { - 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] diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index d509de14e55..f61028f78c0 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -455,11 +455,6 @@ pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { 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> { - 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(