diff --git a/lua/doom/core/commands.lua b/lua/doom/core/commands.lua index 54522f49..89d22a84 100644 --- a/lua/doom/core/commands.lua +++ b/lua/doom/core/commands.lua @@ -8,3 +8,5 @@ vim.cmd([[command! DoomManual lua require("doom.core.functions").open_docs()]]) -- Set a custom command to create a crash report can be called by using -- :DoomReport. vim.cmd([[command! DoomReport lua require("doom.core.functions").create_report(]]) + +vim.cmd([[command! -nargs=* DoomNuke lua require("doom.core.functions").nuke()]]) diff --git a/lua/doom/core/functions.lua b/lua/doom/core/functions.lua index 04cc0755..40870374 100644 --- a/lua/doom/core/functions.lua +++ b/lua/doom/core/functions.lua @@ -178,8 +178,9 @@ end -- Set the indent and tab related numbers. -- Negative numbers mean tabstop -- Really though? Tabs? functions.set_indent = function() - local indent = - tonumber(vim.fn.input("Set indent (>0 uses spaces, <0 uses tabs, 0 uses vim defaults): ")) + local indent = tonumber( + vim.fn.input("Set indent (>0 uses spaces, <0 uses tabs, 0 uses vim defaults): ") + ) if not indent then indent = -8 end @@ -251,4 +252,30 @@ functions.sugar_folds = function() return string.format("%s ... (%d lines)", start_line, vim.v.foldend - vim.v.foldstart + 1) end +--- Nukes the doom install config, causes a fresh install on next boot. +---@param target string Options of what to nuke +functions.nuke = function(target) + if target == nil or #target == 0 then + vim.notify( + "Warning, this command deletes packer caches and causes a re-install of doom-nvim on next launch.\n\n :DoomNuke `plugins`|`cache`|`all`. \n\t `cache` - Clear packer_compiled.lua\n\t `plugins` - Clear all installed plugins\n\t `all` - Delete all of the above." + ) + return + end + + local log = require("doom.utils.logging") + -- Delete packer compiled + if target == "all" or target == "cache" then + os.remove(system.doom_compile_path) + log.info("DoomNuke: Deleting packer compiled.") + end + + if target == "all" or target == "plugins" then + -- Delete all plugins + local util = require("packer.util") + local plugin_dir = util.join_paths(vim.fn.stdpath("data"), "site", "pack") + fs.rm_dir(plugin_dir) + log.info("DoomNuke: Deleting packer plugins. Doom-nvim will re-install on next launch.") + end +end + return functions diff --git a/lua/doom/utils/fs.lua b/lua/doom/utils/fs.lua index 7d819a41..c30e14ac 100644 --- a/lua/doom/utils/fs.lua +++ b/lua/doom/utils/fs.lua @@ -1,5 +1,31 @@ +local luv = vim.loop local fs = {} +if jit ~= nil then + fs.is_windows = jit.os == 'Windows' +else + fs.is_windows = package.config:sub(1, 1) == '\\' +end + +if fs.is_windows and vim.o.shellslash then + fs.use_shellslash = true +else + fs.use_shallslash = false +end + +fs.get_seperator = function() + if fs.is_windows and not fs.use_shellslash then + return '\\' + end + return '/' +end + +--- Joins a number of strings into a valid path +---@vararg string[] String segments to convert to file system path +fs.join_paths = function(...) + return table.concat({ ... }, fs.get_seperator()) +end + --- Check if the given file exists --- @param path string The path of the file --- @return boolean @@ -41,4 +67,34 @@ fs.write_file = function(path, content, mode) end) end +fs.rm_dir = function(path) + local handle = luv.fs_scandir(path) + + if type(handle) == "string" then + return fs.notify.error(handle) + end + + while true do + local name, t = luv.fs_scandir_next(handle) + if not name then + break + end + + local new_cwd = fs.join_paths( path, name ) + if t == "directory" then + local success = fs.rm_dir(new_cwd) + if not success then + return false + end + else + local success = luv.fs_unlink(new_cwd) + if not success then + return false + end + end + end + + return luv.fs_rmdir(path) +end + return fs