Skip to content

Commit

Permalink
feat(config): add option to restart LÖVE on file save
Browse files Browse the repository at this point in the history
  • Loading branch information
S1M0N38 committed Feb 11, 2024
1 parent 00229e7 commit 6689ac3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lua/love2d/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ local config = {}
config.defaults = {
path_to_love = "love",
path_to_love_library = vim.fn.globpath(vim.o.runtimepath, "love2d/library"),
restart_on_save = false,
}

---@class options
---@field path_to_love? string: The path to the Love2D executable
---@field path_to_love_library? string: The path to the Love2D library. Set to "" to disable LSP
---@field restart_on_save? boolean: Restart Love2D when a file is saved
config.options = {}

---Setup the LSP for love2d using lspconfig
Expand All @@ -27,6 +29,28 @@ local function setup_lsp(library_path)
end
end

---Create auto commands for love2d:
--- - Restart on save: Restart Love2D when a file is saved.
local function create_auto_commands()
if config.options.restart_on_save then
vim.api.nvim_create_autocmd("BufWritePost", {
group = vim.api.nvim_create_augroup("love2d_restart_on_save", { clear = true }),
pattern = { "*.lua" },
callback = function()
local love2d = require("love2d")
local path = love2d.find_src_path("")
if path then
love2d.stop()
vim.defer_fn(function()
love2d.run(path)
end, 500)
end
end,
})
end
-- add here other auto commands ...
end

---Setup the love2d configuration.
---It must be called before running a love2d project.
---@param opts? options: config table
Expand All @@ -40,6 +64,7 @@ config.setup = function(opts)
end
setup_lsp(library_path)
end
create_auto_commands()
end

return config

0 comments on commit 6689ac3

Please sign in to comment.