Update: friendly-snippets replacement fix when used nvim command with sudo.

This commit is contained in:
psychhim
2025-10-28 01:49:08 +05:30
parent 522d04f7c2
commit a13c4f18ad

View File

@@ -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()