diff options
| author | Steven Sojka <steelsojka@users.noreply.github.com> | 2020-08-17 11:39:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-17 11:39:22 -0500 |
| commit | b796f0725d913cba0a292bb7b750f0403b07c94a (patch) | |
| tree | 6645909fcf422b0d77f513514ece4a539135aba4 /lua/nvim-treesitter/textobjects/attach.lua | |
| parent | 0b3cd6c131a5ee9e3377c6b348c2b8995a848967 (diff) | |
| parent | 52168114594d791a3ae6092ab2489758da7b3ae8 (diff) | |
Merge pull request #305 from theHamsta/textobjects-submodules
Textobjects submodules
Diffstat (limited to 'lua/nvim-treesitter/textobjects/attach.lua')
| -rw-r--r-- | lua/nvim-treesitter/textobjects/attach.lua | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/textobjects/attach.lua b/lua/nvim-treesitter/textobjects/attach.lua new file mode 100644 index 00000000..22d6b650 --- /dev/null +++ b/lua/nvim-treesitter/textobjects/attach.lua @@ -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 |
