From 797f1b0ab34376a905d6debdf8905d95f11a8051 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 13 Jul 2024 16:58:04 +0200 Subject: [PATCH] feat: re-generate lookup --- README.md | 18 ++++++----- .../features/testify/lookup.lua | 6 ++++ lua/neotest-golang/init.lua | 12 ++++--- lua/neotest-golang/lib/debounce.lua | 32 +++++++++++++++++++ lua/neotest-golang/lib/init.lua | 1 + lua/neotest-golang/options.lua | 2 ++ 6 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 lua/neotest-golang/lib/debounce.lua diff --git a/README.md b/README.md index a6e893bb..7b4e70df 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,16 @@ You can run `:checkhealth neotest-golang` to review common issues. ## ⚙️ Configuration -| Argument | Default value | Description | -| ------------------------ | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `go_test_args` | `{ "-v", "-race", "-count=1" }` | Arguments to pass into `go test`. | -| `dap_go_enabled` | `false` | Leverage [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go) for debugging tests. | -| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. | -| `testify_enabled` | `false` | Enable support for [testify](https://github.com/stretchr/testify) suites. See [here](https://github.com/fredrikaverpil/neotest-golang#testify-suites) for more info. | -| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. | -| `warn_test_not_executed` | `true` | Warn if test was not executed. | +| Argument | Default value | Description | +| ------------------------- | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `go_test_args` | `{ "-v", "-race", "-count=1" }` | Arguments to pass into `go test`. | +| `dap_go_enabled` | `false` | Leverage [leoluz/nvim-dap-go](https://github.com/leoluz/nvim-dap-go) for debugging tests. | +| `dap_go_opts` | `{}` | Options to pass into `require("dap-go").setup()`. | +| `testify_enabled` | `false` | Enable support for [testify](https://github.com/stretchr/testify) suites. See [here](https://github.com/fredrikaverpil/neotest-golang#testify-suites) for more info. | +| `testify_generate_lookup` | `true` | Automatically re-generate testify lookup when recalculating Neotest tree. | +| `testify_debounce_delay` | `500` | The time to debounce/delay lookup re-generation (in milliseconds). | +| `warn_test_name_dupes` | `true` | Warn about duplicate test names within the same Go package. | +| `warn_test_not_executed` | `true` | Warn if test was not executed. | ### Example configuration: custom `go test` arguments diff --git a/lua/neotest-golang/features/testify/lookup.lua b/lua/neotest-golang/features/testify/lookup.lua index 06aaa0b8..1b50cc87 100644 --- a/lua/neotest-golang/features/testify/lookup.lua +++ b/lua/neotest-golang/features/testify/lookup.lua @@ -1,5 +1,6 @@ --- Lookup table for renaming Neotest namespaces (receiver type to testify suite function). +local options = require("neotest-golang.options") local lib = require("neotest-golang.lib") local query = require("neotest-golang.features.testify.query") @@ -63,6 +64,10 @@ M.query = [[ (identifier)))))) ]] +M.debounced_generate = lib.debounce.debounce(function() + M.generate() +end, options.get().testify_debunce_delay) + --- The lookup table. --- @type table local lookup_table = {} @@ -79,6 +84,7 @@ end --- Generate the lookup table for testify suites. --- @return table The generated lookup table function M.generate() + vim.notify("Generating testify lookup...", vim.log.levels.INFO) local cwd = vim.fn.getcwd() local filepaths = lib.find.go_test_filepaths(cwd) local lookup = {} diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index bc1bf9d1..c0401752 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -14,11 +14,7 @@ local M = {} --- @field name string M.Adapter = { name = "neotest-golang", - init = function() - if options.get().testify_enabled == true then - testify.lookup.generate() - end - end, + init = function() end, } --- Find the project root directory given a current directory to work from. @@ -60,6 +56,12 @@ end --- @param file_path string Absolute file path --- @return neotest.Tree | nil function M.Adapter.discover_positions(file_path) + if + options.get().testify_enabled and options.get().testify_generate_lookup + then + testify.lookup.debounced_generate() + end + return query.detect_tests(file_path) end diff --git a/lua/neotest-golang/lib/debounce.lua b/lua/neotest-golang/lib/debounce.lua new file mode 100644 index 00000000..5ab5f246 --- /dev/null +++ b/lua/neotest-golang/lib/debounce.lua @@ -0,0 +1,32 @@ +local M = {} + +-- Table to store timers for each debounced function +local timers = {} + +-- Debounce function +-- @param func The function to debounce +-- @param delay The delay in milliseconds +-- @return The debounced function +function M.debounce(func, delay) + return function(...) + local args = { ... } + local key = tostring(func) + + if timers[key] then + vim.loop.timer_stop(timers[key]) + end + + timers[key] = vim.loop.new_timer() + timers[key]:start( + delay, + 0, + vim.schedule_wrap(function() + func(unpack(args)) + timers[key]:close() + timers[key] = nil + end) + ) + end +end + +return M diff --git a/lua/neotest-golang/lib/init.lua b/lua/neotest-golang/lib/init.lua index 7ffb8f4f..29d724e6 100644 --- a/lua/neotest-golang/lib/init.lua +++ b/lua/neotest-golang/lib/init.lua @@ -2,6 +2,7 @@ local M = {} M.convert = require("neotest-golang.lib.convert") M.cmd = require("neotest-golang.lib.cmd") +M.debounce = require("neotest-golang.lib.debounce") M.find = require("neotest-golang.lib.find") M.json = require("neotest-golang.lib.json") diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index d8ce3585..561f0a01 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -9,6 +9,8 @@ local opts = { dap_go_enabled = false, dap_go_opts = {}, testify_enabled = false, + testify_generate_lookup = true, + testify_debunce_delay = 500, warn_test_name_dupes = true, warn_test_not_executed = true,