From 7b40b51027a70793295ad367e42261d0251ee28e Mon Sep 17 00:00:00 2001 From: Timofey K Date: Thu, 5 Sep 2024 00:02:57 +0300 Subject: [PATCH] draft --- lua/copy_path/init.lua | 50 ++++++++++++++++++++++++++++++++++++++++++ readme.md | 9 ++++++++ 2 files changed, 59 insertions(+) create mode 100644 lua/copy_path/init.lua create mode 100644 readme.md diff --git a/lua/copy_path/init.lua b/lua/copy_path/init.lua new file mode 100644 index 0000000..b696a58 --- /dev/null +++ b/lua/copy_path/init.lua @@ -0,0 +1,50 @@ +local M = { + path = nil +} + +function M.copy_file_path() + local file_path = vim.fn.expand('') + + 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:, path::, path::: + 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 + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..c4e2331 --- /dev/null +++ b/readme.md @@ -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) +```