test.lua (967B)
1 local tsu = require('nvim-treesitter.ts_utils') 2 3 local M = {} 4 5 -- find test node 6 M.findTestNode = function(node) 7 node = node or tsu.get_node_at_cursor() 8 while node do 9 if (node:type():find('selector')) then 10 local prev = tsu.get_previous_node(node) 11 if prev and prev:type():find('identifier') then 12 if tsu.get_node_text(prev)[1] == 'test' then 13 return prev 14 end 15 end 16 end 17 node = node:parent() 18 end 19 end 20 21 function M.getTestName(node) 22 node = node or tsu.get_node_at_cursor() 23 local testNode = M.findTestNode(node) 24 if testNode then 25 local selector = tsu.get_next_node(testNode) 26 if selector:type():find('selector') then 27 local child = selector:named_child() 28 while child do 29 if child:type():find('string_literal') then 30 local test = tsu.get_node_text(child)[1] 31 return test 32 end 33 child = child:named_child() 34 end 35 end 36 end 37 return '' 38 end 39 40 return M