mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 00:21:15 +01:00
Update: custom_friendly_snippets folder for all snippets.
This commit is contained in:
4
init.lua
4
init.lua
@@ -271,8 +271,8 @@ require('lazy').setup({
|
||||
-- custom/keymaps.lua file
|
||||
require 'keymaps'
|
||||
|
||||
-- HTML snippet fixer
|
||||
require 'replace_html_snippets'
|
||||
-- Copy custom snippets from custom_friendly_snippets folder
|
||||
require 'replace_with_custom_snippets'
|
||||
|
||||
-- [[ Setting options ]]
|
||||
-- See `:help vim.o`
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
local uv = vim.loop
|
||||
local home = uv.os_homedir()
|
||||
-- Detect OS and friendly-snippets path
|
||||
local function get_friendly_snippets_html_path()
|
||||
local os_name = uv.os_uname().sysname
|
||||
if os_name == 'Windows_NT' then
|
||||
return home .. '\\.local\\share\\nvim\\lazy\\friendly-snippets\\snippets\\html.json'
|
||||
else
|
||||
return home .. '/.local/share/nvim/lazy/friendly-snippets/snippets/html.json'
|
||||
end
|
||||
end
|
||||
local target_path = get_friendly_snippets_html_path()
|
||||
local source_path = vim.fn.stdpath 'config' .. '/lua/custom_html_snippets.json'
|
||||
-- Read source file
|
||||
local source_file = io.open(source_path, 'r')
|
||||
if not source_file then
|
||||
vim.notify('Custom html.json not found at ' .. source_path, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
local source_content = source_file:read '*a'
|
||||
source_file:close()
|
||||
-- Read target file
|
||||
local target_content = ''
|
||||
local target_file = io.open(target_path, 'r')
|
||||
if target_file then
|
||||
target_content = target_file:read '*a'
|
||||
target_file:close()
|
||||
end
|
||||
-- Only update if the contents differ
|
||||
if target_content ~= source_content then
|
||||
-- Create backup only if it doesn't exist
|
||||
local backup_path = target_path .. '.bak'
|
||||
if not uv.fs_stat(backup_path) and target_content ~= '' then
|
||||
local backup_file = io.open(backup_path, 'w')
|
||||
if backup_file then
|
||||
backup_file:write(target_content)
|
||||
backup_file:close()
|
||||
else
|
||||
vim.notify('Failed to create backup: ' .. backup_path, vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
-- Write custom file
|
||||
local output = io.open(target_path, 'w')
|
||||
if output then
|
||||
output:write(source_content)
|
||||
output:close()
|
||||
else
|
||||
vim.notify('Failed to write custom html.json to ' .. target_path, vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
81
lua/replace_with_custom_snippets.lua
Normal file
81
lua/replace_with_custom_snippets.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
local uv = vim.loop
|
||||
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 home .. '\\.local\\share\\nvim\\lazy\\friendly-snippets\\snippets\\'
|
||||
else
|
||||
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)
|
||||
if not handle then
|
||||
vim.notify('Custom snippets folder not found: ' .. custom_snippets_dir, vim.log.levels.ERROR)
|
||||
return {}
|
||||
end
|
||||
local files = {}
|
||||
while true do
|
||||
local name, type = uv.fs_scandir_next(handle)
|
||||
if not name then
|
||||
break
|
||||
end
|
||||
if type == 'file' then
|
||||
table.insert(files, name)
|
||||
end
|
||||
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)
|
||||
goto continue
|
||||
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')
|
||||
if target_file then
|
||||
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
|
||||
local backup_path = target_path .. '.bak'
|
||||
if not uv.fs_stat(backup_path) and target_content ~= '' then
|
||||
local backup_file = io.open(backup_path, 'w')
|
||||
if backup_file then
|
||||
backup_file:write(target_content)
|
||||
backup_file:close()
|
||||
else
|
||||
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
|
||||
output:write(source_content)
|
||||
output:close()
|
||||
vim.notify('Updated snippet: ' .. filename, vim.log.levels.INFO)
|
||||
else
|
||||
vim.notify('Failed to write: ' .. target_path, vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
-- Run the function
|
||||
replace_snippets()
|
||||
Reference in New Issue
Block a user