Skip to content

Commit 9ca219e

Browse files
authored
perf: lazily require modules (#121)
* fix: add nil check * perf: lazily require modules
1 parent 25b2187 commit 9ca219e

File tree

8 files changed

+35
-27
lines changed

8 files changed

+35
-27
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.8.5] - 2023-09-02
9+
### Changed
10+
- Some plugin startup time improvements.
11+
812
## [0.8.4] - 2023-06-19
913
### Fixed
1014
- Don't pass `-p` option to `tasty` if no paths to filter on are detected.

lua/neotest-haskell/hspec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local treesitter = require('neotest-haskell.treesitter')
2-
local util = require('neotest-haskell.util')
32
local position = require('neotest-haskell.position')
43
local results = require('neotest-haskell.results')
54

@@ -122,6 +121,7 @@ end
122121
---@param test_name string The name of the test.
123122
---@return neotest.Error[] hspec_errors The errors.
124123
local function parse_errors(raw_lines, test_name)
124+
local util = require('neotest-haskell.util')
125125
local failures_found = false
126126
local pos_found = false
127127
local error_message = nil

lua/neotest-haskell/init.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@
7373

7474
local base = require('neotest-haskell.base')
7575
local runner = require('neotest-haskell.runner')
76-
local logger = require('neotest.logging')
77-
local validate = require('neotest-haskell.validate')
78-
local lib = require('neotest.lib')
7976

8077
---@type neotest.Adapter
8178
local HaskellNeotestAdapter = { name = 'neotest-haskell' }
@@ -88,6 +85,7 @@ local HaskellNeotestAdapter = { name = 'neotest-haskell' }
8885
---@see neotest.Adapter
8986
---@private
9087
function HaskellNeotestAdapter.root(dir)
88+
local lib = require('neotest.lib')
9189
local multi_package_or_stack_project_root_directory = lib.files.match_root_pattern('cabal.project', 'stack.yaml')(dir)
9290
return multi_package_or_stack_project_root_directory or lib.files.match_root_pattern('*.cabal', 'package.yaml')(dir)
9391
end
@@ -128,6 +126,7 @@ end
128126
---@return neotest.Tree | nil
129127
---@private
130128
function HaskellNeotestAdapter.discover_positions(file_path)
129+
local logger = require('neotest.logging')
131130
local handler = runner.select_framework(file_path, frameworks)
132131
local pos = handler.parse_positions(file_path)
133132
logger.debug('Found positions: ' .. vim.inspect(pos))
@@ -192,6 +191,7 @@ end
192191
setmetatable(HaskellNeotestAdapter, {
193192
---@param opts NeotestHaskellOpts
194193
__call = function(_, opts)
194+
local validate = require('neotest-haskell.validate')
195195
validate.validate(opts)
196196
if opts.build_tools then
197197
build_tools = opts.build_tools

lua/neotest-haskell/results.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local lib = require('neotest.lib')
2-
31
local results = {}
42

53
---Get the file root from a test tree.
@@ -24,6 +22,8 @@ end
2422
---@param get_skipped_name fun(line:string, lines:string[], idx:integer):string? Function to extract a skipped test name
2523
---@return fun(context:RunContext, out_path:string, tree:neotest.Tree):table<string,neotest.Result> result_parser
2624
function results.mk_result_parser(parse_errors, get_failed_name, get_succeeded_name, get_skipped_name)
25+
local lib = require('neotest.lib')
26+
2727
---@param context RunContext The run context.
2828
---@param out_path string Path to an hspec test results output file.
2929
---@param tree neotest.Tree The test tree at the position that was run.

lua/neotest-haskell/runner.lua

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
local ok, nio = pcall(require, 'nio')
2-
if not ok then
3-
nio = require('neotest.async')
4-
end
5-
local lib = require('neotest.lib')
6-
local Path = require('plenary.path')
7-
local logger = require('neotest.logging')
8-
local treesitter_hs = require('neotest-haskell.treesitter')
9-
101
local runner = {}
112

123
---Check if the given directory contains a file matching a list of patterns.
134
---@param directory string The directory to check for.
145
---@param patterns string[] The patterns to check for.
156
---@return boolean
167
local function directory_contains_file_matching(directory, patterns)
8+
local Path = require('plenary.path')
179
for _, pattern in ipairs(patterns) do
1810
for _, file in ipairs(vim.fn.glob(Path:new(directory, pattern).filename, true, true)) do
1911
if Path:new(file):exists() then
@@ -28,8 +20,13 @@ end
2820
---If a *.cabal is present, this is *.
2921
---Otherwise, we assume the package name is the same as the directory name.
3022
---@param package_root string The package root directory.
31-
---@return string package_name The assumed package name.
23+
---@return string | nil package_name The assumed package name.
3224
local function get_package_name(package_root)
25+
local ok, nio = pcall(require, 'nio')
26+
if not ok then
27+
nio = require('neotest.async')
28+
end
29+
local Path = require('plenary.path')
3330
---@diagnostic disable-next-line -- nio.fn is private?
3431
for _, package_file_path in ipairs(nio.fn.glob(Path:new(package_root, '*.cabal').filename, true, true)) do
3532
local package_file_name = package_file_path and vim.fn.fnamemodify(package_file_path, ':t')
@@ -71,6 +68,7 @@ end
7168
---@return boolean
7269
---@async
7370
local function has_module(test_file_content, qualified_modules)
71+
local treesitter_hs = require('neotest-haskell.treesitter')
7472
for _, qualified_module in pairs(qualified_modules) do
7573
local modules = {}
7674
for module in qualified_module:gmatch('([^%.]+)') do
@@ -105,6 +103,8 @@ end
105103
---@return TestFrameworkHandler handler
106104
---@async
107105
function runner.select_framework(test_file_path, frameworks)
106+
local lib = require('neotest.lib')
107+
local logger = require('neotest.logging')
108108
local content = lib.files.read(test_file_path)
109109
---@type FrameworkSpec[]
110110
local framework_specs = {}
@@ -138,6 +138,8 @@ end
138138
---@param build_tools build_tool[] List of build tools to choose from.
139139
---@return fun(neotest.Tree):neotest.RunSpec mk_command A function that builds the runner command using the selected build tool for a test tree.
140140
function runner.select_build_tool(handler, test_file_path, build_tools)
141+
local lib = require('neotest.lib')
142+
local logger = require('neotest.logging')
141143
-- A package always has a *.cabal file (or in rare cases just a package.yaml file).
142144
local package_root = lib.files.match_root_pattern('*.cabal', 'package.yaml')(test_file_path)
143145
if not package_root then
@@ -173,7 +175,9 @@ function runner.select_build_tool(handler, test_file_path, build_tools)
173175
local command = { selected_build_tool, 'test' }
174176
if is_multi_package_project then
175177
local package_name = get_package_name(package_root)
176-
table.insert(command, package_name)
178+
if package_name then
179+
table.insert(command, package_name)
180+
end
177181
end
178182
return function(pos)
179183
local test_opts = pos and get_test_opts(pos)

lua/neotest-haskell/sydtest.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local hspec = require('neotest-haskell.hspec')
22
local treesitter = require('neotest-haskell.treesitter')
3-
local util = require('neotest-haskell.util')
43
local position = require('neotest-haskell.position')
54
local results = require('neotest-haskell.results')
65

@@ -124,6 +123,7 @@ end
124123
---@param test_name string The name of the test.
125124
---@return neotest.Error[] hspec_errors The errors.
126125
local function parse_errors(raw_lines, test_name)
126+
local util = require('neotest-haskell.util')
127127
local failures_found = false
128128
local err_msg_pos_found = false
129129
local error_message = nil

lua/neotest-haskell/tasty.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local util = require('neotest-haskell.util')
33
local position = require('neotest-haskell.position')
44
local results = require('neotest-haskell.results')
55
local hspec = require('neotest-haskell.hspec')
6-
local logger = require('neotest.logging')
76

87
local tasty = {}
98

@@ -36,6 +35,7 @@ local function parse_top_level_tasty_nodes(pos)
3635
end
3736
local function concat_subpatterns(subpatterns)
3837
if not subpatterns or #subpatterns == 0 then
38+
local logger = require('neotest.logging')
3939
logger.error('Could not detect tasty top level nodes.')
4040
return nil
4141
end
@@ -51,6 +51,7 @@ end
5151
local function parse_tasty_tree(pos)
5252
local function format_result(result)
5353
if not result or result == '' then
54+
local logger = require('neotest.logging')
5455
logger.error('Could not detect any tasty patterns.')
5556
return nil
5657
end

lua/neotest-haskell/treesitter.lua

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
local lib = require('neotest.lib')
2-
3-
local ok, nio = pcall(require, 'nio')
4-
if not ok then
5-
---@diagnostic disable-next-line: undefined-field
6-
nio = require('neotest.async').util
7-
end
8-
91
local treesitter = {}
102

113
---@class FileRef
@@ -19,6 +11,7 @@ local treesitter = {}
1911
---@param file_ref FileRef
2012
---@return FileContentRef content_ref
2113
local function to_file_content_ref(file_ref)
14+
local lib = require('neotest.lib')
2215
return {
2316
content = lib.files.read(file_ref.file),
2417
}
@@ -34,13 +27,19 @@ function treesitter.iter_ts_matches(query, source)
3427
source = to_file_content_ref(source)
3528
end
3629
local lang = require('nvim-treesitter.parsers').ft_to_lang('haskell')
30+
local ok, nio = pcall(require, 'nio')
31+
if not ok then
32+
---@diagnostic disable-next-line: undefined-field
33+
nio = require('neotest.async').util
34+
end
3735
nio.scheduler()
3836
local lang_tree = vim.treesitter.get_string_parser(
3937
source.content,
4038
lang,
4139
-- Prevent neovim from trying to read the query from injection files
4240
{ injections = { [lang] = '' } }
4341
)
42+
local lib = require('neotest.lib')
4443
---@type userdata
4544
local root = lib.treesitter.fast_parse(lang_tree):root()
4645
local normalised_query = lib.treesitter.normalise_query(lang, query)

0 commit comments

Comments
 (0)