From b6c662c573014710d4e8d9fd9253793141d8bbe0 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 19 Jul 2019 08:53:12 +0300 Subject: If possible, show type lenses for the let bindings --- crates/ra_ide_api/src/display/structure.rs | 22 ++++++++++++++ crates/ra_lsp_server/src/main_loop/handlers.rs | 42 ++++++++++++++++---------- 2 files changed, 48 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index 2ba10b2ef..223941688 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -1,5 +1,6 @@ use crate::TextRange; +use ra_syntax::ast::PatKind; use ra_syntax::{ algo::visit::{visitor, Visitor}, ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, @@ -155,6 +156,27 @@ fn structure_node(node: &SyntaxNode) -> Option { } decl(mc) }) + .visit(|let_statement: &ast::LetStmt| { + let let_syntax = let_statement.syntax(); + + let mut label = String::new(); + collapse_ws(let_syntax, &mut label); + + let pat = match let_statement.pat()?.kind() { + PatKind::BindPat(bind_pat) => bind_pat, + _ => return None, + }; + + Some(StructureNode { + parent: None, + label, + navigation_range: pat.syntax().range(), + node_range: let_syntax.range(), + kind: let_syntax.kind(), + detail: None, + deprecated: false, + }) + }) .accept(&node)? } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 68865b755..ea947417f 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -726,29 +726,39 @@ pub fn handle_code_lens( } } - // Handle impls - lenses.extend( - world - .analysis() - .file_structure(file_id) - .into_iter() - .filter(|it| match it.kind { - SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => true, - _ => false, - }) - .map(|it| { + lenses.extend(world.analysis().file_structure(file_id).into_iter().filter_map(|it| { + match it.kind { + // Handle impls + SyntaxKind::TRAIT_DEF | SyntaxKind::STRUCT_DEF | SyntaxKind::ENUM_DEF => { let range = it.node_range.conv_with(&line_index); let pos = range.start; let lens_params = req::TextDocumentPositionParams::new(params.text_document.clone(), pos); - CodeLens { + Some(CodeLens { range, command: None, data: Some(to_value(CodeLensResolveData::Impls(lens_params)).unwrap()), - } - }), - ); - + }) + } + // handle let statements + SyntaxKind::LET_STMT => world + .analysis() + .type_of(FileRange { range: it.navigation_range, file_id }) + .ok() + .and_then(std::convert::identity) + .filter(|resolved_type| "{unknown}" != resolved_type) + .map(|resolved_type| CodeLens { + range: it.node_range.conv_with(&line_index), + command: Some(Command { + title: resolved_type, + command: String::new(), + arguments: None, + }), + data: None, + }), + _ => None, + } + })); Ok(Some(lenses)) } -- cgit v1.2.3