diff --git a/CHANGELOG.md b/CHANGELOG.md index 926d5d9..feea03a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.8.5] - 2023-09-02 +### Changed +- Some plugin startup time improvements. + ## [0.8.4] - 2023-06-19 ### Fixed - Don't pass `-p` option to `tasty` if no paths to filter on are detected. diff --git a/lua/neotest-haskell/hspec.lua b/lua/neotest-haskell/hspec.lua index e722b23..b691082 100644 --- a/lua/neotest-haskell/hspec.lua +++ b/lua/neotest-haskell/hspec.lua @@ -1,5 +1,4 @@ local treesitter = require('neotest-haskell.treesitter') -local util = require('neotest-haskell.util') local position = require('neotest-haskell.position') local results = require('neotest-haskell.results') @@ -122,6 +121,7 @@ end ---@param test_name string The name of the test. ---@return neotest.Error[] hspec_errors The errors. local function parse_errors(raw_lines, test_name) + local util = require('neotest-haskell.util') local failures_found = false local pos_found = false local error_message = nil diff --git a/lua/neotest-haskell/init.lua b/lua/neotest-haskell/init.lua index 2c220ef..e75342c 100644 --- a/lua/neotest-haskell/init.lua +++ b/lua/neotest-haskell/init.lua @@ -73,9 +73,6 @@ local base = require('neotest-haskell.base') local runner = require('neotest-haskell.runner') -local logger = require('neotest.logging') -local validate = require('neotest-haskell.validate') -local lib = require('neotest.lib') ---@type neotest.Adapter local HaskellNeotestAdapter = { name = 'neotest-haskell' } @@ -88,6 +85,7 @@ local HaskellNeotestAdapter = { name = 'neotest-haskell' } ---@see neotest.Adapter ---@private function HaskellNeotestAdapter.root(dir) + local lib = require('neotest.lib') local multi_package_or_stack_project_root_directory = lib.files.match_root_pattern('cabal.project', 'stack.yaml')(dir) return multi_package_or_stack_project_root_directory or lib.files.match_root_pattern('*.cabal', 'package.yaml')(dir) end @@ -128,6 +126,7 @@ end ---@return neotest.Tree | nil ---@private function HaskellNeotestAdapter.discover_positions(file_path) + local logger = require('neotest.logging') local handler = runner.select_framework(file_path, frameworks) local pos = handler.parse_positions(file_path) logger.debug('Found positions: ' .. vim.inspect(pos)) @@ -192,6 +191,7 @@ end setmetatable(HaskellNeotestAdapter, { ---@param opts NeotestHaskellOpts __call = function(_, opts) + local validate = require('neotest-haskell.validate') validate.validate(opts) if opts.build_tools then build_tools = opts.build_tools diff --git a/lua/neotest-haskell/results.lua b/lua/neotest-haskell/results.lua index c801732..1b868ca 100644 --- a/lua/neotest-haskell/results.lua +++ b/lua/neotest-haskell/results.lua @@ -1,5 +1,3 @@ -local lib = require('neotest.lib') - local results = {} ---Get the file root from a test tree. @@ -24,6 +22,8 @@ end ---@param get_skipped_name fun(line:string, lines:string[], idx:integer):string? Function to extract a skipped test name ---@return fun(context:RunContext, out_path:string, tree:neotest.Tree):table result_parser function results.mk_result_parser(parse_errors, get_failed_name, get_succeeded_name, get_skipped_name) + local lib = require('neotest.lib') + ---@param context RunContext The run context. ---@param out_path string Path to an hspec test results output file. ---@param tree neotest.Tree The test tree at the position that was run. diff --git a/lua/neotest-haskell/runner.lua b/lua/neotest-haskell/runner.lua index 15401a7..112beb5 100644 --- a/lua/neotest-haskell/runner.lua +++ b/lua/neotest-haskell/runner.lua @@ -1,12 +1,3 @@ -local ok, nio = pcall(require, 'nio') -if not ok then - nio = require('neotest.async') -end -local lib = require('neotest.lib') -local Path = require('plenary.path') -local logger = require('neotest.logging') -local treesitter_hs = require('neotest-haskell.treesitter') - local runner = {} ---Check if the given directory contains a file matching a list of patterns. @@ -14,6 +5,7 @@ local runner = {} ---@param patterns string[] The patterns to check for. ---@return boolean local function directory_contains_file_matching(directory, patterns) + local Path = require('plenary.path') for _, pattern in ipairs(patterns) do for _, file in ipairs(vim.fn.glob(Path:new(directory, pattern).filename, true, true)) do if Path:new(file):exists() then @@ -28,8 +20,13 @@ end ---If a *.cabal is present, this is *. ---Otherwise, we assume the package name is the same as the directory name. ---@param package_root string The package root directory. ----@return string package_name The assumed package name. +---@return string | nil package_name The assumed package name. local function get_package_name(package_root) + local ok, nio = pcall(require, 'nio') + if not ok then + nio = require('neotest.async') + end + local Path = require('plenary.path') ---@diagnostic disable-next-line -- nio.fn is private? for _, package_file_path in ipairs(nio.fn.glob(Path:new(package_root, '*.cabal').filename, true, true)) do local package_file_name = package_file_path and vim.fn.fnamemodify(package_file_path, ':t') @@ -71,6 +68,7 @@ end ---@return boolean ---@async local function has_module(test_file_content, qualified_modules) + local treesitter_hs = require('neotest-haskell.treesitter') for _, qualified_module in pairs(qualified_modules) do local modules = {} for module in qualified_module:gmatch('([^%.]+)') do @@ -105,6 +103,8 @@ end ---@return TestFrameworkHandler handler ---@async function runner.select_framework(test_file_path, frameworks) + local lib = require('neotest.lib') + local logger = require('neotest.logging') local content = lib.files.read(test_file_path) ---@type FrameworkSpec[] local framework_specs = {} @@ -138,6 +138,8 @@ end ---@param build_tools build_tool[] List of build tools to choose from. ---@return fun(neotest.Tree):neotest.RunSpec mk_command A function that builds the runner command using the selected build tool for a test tree. function runner.select_build_tool(handler, test_file_path, build_tools) + local lib = require('neotest.lib') + local logger = require('neotest.logging') -- A package always has a *.cabal file (or in rare cases just a package.yaml file). local package_root = lib.files.match_root_pattern('*.cabal', 'package.yaml')(test_file_path) if not package_root then @@ -173,7 +175,9 @@ function runner.select_build_tool(handler, test_file_path, build_tools) local command = { selected_build_tool, 'test' } if is_multi_package_project then local package_name = get_package_name(package_root) - table.insert(command, package_name) + if package_name then + table.insert(command, package_name) + end end return function(pos) local test_opts = pos and get_test_opts(pos) diff --git a/lua/neotest-haskell/sydtest.lua b/lua/neotest-haskell/sydtest.lua index be03f7b..ebd6764 100644 --- a/lua/neotest-haskell/sydtest.lua +++ b/lua/neotest-haskell/sydtest.lua @@ -1,6 +1,5 @@ local hspec = require('neotest-haskell.hspec') local treesitter = require('neotest-haskell.treesitter') -local util = require('neotest-haskell.util') local position = require('neotest-haskell.position') local results = require('neotest-haskell.results') @@ -124,6 +123,7 @@ end ---@param test_name string The name of the test. ---@return neotest.Error[] hspec_errors The errors. local function parse_errors(raw_lines, test_name) + local util = require('neotest-haskell.util') local failures_found = false local err_msg_pos_found = false local error_message = nil diff --git a/lua/neotest-haskell/tasty.lua b/lua/neotest-haskell/tasty.lua index 0bb3dce..25d3fff 100644 --- a/lua/neotest-haskell/tasty.lua +++ b/lua/neotest-haskell/tasty.lua @@ -3,7 +3,6 @@ local util = require('neotest-haskell.util') local position = require('neotest-haskell.position') local results = require('neotest-haskell.results') local hspec = require('neotest-haskell.hspec') -local logger = require('neotest.logging') local tasty = {} @@ -36,6 +35,7 @@ local function parse_top_level_tasty_nodes(pos) end local function concat_subpatterns(subpatterns) if not subpatterns or #subpatterns == 0 then + local logger = require('neotest.logging') logger.error('Could not detect tasty top level nodes.') return nil end @@ -51,6 +51,7 @@ end local function parse_tasty_tree(pos) local function format_result(result) if not result or result == '' then + local logger = require('neotest.logging') logger.error('Could not detect any tasty patterns.') return nil end diff --git a/lua/neotest-haskell/treesitter.lua b/lua/neotest-haskell/treesitter.lua index d102775..a745ff0 100644 --- a/lua/neotest-haskell/treesitter.lua +++ b/lua/neotest-haskell/treesitter.lua @@ -1,11 +1,3 @@ -local lib = require('neotest.lib') - -local ok, nio = pcall(require, 'nio') -if not ok then - ---@diagnostic disable-next-line: undefined-field - nio = require('neotest.async').util -end - local treesitter = {} ---@class FileRef @@ -19,6 +11,7 @@ local treesitter = {} ---@param file_ref FileRef ---@return FileContentRef content_ref local function to_file_content_ref(file_ref) + local lib = require('neotest.lib') return { content = lib.files.read(file_ref.file), } @@ -34,6 +27,11 @@ function treesitter.iter_ts_matches(query, source) source = to_file_content_ref(source) end local lang = require('nvim-treesitter.parsers').ft_to_lang('haskell') + local ok, nio = pcall(require, 'nio') + if not ok then + ---@diagnostic disable-next-line: undefined-field + nio = require('neotest.async').util + end nio.scheduler() local lang_tree = vim.treesitter.get_string_parser( source.content, @@ -41,6 +39,7 @@ function treesitter.iter_ts_matches(query, source) -- Prevent neovim from trying to read the query from injection files { injections = { [lang] = '' } } ) + local lib = require('neotest.lib') ---@type userdata local root = lib.treesitter.fast_parse(lang_tree):root() local normalised_query = lib.treesitter.normalise_query(lang, query)