Skip to content

Commit eb21b0f

Browse files
committed
feat: lua autocmds
1 parent e180388 commit eb21b0f

File tree

7 files changed

+64
-110
lines changed

7 files changed

+64
-110
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
neovim/
22
plenary.nvim/
3+
doc/tags
34
Session.vim

doc/tags

-25
This file was deleted.

lua/neotest/client/init.lua

+49-11
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,56 @@ function NeotestClient:_start()
503503
logger.info("Initialising client")
504504
local start = async.fn.localtime()
505505
self._started = true
506-
vim.schedule(function()
507-
vim.cmd([[
508-
augroup NeotestClient
509-
au!
510-
autocmd BufAdd,BufWritePost * lua require("neotest")._update_positions(vim.fn.expand("<afile>:p", { refresh = true}))
511-
autocmd DirChanged * lua require("neotest")._dir_changed()
512-
autocmd BufAdd,BufDelete * lua require("neotest")._update_files(vim.fn.expand("<afile>:p:h"))
513-
autocmd BufEnter * lua require("neotest")._focus_file(vim.fn.expand("<afile>:p"))
514-
autocmd CursorHold,BufEnter * lua require("neotest")._focus_position(vim.fn.expand("<afile>:p"), vim.fn.line("."))
515-
augroup END
516-
]])
506+
local augroup = async.api.nvim_create_augroup("NeotestClient", { clear = true })
507+
local function async_autocmd(event, callback)
508+
async.api.nvim_create_autocmd(event, {
509+
callback = callback,
510+
group = augroup,
511+
})
512+
end
513+
514+
async_autocmd("BufAdd,BufWritePost", function()
515+
local file_path = vim.fn.expand("<afile>:p")
516+
async.run(function()
517+
local adapter_id = self:get_adapter(file_path)
518+
if not self:get_position(file_path, { adapter = adapter_id }) then
519+
if not adapter_id then
520+
return
521+
end
522+
self:_update_positions(lib.files.parent(file_path), { adapter = adapter_id })
523+
end
524+
self:_update_positions(file_path, { adapter = adapter_id })
525+
end)
526+
end)
527+
528+
async_autocmd("DirChanged", function()
529+
local dir = vim.fn.getcwd()
530+
async.run(function()
531+
self:_update_adapters(dir)
532+
end)
533+
end)
534+
535+
async_autocmd("BufAdd,BufDelete", function()
536+
local updated_dir = vim.fn.expand("<afile>:p:h")
537+
async.run(function()
538+
self:_update_positions(updated_dir)
539+
end)
517540
end)
541+
542+
async_autocmd("BufEnter", function()
543+
local path = vim.fn.expand("<afile>:p")
544+
async.run(function()
545+
self:_set_focused_file(path)
546+
end)
547+
end)
548+
549+
async_autocmd("CursorHold,BufEnter", function()
550+
local path, line = vim.fn.expand("<afile>:p"), vim.fn.line(".")
551+
async.run(function()
552+
self:_set_focused_position(path, line - 1)
553+
end)
554+
end)
555+
518556
self:_update_adapters(async.fn.getcwd())
519557
local end_time = async.fn.localtime()
520558
logger.info("Initialisation finished in", end_time - start, "seconds")

lua/neotest/consumers/status.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local async = require("neotest.async")
2-
local consumer_name = "neotest-status"
2+
local sign_group = "neotest-status"
33
local config = require("neotest.config")
44

55
---@param client neotest.Client
@@ -24,7 +24,7 @@ local function init(client)
2424
local function render_files(adapter_id, files)
2525
for _, file_path in pairs(files) do
2626
local results = client:get_results(adapter_id)
27-
async.fn.sign_unplace(consumer_name, { buffer = file_path })
27+
async.fn.sign_unplace(sign_group, { buffer = file_path })
2828
local tree = client:get_position(file_path, { adapter = adapter_id })
2929
for _, pos in tree:iter() do
3030
if pos.type ~= "file" then
@@ -38,7 +38,7 @@ local function init(client)
3838
if icon then
3939
async.fn.sign_place(
4040
0,
41-
consumer_name,
41+
sign_group,
4242
icon,
4343
pos.path,
4444
{ lnum = pos.range[1] + 1, priority = 1000 }
@@ -60,7 +60,7 @@ local function init(client)
6060
local files = {}
6161
for _, pos_id in pairs(position_ids) do
6262
local node = client:get_position(pos_id, { adapter = adapter_id })
63-
if node then
63+
if node and node:data().type ~= "dir" then
6464
local file = node:data().path
6565
files[file] = files[file] or {}
6666
table.insert(files[file], pos_id)
@@ -73,7 +73,7 @@ local function init(client)
7373
local files = {}
7474
for pos_id, _ in pairs(results) do
7575
local node = client:get_position(pos_id, { adapter = adapter_id })
76-
if node then
76+
if node and node:data().type ~= "dir" then
7777
local file = node:data().path
7878
files[file] = true
7979
end

lua/neotest/init.lua

-39
Original file line numberDiff line numberDiff line change
@@ -178,45 +178,6 @@ function neotest.attach(args)
178178
end)
179179
end
180180

181-
function neotest._update_positions(file_path)
182-
pcall(function()
183-
async.run(function()
184-
local adapter_id = client:get_adapter(file_path)
185-
if not client:get_position(file_path, { adapter = adapter_id }) then
186-
if not adapter_id then
187-
return
188-
end
189-
client:_update_positions(lib.files.parent(file_path), { adapter = adapter_id })
190-
end
191-
client:_update_positions(file_path, { adapter = adapter_id })
192-
end)
193-
end)
194-
end
195-
196-
function neotest._update_files(path)
197-
async.run(function()
198-
client:_update_positions(path)
199-
end)
200-
end
201-
202-
function neotest._dir_changed()
203-
async.run(function()
204-
client:_update_adapters(async.fn.getcwd())
205-
end)
206-
end
207-
208-
function neotest._focus_file(path)
209-
async.run(function()
210-
client:_set_focused_file(path)
211-
end)
212-
end
213-
214-
function neotest._focus_position(path, line)
215-
async.run(function()
216-
client:_set_focused_position(path, line - 1)
217-
end)
218-
end
219-
220181
setmetatable(neotest, {
221182
__index = function(_, key)
222183
return consumers[key]

lua/neotest/lib/ui/float.lua

+6-27
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ local config = require("neotest.config")
44

55
local M = {}
66

7-
---@type table<integer, neotest.Float>
8-
local windows = {}
9-
107
---@class neotest.Float
118
local Float = { win_id = nil, listeners = { close = {} }, position = {} }
129

@@ -103,34 +100,16 @@ function M.open(settings)
103100

104101
---@type neotest.Float
105102
local win = Float:new(win_id, position)
106-
win:listen("close", function()
107-
windows[win] = nil
108-
end)
109-
windows[win_id] = win
110103

111104
if opts.auto_close ~= false then
112-
vim.cmd(
113-
"au WinEnter,CursorMoved * ++once lua require('neotest.lib.ui.float')._auto_close("
114-
.. win_id
115-
.. ")"
116-
)
105+
local function auto_close()
106+
if not win:close(false) then
107+
vim.api.nvim_create_autocmd("WinEnter,CursorMoved", { callback = auto_close, once = true })
108+
end
109+
end
110+
vim.api.nvim_create_autocmd("WinEnter,CursorMoved", { callback = auto_close, once = true })
117111
end
118112
return win
119113
end
120114

121-
function M._auto_close(win)
122-
if not M.close(win, false) then
123-
vim.cmd(
124-
"au WinEnter,CursorMoved * ++once lua require('neotest.lib.ui.float')._auto_close("
125-
.. win
126-
.. ")"
127-
)
128-
end
129-
end
130-
131-
function M.close(win, force)
132-
local win = windows[win]
133-
return win:close(force)
134-
end
135-
136115
return M

tests/unit/client/init_spec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A = function(...)
1010
end
1111

1212
describe("neotest client", function()
13-
local mock_adapter, mock_adapters, mock_strategy, client
13+
local mock_adapter, mock_strategy, client
1414
local dir = async.fn.getcwd()
1515
local files
1616
before_each(function()
@@ -58,10 +58,10 @@ describe("neotest client", function()
5858
return pos.id
5959
end)
6060
end,
61-
build_spec = function(args)
61+
build_spec = function()
6262
return {}
6363
end,
64-
results = function(spec, _, tree)
64+
results = function(_, _, tree)
6565
local results = {}
6666
for _, pos in tree:iter() do
6767
if pos.type == "file" or pos.type == "test" then

0 commit comments

Comments
 (0)