Skip to content

Commit

Permalink
feat: add open_url config option (#403)
Browse files Browse the repository at this point in the history
Implement functionality discussed in #402.

---------

Co-authored-by: Github Actions <actions@github>
  • Loading branch information
AntonVucinic and Github Actions authored Oct 4, 2024
1 parent bf7eb0b commit 8cfe955
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/haskell-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ haskell-tools.tools.Opts *haskell-tools.tools.Opts*
fast-tags module options.
{log?} (haskell-tools.log.Opts)
haskell-tools logger options.
{open_url?} (fun(url:string))
custom action for opening a url


haskell-tools.codeLens.Opts *haskell-tools.codeLens.Opts*
Expand Down
7 changes: 7 additions & 0 deletions lua/haskell-tools/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ function Check.validate(cfg)
if not ok then
return false, err
end
local open_url = tools.open_url
ok, err = validate('tools.open_url', {
open_url = { open_url, 'function' },
})
if not ok then
return false, err
end
local repl = tools.repl
local valid_handlers = { 'builtin', 'toggleterm' }
local valid_backends = { 'cabal', 'stack' }
Expand Down
3 changes: 3 additions & 0 deletions lua/haskell-tools/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ vim.g.haskell_tools = vim.g.haskell_tools
---
---haskell-tools logger options.
---@field log? haskell-tools.log.Opts
---
---custom action for opening a url
---@field open_url? fun(url: string)

---@class haskell-tools.codeLens.Opts
---
Expand Down
4 changes: 4 additions & 0 deletions lua/haskell-tools/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ local HTDefaultConfig = {
---@see vim.log.levels
level = vim.log.levels.WARN,
},
---@type fun(url: string) custom action for opening url
open_url = function(url)
require('haskell-tools.os').open_browser(url)
end,
},
---@class haskell-tools.lsp.ClientConfig haskell-language-server client options.
hls = {
Expand Down
3 changes: 1 addition & 2 deletions lua/haskell-tools/hoogle/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
---@brief ]]

local deps = require('haskell-tools.deps')
local OS = require('haskell-tools.os')
local actions = deps.require_telescope('telescope.actions')
local actions_state = deps.require_telescope('telescope.actions.state')
local entry_display = deps.require_telescope('telescope.pickers.entry_display')
Expand All @@ -34,7 +33,7 @@ function Helpers.hoogle_attach_mappings(buf, map)
map('i', '<C-b>', function()
-- Open in browser
local entry = actions_state.get_selected_entry()
OS.open_browser(entry.url)
require('haskell-tools.config.internal').tools.open_url(entry.url)
end)
map('i', '<C-r>', function()
-- Replace word under cursor
Expand Down
4 changes: 2 additions & 2 deletions lua/haskell-tools/hoogle/web.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

local log = require('haskell-tools.log.internal')
local deps = require('haskell-tools.deps')
local OS = require('haskell-tools.os')

---@class haskell-tools.hoogle.handler.Web
local WebHoogleHandler = {}
Expand Down Expand Up @@ -122,7 +121,8 @@ function WebHoogleHandler.browser_search(search_term, opts)
opts = vim.tbl_deep_extend('keep', opts or {}, {
hoogle = { json = false },
})
OS.open_browser(mk_hoogle_request(search_term, opts))
local HTConfig = require('haskell-tools.config.internal')
HTConfig.tools.open_url(mk_hoogle_request(search_term, opts))
end

return WebHoogleHandler
7 changes: 3 additions & 4 deletions lua/haskell-tools/lsp/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
local log = require('haskell-tools.log.internal')
local lsp_util = vim.lsp.util
local HtParser = require('haskell-tools.parser')
local OS = require('haskell-tools.os')
local HtProjectHelpers = require('haskell-tools.project.helpers')

local hover = {}
Expand Down Expand Up @@ -142,6 +141,7 @@ end
---@return number|nil winnr
function hover.on_hover(_, result, ctx, config)
local ht = require('haskell-tools')
local HTConfig = require('haskell-tools.config.internal')
config = config or {}
config.focus_id = ctx.method
if vim.api.nvim_get_current_buf() ~= ctx.bufnr then
Expand Down Expand Up @@ -193,7 +193,7 @@ function hover.on_hover(_, result, ctx, config)
local uri = string.match(value, '%[Documentation%]%((.+)%)')
local action = function()
log.debug { 'Hover: Open documentation in browser', uri }
OS.open_browser(uri)
HTConfig.tools.open_url(uri)
close_hover()
end
table.insert(_state.commands, action)
Expand All @@ -205,7 +205,7 @@ function hover.on_hover(_, result, ctx, config)
local uri = string.match(value, '%[Source%]%((.+)%)')
local action = function()
log.debug { 'Hover: View source in browser', uri }
OS.open_browser(uri)
HTConfig.tools.open_url(uri)
close_hover()
end
table.insert(_state.commands, action)
Expand Down Expand Up @@ -311,7 +311,6 @@ function hover.on_hover(_, result, ctx, config)
table.insert(markdown_lines, #actions + 1, '')
table.insert(markdown_lines, #actions + 1, '')
end
local HTConfig = require('haskell-tools.config.internal')
local opts = HTConfig.tools.hover
config = vim.tbl_extend('keep', {
border = opts.border,
Expand Down

0 comments on commit 8cfe955

Please sign in to comment.