Merge pull request #305 from theHamsta/textobjects-submodules

Textobjects submodules
This commit is contained in:
Steven Sojka 2020-08-17 11:39:22 -05:00 committed by GitHub
commit b796f0725d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 513 additions and 133 deletions

View file

@ -147,7 +147,31 @@ require'nvim-treesitter.configs'.setup {
["ad"] = "@comment.outer",
["am"] = "@call.outer",
["im"] = "@call.inner"
}
},
-- swap parameters (keymap -> textobject query)
swap_next = {
["<a-p>"] = "@parameter.inner",
},
swap_previous = {
["<a-P>"] = "@parameter.inner",
},
-- set mappings to go to start/end of adjacent textobjects (keymap -> textobject query)
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer",
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer",
},
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer",
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer",
},
},
ensure_installed = "all" -- one of "all", "language", or a list of languages
}

View file

@ -94,7 +94,31 @@ By default, everything is disabled. To enable support for features, in your `ini
["ad"] = "@comment.outer",
["am"] = "@call.outer",
["im"] = "@call.inner"
}
},
-- swap parameters (keymap -> textobject query)
swap_next = {
["<a-p>"] = "@parameter.inner",
},
swap_previous = {
["<a-P>"] = "@parameter.inner",
},
-- set mappings to go to start/end of adjacent textobjects (keymap -> textobject query)
goto_previous_start = {
["[m"] = "@function.outer",
["[["] = "@class.outer",
},
goto_previous_end = {
["[M"] = "@function.outer",
["[]"] = "@class.outer",
},
goto_next_start = {
["]m"] = "@function.outer",
["]]"] = "@class.outer",
},
goto_next_end = {
["]M"] = "@function.outer",
["]["] = "@class.outer",
},
},
ensure_installed = "all" -- one of "all", "language", or a list of languages
}

View file

@ -7,7 +7,7 @@ local utils = require'nvim-treesitter.utils'
local M = {}
local function has_some_textobject_mapping(lang)
for _, v in pairs(M.get_module('textobjects').keymaps) do
for _, v in pairs(M.get_module('textobjects.select').keymaps) do
if type(v) == 'table' then
if v[lang] then
return true
@ -79,13 +79,33 @@ local builtin_modules = {
}
},
textobjects = {
module_path = 'nvim-treesitter.textobjects',
enable = false,
disable = {},
is_supported = function(lang)
return has_some_textobject_mapping(lang) or queries.has_textobjects(lang)
end,
keymaps = {}
select = {
module_path = 'nvim-treesitter.textobjects.select',
enable = false,
disable = {},
is_supported = function(lang)
return has_some_textobject_mapping(lang) or queries.has_textobjects(lang)
end,
keymaps = {},
},
move = {
module_path = 'nvim-treesitter.textobjects.move',
enable = false,
disable = {},
is_supported = queries.has_textobjects,
goto_next_start = {},
goto_next_end = {},
goto_previous_start = {},
goto_previous_end = {},
},
swap = {
module_path = 'nvim-treesitter.textobjects.swap',
enable = false,
disable = {},
is_supported = queries.has_textobjects,
swap_next = {},
swap_previous = {},
},
}
}

View file

@ -213,6 +213,36 @@ function M.get_capture_matches(bufnr, capture_string, query_group)
return matches
end
function M.find_best_match(bufnr, capture_string, query_group, filter_predicate, scoring_function)
if not string.sub(capture_string, 1,2) == '@' then
api.nvim_err_writeln('capture_string must start with "@"')
return
end
--remove leading "@"
capture_string = string.sub(capture_string, 2)
local best
local best_score
for maybe_match in M.iter_group_results(bufnr, query_group) do
local match = utils.get_at_path(maybe_match, capture_string)
if match and filter_predicate(match) then
local current_score = scoring_function(match)
if not best then
best = match
best_score = current_score
end
if current_score > best_score then
best = match
best_score = current_score
end
end
end
return best
end
-- Iterates matches from a query file.
-- @param bufnr the buffer
-- @param query_group the query file to use

View file

@ -1,6 +1,7 @@
-- Definition based navigation module
local ts_utils = require'nvim-treesitter.ts_utils'
local utils = require'nvim-treesitter.utils'
local locals = require'nvim-treesitter.locals'
local configs = require'nvim-treesitter.configs'
local api = vim.api
@ -11,8 +12,7 @@ function M.goto_definition(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
local node_at_point = ts_utils.get_node_at_cursor()
-- Set the item in jump list
vim.cmd "normal! m'"
utils.set_jump()
if not node_at_point then return end

View file

@ -1,113 +0,0 @@
local api = vim.api
local ts = vim.treesitter
local configs = require "nvim-treesitter.configs"
local parsers = require "nvim-treesitter.parsers"
local queries = require'nvim-treesitter.query'
local ts_utils = require'nvim-treesitter.ts_utils'
local M = {}
function M.select_textobject(query_string)
local bufnr = vim.api.nvim_get_current_buf()
local lang = parsers.get_buf_lang(bufnr)
if not lang then return end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
local matches = {}
if string.match(query_string, '^@.*') then
matches = queries.get_capture_matches(bufnr, query_string, 'textobjects')
else
local parser = parsers.get_parser(bufnr, lang)
local root = parser:parse():root()
local start_row, _, end_row, _ = root:range()
local query = ts.parse_query(lang, query_string)
for m in queries.iter_prepared_matches(query, root, bufnr, start_row, end_row) do
for _, n in pairs(m) do
if n.node then
table.insert(matches, n)
end
end
end
end
local match_length
local smallest_range
local earliest_start
for _, m in pairs(matches) do
if m.node and ts_utils.is_in_node_range(m.node, row, col) then
local length = ts_utils.node_length(m.node)
if not match_length or length < match_length then
smallest_range = m
match_length = length
end
-- for nodes with same length take the one with earliest start
if match_length and length == smallest_range then
local start = m.start
if start then
local _, _, start_byte = m.start.node:start()
if not earliest_start or start_byte < earliest_start then
smallest_range = m
match_length = length
earliest_start = start_byte
end
end
end
end
end
if smallest_range then
if smallest_range.start then
local start_range = {smallest_range.start.node:range()}
local node_range = {smallest_range.node:range()}
ts_utils.update_selection(bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]})
else
ts_utils.update_selection(bufnr, smallest_range.node)
end
end
end
function M.attach(bufnr, lang)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects")
local lang = lang or parsers.get_buf_lang(buf)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
local cmd = ":lua require'nvim-treesitter.textobjects'.select_textobject('"..query.."')<CR>"
api.nvim_buf_set_keymap(buf, "o", mapping, cmd, {silent = true, noremap = true })
api.nvim_buf_set_keymap(buf, "v", mapping, cmd, {silent = true, noremap = true })
end
end
end
function M.detach(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects")
local lang = parsers.get_buf_lang(bufnr)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
api.nvim_buf_del_keymap(buf, "o", mapping)
api.nvim_buf_del_keymap(buf, "v", mapping)
end
end
end
return M

View file

@ -0,0 +1,61 @@
local configs = require'nvim-treesitter.configs'
local parsers = require'nvim-treesitter.parsers'
local queries = require'nvim-treesitter.query'
local api = vim.api
local M = {}
function M.make_attach(normal_mode_functions, submodule)
return function(bufnr, lang)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects."..submodule)
local lang = lang or parsers.get_buf_lang(buf)
for _, function_call in pairs(normal_mode_functions) do
for mapping, query in pairs(config[function_call] or {}) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
local cmd = ":lua require'nvim-treesitter.textobjects."..submodule.."'."..function_call.."('"..query.."')<CR>"
api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true, noremap = true })
end
end
end
end
end
function M.make_detach(normal_mode_functions, submodule)
return function(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects."..submodule)
local lang = parsers.get_buf_lang(bufnr)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
api.nvim_buf_del_keymap(buf, "o", mapping)
api.nvim_buf_del_keymap(buf, "v", mapping)
end
end
for _, function_call in pairs(normal_mode_functions) do
for mapping, query in pairs(config[function_call] or {}) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
api.nvim_buf_del_keymap(buf, "n", mapping)
end
end
end
end
end
return M

View file

@ -0,0 +1,41 @@
local utils = require'nvim-treesitter.utils'
local shared = require'nvim-treesitter.textobjects.shared'
local attach = require'nvim-treesitter.textobjects.attach'
local api = vim.api
local M = {}
function M.goto_adjacent(query_string, forward, start, same_parent, overlapping_range_ok)
local bufnr, _, node = shared.textobject_at_point(query_string)
local adjacent = shared.get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr)
if adjacent then
utils.set_jump()
local adjacent_textobject_range = {adjacent:range()}
local position
if start then
position = { adjacent_textobject_range[1] + 1, adjacent_textobject_range[2] }
else
position = { adjacent_textobject_range[3] + 1, adjacent_textobject_range[4] }
end
api.nvim_win_set_cursor(api.nvim_get_current_win(), position)
end
end
-- luacheck: push ignore 631
M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', not 'same_parent', 'overlap ok') end
M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', not 'start', not 'same_parent', 'overlap ok') end
M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, not 'forward', 'start', not 'same_parent', 'overlap ok') end
M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, not 'forward', not 'start', not 'same_parent', 'overlap ok') end
-- luacheck: pop
local normal_mode_functions = {"goto_next_start",
"goto_next_end",
"goto_previous_start",
"goto_previous_end"}
M.attach = attach.make_attach(normal_mode_functions, "move")
M.deattach = attach.make_detach(normal_mode_functions, "move")
return M

View file

@ -0,0 +1,55 @@
local api = vim.api
local configs = require'nvim-treesitter.configs'
local parsers = require'nvim-treesitter.parsers'
local queries = require'nvim-treesitter.query'
local shared = require'nvim-treesitter.textobjects.shared'
local ts_utils = require'nvim-treesitter.ts_utils'
local M = {}
function M.select_textobject(query_string)
local bufnr, textobject = shared.textobject_at_point(query_string)
if textobject then
ts_utils.update_selection(bufnr, textobject)
end
end
function M.attach(bufnr, lang)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects.select")
local lang = lang or parsers.get_buf_lang(buf)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
local cmd = ":lua require'nvim-treesitter.textobjects.select'.select_textobject('"..query.."')<CR>"
api.nvim_buf_set_keymap(buf, "o", mapping, cmd, {silent = true, noremap = true })
api.nvim_buf_set_keymap(buf, "v", mapping, cmd, {silent = true, noremap = true })
end
end
end
function M.detach(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects.select")
local lang = parsers.get_buf_lang(bufnr)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then
query = query[lang]
elseif not queries.get_query(lang, 'textobjects') then
query = nil
end
if query then
api.nvim_buf_del_keymap(buf, "o", mapping)
api.nvim_buf_del_keymap(buf, "v", mapping)
end
end
end
return M

View file

@ -0,0 +1,142 @@
local api = vim.api
local ts = vim.treesitter
local parsers = require "nvim-treesitter.parsers"
local queries = require'nvim-treesitter.query'
local ts_utils = require'nvim-treesitter.ts_utils'
local M = {}
function M.textobject_at_point(query_string)
local bufnr = vim.api.nvim_get_current_buf()
local lang = parsers.get_buf_lang(bufnr)
if not lang then return end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
local matches = {}
if string.match(query_string, '^@.*') then
matches = queries.get_capture_matches(bufnr, query_string, 'textobjects')
else
local parser = parsers.get_parser(bufnr, lang)
local root = parser:parse():root()
local start_row, _, end_row, _ = root:range()
local query = ts.parse_query(lang, query_string)
for m in queries.iter_prepared_matches(query, root, bufnr, start_row, end_row) do
for _, n in pairs(m) do
if n.node then
table.insert(matches, n)
end
end
end
end
local match_length
local smallest_range
local earliest_start
for _, m in pairs(matches) do
if m.node and ts_utils.is_in_node_range(m.node, row, col) then
local length = ts_utils.node_length(m.node)
if not match_length or length < match_length then
smallest_range = m
match_length = length
end
-- for nodes with same length take the one with earliest start
if match_length and length == smallest_range then
local start = m.start
if start then
local _, _, start_byte = m.start.node:start()
if not earliest_start or start_byte < earliest_start then
smallest_range = m
match_length = length
earliest_start = start_byte
end
end
end
end
end
if smallest_range then
if smallest_range.start then
local start_range = {smallest_range.start.node:range()}
local node_range = {smallest_range.node:range()}
return bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]}, smallest_range.node
else
return bufnr, {smallest_range.node:range()}, smallest_range.node
end
end
end
function M.get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr)
local fn = forward and M.next_textobject or M.previous_textobject
return fn(node, query_string, same_parent, overlapping_range_ok, bufnr)
end
function M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr)
local node = node or ts_utils.get_node_at_cursor()
local bufnr = bufnr or api.nvim_get_current_buf()
if not node then return end
local _, _, node_end = node:end_()
local search_start, _
if overlapping_range_ok then
_, _, search_start = node:start()
else
_, _, search_start = node:end_()
end
local function scoring_function(match)
if match.node == node then return end
if not same_parent or node:parent() == match.node:parent() then
local _, _, start = match.node:start()
local _, _, end_ = match.node:end_()
return start > search_start and end_ >= node_end
end
end
local function filter_function(match)
local _, _, node_start = match.node:start()
return -node_start
end
local next_node = queries.find_best_match(bufnr, query_string, 'textobjects', scoring_function, filter_function)
return next_node and next_node.node
end
function M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr)
local node = node or ts_utils.get_node_at_cursor()
local bufnr = bufnr or api.nvim_get_current_buf()
if not node then return end
local _, _, node_start = node:start()
local search_end, _
if overlapping_range_ok then
_, _, search_end = node:end_()
search_end = search_end + 1
else
_, _, search_end = node:start()
end
local function scoring_function(match)
if not same_parent or node:parent() == match.node:parent() then
local _, _, end_ = match.node:end_()
local _, _, start = match.node:start()
return end_ < search_end and start < node_start
end
end
local function filter_function(match)
local _, _, node_end = match.node:end_()
return node_end
end
local previous_node = queries.find_best_match(bufnr, query_string, 'textobjects', scoring_function, filter_function)
return previous_node and previous_node.node
end
return M

View file

@ -0,0 +1,35 @@
local ts_utils = require'nvim-treesitter.ts_utils'
local shared = require'nvim-treesitter.textobjects.shared'
local attach = require'nvim-treesitter.textobjects.attach'
local M = {}
local function swap_textobject(query_string, direction)
local bufnr, textobject_range, node = shared.textobject_at_point(query_string)
if not node then return end
local step = direction > 0 and 1 or -1
local overlapping_range_ok = false
local same_parent = true
for _ = 1, math.abs(direction), step do
local forward = direction > 0
local adjacent = shared.get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr)
ts_utils.swap_nodes(textobject_range, adjacent, bufnr, "yes, set cursor!")
end
end
function M.swap_next(query_string)
swap_textobject(query_string, 1)
end
function M.swap_previous(query_string)
swap_textobject(query_string, -1)
end
local normal_mode_functions = {"swap_next",
"swap_previous"}
M.attach = attach.make_attach(normal_mode_functions, "swap")
M.deattach = attach.make_detach(normal_mode_functions, "swap")
return M

View file

@ -1,6 +1,7 @@
local api = vim.api
local parsers = require'nvim-treesitter.parsers'
local utils = require'nvim-treesitter.utils'
local M = {}
@ -13,7 +14,7 @@ function M.get_node_text(node, bufnr)
if not node then return {} end
-- We have to remember that end_col is end-exclusive
local start_row, start_col, end_row, end_col = node:range()
local start_row, start_col, end_row, end_col = M.get_node_range(node)
if start_row ~= end_row then
local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row+1, false)
@ -131,12 +132,7 @@ end
-- Set visual selection to node
function M.update_selection(buf, node)
local start_row, start_col, end_row, end_col
if type(node) == 'table' then
start_row, start_col, end_row, end_col = unpack(node)
else
start_row, start_col, end_row, end_col = node:range()
end
local start_row, start_col, end_row, end_col = M.get_node_range(node)
if end_row == vim.fn.line('$') then
end_col = #vim.fn.getline('$')
@ -187,8 +183,16 @@ function M.is_in_node_range(node, line, col)
end
end
function M.get_node_range(node_or_range)
if type(node_or_range) == 'table' then
return unpack(node_or_range)
else
return node_or_range:range()
end
end
function M.node_to_lsp_range(node)
local start_line, start_col, end_line, end_col = node:range()
local start_line, start_col, end_line, end_col = M.get_node_range(node)
local rtn = {}
rtn.start = { line = start_line, character = start_col }
rtn['end'] = { line = end_line, character = end_col }
@ -225,4 +229,51 @@ function M.memoize_by_buf_tick(fn, bufnr_fn)
end
end
function M.swap_nodes(node_or_range1, node_or_range2, bufnr, cursor_to_second)
if not node_or_range1 or not node_or_range2 then return end
local range1 = M.node_to_lsp_range(node_or_range1)
local range2 = M.node_to_lsp_range(node_or_range2)
local text1 = M.get_node_text(node_or_range1)
local text2 = M.get_node_text(node_or_range2)
local edit1 = { range = range1, newText = table.concat(text2, '\n') }
local edit2 = { range = range2, newText = table.concat(text1, '\n') }
vim.lsp.util.apply_text_edits({edit1, edit2}, bufnr)
if cursor_to_second then
utils.set_jump()
local char_delta = 0
local line_delta = 0
if range1["end"].line < range2.start.line
or (range1["end"].line == range2.start.line and range1["end"].character < range2.start.character) then
line_delta = #text2 - #text1
end
if range1["end"].line == range2.start.line and range1["end"].character < range2.start.character then
if line_delta ~= 0 then
--- why?
--correction_after_line_change = -range2.start.character
--text_now_before_range2 = #(text2[#text2])
--space_between_ranges = range2.start.character - range1["end"].character
--char_delta = correction_after_line_change + text_now_before_range2 + space_between_ranges
--- Equivalent to:
char_delta = #(text2[#text2]) - range1["end"].character
-- add range1.start.character if last line of range1 (now text2) does not start at 0
if range1.start.line == range2.start.line + line_delta then
char_delta = char_delta + range1.start.character
end
else
char_delta = #(text2[#text2]) - #(text1[#text1])
end
end
api.nvim_win_set_cursor(api.nvim_get_current_win(),
{range2.start.line + 1 + line_delta,
range2.start.character + char_delta})
end
end
return M

View file

@ -67,4 +67,8 @@ function M.print_warning(text)
api.nvim_command(string.format([[echohl WarningMsg | echo "%s" | echohl None]], text))
end
function M.set_jump()
vim.cmd "normal! m'"
end
return M

View file

@ -53,3 +53,6 @@
(preproc_else
(_) @statement.outer)
(parameter_list
(parameter_declaration) @parameter.inner)

View file

@ -13,3 +13,6 @@
(template_declaration
(class_specifier) @class.outer) @class.outer.start
(parameter_list
(optional_parameter_declaration) @parameter.inner)