mirror of
https://github.com/LazyVim/starter.git
synced 2026-07-25 14:01:03 +00:00
Update keymaps.lua
This commit is contained in:
parent
8c77d9ab6b
commit
93cac62b08
2 changed files with 448 additions and 213 deletions
53
lua/config/go_keymaps.lua
Normal file
53
lua/config/go_keymaps.lua
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
function LiveGrepWithExtension(extension)
|
||||||
|
require('telescope.builtin').live_grep({
|
||||||
|
additional_args = function(opts)
|
||||||
|
return { "--glob", "*." .. extension }
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command(
|
||||||
|
"LgExtension",
|
||||||
|
function(opts)
|
||||||
|
LiveGrepWithExtension(opts.args)
|
||||||
|
end,
|
||||||
|
{ nargs = 1 }
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
function LiveGrepUp(n)
|
||||||
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
local full_path = vim.api.nvim_buf_get_name(bufnr)
|
||||||
|
local file_path = vim.fs.dirname(full_path)
|
||||||
|
-- 用系统的路径分隔符分割路径
|
||||||
|
local path_sep = package.config:sub(1, 1)
|
||||||
|
local components = {}
|
||||||
|
|
||||||
|
-- 分割路径到各个组件
|
||||||
|
for component in string.gmatch(file_path, "[^" .. path_sep .. "]+") do
|
||||||
|
table.insert(components, component)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 计算向上n级的路径
|
||||||
|
local up_n = #components - n
|
||||||
|
if up_n < 1 then
|
||||||
|
up_n = 1
|
||||||
|
end -- 确保不会超出根目录
|
||||||
|
local new_cwd = path_sep
|
||||||
|
for i = 1, up_n do
|
||||||
|
new_cwd = new_cwd .. components[i] .. (i < up_n and path_sep or "")
|
||||||
|
end
|
||||||
|
|
||||||
|
print("search under:" .. new_cwd)
|
||||||
|
-- 使用新的目录执行Telescope live_grep
|
||||||
|
require("telescope.builtin").live_grep({ cwd = new_cwd })
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command(
|
||||||
|
"LgUp",
|
||||||
|
function(opts)
|
||||||
|
local n = tonumber(opts.args) or 0
|
||||||
|
LiveGrepUp(n)
|
||||||
|
end,
|
||||||
|
{ nargs = 1 } -- 命令接受一个参数
|
||||||
|
)
|
||||||
|
|
@ -2,20 +2,22 @@
|
||||||
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
|
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
|
||||||
-- Add any additional keymaps here
|
-- Add any additional keymaps here
|
||||||
|
|
||||||
|
require('config.go_keymaps')
|
||||||
|
|
||||||
function SaveHttpResp()
|
function SaveHttpResp()
|
||||||
-- 获取当前时间并格式化为时间戳
|
-- 获取当前时间并格式化为时间戳
|
||||||
local timestamp = os.date("%Y%m%d%H%M%S")
|
local timestamp = os.date("%Y%m%d%H%M%S")
|
||||||
|
|
||||||
-- 获取当前文件的目录路径
|
-- 获取当前文件的目录路径
|
||||||
local current_path = vim.fn.expand('%:p:h')
|
local current_path = vim.fn.expand("%:p:h")
|
||||||
|
|
||||||
-- 定位到 rest.nvim 的响应缓冲区
|
-- 定位到 rest.nvim 的响应缓冲区
|
||||||
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||||
if vim.bo[bufnr].filetype == 'httpResult' then
|
if vim.bo[bufnr].filetype == "httpResult" then
|
||||||
-- 设置保存文件的完整路径,包含时间戳
|
-- 设置保存文件的完整路径,包含时间戳
|
||||||
local filename = current_path .. "/log/response_" .. timestamp .. ".txt"
|
local filename = current_path .. "/log/response_" .. timestamp .. ".txt"
|
||||||
vim.api.nvim_buf_call(bufnr, function()
|
vim.api.nvim_buf_call(bufnr, function()
|
||||||
vim.cmd('w ' .. filename)
|
vim.cmd("w " .. filename)
|
||||||
end)
|
end)
|
||||||
print("Response saved to " .. filename)
|
print("Response saved to " .. filename)
|
||||||
break
|
break
|
||||||
|
|
@ -23,14 +25,9 @@ function SaveHttpResp()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>rr',
|
vim.api.nvim_set_keymap("n", "<leader>rr", "<cmd>Rest run<cr>", { noremap = true, silent = true })
|
||||||
"<cmd>Rest run<cr>",
|
|
||||||
{ noremap = true, silent = true })
|
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>rs',
|
|
||||||
":lua SaveHttpResp()<CR>",
|
|
||||||
{ noremap = true, silent = true })
|
|
||||||
|
|
||||||
|
vim.api.nvim_set_keymap("n", "<leader>rs", ":lua SaveHttpResp()<CR>", { noremap = true, silent = true })
|
||||||
|
|
||||||
function GetGoplsRootDir()
|
function GetGoplsRootDir()
|
||||||
local clients = vim.lsp.get_active_clients()
|
local clients = vim.lsp.get_active_clients()
|
||||||
|
|
@ -43,10 +40,10 @@ function GetGoplsRootDir()
|
||||||
end
|
end
|
||||||
|
|
||||||
function GoToPathAndLine(input)
|
function GoToPathAndLine(input)
|
||||||
if input == '' then
|
if input == "" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local parts = vim.split(input, ':')
|
local parts = vim.split(input, ":")
|
||||||
local file = parts[1]
|
local file = parts[1]
|
||||||
local line = 1
|
local line = 1
|
||||||
if #parts > 1 then
|
if #parts > 1 then
|
||||||
|
|
@ -61,10 +58,15 @@ function GoToPathAndLine(input)
|
||||||
if goplsRootDir then
|
if goplsRootDir then
|
||||||
pwd = goplsRootDir
|
pwd = goplsRootDir
|
||||||
end
|
end
|
||||||
vim.cmd('edit +' .. line .. ' ' .. pwd .. '/' .. file)
|
vim.cmd("edit +" .. line .. " " .. pwd .. "/" .. file)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', 'gto', ':lua GoToPathAndLine(vim.fn.input("Enter path and line: "))<CR>', { noremap = true })
|
vim.api.nvim_set_keymap(
|
||||||
|
"n",
|
||||||
|
"gto",
|
||||||
|
':lua GoToPathAndLine(vim.fn.input("Enter path and line: "))<CR>',
|
||||||
|
{ noremap = true }
|
||||||
|
)
|
||||||
|
|
||||||
function ExportExpandToClipboard()
|
function ExportExpandToClipboard()
|
||||||
local pwd = vim.fn.getcwd()
|
local pwd = vim.fn.getcwd()
|
||||||
|
|
@ -72,40 +74,45 @@ function ExportExpandToClipboard()
|
||||||
if goplsRootDir then
|
if goplsRootDir then
|
||||||
pwd = goplsRootDir
|
pwd = goplsRootDir
|
||||||
end
|
end
|
||||||
local rf = vim.fn.expand('%:p') .. ':' .. vim.fn.line('.')
|
local rf = vim.fn.expand("%:p") .. ":" .. vim.fn.line(".")
|
||||||
local expanded = string.sub(rf, string.len(pwd .. '/') + 1)
|
local expanded = string.sub(rf, string.len(pwd .. "/") + 1)
|
||||||
vim.fn.setreg('+', expanded)
|
vim.fn.setreg("+", expanded)
|
||||||
print('Expanded path copied to clipboard: ' .. expanded)
|
print("Expanded path copied to clipboard: " .. expanded)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', 'gcr', ':lua ExportExpandToClipboard()<CR>', { noremap = true })
|
vim.api.nvim_set_keymap("n", "gcr", ":lua ExportExpandToClipboard()<CR>", { noremap = true })
|
||||||
|
|
||||||
|
|
||||||
local function check_spelling()
|
local function check_spelling()
|
||||||
-- 保存当前文件
|
-- 保存当前文件
|
||||||
vim.cmd('write')
|
vim.cmd("write")
|
||||||
|
|
||||||
-- 获取当前文件的路径
|
-- 获取当前文件的路径
|
||||||
local current_file = vim.fn.expand('%:p')
|
local current_file = vim.fn.expand("%:p")
|
||||||
print('Spell check in: ' .. current_file)
|
print("Spell check in: " .. current_file)
|
||||||
|
|
||||||
-- 构建CSpell命令
|
-- 构建CSpell命令
|
||||||
local command = 'cspell --config /Users/onns/.onns/weiyun/code/config/vim/cspell.yaml -r "/Users/onns" ' ..
|
local command = 'cspell --config /Users/onns/.onns/weiyun/code/config/vim/cspell.yaml -r "/Users/onns" '
|
||||||
current_file
|
.. current_file
|
||||||
|
|
||||||
-- 在新的终端窗口中执行CSpell
|
-- 在新的终端窗口中执行CSpell
|
||||||
vim.cmd('split | terminal ' .. command)
|
vim.cmd("split | terminal " .. command)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 将Lua函数绑定到Neovim命令
|
-- 将Lua函数绑定到Neovim命令
|
||||||
vim.api.nvim_create_user_command('SpellCheck', check_spelling, {})
|
vim.api.nvim_create_user_command("SpellCheck", check_spelling, {})
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>pj',
|
vim.api.nvim_set_keymap(
|
||||||
|
"n",
|
||||||
|
"<leader>pj",
|
||||||
[[:.s/\v(\w+) \= (\d+).*;/\1 = \2 [(gogoproto.jsontag) = '\1', json_name = '\1'];<CR>]],
|
[[:.s/\v(\w+) \= (\d+).*;/\1 = \2 [(gogoproto.jsontag) = '\1', json_name = '\1'];<CR>]],
|
||||||
{ noremap = true, silent = true })
|
{ noremap = true, silent = true }
|
||||||
vim.api.nvim_set_keymap('n', '<leader>pf',
|
)
|
||||||
|
vim.api.nvim_set_keymap(
|
||||||
|
"n",
|
||||||
|
"<leader>pf",
|
||||||
[[:.s/\v(\w+) \= (\d+).*;/\1 = \2 [(gogoproto.moretags) = 'form:"\1"',(gogoproto.jsontag) = '\1', json_name = '\1'];<CR>]],
|
[[:.s/\v(\w+) \= (\d+).*;/\1 = \2 [(gogoproto.moretags) = 'form:"\1"',(gogoproto.jsontag) = '\1', json_name = '\1'];<CR>]],
|
||||||
{ noremap = true, silent = true })
|
{ noremap = true, silent = true }
|
||||||
|
)
|
||||||
|
|
||||||
function TodoTelescopeWithCWD()
|
function TodoTelescopeWithCWD()
|
||||||
local pwd = vim.fn.getcwd()
|
local pwd = vim.fn.getcwd()
|
||||||
|
|
@ -113,11 +120,11 @@ function TodoTelescopeWithCWD()
|
||||||
if goplsRootDir then
|
if goplsRootDir then
|
||||||
pwd = goplsRootDir
|
pwd = goplsRootDir
|
||||||
end
|
end
|
||||||
print('TodoTelescope at : ' .. pwd)
|
print("TodoTelescope at : " .. pwd)
|
||||||
vim.cmd("TodoTelescope cwd=" .. pwd)
|
vim.cmd("TodoTelescope cwd=" .. pwd)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>fw', ':lua TodoTelescopeWithCWD()<CR>', { noremap = true })
|
vim.api.nvim_set_keymap("n", "<leader>fw", ":lua TodoTelescopeWithCWD()<CR>", { noremap = true })
|
||||||
|
|
||||||
-- 待定列表
|
-- 待定列表
|
||||||
local targets = { "cmd", "CHANGELOG.md", "go.mod", ".git" }
|
local targets = { "cmd", "CHANGELOG.md", "go.mod", ".git" }
|
||||||
|
|
@ -137,19 +144,19 @@ end
|
||||||
-- 从当前路径向上查找目标文件或目录
|
-- 从当前路径向上查找目标文件或目录
|
||||||
local function find_upwards()
|
local function find_upwards()
|
||||||
-- 获取当前文件的路径
|
-- 获取当前文件的路径
|
||||||
local path = vim.fn.expand('%:p:h')
|
local path = vim.fn.expand("%:p:h")
|
||||||
|
|
||||||
while path and path ~= '' do
|
while path and path ~= "" do
|
||||||
-- 检查目标文件或目录是否在当前路径
|
-- 检查目标文件或目录是否在当前路径
|
||||||
for _, target in pairs(targets) do
|
for _, target in pairs(targets) do
|
||||||
local fullpath = path .. '/' .. target
|
local fullpath = path .. "/" .. target
|
||||||
if file_exists(fullpath) then
|
if file_exists(fullpath) then
|
||||||
return path
|
return path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 移动到上一级目录
|
-- 移动到上一级目录
|
||||||
path = vim.fn.fnamemodify(path, ':h')
|
path = vim.fn.fnamemodify(path, ":h")
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
@ -160,12 +167,11 @@ function TodoTelescopeWithProject()
|
||||||
if projectDir then
|
if projectDir then
|
||||||
pwd = projectDir
|
pwd = projectDir
|
||||||
end
|
end
|
||||||
print('TodoTelescope at : ' .. pwd)
|
print("TodoTelescope at : " .. pwd)
|
||||||
vim.cmd("TodoTelescope cwd=" .. pwd)
|
vim.cmd("TodoTelescope cwd=" .. pwd)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>fp', ':lua TodoTelescopeWithProject()<CR>', { noremap = true })
|
vim.api.nvim_set_keymap("n", "<leader>fp", ":lua TodoTelescopeWithProject()<CR>", { noremap = true })
|
||||||
|
|
||||||
|
|
||||||
function InsertGitBranch()
|
function InsertGitBranch()
|
||||||
local cwd = vim.fn.getcwd()
|
local cwd = vim.fn.getcwd()
|
||||||
|
|
@ -179,15 +185,14 @@ function InsertGitBranch()
|
||||||
local line_num = vim.api.nvim_win_get_cursor(0)[1]
|
local line_num = vim.api.nvim_win_get_cursor(0)[1]
|
||||||
local todo_info = "// TODO: onns " .. git_branch .. " "
|
local todo_info = "// TODO: onns " .. git_branch .. " "
|
||||||
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num - 1, false, { todo_info })
|
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num - 1, false, { todo_info })
|
||||||
vim.api.nvim_command('startinsert')
|
vim.api.nvim_command("startinsert")
|
||||||
vim.api.nvim_win_set_cursor(0, { line_num, #todo_info })
|
vim.api.nvim_win_set_cursor(0, { line_num, #todo_info })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>ig', ':lua InsertGitBranch()<CR>', { noremap = true })
|
vim.api.nvim_set_keymap("n", "<leader>ig", ":lua InsertGitBranch()<CR>", { noremap = true })
|
||||||
|
|
||||||
|
local ts_utils = require("nvim-treesitter.ts_utils")
|
||||||
local ts_utils = require 'nvim-treesitter.ts_utils'
|
|
||||||
|
|
||||||
function JumpToFunctionName()
|
function JumpToFunctionName()
|
||||||
-- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('[f', true, true, true), 'm', true)
|
-- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('[f', true, true, true), 'm', true)
|
||||||
|
|
@ -218,7 +223,7 @@ function JumpToFunctionName()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '[n', '<cmd>lua JumpToFunctionName()<CR>', { noremap = true, silent = true })
|
vim.api.nvim_set_keymap("n", "[n", "<cmd>lua JumpToFunctionName()<CR>", { noremap = true, silent = true })
|
||||||
|
|
||||||
function GetGoImportPath()
|
function GetGoImportPath()
|
||||||
local current_file = vim.api.nvim_buf_get_name(0)
|
local current_file = vim.api.nvim_buf_get_name(0)
|
||||||
|
|
@ -242,48 +247,225 @@ function GetGoImportPath()
|
||||||
|
|
||||||
local module_root = vim.fn.fnamemodify(go_mod_path, ":h")
|
local module_root = vim.fn.fnamemodify(go_mod_path, ":h")
|
||||||
|
|
||||||
local package_path = string.sub(current_path, string.len(module_root .. '/') + 1)
|
local package_path = string.sub(current_path, string.len(module_root .. "/") + 1)
|
||||||
|
|
||||||
local import_package_name = module_name .. "/" .. package_path
|
local import_package_name = module_name .. "/" .. package_path
|
||||||
vim.fn.setreg('+', import_package_name)
|
vim.fn.setreg("+", import_package_name)
|
||||||
print("Import path copied to clipboard: " .. import_package_name)
|
print("Import path copied to clipboard: " .. import_package_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', 'gcp', ':lua GetGoImportPath()<CR>', { noremap = true })
|
vim.api.nvim_set_keymap("n", "gcp", ":lua GetGoImportPath()<CR>", { noremap = true })
|
||||||
|
|
||||||
vim.api.nvim_set_keymap('n', '<leader>fm',
|
vim.api.nvim_set_keymap(
|
||||||
|
"n",
|
||||||
|
"<leader>fm",
|
||||||
":lua require('bookmarks').toggle_bookmarks()<CR>",
|
":lua require('bookmarks').toggle_bookmarks()<CR>",
|
||||||
{ noremap = true, silent = true })
|
{ noremap = true, silent = true }
|
||||||
|
|
||||||
function LiveGrepUp(n)
|
|
||||||
-- 获取当前工作目录
|
|
||||||
local cwd = vim.fn.getcwd()
|
|
||||||
-- 用系统的路径分隔符分割路径
|
|
||||||
local path_sep = package.config:sub(1, 1)
|
|
||||||
local components = {}
|
|
||||||
|
|
||||||
-- 分割路径到各个组件
|
|
||||||
for component in string.gmatch(cwd, "[^" .. path_sep .. "]+") do
|
|
||||||
table.insert(components, component)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 计算向上n级的路径
|
|
||||||
local up_n = #components - n
|
|
||||||
if up_n < 1 then up_n = 1 end -- 确保不会超出根目录
|
|
||||||
local new_cwd = path_sep
|
|
||||||
for i = 1, up_n do
|
|
||||||
new_cwd = new_cwd .. components[i] .. (i < up_n and path_sep or "")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 使用新的目录执行Telescope live_grep
|
|
||||||
require('telescope.builtin').live_grep({ cwd = new_cwd })
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command(
|
|
||||||
'Lg',
|
|
||||||
function(opts)
|
|
||||||
local n = tonumber(opts.args) or 0
|
|
||||||
LiveGrepUp(n)
|
|
||||||
end,
|
|
||||||
{ nargs = 1 } -- 命令接受一个参数
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
local function get_child_index(node)
|
||||||
|
local parent = node:parent()
|
||||||
|
if not parent then
|
||||||
|
return nil, 0
|
||||||
|
end -- 如果没有父节点,则返回nil
|
||||||
|
|
||||||
|
local child_count = parent:child_count()
|
||||||
|
local index = 0
|
||||||
|
-- 遍历父节点的所有子节点
|
||||||
|
for child in parent:iter_children() do
|
||||||
|
if child:id() == node:id() then
|
||||||
|
return index, child_count -- 当找到匹配的子节点时,返回当前索引
|
||||||
|
end
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil, child_count -- 如果没有找到匹配的子节点,返回nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO: onns main 待实现
|
||||||
|
function JumpToCall(direction)
|
||||||
|
local node = ts_utils.get_node_at_cursor()
|
||||||
|
local function search_upwards(current_node)
|
||||||
|
if not current_node then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
print("Child " .. i .. ": " .. child:type())
|
||||||
|
if current_node:type() == "call_expression" then
|
||||||
|
return current_node
|
||||||
|
else
|
||||||
|
local node_index, _ = get_child_index(node)
|
||||||
|
if node_index == 0 then
|
||||||
|
return search_upwards(current_node:parent())
|
||||||
|
else
|
||||||
|
return search_upwards(current_node:parent():child(node_index - 1))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function search_downwards(current_node, count)
|
||||||
|
if count == 200 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if not current_node then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if current_node:type() == "call_expression" then
|
||||||
|
return current_node
|
||||||
|
end
|
||||||
|
for child in current_node:iter_children() do
|
||||||
|
local found = search_downwards(child, count + 1)
|
||||||
|
if found then
|
||||||
|
return found
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
while current_node do
|
||||||
|
local parent = current_node:parent()
|
||||||
|
local node_index, node_count = get_child_index(current_node)
|
||||||
|
if node_count == 0 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if node_index < node_count - 1 then
|
||||||
|
local child = parent:child(node_index + 1)
|
||||||
|
local found = search_downwards(child, count + 1)
|
||||||
|
if found then
|
||||||
|
return found
|
||||||
|
end
|
||||||
|
end
|
||||||
|
current_node = parent:parent()
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local res_node = nil
|
||||||
|
if direction == "up" then
|
||||||
|
res_node = search_upwards(node:parent())
|
||||||
|
elseif direction == "down" then
|
||||||
|
res_node = search_downwards(node, 0)
|
||||||
|
else
|
||||||
|
error("Direction must be 'up' or 'down'.")
|
||||||
|
end
|
||||||
|
if res_node then
|
||||||
|
print("Child count: ->" .. res_node:type())
|
||||||
|
local start_row, start_col, _, _ = res_node:range()
|
||||||
|
vim.api.nvim_win_set_cursor(0, { start_row + 1, start_col })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_set_keymap("n", "]od", '<cmd>lua JumpToCall("down")<CR>', { noremap = true, silent = true })
|
||||||
|
|
||||||
|
local function _set_cursor(node)
|
||||||
|
-- TODO: check the validity of range
|
||||||
|
local start_row, start_col, _, _ = node:range()
|
||||||
|
vim.api.nvim_win_set_cursor(0, { start_row + 1, start_col })
|
||||||
|
end
|
||||||
|
|
||||||
|
function JumpToNextFuncCall()
|
||||||
|
local node = ts_utils.get_node_at_cursor()
|
||||||
|
local ctr = 0 -- ugly prevention of endless loop
|
||||||
|
|
||||||
|
while node do
|
||||||
|
ctr = ctr + 1
|
||||||
|
|
||||||
|
local sibling = node:next_sibling()
|
||||||
|
print(sibling)
|
||||||
|
-- if the current node does not have a leftmore sibling then we traverse its parent node
|
||||||
|
if not sibling then
|
||||||
|
node = node:parent()
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
node = sibling
|
||||||
|
|
||||||
|
-- TODO: which kinds of nodes contain function_call? (for performance enhancement)
|
||||||
|
-- currently, for all previous nodes we do a DFS traverse to find the first "function_call"
|
||||||
|
local first_function_call_node = nil
|
||||||
|
local stack = { node }
|
||||||
|
|
||||||
|
while #stack > 0 do
|
||||||
|
local current_node = table.remove(stack)
|
||||||
|
|
||||||
|
if current_node:type() == "call_expression" then
|
||||||
|
first_function_call_node = current_node
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
local children = {}
|
||||||
|
for child in current_node:iter_children() do
|
||||||
|
table.insert(children, 1, child)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, child in ipairs(children) do
|
||||||
|
table.insert(stack, child)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if first_function_call_node then
|
||||||
|
_set_cursor(first_function_call_node)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if ctr > 1000 then
|
||||||
|
print("endless loop...")
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function JumpToLastFuncCall()
|
||||||
|
local node = ts_utils.get_node_at_cursor()
|
||||||
|
local ctr = 0 -- ugly prevention of endless loop
|
||||||
|
|
||||||
|
while node do
|
||||||
|
ctr = ctr + 1
|
||||||
|
|
||||||
|
local sibling = node:prev_sibling()
|
||||||
|
print(sibling)
|
||||||
|
-- if the current node does not have a leftmore sibling then we traverse its parent node
|
||||||
|
if not sibling then
|
||||||
|
node = node:parent()
|
||||||
|
goto continue
|
||||||
|
end
|
||||||
|
|
||||||
|
node = sibling
|
||||||
|
|
||||||
|
-- TODO: which kinds of nodes contain function_call? (for performance enhancement)
|
||||||
|
-- currently, for all previous nodes we do a DFS traverse to find the last "function_call"
|
||||||
|
local last_function_call_node = nil
|
||||||
|
local stack = { node }
|
||||||
|
|
||||||
|
while #stack > 0 do
|
||||||
|
local current_node = table.remove(stack)
|
||||||
|
|
||||||
|
if current_node:type() == "call_expression" then
|
||||||
|
last_function_call_node = current_node
|
||||||
|
end
|
||||||
|
|
||||||
|
local children = {}
|
||||||
|
for child in current_node:iter_children() do
|
||||||
|
table.insert(children, 1, child)
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, child in ipairs(children) do
|
||||||
|
table.insert(stack, child)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if last_function_call_node then
|
||||||
|
_set_cursor(last_function_call_node)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if ctr > 1000 then
|
||||||
|
print("endless loop...")
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
::continue::
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_set_keymap("n", "]oc", "<cmd>lua JumpToNextFuncCall()<CR>", { noremap = true, silent = true })
|
||||||
|
vim.api.nvim_set_keymap("n", "[oc", "<cmd>lua JumpToLastFuncCall()<CR>", { noremap = true, silent = true })
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue