mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-07-01 16:42:21 +02:00
Fix: replace_with_custom_snippets.lua breaking the original repo folder
This commit is contained in:
3
init.lua
3
init.lua
@@ -8,9 +8,6 @@ vim.g.loaded_netrwPlugin = 1
|
|||||||
-- keymaps.lua
|
-- keymaps.lua
|
||||||
require 'keymaps'
|
require 'keymaps'
|
||||||
|
|
||||||
-- Copy custom snippets from custom_friendly_snippets folder
|
|
||||||
require 'replace_with_custom_snippets'
|
|
||||||
|
|
||||||
-- :UpdateKickestEnd command to safely update KickestEnd.nvim config from origin/master
|
-- :UpdateKickestEnd command to safely update KickestEnd.nvim config from origin/master
|
||||||
require 'update_kickestend'
|
require 'update_kickestend'
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,45 @@ return {
|
|||||||
local cmp = require 'cmp'
|
local cmp = require 'cmp'
|
||||||
local luasnip = require 'luasnip'
|
local luasnip = require 'luasnip'
|
||||||
require('luasnip.loaders.from_vscode').lazy_load()
|
require('luasnip.loaders.from_vscode').lazy_load()
|
||||||
--require('luasnip.loaders.from_vscode').lazy_load({ paths = { "~/.config/nvim/my_snippets" } })
|
do
|
||||||
|
local parser = require 'luasnip.util.parser'
|
||||||
|
local custom_dir = vim.fn.stdpath 'config' .. '/custom_friendly_snippets'
|
||||||
|
local handle = vim.loop.fs_scandir(custom_dir)
|
||||||
|
if handle then
|
||||||
|
while true do
|
||||||
|
local name, ftype = vim.loop.fs_scandir_next(handle)
|
||||||
|
if not name then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
if ftype == 'file' and name:match '%.json$' then
|
||||||
|
local filetype = name:gsub('%.json$', '')
|
||||||
|
local lines_ok, lines = pcall(vim.fn.readfile, custom_dir .. '/' .. name)
|
||||||
|
if lines_ok then
|
||||||
|
local decode_ok, data = pcall(vim.json.decode, table.concat(lines, '\n'))
|
||||||
|
if decode_ok and type(data) == 'table' then
|
||||||
|
local snippets = {}
|
||||||
|
for snip_name, snip in pairs(data) do
|
||||||
|
local prefixes = type(snip.prefix) == 'table' and snip.prefix or { snip.prefix }
|
||||||
|
local body = type(snip.body) == 'table' and table.concat(snip.body, '\n') or snip.body
|
||||||
|
for _, prefix in ipairs(prefixes) do
|
||||||
|
local parse_ok, parsed =
|
||||||
|
pcall(parser.parse_snippet, { trig = prefix, name = snip_name, desc = snip.description }, body)
|
||||||
|
if parse_ok then
|
||||||
|
table.insert(snippets, parsed)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
luasnip.add_snippets(filetype, snippets, { default_priority = 2000 })
|
||||||
|
else
|
||||||
|
vim.notify('Failed to decode custom snippet file: ' .. name, vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
vim.notify('Failed to read custom snippet file: ' .. name, vim.log.levels.ERROR)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
luasnip.config.setup {}
|
luasnip.config.setup {}
|
||||||
cmp.setup {
|
cmp.setup {
|
||||||
snippet = {
|
snippet = {
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
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
|
|
||||||
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
|
|
||||||
|
|
||||||
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
|
|
||||||
-- Wait until lazy.nvim has finished loading plugins
|
|
||||||
vim.api.nvim_create_autocmd('User', {
|
|
||||||
pattern = 'LazyDone',
|
|
||||||
callback = replace_snippets,
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user