|
1 | | -local M = {} |
2 | | - |
3 | 1 | ---@param node TSNode |
4 | | ----@param targets string[] |
5 | 2 | ---@return boolean |
6 | | -M.is_sibling = function(node, targets) |
7 | | - local sibling = node:next_sibling() |
8 | | - if sibling == nil then |
9 | | - return false |
10 | | - end |
11 | | - return vim.tbl_contains(targets, sibling:type()) |
| 3 | +local function in_section(node) |
| 4 | + -- reaching a section or document means we are outside section |
| 5 | + return not vim.tbl_contains({ 'section', 'document' }, node:type()) |
12 | 6 | end |
13 | 7 |
|
14 | | ---- Walk through all parent nodes and count the number of list nodes |
| 8 | +local M = {} |
| 9 | + |
| 10 | +---Walk through parent nodes, count the number of target nodes |
15 | 11 | ---@param node TSNode |
| 12 | +---@param target string |
16 | 13 | ---@return integer |
17 | | -M.get_list_level = function(node) |
| 14 | +M.level_in_section = function(node, target) |
18 | 15 | local level = 0 |
19 | 16 | local parent = node:parent() |
20 | | - while parent ~= nil do |
21 | | - local parent_type = parent:type() |
22 | | - if vim.tbl_contains({ 'section', 'document' }, parent_type) then |
23 | | - -- reaching a section or document means we are clearly at the top of the list |
24 | | - break |
25 | | - elseif parent_type == 'list' then |
26 | | - -- found a list increase the level and continue |
| 17 | + while parent ~= nil and in_section(parent) do |
| 18 | + if parent:type() == target then |
27 | 19 | level = level + 1 |
28 | 20 | end |
29 | 21 | parent = parent:parent() |
30 | 22 | end |
31 | 23 | return level |
32 | 24 | end |
33 | 25 |
|
34 | | ---- Walk through parent nodes until target, return first found |
| 26 | +---Walk through parent nodes, return first target node |
35 | 27 | ---@param node TSNode |
36 | 28 | ---@param target string |
37 | 29 | ---@return TSNode? |
38 | | -M.get_parent = function(node, target) |
| 30 | +M.parent_in_section = function(node, target) |
39 | 31 | local parent = node:parent() |
40 | | - while parent ~= nil do |
41 | | - local parent_type = parent:type() |
42 | | - if vim.tbl_contains({ 'section', 'document' }, parent_type) then |
43 | | - -- reaching a section or document means we are clearly outside our target |
44 | | - break |
45 | | - elseif parent_type == target then |
| 32 | + while parent ~= nil and in_section(parent) do |
| 33 | + if parent:type() == target then |
46 | 34 | return parent |
47 | 35 | end |
48 | 36 | parent = parent:parent() |
49 | 37 | end |
50 | 38 | return nil |
51 | 39 | end |
52 | 40 |
|
| 41 | +---@param node TSNode |
| 42 | +---@param targets string[] |
| 43 | +---@return TSNode? |
| 44 | +M.sibling = function(node, targets) |
| 45 | + local sibling = node:next_sibling() |
| 46 | + while sibling ~= nil do |
| 47 | + if vim.tbl_contains(targets, sibling:type()) then |
| 48 | + return sibling |
| 49 | + end |
| 50 | + sibling = sibling:next_sibling() |
| 51 | + end |
| 52 | + return nil |
| 53 | +end |
| 54 | + |
53 | 55 | ---@param node TSNode |
54 | 56 | ---@param target string |
55 | 57 | ---@return TSNode? |
56 | | -M.get_child = function(node, target) |
| 58 | +M.child = function(node, target) |
57 | 59 | for child in node:iter_children() do |
58 | 60 | if child:type() == target then |
59 | 61 | return child |
|
0 commit comments