Skip to content

Commit 20c64cc

Browse files
committed
feat: Extend the server with the hover_range capability
1 parent 6a2a0b7 commit 20c64cc

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

crates/ide/src/hover.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ fn try_hover_for_lint(attr: &ast::Attr, token: &SyntaxToken) -> Option<RangeInfo
241241
))
242242
}
243243

244+
pub(crate) fn hover_range(
245+
db: &RootDatabase,
246+
range: FileRange,
247+
config: &HoverConfig,
248+
) -> Option<RangeInfo<HoverResult>> {
249+
}
250+
244251
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
245252
fn to_action(nav_target: NavigationTarget) -> HoverAction {
246253
HoverAction::Implementation(FilePosition {

crates/ide/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ impl Analysis {
423423
self.with_db(|db| hover::hover(db, position, config))
424424
}
425425

426+
/// Returns a short text displaying the type for the expression.
427+
pub fn hover_range(
428+
&self,
429+
config: &HoverConfig,
430+
range: FileRange,
431+
) -> Cancellable<Option<RangeInfo<HoverResult>>> {
432+
self.with_db(|db| hover::hover_range(db, range, config))
433+
}
434+
426435
/// Return URL(s) for the documentation of the symbol under the cursor.
427436
pub fn external_docs(
428437
&self,

crates/rust-analyzer/src/handlers.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,29 @@ pub(crate) fn handle_signature_help(
867867

868868
pub(crate) fn handle_hover(
869869
snap: GlobalStateSnapshot,
870-
params: lsp_types::HoverParams,
870+
params: lsp_ext::HoverParams,
871871
) -> Result<Option<lsp_ext::Hover>> {
872872
let _p = profile::span("handle_hover");
873-
let position = from_proto::file_position(&snap, params.text_document_position_params)?;
874-
let info = match snap.analysis.hover(&snap.config.hover(), position)? {
875-
None => return Ok(None),
876-
Some(info) => info,
873+
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
874+
let range = from_proto::file_range(&snap, params.text_document, params.range)?;
875+
876+
let info = if range.end - range.start == 1 {
877+
// It's a hover over a position
878+
match snap
879+
.analysis
880+
.hover(&snap.config.hover(), FilePosition { file_id, offset: range.start })?
881+
{
882+
None => return Ok(None),
883+
Some(info) => info,
884+
}
885+
} else {
886+
// It's a hover over a range
887+
match snap.analysis.hover_range(&snap.config.hover(), range)? {
888+
None => return Ok(None),
889+
Some(info) => info,
890+
}
877891
};
892+
878893
let line_index = snap.file_line_index(position.file_id)?;
879894
let range = to_proto::range(&line_index, info.range);
880895
let hover = lsp_ext::Hover {

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,18 @@ pub struct SnippetTextEdit {
376376
pub enum HoverRequest {}
377377

378378
impl Request for HoverRequest {
379-
type Params = lsp_types::HoverParams;
379+
type Params = HoverParams;
380380
type Result = Option<Hover>;
381381
const METHOD: &'static str = "textDocument/hover";
382382
}
383383

384+
#[derive(Deserialize, Serialize, Debug)]
385+
#[serde(rename_all = "camelCase")]
386+
pub struct HoverParams {
387+
pub text_document: TextDocumentIdentifier,
388+
pub range: Range,
389+
}
390+
384391
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
385392
pub struct Hover {
386393
#[serde(flatten)]

0 commit comments

Comments
 (0)