Skip to content

Commit

Permalink
feat(summary): target positions
Browse files Browse the repository at this point in the history
See #63
  • Loading branch information
rcarriga committed Jul 10, 2022
1 parent eb8b29d commit aff1eac
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
14 changes: 13 additions & 1 deletion doc/neotest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ neotest.setup({user_config}) *neotest.setup()*
running = "NeotestRunning",
select_win = "NeotestWinSelect",
skipped = "NeotestSkipped",
target = "NeotestTarget",
test = "NeotestTest"
},
icons = {
Expand Down Expand Up @@ -118,6 +119,7 @@ neotest.setup({user_config}) *neotest.setup()*
mappings = {
attach = "a",
clear_marked = "M",
clear_target = "T",
expand = { "<CR>", "<2-LeftMouse>" },
expand_all = "e",
jumpto = "i",
Expand All @@ -126,7 +128,8 @@ neotest.setup({user_config}) *neotest.setup()*
run = "r",
run_marked = "R",
short = "O",
stop = "u"
stop = "u",
target = "t"
}
}
}
Expand Down Expand Up @@ -492,6 +495,15 @@ neotest.summary.clear_marked({args}) *neotest.summary.clear_marked()*
{adapter} (string) Adapter ID, if not given all adapters are used


neotest.summary.target({adapter_id}, {position_id}) *neotest.summary.target()*
Set the target for an adapter tree


Parameters: ~
{adapter_id} (string)
{position_id} (string|nil) Position ID to target, nil to reset target



================================================================================
*neotest.jump*
Expand Down
4 changes: 4 additions & 0 deletions lua/neotest/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ vim.cmd([[
hi default NeotestAdapterName ctermfg=Red guifg=#F70067
hi default NeotestWinSelect ctermfg=Cyan guifg=#00f1f5 gui=bold
hi default NeotestMarked ctermfg=Brown guifg=#F79000 gui=bold
hi default NeotestTarget ctermfg=Red guifg=#F70067
]])

---@class neotest.Config
Expand Down Expand Up @@ -114,6 +115,7 @@ local default_config = {
adapter_name = "NeotestAdapterName",
select_win = "NeotestWinSelect",
marked = "NeotestMarked",
target = "NeotestTarget",
},
floating = {
border = "rounded",
Expand Down Expand Up @@ -143,6 +145,8 @@ local default_config = {
mark = "m",
run_marked = "R",
clear_marked = "M",
target = "t",
clear_target = "T",
},
},
output = {
Expand Down
25 changes: 24 additions & 1 deletion lua/neotest/consumers/summary/component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ local lib = require("neotest.lib")
---@field child_components table<number, SummaryComponent>
---@field marked table<string, boolean>
---@field adapter_id integer
---@field target string?
local SummaryComponent = {}

function SummaryComponent:new(client, adapter_id)
local elem = {
client = client,
target = nil,
expanded_positions = {},
adapter_id = adapter_id,
marked = {},
Expand All @@ -37,6 +39,20 @@ end
---@param canvas Canvas
---@param tree neotest.Tree
function SummaryComponent:render(canvas, tree, expanded, focused, indent)
if self.target then
tree = tree:get_key(self.target)
if not tree then
return
end
canvas:add_mapping("clear_target", function()
require("neotest").summary.target(self.adapter_id)
end)
canvas:write(tree:data().name .. "\n", { group = config.highlights.target })
end
self:_render(canvas, tree, expanded, focused, indent)
end

function SummaryComponent:_render(canvas, tree, expanded, focused, indent)
indent = indent or ""
local children = tree:children()
local neotest = require("neotest")
Expand Down Expand Up @@ -95,7 +111,14 @@ function SummaryComponent:render(canvas, tree, expanded, focused, indent)
neotest.summary.render(positions)
end)
)

canvas:add_mapping("target", function()
neotest.summary.target(self.adapter_id, position.id)
end)
end
canvas:add_mapping("clear_target", function()
neotest.summary.target(self.adapter_id)
end)
if position.type ~= "dir" then
canvas:add_mapping("jumpto", function()
local buf = vim.fn.bufadd(position.path)
Expand Down Expand Up @@ -164,7 +187,7 @@ function SummaryComponent:render(canvas, tree, expanded, focused, indent)
canvas:write(position.name .. "\n", { group = name_groups })

if self.expanded_positions[position.id] then
self:render(canvas, node, expanded, focused, chid_indent)
self:_render(canvas, node, expanded, focused, chid_indent)
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lua/neotest/consumers/summary/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local lib = require("neotest.lib")
local logger = require("neotest.logging")
local config = require("neotest.config")
local Canvas = require("neotest.consumers.summary.canvas")
Expand Down Expand Up @@ -248,6 +249,18 @@ function neotest.summary.clear_marked(args)
render()
end

---Set the target for an adapter tree
---@param adapter_id string
---@param position_id string | nil: Position ID to target, nil to reset target
function neotest.summary.target(adapter_id, position_id)
local component = components[adapter_id]
if not component then
lib.notify(("No tree found for adapter %s"):format(adapter_id))
end
component.target = position_id
render()
end

function neotest.summary.expand(pos_id, recursive)
async.run(function()
expand(pos_id, recursive)
Expand Down

0 comments on commit aff1eac

Please sign in to comment.