-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from MrcJkb/7-hoogle-search
hoogle search
- Loading branch information
Showing
11 changed files
with
451 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local deps = require('haskell-tools.deps') | ||
local util = require('haskell-tools.util') | ||
|
||
local M = {} | ||
|
||
function M.has_hoogle() | ||
return vim.fn.executable('hoogle') == 1 | ||
end | ||
|
||
local function mk_hoogle_args(search_term, opts) | ||
local count = opts.count or 50 | ||
local cmd = vim.tbl_flatten { '--json', '--count=' .. count, search_term, } | ||
return cmd | ||
end | ||
|
||
local function setup_telescope_search() | ||
|
||
local pickers = deps.require_telescope('telescope.pickers') | ||
local finders = deps.require_telescope('telescope.finders') | ||
local previewers = deps.require_telescope('telescope.previewers') | ||
local config = deps.require_telescope('telescope.config').values | ||
local telescope_util = require('haskell-tools.telescope-util') | ||
local Job = deps.require_plenary('plenary.job') | ||
|
||
function M.telescope_search(search_term, opts) | ||
opts = util.tbl_merge(opts or {}, { | ||
layout_strategy = 'horizontal', | ||
layout_config = { preview_width = 80 }, | ||
}) | ||
opts.entry_maker = opts.entry_maker or telescope_util.mk_hoogle_entry | ||
Job:new({ | ||
command = 'hoogle', | ||
args = mk_hoogle_args(search_term, opts), | ||
on_exit = function(j, return_val) | ||
vim.schedule(function() | ||
if (return_val ~= 0) then | ||
error('haskell-toos: hoogle search failed. Return value: ' .. return_val) | ||
end | ||
local output = j:result()[1] | ||
if #output < 1 then | ||
return | ||
end | ||
pickers.new(opts, { | ||
prompt_title = 'Hoogle: ' .. search_term, | ||
sorter = config.generic_sorter(opts), | ||
finder = finders.new_table { | ||
results = vim.json.decode(output), | ||
entry_maker = telescope_util.mk_hoogle_entry | ||
}, | ||
previewer = previewers.display_content.new(opts), | ||
attach_mappings = telescope_util.attach_mappings, | ||
}):find() | ||
end) | ||
end | ||
}):start() | ||
end | ||
end | ||
|
||
function M.setup() | ||
if M.has_hoogle() then | ||
setup_telescope_search() | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
local deps = require('haskell-tools.deps') | ||
local util = require('haskell-tools.util') | ||
|
||
local M = {} | ||
|
||
local char_to_hex = function(c) | ||
return string.format("%%%02X", string.byte(c)) | ||
end | ||
|
||
local function urlencode(url) | ||
if url == nil then | ||
return | ||
end | ||
url = url:gsub("\n", "\r\n") | ||
url = url:gsub("([^%w ])", char_to_hex) | ||
url = url:gsub(" ", "+") | ||
return url | ||
end | ||
|
||
local function mk_hoogle_request(search_term, opts) | ||
local hoogle_opts = opts.hoogle or {} | ||
local scope_param = hoogle_opts.scope and '&scope=' .. hoogle_opts.scope or '' | ||
return 'https://hoogle.haskell.org/?hoogle=' | ||
.. urlencode(search_term) | ||
.. scope_param | ||
.. (hoogle_opts.json and '&mode=json' or '') | ||
end | ||
|
||
local function setup_telescope_search() | ||
local pickers = deps.require_telescope('telescope.pickers') | ||
local finders = deps.require_telescope('telescope.finders') | ||
local previewers = deps.require_telescope('telescope.previewers') | ||
local config = deps.require_telescope('telescope.config').values | ||
local telescope_util = require('haskell-tools.telescope-util') | ||
local async = deps.require_plenary('plenary.async') | ||
|
||
local curl = deps.require_plenary('plenary.curl') | ||
|
||
function M.telescope_search(search_term, opts) | ||
async.run(function() | ||
if vim.fn.executable('curl') == 0 then | ||
error("haskell-tools.hoogle-web: 'curl' executable not found! Aborting.") | ||
return | ||
end | ||
opts = util.tbl_merge(opts or {}, { | ||
layout_strategy = 'horizontal', | ||
layout_config = { preview_width = 80 }, | ||
hoogle = { json = true }, | ||
}) | ||
local response = curl.get { | ||
url = mk_hoogle_request(search_term, opts), | ||
accept = 'application/json', | ||
} | ||
local results = vim.json.decode(response.body) | ||
pickers.new(opts, { | ||
prompt_title = 'Hoogle: ' .. search_term, | ||
finder = finders.new_table { | ||
results = results, | ||
entry_maker = telescope_util.mk_hoogle_entry | ||
}, | ||
sorter = config.generic_sorter(opts), | ||
previewer = previewers.display_content.new(opts), | ||
attach_mappings = telescope_util.attach_mappings, | ||
}):find() | ||
end) | ||
end | ||
end | ||
|
||
local function setup_browser_search() | ||
function M.browser_search(search_term, opts) | ||
opts = util.tbl_merge(opts or {}, { | ||
hoogle = { json = false }, | ||
}) | ||
util.open_browser(mk_hoogle_request(search_term, opts)) | ||
end | ||
end | ||
|
||
function M.setup() | ||
if deps.has_telescope() then | ||
setup_telescope_search() | ||
end | ||
setup_browser_search() | ||
end | ||
|
||
return M |
Oops, something went wrong.