This commit is contained in:
Timofey K 2024-09-05 00:02:57 +03:00
commit 7b40b51027
2 changed files with 59 additions and 0 deletions

50
lua/copy_path/init.lua Normal file
View file

@ -0,0 +1,50 @@
local M = {
path = nil
}
function M.copy_file_path()
local file_path = vim.fn.expand('<cWORD>')
if file_path == '' then
print('No file path found under the cursor')
return
end
M.path = file_path
end
function M.open_file()
local path = M.path
if path == nil or path == '' then
print('Register "c" is empty')
return
end
-- possible path, path:<line>, path:<line>:<col>, path:<line>:<col>:
local pattern = "^([^:]*):?(%d*):?(%d*):?$"
local file_path, line_number, line_col = string.match(path, pattern)
if line_number and line_number == "" then
line_number = nil
end
if line_col and line_col == "" then
line_col = nil
end
if file_path and line_number then
line_number = tonumber(line_number)
line_col = (line_col and tonumber(line_col) - 1) or 0
vim.cmd('edit ' .. file_path)
vim.api.nvim_win_set_cursor(0, {line_number, line_col})
elseif file_path and file_path ~= "" then
vim.cmd('edit ' .. file_path)
else
print('Invalid format in register "c". Expected "filepath:linenumber" but get', path)
end
end
return M

9
readme.md Normal file
View file

@ -0,0 +1,9 @@
### Copy path
This plugin allows you to copy the path that the cursor points to in the vim, and then open this file in any buffer
```
vim.keymap.set("n", "gy", require("copy_path").copy_file_path)
vim.keymap.set("n", "gp", require("copy_path").open_file)
```