Neotestでテスト対象ファイルを開いているときにwatch
Neotest利用時に_test
のようなファイルではないファイルを開いているところからwatchを始める方法。
以下のような処理を行う。
現在のバッファのファイルのテストファイルを取得する関数Test_file_name
を実行し、そのパスに対してneotest.watch.watch()
する。
最後にneotest.summary.toggle()
する。
注意点としてテストファイルを一回も開いていないとLSP未起動のエラーが発生する。
local neotest = require('neotest')
function Neotest_watch()
local test_file = Test_file_name()
neotest.watch.watch(test_file)
neotest.summary.toggle()
end
function Test_file_name()
if vim.bo.filetype == 'go' then
-- _test.goでない場合は_test.goをwatchする
if not vim.fn.expand('%:t'):match('_test.go') then
return vim.fn.expand('%:r') .. '_test.go'
else
return vim.fn.expand('%')
end
else
return vim.fn.expand('%')
end
end
end,