Merge pull request #6 from mathstylish/feat/improvements

Improvements
This commit is contained in:
Barrett Ruth 2024-02-12 16:22:08 -05:00 committed by GitHub
commit 4e1dd2956b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -23,11 +23,26 @@ local function find_cached_dir(dir)
return cur
end
local function is_running(dir)
local cached_dir = find_cached_dir(dir)
return cached_dir and job_cache[cached_dir]
end
M.config = {
-- let live-server handle the defaults
args = {},
-- 8080 default is commonly used
args = { '--port=5555' },
}
M.toggle = function()
local dir = vim.fn.expand '%:p:h'
local running = is_running(dir)
if not running then
M.start()
return
end
M.stop()
end
M.setup = function(user_config)
M.config = vim.tbl_deep_extend('force', M.config, user_config or {})
@ -41,13 +56,14 @@ M.setup = function(user_config)
vim.api.nvim_create_user_command('LiveServerStart', M.start, {})
vim.api.nvim_create_user_command('LiveServerStop', M.stop, {})
vim.api.nvim_create_user_command('LiveServerToggle', M.toggle, {})
end
M.start = function()
local dir = vim.fn.expand '%:p:h'
local cached_dir = find_cached_dir(dir)
local running = is_running(dir)
if cached_dir then
if running then
log('live-server already running', 'INFO')
return
end
@ -82,11 +98,15 @@ end
M.stop = function()
local dir = vim.fn.expand '%:p:h'
local cached_dir = find_cached_dir(dir)
local running = is_running(dir)
if cached_dir then
vim.fn.jobstop(job_cache[cached_dir])
job_cache[cached_dir] = nil
if running then
local cached_dir = find_cached_dir(dir)
if cached_dir then
vim.fn.jobstop(job_cache[cached_dir])
job_cache[cached_dir] = nil
log('live-server stopped', 'INFO')
end
end
end