From 5e32f16de65a1c0c9e9319a4bbba3b00aa0b9906 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Sun, 20 Aug 2023 19:15:25 +0200 Subject: [PATCH] refactor: move evaluate to types.internal module --- lua/haskell-tools/config/internal.lua | 4 ++-- lua/haskell-tools/health.lua | 4 ++-- lua/haskell-tools/internal.lua | 6 +++--- lua/haskell-tools/lsp.lua | 10 +++++----- lua/haskell-tools/lsp/util.lua | 4 ++-- lua/haskell-tools/repl.lua | 20 ++++++++++---------- lua/haskell-tools/tags.lua | 12 ++++++------ lua/haskell-tools/types/internal.lua | 16 ++++++++++++++++ lua/haskell-tools/util.lua | 13 ------------- tests/lsp_spec.lua | 10 +++++----- 10 files changed, 51 insertions(+), 48 deletions(-) diff --git a/lua/haskell-tools/config/internal.lua b/lua/haskell-tools/config/internal.lua index 4071327d..d071ba58 100644 --- a/lua/haskell-tools/config/internal.lua +++ b/lua/haskell-tools/config/internal.lua @@ -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 diff --git a/lua/haskell-tools/health.lua b/lua/haskell-tools/health.lua index fbff05a8..51d10452 100644 --- a/lua/haskell-tools/health.lua +++ b/lua/haskell-tools/health.lua @@ -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') @@ -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 diff --git a/lua/haskell-tools/internal.lua b/lua/haskell-tools/internal.lua index be1e1972..d4090d18 100644 --- a/lua/haskell-tools/internal.lua +++ b/lua/haskell-tools/internal.lua @@ -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') @@ -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 diff --git a/lua/haskell-tools/lsp.lua b/lua/haskell-tools/lsp.lua index 2cf24f0b..1d8b3831 100644 --- a/lua/haskell-tools/lsp.lua +++ b/lua/haskell-tools/lsp.lua @@ -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') @@ -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 @@ -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, @@ -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, diff --git a/lua/haskell-tools/lsp/util.lua b/lua/haskell-tools/lsp/util.lua index 7b160f51..ceffa826 100644 --- a/lua/haskell-tools/lsp/util.lua +++ b/lua/haskell-tools/lsp/util.lua @@ -8,7 +8,7 @@ --- LSP utilities ---@brief ]] -local HtUtil = require('haskell-tools.util') +local Types = require('haskell-tools.types.internal') ---@class LspUtil local LspUtil = {} @@ -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.') diff --git a/lua/haskell-tools/repl.lua b/lua/haskell-tools/repl.lua index a918c27d..9ef92117 100644 --- a/lua/haskell-tools/repl.lua +++ b/lua/haskell-tools/repl.lua @@ -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. @@ -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 } @@ -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 @@ -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 @@ -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') diff --git a/lua/haskell-tools/tags.lua b/lua/haskell-tools/tags.lua index cbdd7a2a..6d634832 100644 --- a/lua/haskell-tools/tags.lua +++ b/lua/haskell-tools/tags.lua @@ -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, @@ -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 @@ -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 @@ -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 diff --git a/lua/haskell-tools/types/internal.lua b/lua/haskell-tools/types/internal.lua index 3d259f57..86265149 100644 --- a/lua/haskell-tools/types/internal.lua +++ b/lua/haskell-tools/types/internal.lua @@ -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 diff --git a/lua/haskell-tools/util.lua b/lua/haskell-tools/util.lua index 3cc16dbd..9510e744 100644 --- a/lua/haskell-tools/util.lua +++ b/lua/haskell-tools/util.lua @@ -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 diff --git a/tests/lsp_spec.lua b/tests/lsp_spec.lua index 60f9db29..5d59e10a 100644 --- a/tests/lsp_spec.lua +++ b/tests/lsp_spec.lua @@ -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