From 1a0a5da1a488033e4b9fdb4c4cd32854ff2859d6 Mon Sep 17 00:00:00 2001 From: Alexander Gonzalez Date: Mon, 26 Jul 2021 12:14:14 -0400 Subject: [PATCH] refactor: Make handle_hover handle ranges too --- crates/ide/src/lib.rs | 2 +- crates/rust-analyzer/src/handlers.rs | 49 +++++++++++---------------- crates/rust-analyzer/src/lsp_ext.rs | 24 +++++++------ crates/rust-analyzer/src/main_loop.rs | 1 - editors/code/src/client.ts | 44 +++--------------------- editors/code/src/lsp_ext.ts | 6 ++-- 6 files changed, 42 insertions(+), 84 deletions(-) diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index b6b741a22ab..91544067125 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -423,7 +423,7 @@ impl Analysis { self.with_db(|db| hover::hover(db, position, config)) } - /// Returns a short text displaying the type for the expression. + /// Returns a short text displaying the type of the expression. pub fn hover_range( &self, config: &HoverConfig, diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 988c163db5f..90202fdcecc 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -36,7 +36,10 @@ use crate::{ from_proto, global_state::{GlobalState, GlobalStateSnapshot}, line_index::LineEndings, - lsp_ext::{self, InlayHint, InlayHintsParams, ViewCrateGraphParams, WorkspaceSymbolParams}, + lsp_ext::{ + self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams, + WorkspaceSymbolParams, + }, lsp_utils::all_edits_are_disjoint, to_proto, LspError, Result, }; @@ -867,42 +870,30 @@ pub(crate) fn handle_signature_help( pub(crate) fn handle_hover( snap: GlobalStateSnapshot, - params: lsp_types::HoverParams, + params: lsp_ext::HoverParams, ) -> Result> { let _p = profile::span("handle_hover"); - let position = from_proto::file_position(&snap, params.text_document_position_params)?; - let info = match snap.analysis.hover(&snap.config.hover(), position)? { - None => return Ok(None), - Some(info) => info, - }; - - let line_index = snap.file_line_index(position.file_id)?; - let range = to_proto::range(&line_index, info.range); - let hover = lsp_ext::Hover { - hover: lsp_types::Hover { - contents: HoverContents::Markup(to_proto::markup_content(info.info.markup)), - range: Some(range), - }, - actions: prepare_hover_actions(&snap, &info.info.actions), - }; - - Ok(Some(hover)) -} - -pub(crate) fn handle_hover_range( - snap: GlobalStateSnapshot, - params: lsp_ext::HoverRangeParams, -) -> Result> { - let _p = profile::span("handle_hover_range"); let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; - let range = from_proto::file_range(&snap, params.text_document, params.range)?; + let hover_result = match params.position { + PositionOrRange::Position(position) => { + let position = from_proto::file_position( + &snap, + lsp_types::TextDocumentPositionParams::new(params.text_document, position), + )?; + snap.analysis.hover(&snap.config.hover(), position)? + } + PositionOrRange::Range(range) => { + let range = from_proto::file_range(&snap, params.text_document, range)?; + snap.analysis.hover_range(&snap.config.hover(), range)? + } + }; - let info = match snap.analysis.hover_range(&snap.config.hover(), range)? { + let info = match hover_result { None => return Ok(None), Some(info) => info, }; - let line_index = snap.file_line_index(range.file_id)?; + let line_index = snap.file_line_index(file_id)?; let range = to_proto::range(&line_index, info.range); let hover = lsp_ext::Hover { hover: lsp_types::Hover { diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 7aed93f992a..d697ec44d15 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -376,24 +376,26 @@ pub struct SnippetTextEdit { pub enum HoverRequest {} impl Request for HoverRequest { - type Params = lsp_types::HoverParams; + type Params = HoverParams; type Result = Option; const METHOD: &'static str = "textDocument/hover"; } -pub enum HoverRangeRequest {} +#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct HoverParams { + pub text_document: TextDocumentIdentifier, + pub position: PositionOrRange, -impl Request for HoverRangeRequest { - type Params = HoverRangeParams; - type Result = Option; - const METHOD: &'static str = "rust-analyzer/hoverRange"; + #[serde(flatten)] + pub work_done_progress_params: WorkDoneProgressParams, } -#[derive(Deserialize, Serialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct HoverRangeParams { - pub text_document: TextDocumentIdentifier, - pub range: Range, +#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)] +#[serde(untagged)] +pub enum PositionOrRange { + Position(lsp_types::Position), + Range(lsp_types::Range), } #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index af3cb0c8792..35fce79f5eb 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -542,7 +542,6 @@ impl GlobalState { .on::(handlers::handle_code_action) .on::(handlers::handle_code_action_resolve) .on::(handlers::handle_hover) - .on::(handlers::handle_hover_range) .on::(handlers::handle_open_docs) .on::(handlers::handle_open_cargo_toml) .on::(handlers::handle_move_item) diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index f0c10698a0c..272cfca66d8 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -57,45 +57,11 @@ export function createClient(serverPath: string, workspace: Workspace, extraEnv: middleware: { async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, _next: lc.ProvideHoverSignature) { const editor = vscode.window.activeTextEditor; - const selection = editor?.selection; - return selection?.contains(position) - ? client - .sendRequest( - ra.hoverRange, - { - textDocument: - client.code2ProtocolConverter.asTextDocumentIdentifier( - document - ), - range: client.code2ProtocolConverter.asRange( - editor?.selection - ), - }, - token - ) - .then( - (result) => - client.protocol2CodeConverter.asHover(result), - (error) => { - client.handleFailedRequest( - lc.HoverRequest.type, - undefined, - error, - null - ); - return Promise.resolve(null); - } - ) - : client - .sendRequest( - lc.HoverRequest.type, - client.code2ProtocolConverter.asTextDocumentPositionParams( - document, - position - ), - token - ) - .then( + const positionOrRange = editor?.selection?.contains(position) ? client.code2ProtocolConverter.asRange(editor.selection) : client.code2ProtocolConverter.asPosition(position); + return client.sendRequest(ra.hover, { + textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), + position: positionOrRange + }, token).then( (result) => { const hover = client.protocol2CodeConverter.asHover(result); diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 2570a2d1f5d..deb0db56eee 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -19,11 +19,11 @@ export const serverStatus = new lc.NotificationType("experim export const reloadWorkspace = new lc.RequestType0("rust-analyzer/reloadWorkspace"); -export const hoverRange = new lc.RequestType("rust-analyzer/hoverRange"); +export const hover = new lc.RequestType("textDocument/hover"); -export interface HoverRangeParams { +export interface HoverParams extends lc.WorkDoneProgressParams{ textDocument: lc.TextDocumentIdentifier; - range: lc.Range; + position: lc.Range | lc.Position; } export interface SyntaxTreeParams {