@@ -79,6 +79,9 @@ M.loaded = false
7979--- @type DefoldNvimConfig
8080M .config = default_config
8181
82+ --- @type integer | nil
83+ M .prev_editor_port = nil
84+
8285--- Returns true if we are in a defold project
8386--- @return boolean
8487function M .is_defold_project ()
@@ -104,6 +107,8 @@ function M.setup(opts)
104107
105108 M .config = vim .tbl_deep_extend (" force" , default_config , opts or {})
106109
110+ -- TODO: check if sidecar is available, if not download it (which shouldnt be necessary with some pkg managers)
111+
107112 -- persist config for babashka
108113 local config_path = babashka .config_path ()
109114 vim .fn .writefile ({
@@ -196,6 +201,18 @@ function M.setup(opts)
196201 end , 0 )
197202end
198203
204+ --- @return integer
205+ function M .editor_port ()
206+ if M .prev_editor_port then
207+ -- TODO: validate port
208+ return M .prev_editor_port
209+ end
210+
211+ local sidecar = require " defold.sidecar"
212+ M .prev_editor_port = sidecar .find_editor_port ()
213+ return M .prev_editor_port
214+ end
215+
199216function M .load_plugin ()
200217 if M .loaded then
201218 return
@@ -206,13 +223,15 @@ function M.load_plugin()
206223 -- register all filetypes
207224 vim .filetype .add (require (" defold.config.filetype" ).full )
208225
226+ local sidecar = require " defold.sidecar"
209227 local babashka = require " defold.service.babashka"
210228 local debugger = require " defold.service.debugger"
211229 local editor = require " defold.editor"
212230 local log = require " defold.service.logger"
213231 local project = require " defold.project"
214232
215233 log .debug " ============= defold.nvim: Loaded plugin"
234+ log .debug (" Sidecar Version:" .. sidecar .version )
216235 log .debug (" Babashka Path: " .. babashka .bb_path ())
217236 log .debug (" Mobdap Path: " .. debugger .mobdap_path ())
218237 log .debug (" Config: " .. vim .inspect (M .config ))
@@ -222,7 +241,7 @@ function M.load_plugin()
222241 vim .api .nvim_create_autocmd (" BufWritePost" , {
223242 pattern = { " *.lua" , " *.script" , " *.gui_script" },
224243 callback = function ()
225- editor .send_command (" hot-reload" , true )
244+ editor .send_command (M . editor_port (), " hot-reload" , true )
226245 end ,
227246 })
228247 end
@@ -256,26 +275,26 @@ function M.load_plugin()
256275 return
257276 end
258277
259- editor .send_command (cmds [idx ])
278+ editor .send_command (M . editor_port (), cmds [idx ])
260279 end )
261280 end , { nargs = 0 , desc = " Select a command to run" })
262281
263282 -- add the ":DefoldSend cmd" command to send commands to the editor
264283 vim .api .nvim_create_user_command (" DefoldSend" , function (opt )
265- editor .send_command (opt .args )
284+ editor .send_command (M . editor_port (), opt .args )
266285 end , { nargs = 1 , desc = " Send a command to the Defold editor" })
267286
268287 -- add the ":DefoldFetch" command to fetch dependencies & annoatations
269288 vim .api .nvim_create_user_command (" DefoldFetch" , function (opt )
270289 -- when a user runs DefoldFetch I recon they also expect us to update the dependencies
271- editor .send_command (" fetch-libraries" , true )
290+ editor .send_command (M . editor_port (), " fetch-libraries" , true )
272291
273292 project .install_dependencies (opt .bang )
274293 end , { bang = true , nargs = 0 , desc = " Fetch & create Defold project dependency annotations" })
275294
276295 -- integrate the debugger into dap
277296 if M .config .debugger .enable then
278- debugger .register_nvim_dap ()
297+ debugger .register_nvim_dap (M . editor_port )
279298 end
280299
281300 -- add snippets
@@ -289,7 +308,7 @@ function M.load_plugin()
289308 log .debug (string.format (" Setup action '%s' for keymap '%s'" , action , vim .json .encode (keymap )))
290309
291310 vim .keymap .set (keymap .mode , keymap .mapping , function ()
292- editor .send_command (action )
311+ editor .send_command (M . editor_port (), action )
293312 end )
294313 end
295314
0 commit comments