Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
on:
pull_request:
push:
branches:
- master

jobs:
test:
name: Test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest

steps:
- uses: actions/checkout@v5
- uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Run tests
run: cargo test --all --verbose
22 changes: 19 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
.lsp
.nrepl*

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

### Linux ###
*~
Expand Down Expand Up @@ -96,6 +96,22 @@ Temporary Items
# iCloud generated files
*.icloud

### Rust ###
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Expand All @@ -122,4 +138,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,lua
# End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,lua,rust
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "defold-nvim"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1.0.100"
netstat2 = "0.11.2"
nvim-oxi = { version = "0.6.0", features = ["neovim-0-11"] }
reqwest = { version = "0.12.24", features = ["blocking", "json"] }
rust-ini = "0.21.3"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
sha3 = "0.10.8"
sysinfo = "0.37.2"
19 changes: 19 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
watch:
watchexec -w src -r 'just build-and-link'

build:
cargo build

link:
#!/usr/bin/env bash
set -e

if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
cp -f "$(pwd)/target/debug/defold_nvim.dll" lua/defold/sidecar.dll
elif [[ "$(uname)" == "Darwin" ]]; then
cp -f "$(pwd)/target/debug/libdefold_nvim.dylib" lua/defold/sidecar.so
else
cp -f "$(pwd)/target/debug/libdefold_nvim.so" lua/defold/sidecar.so
fi

build-and-link: build link
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ You're in luck, powershell is surprisingly capable so there is nothing else need
"mfussenegger/nvim-dap",
},

version = "*",

opts = {
-- config options, see below
},
Expand Down Expand Up @@ -150,11 +152,6 @@ local config = {
custom_arguments = nil,
},

babashka = {
-- Use a custom executable for babashka (default: nil)
custom_executable = nil,
},

-- setup keymaps for Defold actions
keymaps = {

Expand Down
2 changes: 0 additions & 2 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
setup {:task (apply defold/run-wrapped :setup *command-line-args*)}
set-default-editor {:task (apply defold/run-wrapped :set-default-editor *command-line-args*)}
install-dependencies {:task (apply defold/run-wrapped :install-dependencies *command-line-args*)}
list-commands {:task (apply defold/run-wrapped :list-commands *command-line-args*)}
list-dependency-dirs {:task (apply defold/run-wrapped :list-dependency-dirs *command-line-args*)}
send-command {:task (apply defold/run-wrapped :send-command *command-line-args*)}
launch-neovim {:task (apply defold/run-wrapped :launch-neovim *command-line-args*)}
focus-neovim {:task (apply defold/run-wrapped :focus-neovim *command-line-args*)}
focus-game {:task (apply defold/run-wrapped :focus-game *command-line-args*)}
Expand Down
23 changes: 10 additions & 13 deletions lua/defold/editor.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
local M = {}

---List all available Defold commands
---@param port integer|nil
---@return table|nil
function M.list_commands()
local babashka = require "defold.service.babashka"
function M.list_commands(port)
local log = require "defold.service.logger"
local sidecar = require "defold.sidecar"

local res = babashka.run_task_json "list-commands"

if not res then
log.error "Could not fetch commands from Defold, maybe the editor isn't running?"
return nil
end
local res = sidecar.list_commands(port)

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

---Sends a command to the Defold editor
---@param port integer|nil
---@param command string
---@param dont_report_error boolean|nil
function M.send_command(command, dont_report_error)
local babashka = require "defold.service.babashka"
function M.send_command(port, command, dont_report_error)
local log = require "defold.service.logger"
local sidecar = require "defold.sidecar"

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

if res.status == 202 then
if not res.error then
return
end

if dont_report_error or false then
if dont_report_error then
return
end

Expand Down
31 changes: 25 additions & 6 deletions lua/defold/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ M.loaded = false
---@type DefoldNvimConfig
M.config = default_config

---@type integer|nil
M.prev_editor_port = nil

---Returns true if we are in a defold project
---@return boolean
function M.is_defold_project()
Expand All @@ -104,6 +107,8 @@ function M.setup(opts)

M.config = vim.tbl_deep_extend("force", default_config, opts or {})

-- TODO: check if sidecar is available, if not download it (which shouldnt be necessary with some pkg managers)

-- persist config for babashka
local config_path = babashka.config_path()
vim.fn.writefile({
Expand Down Expand Up @@ -196,6 +201,18 @@ function M.setup(opts)
end, 0)
end

---@return integer
function M.editor_port()
if M.prev_editor_port then
-- TODO: validate port
return M.prev_editor_port
end

local sidecar = require "defold.sidecar"
M.prev_editor_port = sidecar.find_editor_port()
return M.prev_editor_port
end

function M.load_plugin()
if M.loaded then
return
Expand All @@ -206,13 +223,15 @@ function M.load_plugin()
-- register all filetypes
vim.filetype.add(require("defold.config.filetype").full)

local sidecar = require "defold.sidecar"
local babashka = require "defold.service.babashka"
local debugger = require "defold.service.debugger"
local editor = require "defold.editor"
local log = require "defold.service.logger"
local project = require "defold.project"

log.debug "============= defold.nvim: Loaded plugin"
log.debug("Sidecar Version:" .. sidecar.version)
log.debug("Babashka Path: " .. babashka.bb_path())
log.debug("Mobdap Path: " .. debugger.mobdap_path())
log.debug("Config: " .. vim.inspect(M.config))
Expand All @@ -222,7 +241,7 @@ function M.load_plugin()
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = { "*.lua", "*.script", "*.gui_script" },
callback = function()
editor.send_command("hot-reload", true)
editor.send_command(M.editor_port(), "hot-reload", true)
end,
})
end
Expand Down Expand Up @@ -256,26 +275,26 @@ function M.load_plugin()
return
end

editor.send_command(cmds[idx])
editor.send_command(M.editor_port(), cmds[idx])
end)
end, { nargs = 0, desc = "Select a command to run" })

-- add the ":DefoldSend cmd" command to send commands to the editor
vim.api.nvim_create_user_command("DefoldSend", function(opt)
editor.send_command(opt.args)
editor.send_command(M.editor_port(), opt.args)
end, { nargs = 1, desc = "Send a command to the Defold editor" })

-- add the ":DefoldFetch" command to fetch dependencies & annoatations
vim.api.nvim_create_user_command("DefoldFetch", function(opt)
-- when a user runs DefoldFetch I recon they also expect us to update the dependencies
editor.send_command("fetch-libraries", true)
editor.send_command(M.editor_port(), "fetch-libraries", true)

project.install_dependencies(opt.bang)
end, { bang = true, nargs = 0, desc = "Fetch & create Defold project dependency annotations" })

-- integrate the debugger into dap
if M.config.debugger.enable then
debugger.register_nvim_dap()
debugger.register_nvim_dap(M.editor_port)
end

-- add snippets
Expand All @@ -289,7 +308,7 @@ function M.load_plugin()
log.debug(string.format("Setup action '%s' for keymap '%s'", action, vim.json.encode(keymap)))

vim.keymap.set(keymap.mode, keymap.mapping, function()
editor.send_command(action)
editor.send_command(M.editor_port(), action)
end)
end

Expand Down
5 changes: 3 additions & 2 deletions lua/defold/service/debugger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function M.setup(custom_executable, custom_arguments)
M.mobdap_path()
end

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

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

editor.send_command "build"
editor.send_command(editor_port_fn(), "build")
end

dap.listeners.after.event_stopped.defold_nvim_switch_focus_on_stop = function(_, _)
Expand Down
15 changes: 15 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
pkgs ? import <nixpkgs> { },
}:

pkgs.mkShell {
nativeBuildInputs = with pkgs; [
pkg-config
];

buildInputs = with pkgs; [
openssl.dev
];

RUST_BACKTRACE = "1";
}
Loading