mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-14 22:21:13 +01:00
Add: live-server
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
"indent-blankline.nvim": { "branch": "master", "commit": "005b56001b2cb30bfa61b7986bc50657816ba4ba" },
|
||||
"kanagawa.nvim": { "branch": "master", "commit": "aef7f5cec0a40dbe7f3304214850c472e2264b10" },
|
||||
"lazy.nvim": { "branch": "main", "commit": "85c7ff3711b730b4030d03144f6db6375044ae82" },
|
||||
"live-server.nvim": { "branch": "main", "commit": "e8a34bc37fc565c678addbb352a59aa6c1842c2b" },
|
||||
"lualine.nvim": { "branch": "master", "commit": "3946f0122255bc377d14a59b27b609fb3ab25768" },
|
||||
"mason-conform.nvim": { "branch": "main", "commit": "48da2ebd5efbaf8a99566eadc3ece82d523f03c5" },
|
||||
"mason-lspconfig.nvim": { "branch": "main", "commit": "7d527c76c43f46294de9c19d39c5a86317809b4b" },
|
||||
|
||||
@@ -187,14 +187,13 @@ local function smart_save(force_save_as)
|
||||
local choice = vim.fn.input(string.format("File '%s' already exists! Overwrite? (y/N): ", filename))
|
||||
if choice:lower() == 'y' then
|
||||
overwrite = true
|
||||
break -- overwrite confirmed
|
||||
break
|
||||
else
|
||||
-- Ask again for a new name
|
||||
default_input = filename
|
||||
print 'Choose a different filename.'
|
||||
end
|
||||
else
|
||||
break -- file doesn't exist, safe to write
|
||||
break
|
||||
end
|
||||
end
|
||||
-- If Save As filename is same as current file, just write and return
|
||||
@@ -215,11 +214,8 @@ local function smart_save(force_save_as)
|
||||
end
|
||||
-- For no-name buffers or Save As, write and set buffer name
|
||||
if current_path == '' then
|
||||
-- No-name buffer: set name first
|
||||
vim.api.nvim_buf_set_name(0, filename)
|
||||
-- >>> filetype detect <<<
|
||||
vim.cmd 'filetype detect'
|
||||
-- Force overwrite if file exists
|
||||
local write_cmd = 'write!'
|
||||
local ok = pcall(function()
|
||||
vim.cmd(write_cmd)
|
||||
@@ -250,15 +246,10 @@ local function smart_save(force_save_as)
|
||||
local old_buf = vim.api.nvim_get_current_buf()
|
||||
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
||||
local undo_history = vim.fn.getbufinfo(old_buf)[1].changedtick
|
||||
-- Open the new file in a fresh buffer
|
||||
vim.cmd('edit ' .. vim.fn.fnameescape(filename))
|
||||
-- >>> filetype detect <<<
|
||||
vim.cmd 'filetype detect'
|
||||
-- Restore cursor
|
||||
vim.api.nvim_win_set_cursor(0, cursor_pos)
|
||||
-- Restore undo history
|
||||
vim.cmd 'undojoin'
|
||||
-- Delete the old buffer without saving
|
||||
vim.api.nvim_buf_delete(old_buf, { force = true })
|
||||
end
|
||||
print('Saved as ' .. filename)
|
||||
@@ -280,11 +271,11 @@ local function smart_save(force_save_as)
|
||||
end
|
||||
-- Save current buffer
|
||||
vim.keymap.set('n', '<leader>w', function()
|
||||
smart_save(false) -- normal save: only ask if buffer is new
|
||||
smart_save(false)
|
||||
end, { desc = 'Save buffer' })
|
||||
-- Save As a new file
|
||||
vim.keymap.set('n', '<leader>W', function()
|
||||
smart_save(true) -- force Save As
|
||||
smart_save(true)
|
||||
end, { desc = 'Save As' })
|
||||
-- Clear sudo password cache on exit
|
||||
vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
|
||||
66
lua/plugins/custom/live-server.lua
Normal file
66
lua/plugins/custom/live-server.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
return {
|
||||
'barrett-ruth/live-server.nvim',
|
||||
build = 'npm install -g live-server',
|
||||
cmd = { 'LiveServerStart', 'LiveServerStop' },
|
||||
keys = {
|
||||
{
|
||||
'<leader>ls',
|
||||
function()
|
||||
vim.cmd 'LiveServerStart'
|
||||
_G.live_server_autosave = true
|
||||
print '✓ Live server started, auto-save enabled'
|
||||
end,
|
||||
desc = 'Start Live Server',
|
||||
},
|
||||
{
|
||||
'<leader>lx',
|
||||
function()
|
||||
vim.cmd 'LiveServerStop'
|
||||
_G.live_server_autosave = false
|
||||
if _G.live_server_timer then
|
||||
_G.live_server_timer:stop()
|
||||
_G.live_server_timer = nil
|
||||
end
|
||||
print '✗ Live server stopped, auto-save disabled'
|
||||
end,
|
||||
desc = 'Stop Live Server',
|
||||
},
|
||||
},
|
||||
config = function()
|
||||
require('live-server').setup()
|
||||
_G.live_server_autosave = true
|
||||
_G.live_server_timer = nil
|
||||
vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, {
|
||||
pattern = { '*.html', '*.css', '*.js' },
|
||||
callback = function()
|
||||
if not _G.live_server_autosave then
|
||||
return
|
||||
end
|
||||
if _G.live_server_timer then
|
||||
_G.live_server_timer:stop()
|
||||
end
|
||||
_G.live_server_timer = vim.defer_fn(function()
|
||||
if vim.bo.modified then
|
||||
vim.cmd 'silent! write'
|
||||
end
|
||||
_G.live_server_timer = nil
|
||||
end, 200)
|
||||
end,
|
||||
})
|
||||
|
||||
-- Custom commands
|
||||
vim.api.nvim_create_user_command('LiveServerAutoSaveOn', function()
|
||||
_G.live_server_autosave = true
|
||||
print '✓ Auto-save enabled'
|
||||
end, {})
|
||||
|
||||
vim.api.nvim_create_user_command('LiveServerAutoSaveOff', function()
|
||||
_G.live_server_autosave = false
|
||||
if _G.live_server_timer then
|
||||
_G.live_server_timer:stop()
|
||||
_G.live_server_timer = nil
|
||||
end
|
||||
print '✗ Auto-save disabled'
|
||||
end, {})
|
||||
end,
|
||||
}
|
||||
Reference in New Issue
Block a user