docs: docs for rename

This commit is contained in:
Folke Lemaitre 2024-11-06 13:07:44 +01:00
parent 6673551e12
commit dd97a2a659
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 40 additions and 3 deletions

View file

@ -1,5 +1,40 @@
# 🍿 rename
LSP-integrated file renaming with support for plugins like
[neo-tree.nvim](https://github.com/nvim-neo-tree/neo-tree.nvim) and [mini.files](https://github.com/echasnovski/mini.files).
## 🚀 Usage
## [mini.files](https://github.com/echasnovski/mini.files)
```lua
vim.api.nvim_create_autocmd("User", {
pattern = "MiniFilesActionRename",
callback = function(event)
Snacks.rename.on_rename_file(event.data.from, event.data.to)
end,
})
```
## [neo-tree.nvim](https://github.com/nvim-neo-tree/neo-tree.nvim)
```lua
{
"nvim-neo-tree/neo-tree.nvim",
opts = function(_, opts)
local function on_move(data)
Snacks.rename.on_rename_file(data.source, data.destination)
end
local events = require("neo-tree.events")
opts.event_handlers = opts.event_handlers or {}
vim.list_extend(opts.event_handlers, {
{ event = events.FILE_MOVED, handler = on_move },
{ event = events.FILE_RENAMED, handler = on_move },
})
end,
}
```
<!-- docgen -->
## 📦 Module

View file

@ -5,14 +5,16 @@ local M = {}
local uv = vim.uv or vim.loop
---@param path string
function M.realpath(path)
local function realpath(path)
return vim.fs.normalize(uv.fs_realpath(path) or path)
end
-- Prompt for the new filename,
-- do the rename, and trigger LSP handlers
function M.rename_file()
local buf = vim.api.nvim_get_current_buf()
local old = assert(M.realpath(vim.api.nvim_buf_get_name(buf)))
local root = assert(M.realpath(uv.cwd() or "."))
local old = assert(realpath(vim.api.nvim_buf_get_name(buf)))
local root = assert(realpath(uv.cwd() or "."))
if old:find(root, 1, true) ~= 1 then
root = vim.fn.fnamemodify(old, ":p:h")