Last active
February 2, 2025 15:16
-
-
Save melMass/47f5c7582c3d9533f8514c2abfe6c862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local M = {} | |
---add suffix to filename while preserving extensiion | |
---@param filename string | |
---@param suffix? string | |
---@return string modified_name, number start_pos, number end_pos | |
local function add_name_suffix(filename, suffix) | |
if not suffix then | |
return filename, 1, #filename | |
end | |
local ext_start = filename:find('%.[^%.]*$') | |
if ext_start then | |
local name = filename:sub(1, ext_start - 1) | |
local ext = filename:sub(ext_start) | |
local new_name = name .. suffix .. ext | |
return new_name, 1, #name + #suffix | |
else -- no extension | |
local new_name = filename .. suffix | |
return new_name, 1, #new_name | |
end | |
end | |
M.yank_path = function(_opts) | |
local oil = require('oil') | |
local entry = oil.get_cursor_entry() | |
local dir = oil.get_current_dir() | |
if not entry or not dir then | |
vim.notify("No entry under cursor or couldn't get current directory") | |
return | |
end | |
vim.fn.setreg(vim.v.register, entry.name .. '\n' .. dir, 'c') | |
end | |
M.paste_symlink = function(opts) | |
local oil = require('oil') | |
local yanked = vim.fn.getreg(vim.v.register) | |
local parts = vim.split(vim.trim(yanked), '\n') | |
if #parts ~= 2 then | |
vim.notify('No proper entry yanked (use actions.yank_path to yank it first)') | |
vim.notify("Found: '" .. yanked .. "' of length (" .. #parts .. ')') | |
return | |
end | |
local src_name, src_dir = parts[1], parts[2] | |
local src_path = src_dir .. src_name | |
local current_dir = oil.get_current_dir() | |
-- add "_link" suffix only if we're in the same directory | |
local suffix = current_dir == src_dir and '_link' or nil | |
local target_name, start_pos, end_pos = add_name_suffix(src_name, suffix) | |
local entry = oil.get_cursor_entry() | |
local line = vim.api.nvim_win_get_cursor(0)[1] | |
if entry ~= nil then | |
line = line + 1 | |
end | |
local link_text = string.format('%s -> %s', target_name, src_path) | |
vim.api.nvim_buf_set_lines(0, line - 1, line, false, { link_text }) | |
-- highlight in visual mode | |
vim.api.nvim_win_set_cursor(0, { line, start_pos - 1 }) | |
vim.cmd('normal! v' .. (end_pos - start_pos) .. 'l') | |
logger:info('Created symlink entry: ' .. link_text) | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment