Skip to content

Instantly share code, notes, and snippets.

@ahmedash95
Created April 25, 2024 19:08
Show Gist options
  • Save ahmedash95/7969316e8a843c4de8814a097f0eada0 to your computer and use it in GitHub Desktop.
Save ahmedash95/7969316e8a843c4de8814a097f0eada0 to your computer and use it in GitHub Desktop.
local ts_utils = require('nvim-treesitter.ts_utils')
local get_master_node = function()
local node = ts_utils.get_node_at_cursor()
if not node then
error('Cannot get node at cursor')
end
-- When in normal mode, find the smallest node starting at the cursor position
local root = ts_utils.get_root_for_node(node)
local start_row = node:start()
local parent = node:parent()
while parent ~= nil and parent ~= root and parent:start() == start_row do
node = parent
parent = node:parent()
end
return node
end
local function selectRange()
local node = get_master_node()
local bufnr = vim.api.nvim_get_current_buf()
ts_utils.update_selection(bufnr, node)
end
-- Define vim command
vim.api.nvim_create_user_command('SelectRange', selectRange, { range = true })
-- Keymap to ctrl w on n and v mode
vim.api.nvim_set_keymap('n', '<C-w>', ':SelectRange<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('v', '<C-w>', ':SelectRange<CR>', { noremap = true, silent = true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment