From 7744ce5742c2628a86a4a9a671deaba24facb0c9 Mon Sep 17 00:00:00 2001 From: Christopher Kaster Date: Sat, 10 Jan 2026 12:06:27 +0100 Subject: [PATCH 1/2] add support for new editor /prefs api --- Cargo.lock | 29 +++------------ crates/core/Cargo.toml | 5 ++- crates/core/src/editor_config.rs | 62 ++++++++++++++++---------------- crates/sidecar/src/lib.rs | 4 +-- lua/defold/init.lua | 15 ++++++-- lua/defold/sidecar.lua | 2 +- 6 files changed, 54 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 035bb4a..31b42cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,7 +498,6 @@ dependencies = [ "anyhow", "dirs", "dmg", - "edn-rs", "flate2", "fs_extra", "pretty_assertions", @@ -626,15 +625,6 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" -[[package]] -name = "edn-rs" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f976aae7770c1b708fa4dbc0187ff1e58cfe805ec3d855903ec344e3d6b52c6" -dependencies = [ - "ordered-float 4.6.0", -] - [[package]] name = "either" version = "1.15.0" @@ -1474,15 +1464,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "ordered-float" -version = "4.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bb71e1b3fa6ca1c61f383464aaf2bb0e2f8e772a1f01d486832464de363b951" -dependencies = [ - "num-traits", -] - [[package]] name = "ordered-multimap" version = "0.7.3" @@ -2032,7 +2013,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" dependencies = [ - "ordered-float 2.10.1", + "ordered-float", "serde", ] @@ -2058,9 +2039,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.148" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3084b546a1dd6289475996f182a22aba973866ea8e8b02c51d9f46b1336a22da" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "itoa", "memchr", @@ -2578,9 +2559,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.7" +version = "2.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08bc136a29a3d1758e07a9cca267be308aeebf5cfd5a10f3f67ab2097683ef5b" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" dependencies = [ "form_urlencoded", "idna", diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 83cb6b6..77feee0 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -17,14 +17,13 @@ reqwest = { version = "0.13.1", default-features = false, features = [ ] } rust-ini = "0.21.3" serde = { version = "1.0.228", features = ["derive"] } -serde_json = "1.0.148" +serde_json = "1.0.149" sha3 = "0.10.8" strum = { version = "0.27.2", features = ["derive"] } tracing = "0.1.44" which = "8.0.0" -url = "2.5.7" +url = "2.5.8" walkdir = "2.5.0" -edn-rs = "0.18.0" serde_yaml = "0.9.34" textwrap = "0.16.2" fs_extra = "1.3.0" diff --git a/crates/core/src/editor_config.rs b/crates/core/src/editor_config.rs index 8b5a130..8943f2c 100644 --- a/crates/core/src/editor_config.rs +++ b/crates/core/src/editor_config.rs @@ -1,13 +1,11 @@ use anyhow::{Context, Result, bail}; -use edn_rs::Edn; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::{ fs, path::{Path, PathBuf}, - str::FromStr, }; -use crate::bridge; +use crate::{bridge, editor}; #[derive(Debug, Deserialize)] pub enum LauncherType { @@ -141,42 +139,46 @@ fn create_runner_script( Ok(script_path) } -pub fn set_default_editor(plugin_root: &Path, launcher_settings: &LauncherSettings) -> Result<()> { - if !plugin_root.exists() { - bail!("plugin root '{}' could not be found", plugin_root.display()); - } +#[derive(Serialize)] +struct EditorConfig { + #[serde(rename = "custom-editor")] + custom_editor: String, - let config_dir = if cfg!(target_os = "macos") { - // on macos defold stores prefs.editor_settings in ~/Library/Preferences - dirs::preference_dir().context("could not find pref dir")? - } else { - dirs::config_dir().context("could not find config dir")? - }; - let path = config_dir.join("Defold").join("prefs.editor_settings"); + #[serde(rename = "open-file")] + open_file: String, - if !path.exists() { - bail!( - "prefs.editor_settings file {} could not be found", - path.display() - ); - } + #[serde(rename = "open-file-at-line")] + open_file_at_line: String, +} - let data = fs::read_to_string(&path)?; +pub fn set_default_editor( + port: u16, + plugin_root: &Path, + launcher_settings: &LauncherSettings, +) -> Result<()> { + if !editor::is_editor_port(port) { + bail!("No edito was found runnign at {port}"); + } - let mut config = Edn::from_str(&data).map_err(|err| anyhow::anyhow!(err.to_string()))?; + if !plugin_root.exists() { + bail!("plugin root '{}' could not be found", plugin_root.display()); + } - config[":code"][":custom-editor"] = Edn::Str( - create_runner_script(plugin_root, launcher_settings)? + let config = EditorConfig { + custom_editor: create_runner_script(plugin_root, launcher_settings)? .to_str() .context("could not convert path to string")? .to_string(), - ); - config[":code"][":open-file"] = Edn::Str("{file}".to_string()); - config[":code"][":open-file-at-line"] = Edn::Str("{file} {line}".to_string()); + open_file: "{file}".to_string(), + open_file_at_line: "{file} {line}".to_string(), + }; - let config_str = &Edn::to_string(&config); + let url = format!("http://localhost:{port}/prefs/code"); - fs::write(path, config_str)?; + reqwest::blocking::Client::new() + .post(url) + .json(&config) + .send()?; Ok(()) } diff --git a/crates/sidecar/src/lib.rs b/crates/sidecar/src/lib.rs index 1546e96..25ed5b5 100644 --- a/crates/sidecar/src/lib.rs +++ b/crates/sidecar/src/lib.rs @@ -156,10 +156,10 @@ fn send_command(_lua: &Lua, (port, cmd): (u16, String)) -> LuaResult<()> { #[instrument(level = "debug", err(Debug), skip_all)] fn set_default_editor( lua: &Lua, - (plugin_root, launcher_settings): (String, LuaValue), + (port, plugin_root, launcher_settings): (u16, String, LuaValue), ) -> LuaResult<()> { let launcher_settings = lua.from_value(launcher_settings)?; - editor_config::set_default_editor(&PathBuf::from(plugin_root), &launcher_settings)?; + editor_config::set_default_editor(port, &PathBuf::from(plugin_root), &launcher_settings)?; Ok(()) } diff --git a/lua/defold/init.lua b/lua/defold/init.lua index 5e55dfb..c084ebb 100644 --- a/lua/defold/init.lua +++ b/lua/defold/init.lua @@ -108,8 +108,16 @@ function M.setup(opts) -- add setup defold command vim.api.nvim_create_user_command("SetupDefold", function() + local project = require "defold.project" + local port = project.editor_port() + + if not port then + log.error "Editor is not running, please make sure the editor for this project is running." + return + end + local sidecar = require "defold.sidecar" - local ok, err = pcall(sidecar.set_default_editor, M.plugin_root(), M.config.launcher) + local ok, err = pcall(sidecar.set_default_editor, port, M.plugin_root(), M.config.launcher) if not ok then log.error(string.format("Could not set default editor because: %s", err)) end @@ -161,10 +169,11 @@ function M.setup(opts) vim.defer_fn(function() local project = require "defold.project" + local port = project.editor_port() - if M.config.defold.set_default_editor then + if M.config.defold.set_default_editor and port then local sidecar = require "defold.sidecar" - local ok, err = pcall(sidecar.set_default_editor, M.plugin_root(), M.config.launcher) + local ok, err = pcall(sidecar.set_default_editor, port, M.plugin_root(), M.config.launcher) if not ok then log.error(string.format("Could not set default editor because: %s", err)) diff --git a/lua/defold/sidecar.lua b/lua/defold/sidecar.lua index 20f7fe8..6af0411 100644 --- a/lua/defold/sidecar.lua +++ b/lua/defold/sidecar.lua @@ -183,7 +183,7 @@ package.cpath = package.cpath ---@field is_editor_port function(port: integer): boolean ---@field list_commands function(port: integer): table ---@field send_command function(port: integer, cmd: string) ----@field set_default_editor function(plugin_root: string, launcher_config: LauncherSettings) +---@field set_default_editor function(port: integer, plugin_root: string, launcher_config: LauncherSettings) ---@field find_bridge_path function(plugin_root: string|nil): string ---@field focus_neovim function(game_root: string) ---@field focus_game function(game_root: string) From 9492d461e88995f116d3597864b654beb38e8087 Mon Sep 17 00:00:00 2001 From: Christopher Kaster Date: Sun, 11 Jan 2026 13:23:08 +0100 Subject: [PATCH 2/2] update readme --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 870e1de..c94c523 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,12 @@ defold.nvim is replacing these variables in the arguments list version = '*', lazy = false, - -- (Optional) Required when using the debugger dependencies = { + -- (Optional) Required when using the debugger "mfussenegger/nvim-dap", + + -- (Optional) Required when using snippets + "L3MON4D3/LuaSnip", }, -- This makes sure the native library downloads at installation @@ -186,18 +189,19 @@ defold.setup(config) ## Setup -### Setup Neovim - -By installing and running the plugin once, Defold should automatically use Neovim as its editor. (Unless you disabled the setting above) +### Initial Setup -If you manually want to setup Defold, run `:SetupDefold` +1. Install the plugin (see above) +2. Install [Defold](https://defold.com/) +3. Start Defold and open a project +4. Open Neovim in the projects directory and run `:SetupDefold` +5. Done, when you try to open files via Defold now they should open in a Neovim window. ### Setup Debugging For debugging we're using [mobdap](https://github.com/atomicptr/mobdap) which is running on top of [MobDebug](https://github.com/pkulchenko/MobDebug) so you need to have that available -in your project. - -The easiest way is using [defold-mobdebug](https://github.com/atomicptr/defold-mobdebug) in your project. +in your project. This plugin is handling the installation of mobdap automatically, but you still need to add MobDebug in +your project. The easiest way is using [defold-mobdebug](https://github.com/atomicptr/defold-mobdebug) in your project. [(Read this)](https://github.com/atomicptr/defold-mobdebug?tab=readme-ov-file#installation)