Skip to content

Commit

Permalink
refactor: defer some requires
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Aug 24, 2023
1 parent 62a2431 commit 4fe968f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
14 changes: 9 additions & 5 deletions lua/haskell-tools/dap.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
---@mod haskell-tools.dap haskell-tools nvim-dap setup

local log = require('haskell-tools.log')
local OS = require('haskell-tools.os')
local deps = require('haskell-tools.deps')
local HtProjectHelpers = require('haskell-tools.project.helpers')
local Path = deps.require_plenary('plenary.path')
local async = deps.require_plenary('plenary.async')

---@param root_dir string
local function get_ghci_dap_cmd(root_dir)
local HtProjectHelpers = require('haskell-tools.project.helpers')
if HtProjectHelpers.is_cabal_project(root_dir) then
return 'cabal exec -- ghci-dap --interactive -i ${workspaceFolder}'
else
Expand All @@ -22,11 +18,14 @@ end
local function find_json_configurations(root_dir, opts)
---@type HsDapLaunchConfiguration[]
local configurations = {}
local Path = deps.require_plenary('plenary.path')
local log = require('haskell-tools.log')
local results = vim.fn.glob(Path:new(root_dir, opts.settings_file_pattern).filename, true, true)
if #results == 0 then
log.info(opts.settings_file_pattern .. ' not found in project root ' .. root_dir)
else
for _, launch_json in pairs(results) do
local OS = require('haskell-tools.os')
local content = OS.read_file(launch_json)
local success, settings = pcall(vim.json.decode, content)
if not success then
Expand All @@ -44,6 +43,7 @@ end
---@return table
local function detect_launch_configurations(root_dir)
local launch_configurations = {}
local Path = deps.require_plenary('plenary.path')
local HTConfig = require('haskell-tools.config.internal')
local dap_opts = HTConfig.dap
---@param entry_point HsEntryPoint
Expand All @@ -70,6 +70,7 @@ local function detect_launch_configurations(root_dir)
}
return HsDapLaunchConfiguration
end
local HtProjectHelpers = require('haskell-tools.project.helpers')
for _, entry_point in pairs(HtProjectHelpers.parse_project_entrypoints(root_dir)) do
table.insert(launch_configurations, mk_launch_configuration(entry_point))
end
Expand Down Expand Up @@ -114,10 +115,13 @@ local DefaultAutoDapConfigOpts = {
---@param opts AddDapConfigOpts|nil
---@return nil
HsDapTools.discover_configurations = function(bufnr, opts)
local async = deps.require_plenary('plenary.async')
local log = require('haskell-tools.log')
async.run(function()
bufnr = bufnr or 0 -- Default to current buffer
opts = vim.tbl_deep_extend('force', {}, DefaultAutoDapConfigOpts, opts or {})
local filename = vim.api.nvim_buf_get_name(bufnr)
local HtProjectHelpers = require('haskell-tools.project.helpers')
local project_root = HtProjectHelpers.match_project_root(filename)
if not project_root then
log.warn('haskell-tools.dap: Unable to detect project root for file ' .. filename)
Expand Down
7 changes: 4 additions & 3 deletions lua/haskell-tools/hoogle.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---@mod haskell-tools.hoogle haskell-tools Hoogle search

local log = require('haskell-tools.log')
local deps = require('haskell-tools.deps')
local HtParser = require('haskell-tools.parser')
local LspHelpers = require('haskell-tools.lsp.helpers')
local lsp_util = vim.lsp.util

---@type fun(sig_or_func_name:string, options:table|nil):nil
Expand All @@ -19,6 +16,7 @@ local function mk_lsp_hoogle_signature_handler(options)
end
local func_name = vim.fn.expand('<cword>')
---@cast func_name string
local HtParser = require('haskell-tools.parser')
local signature_or_func_name = HtParser.try_get_signatures_from_markdown(func_name, result.contents.value)
or func_name
log.debug { 'Hoogle LSP signature search', signature_or_func_name }
Expand Down Expand Up @@ -67,6 +65,7 @@ elseif opts.mode == 'telescope-local' then
set_web_handler()
return
end
local deps = require('haskell-tools.deps')
if not deps.has_telescope() then
local msg = 'handler set to "telescope-local" but telescope.nvim is not installed.'
log.warn(msg)
Expand All @@ -78,6 +77,7 @@ elseif opts.mode == 'telescope-local' then
elseif opts.mode == 'browser' then
set_browser_handler()
elseif opts.mode == 'auto' then
local deps = require('haskell-tools.deps')
if not deps.has_telescope() then
set_browser_handler()
elseif hoogle_local.has_hoogle() then
Expand All @@ -99,6 +99,7 @@ HoogleTools.hoogle_signature = function(options)
handler(options.search_term)
return
end
local LspHelpers = require('haskell-tools.lsp.helpers')
local clients = LspHelpers.get_clients { bufnr = vim.api.nvim_get_current_buf() }
if #clients > 0 then
lsp_hoogle_signature(options)
Expand Down
12 changes: 7 additions & 5 deletions lua/haskell-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
local HTConfig = require('haskell-tools.config.internal')
local log = require('haskell-tools.log')
local Types = require('haskell-tools.types.internal')
local HtProjectHelpers = require('haskell-tools.project.helpers')
local OS = require('haskell-tools.os')
local deps = require('haskell-tools.deps')
local Path = deps.require_plenary('plenary.path')
local LspHelpers = require('haskell-tools.lsp.helpers')
local uv = vim.uv
---@diagnostic disable-next-line: deprecated
or vim.loop
Expand Down Expand Up @@ -42,6 +37,7 @@ end
---@param client lsp.Client
---@return nil
local function fix_cabal_client(client)
local LspHelpers = require('haskell-tools.lsp.helpers')
if client.name == LspHelpers.cabal_client_name and client.server_capabilities then
client.server_capabilities = vim.tbl_extend('force', client.server_capabilities, {
foldingRangeProvider = false,
Expand Down Expand Up @@ -87,12 +83,15 @@ HlsTools.load_hls_settings = function(project_root, opts)
end
local default_opts = { settings_file_pattern = 'hls.json' }
opts = vim.tbl_deep_extend('force', {}, default_opts, opts or {})
local deps = require('haskell-tools.deps')
local Path = deps.require_plenary('plenary.path')
local results = vim.fn.glob(Path:new(project_root, opts.settings_file_pattern).filename, true, true)
if #results == 0 then
log.info(opts.settings_file_pattern .. ' not found in project root ' .. project_root)
return default_settings
end
local settings_json = results[1]
local OS = require('haskell-tools.os')
local content = OS.read_file(settings_json)
local success, settings = pcall(vim.json.decode, content)
if not success then
Expand All @@ -119,9 +118,11 @@ HlsTools.start = function(bufnr)
vim.notify('haskell-tools: ' .. msg, vim.log.levels.ERROR)
return
end
local HtProjectHelpers = require('haskell-tools.project.helpers')
local is_cabal = HtProjectHelpers.is_cabal_file(bufnr)
local project_root = ht.project.root_dir(file)
local hls_settings = type(hls_opts.settings) == 'function' and hls_opts.settings(project_root) or hls_opts.settings
local LspHelpers = require('haskell-tools.lsp.helpers')
local cmd = LspHelpers.get_hls_cmd()
local hls_bin = cmd[1]
if vim.fn.executable(hls_bin) == 0 then
Expand Down Expand Up @@ -178,6 +179,7 @@ end
---@return table[] clients A list of clients that will be stopped
HlsTools.stop = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local LspHelpers = require('haskell-tools.lsp.helpers')
local clients = LspHelpers.get_active_ht_clients(bufnr)
vim.lsp.stop_client(clients)
return clients
Expand Down
6 changes: 5 additions & 1 deletion lua/haskell-tools/project.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---@mod haskell-tools.project haskell-tools Project module

local log = require('haskell-tools.log')
local HtProjectHelpers = require('haskell-tools.project.helpers')
local deps = require('haskell-tools.deps')

---@brief [[
Expand All @@ -22,6 +21,7 @@ local function telescope_package_search(callback, opts)
vim.notify(err_msg, vim.log.levels.ERROR)
return
end
local HtProjectHelpers = require('haskell-tools.project.helpers')
local package_root = HtProjectHelpers.match_package_root(file)
if not package_root then
local err_msg = 'Telescope package search: No package root found for file ' .. file
Expand Down Expand Up @@ -63,6 +63,7 @@ local HsProjectTools = {}
---@param project_file string The path to a project file
---@return string|nil
HsProjectTools.root_dir = function(project_file)
local HtProjectHelpers = require('haskell-tools.project.helpers')
return HtProjectHelpers.match_cabal_project_root(project_file)
or HtProjectHelpers.match_stack_project_root(project_file)
or HtProjectHelpers.match_package_root(project_file)
Expand All @@ -72,6 +73,7 @@ end
---Open the package.yaml of the package containing the current buffer.
---@return nil
HsProjectTools.open_package_yaml = function()
local HtProjectHelpers = require('haskell-tools.project.helpers')
vim.schedule(function()
local file = vim.api.nvim_buf_get_name(0)
local result = HtProjectHelpers.get_package_yaml(file)
Expand All @@ -93,6 +95,7 @@ end
---@return nil
HsProjectTools.open_package_cabal = function()
vim.schedule(function()
local HtProjectHelpers = require('haskell-tools.project.helpers')
local file = vim.api.nvim_buf_get_name(0)
if vim.fn.filewritable(file) ~= 0 and not HtProjectHelpers.is_cabal_project(file) then
vim.notify('HsPackageCabal: Not a cabal project?', vim.log.levels.ERROR)
Expand All @@ -113,6 +116,7 @@ end
---@return nil
HsProjectTools.open_project_file = function()
vim.schedule(function()
local HtProjectHelpers = require('haskell-tools.project.helpers')
local file = vim.api.nvim_buf_get_name(0)
local stack_project_root = HtProjectHelpers.match_stack_project_root(file)
if stack_project_root then
Expand Down
3 changes: 2 additions & 1 deletion lua/haskell-tools/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
---@bruief ]]

local log = require('haskell-tools.log')
local HtProjectHelpers = require('haskell-tools.project.helpers')
local Types = require('haskell-tools.types.internal')

---Extend a repl command for `file`.
Expand All @@ -14,6 +13,7 @@ local Types = require('haskell-tools.types.internal')
---@param file string|nil An optional project file
---@return string[]|nil
local function extend_repl_cmd(cmd, file)
local HtProjectHelpers = require('haskell-tools.project.helpers')
if file == nil then
file = vim.api.nvim_buf_get_name(0)
log.debug('extend_repl_cmd: No file specified. Using current buffer: ' .. file)
Expand Down Expand Up @@ -78,6 +78,7 @@ local function mk_repl_cmd(file)
end
local HTConfig = require('haskell-tools.config.internal')
local opts = HTConfig.tools.repl
local HtProjectHelpers = require('haskell-tools.project.helpers')
if Types.evaluate(opts.prefer) == 'stack' and HtProjectHelpers.is_stack_project(chk_path) then
return mk_stack_repl_cmd(file)
end
Expand Down
3 changes: 2 additions & 1 deletion lua/haskell-tools/tags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local HTConfig = require('haskell-tools.config.internal')
local Types = require('haskell-tools.types.internal')
local log = require('haskell-tools.log')
local deps = require('haskell-tools.deps')
local HtProjectHelpers = require('haskell-tools.project.helpers')

local _state = {
fast_tags_generating = false,
Expand All @@ -29,6 +28,7 @@ local FastTagsTools = {}
FastTagsTools.generate_project_tags = function(path, opts)
path = path or vim.api.nvim_buf_get_name(0)
opts = vim.tbl_extend('force', { refresh = true }, opts or {})
local HtProjectHelpers = require('haskell-tools.project.helpers')
local project_root = HtProjectHelpers.match_project_root(path) or vim.fn.getcwd() or 'UNDEFINED'
if opts.refresh == false and _state.projects[project_root] then
log.debug('Project tags already generated. Skipping.')
Expand Down Expand Up @@ -57,6 +57,7 @@ end
FastTagsTools.generate_package_tags = function(path)
path = path or vim.api.nvim_buf_get_name(0)
_state.fast_tags_generating = true
local HtProjectHelpers = require('haskell-tools.project.helpers')
local rel_package_root = HtProjectHelpers.match_package_root(path)
if not rel_package_root then
log.warn('generate_package_tags: No rel_package root found.')
Expand Down

0 comments on commit 4fe968f

Please sign in to comment.