Skip to content

Commit

Permalink
perf: lazily require modules (#121)
Browse files Browse the repository at this point in the history
* fix: add nil check

* perf: lazily require modules
  • Loading branch information
mrcjkb authored Sep 2, 2023
1 parent 25b2187 commit 9ca219e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-haskell/hspec.lua
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lua/neotest-haskell/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lua/neotest-haskell/results.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local lib = require('neotest.lib')

local results = {}

---Get the file root from a test tree.
Expand All @@ -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<string,neotest.Result> 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.
Expand Down
26 changes: 15 additions & 11 deletions lua/neotest-haskell/runner.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
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.
---@param directory string The directory to check for.
---@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
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lua/neotest-haskell/sydtest.lua
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lua/neotest-haskell/tasty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lua/neotest-haskell/treesitter.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
}
Expand All @@ -34,13 +27,19 @@ 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,
lang,
-- 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)
Expand Down

0 comments on commit 9ca219e

Please sign in to comment.