Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bindings for hover actions #395

Closed
expipiplus1 opened this issue Sep 20, 2024 · 2 comments · Fixed by #398 or #397
Closed

Allow bindings for hover actions #395

expipiplus1 opened this issue Sep 20, 2024 · 2 comments · Fixed by #398 or #397
Labels
enhancement New feature or request

Comments

@expipiplus1
Copy link

Feature description

Is it possible to add support for automatically executing the hover actions in a hover meny (for example to have a keybinding to open source or documentation in browser)?

(It's definitely possible with send_keys or whatever KK/Open documentation in browser<CR><CR>, but hardly elegant)

@expipiplus1 expipiplus1 added the enhancement New feature or request label Sep 20, 2024
@mrcjkb
Copy link
Owner

mrcjkb commented Sep 20, 2024

Hey 👋

Sure, I could probably add some <Plug> mappings.
I'll look into it when I have some time.

@expipiplus1
Copy link
Author

expipiplus1 commented Sep 20, 2024

here's what I'm using, I updated it to support Source and Documentation, and also go to definition if it's available instead

It makes a great gd mapping

local function open_lsp_browser_link(link_type)
  local function make_hover_request(callback, retry)
    -- First, try to go to definition
    local definition_found = false
    vim.lsp.buf.definition {
      on_list = function()
        definition_found = true
        vim.lsp.buf.definition()
      end,
    }

    -- If definition is found, we're done
    if definition_found then return end

    -- If no definition, proceed with hover request
    local params = vim.lsp.util.make_position_params()
    vim.lsp.buf_request(0, "textDocument/hover", params, function(err, result, _, _)
      if err or not result or not result.contents then
        if not retry then print "No hover information available" end
        return
      end

      local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
      local uri

      for _, line in ipairs(markdown_lines) do
        if vim.startswith(line, "[" .. link_type .. "]") then
          uri = string.match(line, "%[" .. link_type .. "%]%((.+)%)")
          if uri then
            callback(uri)
            return
          end
        end
      end

      if not retry then
        -- If we didn't find the link, try once more
        vim.defer_fn(function() make_hover_request(callback, true) end, 100)
      else
        print("Could not find " .. link_type .. " link")
      end
    end)
  end

  make_hover_request(function(uri)
    if uri then
      local OS = require "haskell-tools.os"
      OS.open_browser(uri)
      print("Opening " .. link_type .. " in browser")
    end
  end)
end

@mrcjkb mrcjkb closed this as completed in f32ba45 Sep 20, 2024
mrcjkb added a commit that referenced this issue Sep 20, 2024
🤖 I have created a release *beep* *boop*
---


##
[4.1.0](4.0.1...v4.1.0)
(2024-09-20)


### Features

* **lsp:** `<Plug>` mappings for hover actions
([#398](#398))
([f32ba45](f32ba45)),
closes [#395](#395)


### Bug Fixes

* **lsp:** disable hover actions if luajit is not available
([24865bf](24865bf))
* nil error when trying to invoke some subcommands
([e7a5bdb](e7a5bdb))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants