Skip to content

Commit

Permalink
update for treesitter get_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-x committed Oct 21, 2024
1 parent 51676b4 commit 12ab6ac
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 74 deletions.
38 changes: 23 additions & 15 deletions lua/go/gotest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ local function get_test_filebufnr()
fn = vfn.fnamemodify(fn, ':p') -- expand to full path
-- check if file exists
if vfn.filereadable(fn) == 0 then
vim.notify('no test file found for ' .. fn, vim.log.levels.WARN)
return 0, 'no test file'
end
local uri = vim.uri_from_fname(fn)
Expand Down Expand Up @@ -501,19 +502,21 @@ end
M.test_func = function(...)
local args = { ... } or {}
log(args)

local ns = M.get_test_func_name()
if empty(ns) then
return M.select_tests()
end

if not vim.api.nvim_get_runtime_file('parser' .. sep .. 'go.so', false)[1] then
local bufnr = get_test_filebufnr()
local p = vim.treesitter.get_parser(bufnr, 'go')
if not p then
-- require('nvim-treesitter.install').commands.TSInstallSync['run!']('go')
vim.notify(
'go treesitter parser not found, please Run `:TSInstallSync go`',
'go treesitter parser not found for file '
.. vim.fn.bufname()
.. ' please Run `:TSInstallSync go` ',
vim.log.levels.WARN
)
end
local ns = M.get_test_func_name()
if empty(ns) then
return M.select_tests()
end
return run_tests_with_ts_node(args, ns)
end

Expand Down Expand Up @@ -666,11 +669,16 @@ end
-- https://github.com/rentziass/dotfiles/blob/master/vim/.config/nvim/lua/rentziass/lsp/go_tests.lua
M.run_file = function()
local bufnr = vim.api.nvim_get_current_buf()
local tree = vim.treesitter.get_parser(bufnr, 'go'):parse()[1]
local parser = vim.treesitter.get_parser(bufnr, 'go')
if not parser then
vim.notify('go treesitter parser not found for ' .. vim.fn.bufname(), vim.log.levels.WARN)
return log('no ts parser found')
end
local tree = parser:parse()[1]
local query = parse('go', require('go.ts.textobjects').query_test_func)

local test_names = {}
local get_node_text=vim.treesitter.get_node_text
local get_node_text = vim.treesitter.get_node_text
for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do
local name = query.captures[id] -- name of the capture in the query
if name == 'test_name' then
Expand All @@ -690,17 +698,17 @@ M.get_testfunc = function()
local bufnr = get_test_filebufnr()

-- Note: the buffer may not be loaded yet
local ok, parser = vim.treesitter.get_parser(bufnr, 'go')
if not ok or not parser then
local parser = vim.treesitter.get_parser(bufnr, 'go')
if not parser then
vim.notify('go treesitter parser not found for ' .. vim.fn.bufname(), vim.log.levels.WARN)
return log('no parser found')
end
local tree = parser:parse()
tree = tree[1]
local tree = parser:parse()[1]
local query = parse('go', require('go.ts.go').query_test_func)

local test_names = {}

local get_node_text=vim.treesitter.get_node_text
local get_node_text = vim.treesitter.get_node_text
for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do
local name = query.captures[id] -- name of the capture in the query
log(node)
Expand Down
5 changes: 3 additions & 2 deletions lua/go/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ local run = function(cmd, opts, uvopts)
end)
end
if code ~= 0 then
log('failed to run', code, output_buf)
log('failed to run', code, output_buf, output_stderr)
vim.schedule(function()
util.error( cmd_str .. ' failed exit code ' .. tostring(code or 0) .. (output_buf or ''))
util.error( cmd_str .. ' failed exit with code: ' .. tostring(code or 0) .. (output_buf or '') ..
(output_stderr or ''))
end)
end

Expand Down
4 changes: 2 additions & 2 deletions lua/go/ts/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ M.get_tbl_testcase_node_name = function(bufnr)
local bufn = bufnr or vim.api.nvim_get_current_buf()
local parser = vim.treesitter.get_parser(bufn, 'go')
if not parser then
return warn('treesitter parser not found')
return warn('treesitter parser not found for' .. vim.fn.bufname(bufn))
end
local tree = parser:parse()
tree = tree[1]
Expand Down Expand Up @@ -226,7 +226,7 @@ M.get_sub_testcase_name = function(bufnr)
local bufn = bufnr or vim.api.nvim_get_current_buf()
local parser = vim.treesitter.get_parser(bufn, 'go')
if not parser then
return warn('treesitter parser not found')
return warn('treesitter parser not found for ' .. vim.fn.bufname(bufn))
end

local sub_case_query = vim.treesitter.query.parse('go', M.query_sub_testcase_node)
Expand Down
92 changes: 37 additions & 55 deletions lua/tests/go_test_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ local busted = require('plenary/busted')
local godir = cur_dir .. '/lua/tests/fixtures'

-- hack latest nvim treestitter get_node_text bug
-- TODO remove this after nvim-treesitter is fixed
local nvim11 = vim.fn.has('nvim-0.11') == 1

describe('should run func test', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
vim.cmd([[packadd go.nvim]])
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')

vim.wait(400, function() end)
it('should test function', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = './coverage/branch_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -31,9 +34,6 @@ describe('should run func test', function()
end)
it('should test function inside a source code', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = './coverage/branch.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -51,9 +51,6 @@ describe('should run func test', function()
end)
it('should test function with additional args to test binary', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -80,7 +77,8 @@ end)
describe('should run test file', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
it('should test function', function()
--
-- go.nvim may not auto loaded
Expand Down Expand Up @@ -111,12 +109,14 @@ end)
describe('should run test file with flags', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
it('should test function', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
local path = 'coverage/branch_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -142,12 +142,15 @@ end)
describe('should run test package: ', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
it('should test function', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
local path = 'coverage/branch_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -166,12 +169,11 @@ end)
describe('should run test: ', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
vim.cmd([[packadd go.nvim]])
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
it('should test function', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -190,12 +192,10 @@ end)
describe('should allow select test func: ', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
it('should test function', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -214,12 +214,10 @@ end)
describe('should run test file with flags inside file: ', function()
-- vim.fn.readfile('minimal.vim')
-- vim.fn.writefile(vim.fn.readfile('fixtures/fmt/hello.go'), name)
local status = require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')
it('should test function with tag', function()
--
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/tag_test.go' -- %:p:h ? %:p
require('go').setup({
trace = true,
Expand All @@ -243,16 +241,17 @@ describe('should run test file with flags inside file: ', function()
end)

describe('should run subcase tests: ', function()
vim.cmd([[packadd go.nvim]])

require('plenary.reload').reload_module('go.nvim')
require('plenary.reload').reload_module('nvim-treesitter/nvim-treesitter')

if nvim11 then
eq(1, 1)
return
end
it('should test subcase in table test style', function()
if nvim11 then
eq(1, 1)
return
end
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go'
require('go').setup({
trace = true,
Expand All @@ -263,17 +262,11 @@ describe('should run subcase tests: ', function()
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos('.', { 1, 18, 11, 0 })
local cmd = require('go.gotest').test_tblcase()
eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTest_branch\\E$\'/\'^\\Qa10\\E$\'' }, cmd)
eq({ 'go', 'test', './coverage', "-test.run='^\\QTest_branch\\E$'/'^\\Qa10\\E$'" }, cmd)
end)

it('should test subcase in table test style when cursor inside test block', function()
if nvim11 then
eq(1, 1)
return
end
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go'
require('go').setup({
trace = true,
Expand All @@ -284,17 +277,13 @@ describe('should run subcase tests: ', function()
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos('.', { 1, 29, 12, 0 })
local cmd = require('go.gotest').test_tblcase()
eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTest_branch\\E$\'/\'^\\Qb10 [step 1..3]\\E$\'' }, cmd)
eq(
{ 'go', 'test', './coverage', "-test.run='^\\QTest_branch\\E$'/'^\\Qb10 [step 1..3]\\E$'" },
cmd
)
end)

it('should test subcase in subtest style', function()
if nvim11 then
eq(1, 1)
return
end
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go'
require('go').setup({
trace = true,
Expand All @@ -305,17 +294,10 @@ describe('should run subcase tests: ', function()
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos('.', { 1, 75, 11, 0 })
local cmd = require('go.gotest').test_tblcase()
eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTestBranchSubTest\\E$\'/\'^\\Qa11\\E$\'' }, cmd)
eq({ 'go', 'test', './coverage', "-test.run='^\\QTestBranchSubTest\\E$'/'^\\Qa11\\E$'" }, cmd)
end)

it('should test subcase in subtest style when cursor inside test block', function()
if nvim11 then
eq(1, 1)
return
end
-- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]])

local path = 'coverage/branch_test.go'
require('go').setup({
trace = true,
Expand All @@ -326,6 +308,6 @@ describe('should run subcase tests: ', function()
vim.cmd("silent exe 'e " .. path .. "'")
vim.fn.setpos('.', { 1, 82, 7, 0 })
local cmd = require('go.gotest').test_tblcase()
eq({ 'go', 'test', './coverage', '-test.run=\'^\\QTestBranchSubTest\\E$\'/\'^\\Qb11\\E$\'' }, cmd)
eq({ 'go', 'test', './coverage', "-test.run='^\\QTestBranchSubTest\\E$'/'^\\Qb11\\E$'" }, cmd)
end)
end)

0 comments on commit 12ab6ac

Please sign in to comment.