Skip to content

Commit 1a0a5da

Browse files
committed
refactor: Make handle_hover handle ranges too
1 parent 2b5798e commit 1a0a5da

File tree

6 files changed

+42
-84
lines changed

6 files changed

+42
-84
lines changed

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ 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.
426+
/// Returns a short text displaying the type of the expression.
427427
pub fn hover_range(
428428
&self,
429429
config: &HoverConfig,

crates/rust-analyzer/src/handlers.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ use crate::{
3636
from_proto,
3737
global_state::{GlobalState, GlobalStateSnapshot},
3838
line_index::LineEndings,
39-
lsp_ext::{self, InlayHint, InlayHintsParams, ViewCrateGraphParams, WorkspaceSymbolParams},
39+
lsp_ext::{
40+
self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams,
41+
WorkspaceSymbolParams,
42+
},
4043
lsp_utils::all_edits_are_disjoint,
4144
to_proto, LspError, Result,
4245
};
@@ -867,42 +870,30 @@ pub(crate) fn handle_signature_help(
867870

868871
pub(crate) fn handle_hover(
869872
snap: GlobalStateSnapshot,
870-
params: lsp_types::HoverParams,
873+
params: lsp_ext::HoverParams,
871874
) -> Result<Option<lsp_ext::Hover>> {
872875
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,
877-
};
878-
879-
let line_index = snap.file_line_index(position.file_id)?;
880-
let range = to_proto::range(&line_index, info.range);
881-
let hover = lsp_ext::Hover {
882-
hover: lsp_types::Hover {
883-
contents: HoverContents::Markup(to_proto::markup_content(info.info.markup)),
884-
range: Some(range),
885-
},
886-
actions: prepare_hover_actions(&snap, &info.info.actions),
887-
};
888-
889-
Ok(Some(hover))
890-
}
891-
892-
pub(crate) fn handle_hover_range(
893-
snap: GlobalStateSnapshot,
894-
params: lsp_ext::HoverRangeParams,
895-
) -> Result<Option<lsp_ext::Hover>> {
896-
let _p = profile::span("handle_hover_range");
897876
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
898-
let range = from_proto::file_range(&snap, params.text_document, params.range)?;
877+
let hover_result = match params.position {
878+
PositionOrRange::Position(position) => {
879+
let position = from_proto::file_position(
880+
&snap,
881+
lsp_types::TextDocumentPositionParams::new(params.text_document, position),
882+
)?;
883+
snap.analysis.hover(&snap.config.hover(), position)?
884+
}
885+
PositionOrRange::Range(range) => {
886+
let range = from_proto::file_range(&snap, params.text_document, range)?;
887+
snap.analysis.hover_range(&snap.config.hover(), range)?
888+
}
889+
};
899890

900-
let info = match snap.analysis.hover_range(&snap.config.hover(), range)? {
891+
let info = match hover_result {
901892
None => return Ok(None),
902893
Some(info) => info,
903894
};
904895

905-
let line_index = snap.file_line_index(range.file_id)?;
896+
let line_index = snap.file_line_index(file_id)?;
906897
let range = to_proto::range(&line_index, info.range);
907898
let hover = lsp_ext::Hover {
908899
hover: lsp_types::Hover {

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,26 @@ 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-
pub enum HoverRangeRequest {}
384+
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
385+
#[serde(rename_all = "camelCase")]
386+
pub struct HoverParams {
387+
pub text_document: TextDocumentIdentifier,
388+
pub position: PositionOrRange,
385389

386-
impl Request for HoverRangeRequest {
387-
type Params = HoverRangeParams;
388-
type Result = Option<Hover>;
389-
const METHOD: &'static str = "rust-analyzer/hoverRange";
390+
#[serde(flatten)]
391+
pub work_done_progress_params: WorkDoneProgressParams,
390392
}
391393

392-
#[derive(Deserialize, Serialize, Debug)]
393-
#[serde(rename_all = "camelCase")]
394-
pub struct HoverRangeParams {
395-
pub text_document: TextDocumentIdentifier,
396-
pub range: Range,
394+
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
395+
#[serde(untagged)]
396+
pub enum PositionOrRange {
397+
Position(lsp_types::Position),
398+
Range(lsp_types::Range),
397399
}
398400

399401
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]

crates/rust-analyzer/src/main_loop.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ impl GlobalState {
542542
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)
543543
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
544544
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)
545-
.on::<lsp_ext::HoverRangeRequest>(handlers::handle_hover_range)
546545
.on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs)
547546
.on::<lsp_ext::OpenCargoToml>(handlers::handle_open_cargo_toml)
548547
.on::<lsp_ext::MoveItem>(handlers::handle_move_item)

editors/code/src/client.ts

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,11 @@ export function createClient(serverPath: string, workspace: Workspace, extraEnv:
5757
middleware: {
5858
async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, _next: lc.ProvideHoverSignature) {
5959
const editor = vscode.window.activeTextEditor;
60-
const selection = editor?.selection;
61-
return selection?.contains(position)
62-
? client
63-
.sendRequest(
64-
ra.hoverRange,
65-
{
66-
textDocument:
67-
client.code2ProtocolConverter.asTextDocumentIdentifier(
68-
document
69-
),
70-
range: client.code2ProtocolConverter.asRange(
71-
editor?.selection
72-
),
73-
},
74-
token
75-
)
76-
.then(
77-
(result) =>
78-
client.protocol2CodeConverter.asHover(result),
79-
(error) => {
80-
client.handleFailedRequest(
81-
lc.HoverRequest.type,
82-
undefined,
83-
error,
84-
null
85-
);
86-
return Promise.resolve(null);
87-
}
88-
)
89-
: client
90-
.sendRequest(
91-
lc.HoverRequest.type,
92-
client.code2ProtocolConverter.asTextDocumentPositionParams(
93-
document,
94-
position
95-
),
96-
token
97-
)
98-
.then(
60+
const positionOrRange = editor?.selection?.contains(position) ? client.code2ProtocolConverter.asRange(editor.selection) : client.code2ProtocolConverter.asPosition(position);
61+
return client.sendRequest(ra.hover, {
62+
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
63+
position: positionOrRange
64+
}, token).then(
9965
(result) => {
10066
const hover =
10167
client.protocol2CodeConverter.asHover(result);

editors/code/src/lsp_ext.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export const serverStatus = new lc.NotificationType<ServerStatusParams>("experim
1919

2020
export const reloadWorkspace = new lc.RequestType0<null, void>("rust-analyzer/reloadWorkspace");
2121

22-
export const hoverRange = new lc.RequestType<HoverRangeParams, lc.Hover | null, void>("rust-analyzer/hoverRange");
22+
export const hover = new lc.RequestType<HoverParams, lc.Hover | null, void>("textDocument/hover");
2323

24-
export interface HoverRangeParams {
24+
export interface HoverParams extends lc.WorkDoneProgressParams{
2525
textDocument: lc.TextDocumentIdentifier;
26-
range: lc.Range;
26+
position: lc.Range | lc.Position;
2727
}
2828

2929
export interface SyntaxTreeParams {

0 commit comments

Comments
 (0)