diff --git a/lua/love2d/init.lua b/lua/love2d/init.lua index ca11410..4344e12 100644 --- a/lua/love2d/init.lua +++ b/lua/love2d/init.lua @@ -4,6 +4,23 @@ local love2d = {} ---@field id number: job-id returned by vim.fn.jobstart ---@field exit_code number: exit-code intercepted by on_exit callback +---Find a valid path to the Love2D project +---@param path string: The path to the Love2D project. If "" search for it. +---@return string?: The path to the Love2D project. nil if not found +love2d.find_src_path = function(path) + local main + if path == "" then + main = vim.fn.findfile("main.lua", ".;") + else + main = vim.fn.findfile("main.lua", path) + end + if main == "" then + vim.notify("No main.lua file found", vim.log.levels.ERROR) + return + end + return vim.fn.fnamemodify(main, ":h") +end + ---Initialize Love2D with options ---@param opts options: The options to initialize Love2D with love2d.setup = function(opts) diff --git a/plugin/love2d.lua b/plugin/love2d.lua index 85215a2..f75369c 100644 --- a/plugin/love2d.lua +++ b/plugin/love2d.lua @@ -1,21 +1,12 @@ vim.api.nvim_create_user_command("LoveRun", function(args) - local path = args.args - local main = "" - if path == "" then - main = vim.fn.findfile("main.lua", ".;") - path = vim.fn.fnamemodify(main, ":h") - else - main = vim.fn.findfile("main.lua", path) - end - if main == "" then - vim.notify("No main.lua file found", vim.log.levels.ERROR) - return - else - ---@cast path string - require("love2d").run(path) + local love2d = require("love2d") + local path = love2d.find_src_path(args.args) + if path then + love2d.run(path) end end, { nargs = "?", complete = "dir" }) -vim.api.nvim_create_user_command("LoveStop", function(args) - require("love2d").stop() +vim.api.nvim_create_user_command("LoveStop", function() + local love2d = require("love2d") + love2d.stop() end, {})