Skip to content

Commit 8ca3bb8

Browse files
committed
feat: Add the hover_range capability
1 parent 20c64cc commit 8ca3bb8

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

crates/ide/src/hover.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use either::Either;
22
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics};
33
use ide_db::{
4-
base_db::SourceDatabase,
4+
base_db::{FileRange, SourceDatabase},
55
defs::{Definition, NameClass, NameRefClass},
66
helpers::{
77
generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
@@ -12,8 +12,12 @@ use ide_db::{
1212
use itertools::Itertools;
1313
use stdx::format_to;
1414
use syntax::{
15-
algo, ast, display::fn_as_proc_macro_label, match_ast, AstNode, AstToken, Direction,
16-
SyntaxKind::*, SyntaxToken, T,
15+
algo::{self, find_node_at_range},
16+
ast,
17+
display::fn_as_proc_macro_label,
18+
match_ast, AstNode, AstToken, Direction,
19+
SyntaxKind::*,
20+
SyntaxToken, T,
1721
};
1822

1923
use crate::{
@@ -246,6 +250,24 @@ pub(crate) fn hover_range(
246250
range: FileRange,
247251
config: &HoverConfig,
248252
) -> Option<RangeInfo<HoverResult>> {
253+
let sema = hir::Semantics::new(db);
254+
let file = sema.parse(range.file_id).syntax().clone();
255+
let expr = find_node_at_range::<ast::Expr>(&file, range.range)?;
256+
let ty = sema.type_of_expr(&expr)?;
257+
258+
if ty.is_unknown() {
259+
return None;
260+
}
261+
262+
let mut res = HoverResult::default();
263+
264+
res.markup = if config.markdown() {
265+
Markup::fenced_block(&ty.display(db))
266+
} else {
267+
ty.display(db).to_string().into()
268+
};
269+
270+
Some(RangeInfo::new(range.range, res))
249271
}
250272

251273
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {

crates/rust-analyzer/src/handlers.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,24 +873,25 @@ pub(crate) fn handle_hover(
873873
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
874874
let range = from_proto::file_range(&snap, params.text_document, params.range)?;
875875

876-
let info = if range.end - range.start == 1 {
876+
let info = if range.range.is_empty() {
877877
// It's a hover over a position
878878
match snap
879879
.analysis
880-
.hover(&snap.config.hover(), FilePosition { file_id, offset: range.start })?
880+
.hover(&snap.config.hover(), FilePosition { file_id, offset: range.range.start() })?
881881
{
882882
None => return Ok(None),
883883
Some(info) => info,
884884
}
885885
} else {
886886
// It's a hover over a range
887+
log::info!("Triggered range hover");
887888
match snap.analysis.hover_range(&snap.config.hover(), range)? {
888889
None => return Ok(None),
889890
Some(info) => info,
890891
}
891892
};
892893

893-
let line_index = snap.file_line_index(position.file_id)?;
894+
let line_index = snap.file_line_index(range.file_id)?;
894895
let range = to_proto::range(&line_index, info.range);
895896
let hover = lsp_ext::Hover {
896897
hover: lsp_types::Hover {

0 commit comments

Comments
 (0)