Fix: rollback to non-sudo custom_snippet replacement handler

This commit is contained in:
psychhim
2025-12-05 02:14:57 +05:30
parent ff70fbac6f
commit 0a7572896d
2 changed files with 19 additions and 32 deletions

View File

@@ -1,20 +1,16 @@
local uv = vim.loop
-- Force user home directory so script works even under sudo
local user_home = '/home/hexzon3'
local home = uv.os_homedir()
-- 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 user_home .. '\\.local\\share\\nvim\\lazy\\friendly-snippets\\snippets\\'
return home .. '\\.local\\share\\nvim\\lazy\\friendly-snippets\\snippets\\'
else
return user_home .. '/.local/share/nvim/lazy/friendly-snippets/snippets/'
return 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)
@@ -34,15 +30,12 @@ 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
vim.notify('Failed to read: ' .. source_path, vim.log.levels.ERROR)
@@ -50,7 +43,6 @@ 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')
@@ -58,7 +50,6 @@ 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
@@ -73,7 +64,6 @@ local function replace_snippets()
end
end
-- Write updated content
local output = io.open(target_path, 'w')
if output then
output:write(source_content)
@@ -83,10 +73,7 @@ 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()