Skip to content

Commit c3dc209

Browse files
committed
replace send/list commands with rust sidecar
1 parent 33f7b88 commit c3dc209

File tree

17 files changed

+459
-136
lines changed

17 files changed

+459
-136
lines changed

.cargo/config.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[target.x86_64-apple-darwin]
2+
rustflags = [
3+
"-C", "link-arg=-undefined",
4+
"-C", "link-arg=dynamic_lookup",
5+
]
6+
7+
[target.aarch64-apple-darwin]
8+
rustflags = [
9+
"-C", "link-arg=-undefined",
10+
"-C", "link-arg=dynamic_lookup",
11+
]

.envrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use nix

.gitignore

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
.lsp
33
.nrepl*
44

5-
# Created by https://www.toptal.com/developers/gitignore/api/windows,macos,linux,lua
6-
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,macos,linux,lua
5+
# Created by https://www.toptal.com/developers/gitignore/api/windows,macos,linux,lua,rust
6+
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,macos,linux,lua,rust
77

88
### Linux ###
99
*~
@@ -96,6 +96,22 @@ Temporary Items
9696
# iCloud generated files
9797
*.icloud
9898

99+
### Rust ###
100+
# Generated by Cargo
101+
# will have compiled files and executables
102+
debug/
103+
target/
104+
105+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
106+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
107+
Cargo.lock
108+
109+
# These are backup files generated by rustfmt
110+
**/*.rs.bk
111+
112+
# MSVC Windows builds of rustc generate these, which store debugging information
113+
*.pdb
114+
99115
### Windows ###
100116
# Windows thumbnail cache files
101117
Thumbs.db
@@ -122,4 +138,4 @@ $RECYCLE.BIN/
122138
# Windows shortcuts
123139
*.lnk
124140

125-
# End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,lua
141+
# End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,lua,rust

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "defold-nvim"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
anyhow = "1.0.100"
11+
netstat2 = "0.11.2"
12+
nvim-oxi = { version = "0.6.0", features = ["neovim-0-11"] }
13+
reqwest = { version = "0.12.24", features = ["blocking", "json"] }
14+
rust-ini = "0.21.3"
15+
serde = { version = "1.0.228", features = ["derive"] }
16+
serde_json = "1.0.145"
17+
sha3 = "0.10.8"
18+
sysinfo = "0.37.2"

Justfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
watch:
2+
watchexec -w src -r 'just build-and-link'
3+
4+
build:
5+
cargo build
6+
7+
link:
8+
#!/usr/bin/env bash
9+
set -e
10+
11+
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
12+
cp -f "$(pwd)/target/debug/defold_nvim.dll" lua/defold/sidecar.dll
13+
elif [[ "$(uname)" == "Darwin" ]]; then
14+
cp -f "$(pwd)/target/debug/libdefold_nvim.dylib" lua/defold/sidecar.so
15+
else
16+
cp -f "$(pwd)/target/debug/libdefold_nvim.so" lua/defold/sidecar.so
17+
fi
18+
19+
build-and-link: build link

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ You're in luck, powershell is surprisingly capable so there is nothing else need
8888
"mfussenegger/nvim-dap",
8989
},
9090

91+
version = "*",
92+
9193
opts = {
9294
-- config options, see below
9395
},
@@ -150,11 +152,6 @@ local config = {
150152
custom_arguments = nil,
151153
},
152154

153-
babashka = {
154-
-- Use a custom executable for babashka (default: nil)
155-
custom_executable = nil,
156-
},
157-
158155
-- setup keymaps for Defold actions
159156
keymaps = {
160157

bb.edn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
setup {:task (apply defold/run-wrapped :setup *command-line-args*)}
55
set-default-editor {:task (apply defold/run-wrapped :set-default-editor *command-line-args*)}
66
install-dependencies {:task (apply defold/run-wrapped :install-dependencies *command-line-args*)}
7-
list-commands {:task (apply defold/run-wrapped :list-commands *command-line-args*)}
87
list-dependency-dirs {:task (apply defold/run-wrapped :list-dependency-dirs *command-line-args*)}
9-
send-command {:task (apply defold/run-wrapped :send-command *command-line-args*)}
108
launch-neovim {:task (apply defold/run-wrapped :launch-neovim *command-line-args*)}
119
focus-neovim {:task (apply defold/run-wrapped :focus-neovim *command-line-args*)}
1210
focus-game {:task (apply defold/run-wrapped :focus-game *command-line-args*)}

lua/defold/editor.lua

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
local M = {}
22

33
---List all available Defold commands
4+
---@param port integer|nil
45
---@return table|nil
5-
function M.list_commands()
6-
local babashka = require "defold.service.babashka"
6+
function M.list_commands(port)
77
local log = require "defold.service.logger"
8+
local sidecar = require "defold.sidecar"
89

9-
local res = babashka.run_task_json "list-commands"
10-
11-
if not res then
12-
log.error "Could not fetch commands from Defold, maybe the editor isn't running?"
13-
return nil
14-
end
10+
local res = sidecar.list_commands(port)
1511

1612
if res.error then
1713
log.error(string.format("Could not fetch commands from Defold, because: %s", res.error))
@@ -22,19 +18,20 @@ function M.list_commands()
2218
end
2319

2420
---Sends a command to the Defold editor
21+
---@param port integer|nil
2522
---@param command string
2623
---@param dont_report_error boolean|nil
27-
function M.send_command(command, dont_report_error)
28-
local babashka = require "defold.service.babashka"
24+
function M.send_command(port, command, dont_report_error)
2925
local log = require "defold.service.logger"
26+
local sidecar = require "defold.sidecar"
3027

31-
local res = babashka.run_task_json("send-command", { command })
28+
local res = sidecar.send_command(port, command)
3229

33-
if res.status == 202 then
30+
if not res.error then
3431
return
3532
end
3633

37-
if dont_report_error or false then
34+
if dont_report_error then
3835
return
3936
end
4037

lua/defold/init.lua

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ M.loaded = false
7979
---@type DefoldNvimConfig
8080
M.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
8487
function 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)
197202
end
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+
199216
function 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

lua/defold/service/debugger.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function M.setup(custom_executable, custom_arguments)
4242
M.mobdap_path()
4343
end
4444

45-
function M.register_nvim_dap()
45+
---@param editor_port_fn fun(): integer
46+
function M.register_nvim_dap(editor_port_fn)
4647
local log = require "defold.service.logger"
4748

4849
local ok, dap = pcall(require, "dap")
@@ -91,7 +92,7 @@ function M.register_nvim_dap()
9192
dap.listeners.after.event_mobdap_waiting_for_connection.defold_nvim_start_game = function(_, _)
9293
log.debug "debugger: connected"
9394

94-
editor.send_command "build"
95+
editor.send_command(editor_port_fn(), "build")
9596
end
9697

9798
dap.listeners.after.event_stopped.defold_nvim_switch_focus_on_stop = function(_, _)

0 commit comments

Comments
 (0)