From 633a2606050f678d0393fc1feb2b9af28fe9a657 Mon Sep 17 00:00:00 2001 From: Micah Halter Date: Mon, 17 Feb 2025 13:26:16 -0500 Subject: [PATCH 1/3] feat: add `rename_file` method for prompting the user to rename a file --- lua/astrocore/init.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lua/astrocore/init.lua b/lua/astrocore/init.lua index 8beacee..87a8316 100644 --- a/lua/astrocore/init.lua +++ b/lua/astrocore/init.lua @@ -427,6 +427,39 @@ function M.with_file(filename, mode, callback, on_error) end end +--- Prompt the user to rename a file +---@param file? string the file to be renamed (defaults to name of current buffer) +---@param on_rename? fun(new: string, old: string) a function to execute after the file is renamed +function M.rename_file(file, on_rename) + local buf = file and vim.fn.bufadd(file) or vim.api.nvim_get_current_buf() + local from = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buf), ":p") + local uv = vim.uv or vim.loop + local root = uv.cwd() or "." + root = vim.fs.normalize(uv.fs_realpath(root) or root) + + if from:find(root, 1, true) ~= 1 then root = vim.fn.fnamemodify(from, ":p:h") end + + local extra = from:sub(#root + 2) + + vim.ui.input({ + prompt = "New File Name: ", + default = extra, + completion = "file", + }, function(to) + if not to or to == "" or to == extra then return end + to = vim.fs.normalize(root .. "/" .. to) + vim.fn.mkdir(vim.fs.dirname(to), "p") + local rename_data = { from = from, to = to } + M.event({ pattern = "RenameFilePre", data = rename_data }, true) + vim.fn.rename(from, to) + if not on_rename then vim.cmd.edit(to) end + vim.api.nvim_buf_delete(buf, { force = true }) + vim.fn.delete(from) + if on_rename then on_rename(to, from) end + M.event({ pattern = "RenameFilePost", data = rename_data }, true) + end) +end + --- Setup and configure AstroCore ---@param opts AstroCoreOpts ---@see astrocore.config From c9acfa9cc7d68d92540936c1a6887f859e073b1f Mon Sep 17 00:00:00 2001 From: AstroNvim Bot Date: Mon, 17 Feb 2025 18:35:12 +0000 Subject: [PATCH 2/3] chore(build): auto-generate vimdoc --- doc/api.md | 13 +++++++++++++ doc/astrocore.txt | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/api.md b/doc/api.md index 54fd44b..f05c3cc 100644 --- a/doc/api.md +++ b/doc/api.md @@ -287,6 +287,19 @@ function astrocore.reload() Partially reload AstroNvim user settings. Includes core vim options, mappings, and highlights. This is an experimental feature and may lead to instabilities until restart. +### rename_file + + +```lua +function astrocore.rename_file(file?: string, on_rename?: fun(new: string, old: string)) +``` + + Prompt the user to rename a file + +*param* `file` — the file to be renamed (defaults to name of current buffer) + +*param* `on_rename` — a function to execute after the file is renamed + ### set_mappings diff --git a/doc/astrocore.txt b/doc/astrocore.txt index ad2d531..3fb0e56 100644 --- a/doc/astrocore.txt +++ b/doc/astrocore.txt @@ -1,4 +1,4 @@ -*astrocore.txt* For Neovim >= 0.9.0 Last change: 2025 February 14 +*astrocore.txt* For Neovim >= 0.9.0 Last change: 2025 February 17 ============================================================================== Table of Contents *astrocore-table-of-contents* @@ -540,6 +540,19 @@ and highlights. This is an experimental feature and may lead to instabilities until restart. +RENAME_FILE ~ + +>lua + function astrocore.rename_file(file?: string, on_rename?: fun(new: string, old: string)) +< + +Prompt the user to rename a file + +_param_ `file` — the file to be renamed (defaults to name of current buffer) + +_param_ `on_rename` — a function to execute after the file is renamed + + SET_MAPPINGS ~ >lua From 063eeedad397f7cf09b99bee320894795ca7259c Mon Sep 17 00:00:00 2001 From: AstroNvim Bot Date: Mon, 17 Feb 2025 13:35:07 -0500 Subject: [PATCH 3/3] chore(main): release 1.15.0 --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a495d..f8ff8f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [1.15.0](https://github.com/AstroNvim/astrocore/compare/v1.14.0...v1.15.0) (2025-02-17) + + +### Features + +* add `rename_file` method for prompting the user to rename a file ([633a260](https://github.com/AstroNvim/astrocore/commit/633a2606050f678d0393fc1feb2b9af28fe9a657)) + ## [1.14.0](https://github.com/AstroNvim/astrocore/compare/v1.13.2...v1.14.0) (2025-02-14)