Skip to content

Commit

Permalink
refactor: move project search into find_src_path
Browse files Browse the repository at this point in the history
  • Loading branch information
S1M0N38 committed Feb 11, 2024
1 parent 7325877 commit 2bc68e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
17 changes: 17 additions & 0 deletions lua/love2d/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 7 additions & 16 deletions plugin/love2d.lua
Original file line number Diff line number Diff line change
@@ -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, {})

0 comments on commit 2bc68e1

Please sign in to comment.