]> git.lizzy.rs Git - rust.git/commitdiff
feat: Extend the server with the hover_range capability
authorAlexander Gonzalez <alexfertel97@gmail.com>
Fri, 23 Jul 2021 02:08:28 +0000 (22:08 -0400)
committerAlexander Gonzalez <alexfertel97@gmail.com>
Tue, 27 Jul 2021 22:29:22 +0000 (18:29 -0400)
crates/ide/src/hover.rs
crates/ide/src/lib.rs
crates/rust-analyzer/src/handlers.rs
crates/rust-analyzer/src/lsp_ext.rs

index 35601f2efc7a0d9df7d43c1b99d1dcbd7879c1bf..afa67f72bed6b8d85610b0ea87aea2d758615b59 100644 (file)
@@ -241,6 +241,13 @@ fn try_hover_for_lint(attr: &ast::Attr, token: &SyntaxToken) -> Option<RangeInfo
     ))
 }
 
+pub(crate) fn hover_range(
+    db: &RootDatabase,
+    range: FileRange,
+    config: &HoverConfig,
+) -> Option<RangeInfo<HoverResult>> {
+}
+
 fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
     fn to_action(nav_target: NavigationTarget) -> HoverAction {
         HoverAction::Implementation(FilePosition {
index 83173e1c6b8afc8f3ba768663d731b3ebfcd5714..b6b741a22ab6e1b5124899d8cdc0f0f4818497ee 100644 (file)
@@ -423,6 +423,15 @@ pub fn hover(
         self.with_db(|db| hover::hover(db, position, config))
     }
 
+    /// Returns a short text displaying the type for the expression.
+    pub fn hover_range(
+        &self,
+        config: &HoverConfig,
+        range: FileRange,
+    ) -> Cancellable<Option<RangeInfo<HoverResult>>> {
+        self.with_db(|db| hover::hover_range(db, range, config))
+    }
+
     /// Return URL(s) for the documentation of the symbol under the cursor.
     pub fn external_docs(
         &self,
index 52b557f156886c2b3cfd7883ca6b67269fa35d11..ede3103abfa00aa83559613484e942bd6b1ae446 100644 (file)
@@ -867,14 +867,29 @@ pub(crate) fn handle_signature_help(
 
 pub(crate) fn handle_hover(
     snap: GlobalStateSnapshot,
-    params: lsp_types::HoverParams,
+    params: lsp_ext::HoverParams,
 ) -> Result<Option<lsp_ext::Hover>> {
     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 file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
+    let range = from_proto::file_range(&snap, params.text_document, params.range)?;
+
+    let info = if range.end - range.start == 1 {
+        // It's a hover over a position
+        match snap
+            .analysis
+            .hover(&snap.config.hover(), FilePosition { file_id, offset: range.start })?
+        {
+            None => return Ok(None),
+            Some(info) => info,
+        }
+    } else {
+        // It's a hover over a range
+        match snap.analysis.hover_range(&snap.config.hover(), range)? {
+            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 {
index f11ad396e702db798ef31d7c2fe794c541af63d2..f6abd95e1e6c10fbd22f2994ab29099eaa5d60ba 100644 (file)
@@ -376,11 +376,18 @@ pub struct SnippetTextEdit {
 pub enum HoverRequest {}
 
 impl Request for HoverRequest {
-    type Params = lsp_types::HoverParams;
+    type Params = HoverParams;
     type Result = Option<Hover>;
     const METHOD: &'static str = "textDocument/hover";
 }
 
+#[derive(Deserialize, Serialize, Debug)]
+#[serde(rename_all = "camelCase")]
+pub struct HoverParams {
+    pub text_document: TextDocumentIdentifier,
+    pub range: Range,
+}
+
 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
 pub struct Hover {
     #[serde(flatten)]