From 6689ac3812e29497c8aa6dc27e8f0e25a12054c2 Mon Sep 17 00:00:00 2001 From: S1M0N38 Date: Sun, 11 Feb 2024 15:43:24 +0100 Subject: [PATCH] =?UTF-8?q?feat(config):=20add=20option=20to=20restart=20L?= =?UTF-8?q?=C3=96VE=20on=20file=20save?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/love2d/config.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lua/love2d/config.lua b/lua/love2d/config.lua index aae49fc..b61f44b 100644 --- a/lua/love2d/config.lua +++ b/lua/love2d/config.lua @@ -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 @@ -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 @@ -40,6 +64,7 @@ config.setup = function(opts) end setup_lsp(library_path) end + create_auto_commands() end return config