|
| 1 | +local M = {} |
| 2 | +local flash = require('flash') |
| 3 | +local Pos = require('flash.search.pos') |
| 4 | + |
| 5 | +local opts = { mode = 'treesj' } |
| 6 | +function M.setup(config) |
| 7 | + opts = config |
| 8 | +end |
| 9 | + |
| 10 | +function M.selector(nodes) |
| 11 | + local currwin = vim.api.nvim_get_current_win() |
| 12 | + local buf = vim.api.nvim_get_current_buf() |
| 13 | + local line_count = vim.api.nvim_buf_line_count(buf) |
| 14 | + local state = flash.jump(vim.tbl_deep_extend('force', { |
| 15 | + matcher = function(win, _) |
| 16 | + if win ~= currwin then |
| 17 | + return {} |
| 18 | + end |
| 19 | + local ret = {} |
| 20 | + local done = {} |
| 21 | + -- https://github.com/folke/flash.nvim/blob/967117690bd677cb7b6a87f0bc0077d2c0be3a27/lua/flash/plugins/treesitter.lua#L52 |
| 22 | + for i, node in ipairs(nodes) do |
| 23 | + local tsn = node.tsnode |
| 24 | + local range = { tsn:range() } |
| 25 | + local match = { |
| 26 | + win = win, |
| 27 | + node = node, |
| 28 | + pos = { range[1] + 1, range[2] }, |
| 29 | + end_pos = { range[3] + 1, range[4] - 1 }, |
| 30 | + index = i, |
| 31 | + } |
| 32 | + |
| 33 | + -- If the match is at the end of the buffer, |
| 34 | + -- then move it to the last character of the last line. |
| 35 | + if match.end_pos[1] > line_count then |
| 36 | + match.end_pos[1] = line_count |
| 37 | + match.end_pos[2] = #vim.api.nvim_buf_get_lines( |
| 38 | + buf, |
| 39 | + match.end_pos[1] - 1, |
| 40 | + match.end_pos[1], |
| 41 | + false |
| 42 | + )[1] |
| 43 | + elseif match.end_pos[2] == -1 then |
| 44 | + -- If the end points to the start of the next line, move it to the |
| 45 | + -- end of the previous line. |
| 46 | + -- Otherwise operations include the first character of the next line |
| 47 | + local line = vim.api.nvim_buf_get_lines( |
| 48 | + buf, |
| 49 | + match.end_pos[1] - 2, |
| 50 | + match.end_pos[1] - 1, |
| 51 | + false |
| 52 | + )[1] |
| 53 | + match.end_pos[1] = match.end_pos[1] - 1 |
| 54 | + match.end_pos[2] = #line |
| 55 | + end |
| 56 | + local id = |
| 57 | + table.concat(vim.tbl_flatten({ match.pos, match.end_pos }), '.') |
| 58 | + if not done[id] then |
| 59 | + done[id] = true |
| 60 | + ret[#ret + 1] = match |
| 61 | + end |
| 62 | + end |
| 63 | + for m, match in ipairs(ret) do |
| 64 | + match.pos = Pos(match.pos) |
| 65 | + match.end_pos = Pos(match.end_pos) |
| 66 | + match.depth = #ret - m |
| 67 | + end |
| 68 | + return ret |
| 69 | + end, |
| 70 | + action = function(match, state) |
| 71 | + state.final_match = match |
| 72 | + end, |
| 73 | + search = { |
| 74 | + multi_window = false, |
| 75 | + wrap = true, |
| 76 | + incremental = false, |
| 77 | + max_length = 0, |
| 78 | + }, |
| 79 | + label = { |
| 80 | + before = true, |
| 81 | + after = true, |
| 82 | + }, |
| 83 | + highlight = { |
| 84 | + matches = false, |
| 85 | + }, |
| 86 | + actions = { |
| 87 | + -- TODO: incremental preview/operations |
| 88 | + }, |
| 89 | + jump = { autojump = true }, |
| 90 | + }, opts or {})) |
| 91 | + local m = state.final_match |
| 92 | + if m then |
| 93 | + return m.node, m.index |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +return M |
0 commit comments