Skip to content

Instantly share code, notes, and snippets.

@tuliopaim
Created November 6, 2023 23:00
Show Gist options
  • Select an option

  • Save tuliopaim/0a52d4ec1e1556d0c62b9800810817be to your computer and use it in GitHub Desktop.

Select an option

Save tuliopaim/0a52d4ec1e1556d0c62b9800810817be to your computer and use it in GitHub Desktop.
lua functions to run dotnet test for the whole file and for the test under the cursor
local M = {}
M.get_class_name = function()
return vim.fn.expand('%:t:r')
end
function M.get_node_text(node, bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local start_row, start_col, _, end_col = node:range()
local line = vim.api.nvim_buf_get_lines(bufnr, start_row, start_row + 1, false)[1]
return line and string.sub(line, start_col + 1, end_col) or ""
end
function M.get_node_at_cursor()
local cursor = vim.api.nvim_win_get_cursor(0)
local line = cursor[1] - 1
local col = cursor[2]
local parser = vim.treesitter.get_parser()
if not parser then
return
end
local lang_tree = parser:language_for_range { line, col, line, col }
for _, tree in ipairs(lang_tree:trees()) do
local root = tree:root()
local node = root:named_descendant_for_range(line, col, line, col)
if node then
return node
end
end
end
M.get_test_name = function()
local node = M.get_node_at_cursor()
if not node then
print ('No node found')
return
end
return M.get_node_text(node)
end
M.test_at_cursor = function()
local class_name = M.get_class_name()
local test_name = M.get_test_name()
if not test_name then
print ('No test name found')
return
end
local cmd = string.format("dotnet test --filter FullyQualifiedName~%s.%s", class_name, test_name)
local output = vim.fn.system(cmd)
M.show_test_results(output)
end
M.test_class = function()
local class_name = M.get_class_name()
local cmd = string.format("dotnet test --filter FullyQualifiedName~%s", class_name)
local output = vim.fn.system(cmd)
M.show_test_results(output)
end
M.show_test_results = function(output)
-- Create a new horizontal split and set it to 10 lines high.
vim.cmd('botright 20new')
-- Get the buffer number of the current buffer (which is the new split).
local bufnr = vim.api.nvim_get_current_buf()
-- Set the buffer to be a scratch buffer.
vim.api.nvim_buf_set_option(bufnr, 'buftype', 'nofile')
vim.api.nvim_buf_set_option(bufnr, 'swapfile', false)
vim.api.nvim_buf_set_option(bufnr, 'bufhidden', 'wipe')
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'dotnet-test-output')
-- Insert the output into the buffer.
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(output, '\n'))
-- Get the number of lines in the buffer.
local lines_count = vim.api.nvim_buf_line_count(bufnr)
-- Go to the end of the buffer.
vim.api.nvim_win_set_cursor(0, {lines_count, 0})
end
vim.cmd [[
command! -nargs=? TestAtCursor lua require'tuliopaim.dotnet-test'.test_at_cursor(<q-args>)
]]
vim.cmd [[
command! -nargs=? TestClass lua require'tuliopaim.dotnet-test'.test_class(<q-args>)
]]
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment