If possible, show type lenses for the let bindings

This commit is contained in:
Kirill Bulatov 2019-07-19 08:53:12 +03:00
parent 7bde8012cb
commit b6c662c573
2 changed files with 48 additions and 16 deletions

View File

@ -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<StructureNode> {
}
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)?
}

View File

@ -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))
}