|
| 1 | +local ts_utils = require("nvim-treesitter.ts_utils") |
| 2 | +local ts_query = require("nvim-treesitter.query") |
| 3 | +local parsers = require("nvim-treesitter.parsers") |
| 4 | +local locals = require("nvim-treesitter.locals") |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +M.get_root = function(lang) |
| 9 | + local parser = parsers.get_parser(0, lang) |
| 10 | + return parser:parse()[1]:root() |
| 11 | +end |
| 12 | + |
| 13 | +M.get_bounded_query = function(query, lang, startR, stopR) |
| 14 | + local success, parsed_query = pcall(function() |
| 15 | + return vim.treesitter.parse_query(lang, query) |
| 16 | + end) |
| 17 | + |
| 18 | + if not success then |
| 19 | + error("Unsuccessful successful first try") |
| 20 | + end |
| 21 | + |
| 22 | + local root = M.get_root(lang) |
| 23 | + |
| 24 | + local out = {} |
| 25 | + for match in ts_query.iter_prepared_matches(parsed_query, root, 0, startR - 1, stopR) do |
| 26 | + locals.recurse_local_nodes(match, function(_, node, path) |
| 27 | + table.insert(out, node) |
| 28 | + end) |
| 29 | + end |
| 30 | + return out; |
| 31 | +end |
| 32 | + |
| 33 | +local refactor_constants = { |
| 34 | + lua = { |
| 35 | + scope = { |
| 36 | + ["function"] = true, |
| 37 | + ["function_definition"] = true |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +-- determines if a contains node b. |
| 43 | +-- @param a the containing node |
| 44 | +-- @param b the node to be contained |
| 45 | +function M.node_contains(a, b) |
| 46 | + if a == nil or b == nil then |
| 47 | + return false |
| 48 | + end |
| 49 | + |
| 50 | + local start_row, start_col, end_row, end_col = b:range() |
| 51 | + return ts_utils.is_in_node_range(a, start_row, start_col) and |
| 52 | + ts_utils.is_in_node_range(a, end_row, end_col) |
| 53 | +end |
| 54 | + |
| 55 | +-- determines if a node exists within a range. Imagine a range selection |
| 56 | +-- across '<,'> and an identifier. Does the identifier exist within the |
| 57 | +-- selection? |
| 58 | +-- |
| 59 | +-- @param a the containing node |
| 60 | +-- @param b the node to be contained |
| 61 | +M.range_contains_node = function(node, start_row, start_col, end_row, end_col) |
| 62 | + local node_start_row, node_start_col, node_end_row, node_end_col = node:range() |
| 63 | + |
| 64 | + -- There are five possible conditions |
| 65 | + -- 1. node start/end row are contained exclusively within the range. |
| 66 | + -- 2. The range is a single line range |
| 67 | + -- - the node start/end row must equal start_row and cols have to exist |
| 68 | + -- within range, inclusive |
| 69 | + -- 3. The node exists solely within the first line |
| 70 | + -- - node_start_col has to be inclusive with start_col, end col doesn't |
| 71 | + -- matter. |
| 72 | + -- 4. The node exists solely within the last line |
| 73 | + -- - node_start_col doesn't matter whereas node_end_col has to be |
| 74 | + -- inclusive with end_col |
| 75 | + -- 5. The node starts / ends on the same rows and has to have each column |
| 76 | + -- considered |
| 77 | + if start_row < node_start_row and end_row > node_end_row then |
| 78 | + return true |
| 79 | + elseif start_row == end_row then |
| 80 | + return start_row == node_start_row and |
| 81 | + end_row == node_end_row and |
| 82 | + start_col <= node_start_col and |
| 83 | + end_col >= node_end_col |
| 84 | + |
| 85 | + elseif start_row == node_start_row and start_row == node_end_row then |
| 86 | + return start_col <= node_start_col |
| 87 | + elseif end_row == node_start_row and end_row == node_end_row then |
| 88 | + return end_col >= node_end_col |
| 89 | + elseif start_row <= node_start_row and end_row >= node_end_row then |
| 90 | + return start_col <= node_start_col and end_col >= node_end_col |
| 91 | + end |
| 92 | + |
| 93 | + return false |
| 94 | +end |
| 95 | + |
| 96 | +M.get_scope_over_selection = function(root, start_line, start_col, end_line, end_col, lang) |
| 97 | + local start_scope = M.get_scope(root, start_line, start_col, lang) |
| 98 | + local end_scope = M.get_scope(root, end_line, end_col, lang) |
| 99 | + |
| 100 | + if start_scope ~= end_scope then |
| 101 | + error("Selection spans over two scopes, cannot determine scope") |
| 102 | + end |
| 103 | + |
| 104 | + return start_scope |
| 105 | +end |
| 106 | + |
| 107 | +M.get_scope = function(root, line, col, lang) |
| 108 | + local function_scopes = {} |
| 109 | + local query = vim.treesitter.get_query(lang, "locals") |
| 110 | + |
| 111 | + for id, n, _ in query:iter_captures(root, 0, 0, -1) do |
| 112 | + if query.captures[id] == "scope" and refactor_constants[lang].scope[n:type()] then |
| 113 | + table.insert(function_scopes, n) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + local out = nil |
| 118 | + for _, scope in pairs(function_scopes) do |
| 119 | + -- TODO: This is a confusing issue |
| 120 | + -- should a scope that contains another scope but terminates at the |
| 121 | + -- same point be the outer or inner? Should potentially be considered |
| 122 | + -- a list of scopes... |
| 123 | + if ts_utils.is_in_node_range(scope, line, col) and |
| 124 | + (out == nil or M.node_contains(out, scope)) then |
| 125 | + |
| 126 | + out = scope |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + return out |
| 131 | +end |
| 132 | + |
| 133 | +local function get_refactoring_query(lang) |
| 134 | + local query = vim.treesitter.get_query(lang, "refactoring") |
| 135 | + if not query then |
| 136 | + error("refactoring not supported in this language. Please provide a queries/<lang>/refactoring.scm") |
| 137 | + end |
| 138 | + return query |
| 139 | +end |
| 140 | + |
| 141 | +local function pluck_by_capture(scope, lang, query, capture_name) |
| 142 | + local local_defs = {} |
| 143 | + local root = M.get_root(lang) |
| 144 | + for id, node, _ in query:iter_captures(root, 0, 0, -1) do |
| 145 | + if query.captures[id] == capture_name and M.node_contains(scope, node) then |
| 146 | + table.insert(local_defs, node) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + return local_defs |
| 151 | +end |
| 152 | + |
| 153 | +M.get_function_args = function(scope, lang) |
| 154 | + return pluck_by_capture(scope, lang, get_refactoring_query(lang), "definition.function_argument") |
| 155 | +end |
| 156 | + |
| 157 | +M.get_locals_defs = function(scope, lang) |
| 158 | + return pluck_by_capture(scope, lang, get_refactoring_query(lang), "definition.local_var") |
| 159 | +end |
| 160 | + |
| 161 | +M.get_all_identifiers = function(scope, lang) |
| 162 | + return pluck_by_capture(scope, lang, vim.treesitter.get_query(lang, "locals"), "reference") |
| 163 | +end |
| 164 | + |
| 165 | +-- is there a better way? |
| 166 | +M.range_to_table = function(node) |
| 167 | + if node == nil then |
| 168 | + return "range nil" |
| 169 | + end |
| 170 | + local a, b, c, d = node:range() |
| 171 | + return {a, b, c, d} |
| 172 | +end |
| 173 | + |
| 174 | +return M |
0 commit comments