From a13c4f18ad5e9fa8b917eeaec80679d7b2405fc5 Mon Sep 17 00:00:00 2001 From: psychhim Date: Tue, 28 Oct 2025 01:49:08 +0530 Subject: [PATCH] Update: friendly-snippets replacement fix when used nvim command with sudo. --- lua/replace_with_custom_snippets.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lua/replace_with_custom_snippets.lua b/lua/replace_with_custom_snippets.lua index ee5d097..bc81def 100644 --- a/lua/replace_with_custom_snippets.lua +++ b/lua/replace_with_custom_snippets.lua @@ -1,16 +1,20 @@ local uv = vim.loop -local home = uv.os_homedir() +-- Force user home directory so script works even under sudo +local user_home = '/home/hexzon3' + -- Detect OS and base path of friendly-snippets local function get_friendly_snippets_base_path() local os_name = uv.os_uname().sysname if os_name == 'Windows_NT' then - return home .. '\\.local\\share\\nvim\\lazy\\friendly-snippets\\snippets\\' + return user_home .. '\\.local\\share\\nvim\\lazy\\friendly-snippets\\snippets\\' else - return home .. '/.local/share/nvim/lazy/friendly-snippets/snippets/' + return user_home .. '/.local/share/nvim/lazy/friendly-snippets/snippets/' end end + local target_base = get_friendly_snippets_base_path() local custom_snippets_dir = vim.fn.stdpath 'config' .. '/custom_friendly_snippets/' + -- Read all files in custom_snippets_dir local function get_custom_snippet_files() local handle = uv.fs_scandir(custom_snippets_dir) @@ -30,12 +34,14 @@ local function get_custom_snippet_files() end return files end + -- Replace each snippet file local function replace_snippets() local files = get_custom_snippet_files() for _, filename in ipairs(files) do local source_path = custom_snippets_dir .. filename local target_path = target_base .. filename + -- Read source content local source_file = io.open(source_path, 'r') if not source_file then @@ -44,6 +50,7 @@ local function replace_snippets() end local source_content = source_file:read '*a' source_file:close() + -- Read target content (if exists) local target_content = '' local target_file = io.open(target_path, 'r') @@ -51,6 +58,7 @@ local function replace_snippets() target_content = target_file:read '*a' target_file:close() end + -- Only update if different if source_content ~= target_content then -- Create backup if not exists @@ -64,6 +72,7 @@ local function replace_snippets() vim.notify('Failed to create backup: ' .. backup_path, vim.log.levels.ERROR) end end + -- Write updated content local output = io.open(target_path, 'w') if output then @@ -74,8 +83,10 @@ local function replace_snippets() vim.notify('Failed to write: ' .. target_path, vim.log.levels.ERROR) end end + ::continue:: end end + -- Run the function replace_snippets()