Skip to content

Commit

Permalink
feat: lua autocmds
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed Apr 16, 2022
1 parent e180388 commit eb21b0f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
neovim/
plenary.nvim/
doc/tags
Session.vim
25 changes: 0 additions & 25 deletions doc/tags

This file was deleted.

60 changes: 49 additions & 11 deletions lua/neotest/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -503,18 +503,56 @@ function NeotestClient:_start()
logger.info("Initialising client")
local start = async.fn.localtime()
self._started = true
vim.schedule(function()
vim.cmd([[
augroup NeotestClient
au!
autocmd BufAdd,BufWritePost * lua require("neotest")._update_positions(vim.fn.expand("<afile>:p", { refresh = true}))
autocmd DirChanged * lua require("neotest")._dir_changed()
autocmd BufAdd,BufDelete * lua require("neotest")._update_files(vim.fn.expand("<afile>:p:h"))
autocmd BufEnter * lua require("neotest")._focus_file(vim.fn.expand("<afile>:p"))
autocmd CursorHold,BufEnter * lua require("neotest")._focus_position(vim.fn.expand("<afile>:p"), vim.fn.line("."))
augroup END
]])
local augroup = async.api.nvim_create_augroup("NeotestClient", { clear = true })
local function async_autocmd(event, callback)
async.api.nvim_create_autocmd(event, {
callback = callback,
group = augroup,
})
end

async_autocmd("BufAdd,BufWritePost", function()
local file_path = vim.fn.expand("<afile>:p")
async.run(function()
local adapter_id = self:get_adapter(file_path)
if not self:get_position(file_path, { adapter = adapter_id }) then
if not adapter_id then
return
end
self:_update_positions(lib.files.parent(file_path), { adapter = adapter_id })
end
self:_update_positions(file_path, { adapter = adapter_id })
end)
end)

async_autocmd("DirChanged", function()
local dir = vim.fn.getcwd()
async.run(function()
self:_update_adapters(dir)
end)
end)

async_autocmd("BufAdd,BufDelete", function()
local updated_dir = vim.fn.expand("<afile>:p:h")
async.run(function()
self:_update_positions(updated_dir)
end)
end)

async_autocmd("BufEnter", function()
local path = vim.fn.expand("<afile>:p")
async.run(function()
self:_set_focused_file(path)
end)
end)

async_autocmd("CursorHold,BufEnter", function()
local path, line = vim.fn.expand("<afile>:p"), vim.fn.line(".")
async.run(function()
self:_set_focused_position(path, line - 1)
end)
end)

self:_update_adapters(async.fn.getcwd())
local end_time = async.fn.localtime()
logger.info("Initialisation finished in", end_time - start, "seconds")
Expand Down
10 changes: 5 additions & 5 deletions lua/neotest/consumers/status.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local async = require("neotest.async")
local consumer_name = "neotest-status"
local sign_group = "neotest-status"
local config = require("neotest.config")

---@param client neotest.Client
Expand All @@ -24,7 +24,7 @@ local function init(client)
local function render_files(adapter_id, files)
for _, file_path in pairs(files) do
local results = client:get_results(adapter_id)
async.fn.sign_unplace(consumer_name, { buffer = file_path })
async.fn.sign_unplace(sign_group, { buffer = file_path })
local tree = client:get_position(file_path, { adapter = adapter_id })
for _, pos in tree:iter() do
if pos.type ~= "file" then
Expand All @@ -38,7 +38,7 @@ local function init(client)
if icon then
async.fn.sign_place(
0,
consumer_name,
sign_group,
icon,
pos.path,
{ lnum = pos.range[1] + 1, priority = 1000 }
Expand All @@ -60,7 +60,7 @@ local function init(client)
local files = {}
for _, pos_id in pairs(position_ids) do
local node = client:get_position(pos_id, { adapter = adapter_id })
if node then
if node and node:data().type ~= "dir" then
local file = node:data().path
files[file] = files[file] or {}
table.insert(files[file], pos_id)
Expand All @@ -73,7 +73,7 @@ local function init(client)
local files = {}
for pos_id, _ in pairs(results) do
local node = client:get_position(pos_id, { adapter = adapter_id })
if node then
if node and node:data().type ~= "dir" then
local file = node:data().path
files[file] = true
end
Expand Down
39 changes: 0 additions & 39 deletions lua/neotest/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,6 @@ function neotest.attach(args)
end)
end

function neotest._update_positions(file_path)
pcall(function()
async.run(function()
local adapter_id = client:get_adapter(file_path)
if not client:get_position(file_path, { adapter = adapter_id }) then
if not adapter_id then
return
end
client:_update_positions(lib.files.parent(file_path), { adapter = adapter_id })
end
client:_update_positions(file_path, { adapter = adapter_id })
end)
end)
end

function neotest._update_files(path)
async.run(function()
client:_update_positions(path)
end)
end

function neotest._dir_changed()
async.run(function()
client:_update_adapters(async.fn.getcwd())
end)
end

function neotest._focus_file(path)
async.run(function()
client:_set_focused_file(path)
end)
end

function neotest._focus_position(path, line)
async.run(function()
client:_set_focused_position(path, line - 1)
end)
end

setmetatable(neotest, {
__index = function(_, key)
return consumers[key]
Expand Down
33 changes: 6 additions & 27 deletions lua/neotest/lib/ui/float.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ local config = require("neotest.config")

local M = {}

---@type table<integer, neotest.Float>
local windows = {}

---@class neotest.Float
local Float = { win_id = nil, listeners = { close = {} }, position = {} }

Expand Down Expand Up @@ -103,34 +100,16 @@ function M.open(settings)

---@type neotest.Float
local win = Float:new(win_id, position)
win:listen("close", function()
windows[win] = nil
end)
windows[win_id] = win

if opts.auto_close ~= false then
vim.cmd(
"au WinEnter,CursorMoved * ++once lua require('neotest.lib.ui.float')._auto_close("
.. win_id
.. ")"
)
local function auto_close()
if not win:close(false) then
vim.api.nvim_create_autocmd("WinEnter,CursorMoved", { callback = auto_close, once = true })
end
end
vim.api.nvim_create_autocmd("WinEnter,CursorMoved", { callback = auto_close, once = true })
end
return win
end

function M._auto_close(win)
if not M.close(win, false) then
vim.cmd(
"au WinEnter,CursorMoved * ++once lua require('neotest.lib.ui.float')._auto_close("
.. win
.. ")"
)
end
end

function M.close(win, force)
local win = windows[win]
return win:close(force)
end

return M
6 changes: 3 additions & 3 deletions tests/unit/client/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A = function(...)
end

describe("neotest client", function()
local mock_adapter, mock_adapters, mock_strategy, client
local mock_adapter, mock_strategy, client
local dir = async.fn.getcwd()
local files
before_each(function()
Expand Down Expand Up @@ -58,10 +58,10 @@ describe("neotest client", function()
return pos.id
end)
end,
build_spec = function(args)
build_spec = function()
return {}
end,
results = function(spec, _, tree)
results = function(_, _, tree)
local results = {}
for _, pos in tree:iter() do
if pos.type == "file" or pos.type == "test" then
Expand Down

0 comments on commit eb21b0f

Please sign in to comment.