Skip to content

Commit

Permalink
refactor: move evaluate to types.internal module
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Aug 20, 2023
1 parent 1a2a770 commit 5e32f16
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 48 deletions.
4 changes: 2 additions & 2 deletions lua/haskell-tools/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ local HTDefaultConfig = {
hls = {
---@type boolean | (fun():boolean) Whether to automatically attach the LSP client. Defaults to `true` if the haskell-language-server executable is found.
auto_attach = function()
local util = require('haskell-tools.util')
local cmd = util.evaluate(HTConfig.hls.cmd)
local Types = require('haskell-tools.types.internal')
local cmd = Types.evaluate(HTConfig.hls.cmd)
---@cast cmd string[]
local hls_bin = cmd[1]
return vim.fn.executable(hls_bin) ~= 0
Expand Down
4 changes: 2 additions & 2 deletions lua/haskell-tools/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

local health = {}

local ht_util = require('haskell-tools.util')
local Types = require('haskell-tools.types.internal')
local deps = require('haskell-tools.deps')
local HTConfig = require('haskell-tools.config.internal')
local h = vim.health or require('health')
Expand Down Expand Up @@ -65,7 +65,7 @@ local external_dependencies = {
if not HTConfig then
return default
end
local cmd = ht_util.evaluate(HTConfig.hls.cmd)
local cmd = Types.evaluate(HTConfig.hls.cmd)
if not cmd or #cmd == 0 then
return default
end
Expand Down
6 changes: 3 additions & 3 deletions lua/haskell-tools/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
---@brief ]]

local HTConfig = require('haskell-tools.config.internal')
local HtUtil = require('haskell-tools.util')
local Types = require('haskell-tools.types.internal')
local HtProjectUtil = require('haskell-tools.project.util')
local LspUtil = require('haskell-tools.lsp.util')
local HaskellTools = require('haskell-tools')
Expand Down Expand Up @@ -49,10 +49,10 @@ end
---ht.start_or_attach()
---@usage ]]
function InternalApi.start_or_attach()
if HtUtil.evaluate(HTConfig.hls.auto_attach) and buf_is_lsp_supported() then
if Types.evaluate(HTConfig.hls.auto_attach) and buf_is_lsp_supported() then
HaskellTools.lsp.start()
end
if HtUtil.evaluate(HTConfig.tools.tags.enable) then
if Types.evaluate(HTConfig.tools.tags.enable) then
HaskellTools.tags.generate_project_tags(nil, { refresh = false })
end
end
Expand Down
10 changes: 5 additions & 5 deletions lua/haskell-tools/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

local HTConfig = require('haskell-tools.config.internal')
local log = require('haskell-tools.log')
local HtUtil = require('haskell-tools.util')
local Types = require('haskell-tools.types.internal')
local HtProjectUtil = require('haskell-tools.project.util')
local OS = require('haskell-tools.os')
local deps = require('haskell-tools.deps')
Expand Down Expand Up @@ -61,13 +61,13 @@ local handlers = {}
local tools_opts = HTConfig.tools
local definition_opts = tools_opts.definition or {}

if HtUtil.evaluate(definition_opts.hoogle_signature_fallback) then
if Types.evaluate(definition_opts.hoogle_signature_fallback) then
local lsp_definition = require('haskell-tools.lsp.definition')
log.debug('Wrapping vim.lsp.buf.definition with Hoogle signature fallback.')
handlers['textDocument/definition'] = lsp_definition.mk_hoogle_fallback_definition_handler(definition_opts)
end
local hover_opts = tools_opts.hover
if HtUtil.evaluate(hover_opts.enable) then
if Types.evaluate(hover_opts.enable) then
local hover = require('haskell-tools.lsp.hover')
handlers['textDocument/hover'] = hover.on_hover
end
Expand Down Expand Up @@ -130,7 +130,7 @@ HlsTools.start = function(bufnr)

local lsp_start_opts = {
name = is_cabal and lsp_util.cabal_client_name or lsp_util.haskell_client_name,
cmd = HtUtil.evaluate(cmd),
cmd = Types.evaluate(cmd),
root_dir = project_root,
capabilities = hls_opts.capabilities,
handlers = handlers,
Expand All @@ -154,7 +154,7 @@ HlsTools.start = function(bufnr)
end)
end
local code_lens_opts = tools_opts.codeLens or {}
if HtUtil.evaluate(code_lens_opts.autoRefresh) then
if Types.evaluate(code_lens_opts.autoRefresh) then
vim.api.nvim_create_autocmd({ 'InsertLeave', 'BufWritePost', 'TextChanged' }, {
group = vim.api.nvim_create_augroup('haskell-tools-code-lens', {}),
callback = buf_refresh_codeLens,
Expand Down
4 changes: 2 additions & 2 deletions lua/haskell-tools/lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
--- LSP utilities
---@brief ]]

local HtUtil = require('haskell-tools.util')
local Types = require('haskell-tools.types.internal')

---@class LspUtil
local LspUtil = {}
Expand Down Expand Up @@ -49,7 +49,7 @@ end
---@return string[] cmd The command to invoke haskell-language-server
LspUtil.get_hls_cmd = function()
local HTConfig = require('haskell-tools.config.internal')
local cmd = HtUtil.evaluate(HTConfig.hls.cmd)
local cmd = Types.evaluate(HTConfig.hls.cmd)
---@cast cmd string[]
assert(type(cmd) == 'table', 'haskell-tools: hls.cmd should evaluate to a string[]')
assert(#cmd > 1, 'haskell-tools: hls.cmd evaluates to an empty list.')
Expand Down
20 changes: 10 additions & 10 deletions lua/haskell-tools/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
---@bruief ]]

local log = require('haskell-tools.log')
local project = require('haskell-tools.project.util')
local ht_util = require('haskell-tools.util')
local HtProjectUtil = require('haskell-tools.project.util')
local Types = require('haskell-tools.types.internal')

---Extend a repl command for `file`.
---If `file` is `nil`, create a repl the nearest package.
Expand All @@ -17,8 +17,8 @@ local function extend_repl_cmd(cmd, file)
if file == nil then
file = vim.api.nvim_buf_get_name(0)
log.debug('extend_repl_cmd: No file specified. Using current buffer: ' .. file)
local project_root = project.match_project_root(file)
local subpackage = project_root and project.get_package_name(file)
local project_root = HtProjectUtil.match_project_root(file)
local subpackage = project_root and HtProjectUtil.get_package_name(file)
if subpackage then
table.insert(cmd, subpackage)
log.debug { 'extend_repl_cmd: Extended cmd with package.', cmd }
Expand All @@ -29,8 +29,8 @@ local function extend_repl_cmd(cmd, file)
end
end
log.debug('extend_repl_cmd: File: ' .. file)
local project_root = project.match_project_root(file)
local subpackage = project_root and project.get_package_name(file)
local project_root = HtProjectUtil.match_project_root(file)
local subpackage = project_root and HtProjectUtil.get_package_name(file)
if not subpackage then
log.debug { 'extend_repl_cmd: No package found.', cmd }
return cmd
Expand Down Expand Up @@ -78,13 +78,13 @@ local function mk_repl_cmd(file)
end
local HTConfig = require('haskell-tools.config.internal')
local opts = HTConfig.tools.repl
if ht_util.evaluate(opts.prefer) == 'stack' and project.is_stack_project(chk_path) then
if Types.evaluate(opts.prefer) == 'stack' and HtProjectUtil.is_stack_project(chk_path) then
return mk_stack_repl_cmd(file)
end
if project.is_cabal_project(chk_path) then
if HtProjectUtil.is_cabal_project(chk_path) then
return mk_cabal_repl_cmd(file)
end
if project.is_stack_project(chk_path) then
if HtProjectUtil.is_stack_project(chk_path) then
return mk_stack_repl_cmd(file)
end
if vim.fn.executable('ghci') == 1 then
Expand All @@ -103,7 +103,7 @@ local opts = HTConfig.tools.repl
---@type ReplHandlerImpl
local handler

local handler_type = ht_util.evaluate(opts.handler)
local handler_type = Types.evaluate(opts.handler)
---@cast handler_type ReplHandler
if handler_type == 'toggleterm' then
log.info('handler = toggleterm')
Expand Down
12 changes: 6 additions & 6 deletions lua/haskell-tools/tags.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---@mod haskell-tools.tags haskell-tools fast-tags module

local HTConfig = require('haskell-tools.config.internal')
local ht_util = require('haskell-tools.util')
local Types = require('haskell-tools.types.internal')
local log = require('haskell-tools.log')
local deps = require('haskell-tools.deps')
local project_util = require('haskell-tools.project.util')
local HtProjectUtil = require('haskell-tools.project.util')

local _state = {
fast_tags_generating = false,
Expand All @@ -29,7 +29,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 project_root = project_util.match_project_root(path) or vim.fn.getcwd() or 'UNDEFINED'
local project_root = HtProjectUtil.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.')
return
Expand Down Expand Up @@ -57,13 +57,13 @@ end
FastTagsTools.generate_package_tags = function(path)
path = path or vim.api.nvim_buf_get_name(0)
_state.fast_tags_generating = true
local rel_package_root = project_util.match_package_root(path)
local rel_package_root = HtProjectUtil.match_package_root(path)
if not rel_package_root then
log.warn('generate_package_tags: No rel_package root found.')
return
end
local package_root = vim.fn.getcwd() .. '/' .. rel_package_root
local project_root = project_util.match_project_root(path) or vim.fn.getcwd()
local project_root = HtProjectUtil.match_project_root(path) or vim.fn.getcwd()
if not package_root then
log.warn('generate_package_tags: No package root found.')
return
Expand All @@ -83,7 +83,7 @@ FastTagsTools.generate_package_tags = function(path)
end)
end

if not ht_util.evaluate(config.enable) then
if not Types.evaluate(config.enable) then
return
end

Expand Down
16 changes: 16 additions & 0 deletions lua/haskell-tools/types/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@
---@field exe_name string
---@field main string
---@field source_dir string

local Types = {}

---Evaluate a value that may be a function
---or an evaluated value
---@generic T
---@param value (fun():T)|T
---@return T
Types.evaluate = function(value)
if type(value) == 'function' then
return value()
end
return value
end

return Types
13 changes: 0 additions & 13 deletions lua/haskell-tools/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,4 @@ HtUtil.trim = function(str)
return (str:match('^%s*(.*)') or str):gsub('%s*$', '')
end

---TODO: Move this to a "types" module?
---Evaluate a value that may be a function
---or an evaluated value
---@generic T
---@param value (fun():T)|T
---@return T
HtUtil.evaluate = function(value)
if type(value) == 'function' then
return value()
end
return value
end

return HtUtil
10 changes: 5 additions & 5 deletions tests/lsp_spec.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
describe('LSP client API', function()
local config = require('haskell-tools.config.internal')
local HtConfig = require('haskell-tools.config.internal')
local ht = require('haskell-tools')
local ht_util = require('haskell-tools.util')
local Types = require('haskell-tools.types.internal')
it('Can load haskell-language-server config', function()
local settings = ht.lsp.load_hls_settings(os.getenv('TEST_CWD'))
assert.not_same(config.hls.default_settings, settings)
assert.not_same(HtConfig.hls.default_settings, settings)
end)
it('Falls back to default haskell-language-server config if none is found', function()
local settings = ht.lsp.load_hls_settings(os.getenv('TEST_CWD'), { settings_file_pattern = 'bla.json' })
assert.same(config.hls.default_settings, settings)
assert.same(HtConfig.hls.default_settings, settings)
end)
local hls_bin = ht_util.evaluate(config.hls.cmd)[1]
local hls_bin = Types.evaluate(HtConfig.hls.cmd)[1]
if vim.fn.executable(hls_bin) ~= 0 then
it('Can spin up haskell-language-server for Cabal project.', function()
--- TODO: Figure out how to add tests for this
Expand Down

0 comments on commit 5e32f16

Please sign in to comment.