Skip to content

Commit 5645e97

Browse files
authored
Add support for new editor "/prefs" api (#48)
* add support for new editor /prefs api * update readme
1 parent 97b65b0 commit 5645e97

7 files changed

Lines changed: 66 additions & 71 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ defold.nvim is replacing these variables in the arguments list
9797
version = '*',
9898
lazy = false,
9999

100-
-- (Optional) Required when using the debugger
101100
dependencies = {
101+
-- (Optional) Required when using the debugger
102102
"mfussenegger/nvim-dap",
103+
104+
-- (Optional) Required when using snippets
105+
"L3MON4D3/LuaSnip",
103106
},
104107

105108
-- This makes sure the native library downloads at installation
@@ -186,18 +189,19 @@ defold.setup(config)
186189

187190
## Setup
188191

189-
### Setup Neovim
190-
191-
By installing and running the plugin once, Defold should automatically use Neovim as its editor. (Unless you disabled the setting above)
192+
### Initial Setup
192193

193-
If you manually want to setup Defold, run `:SetupDefold`
194+
1. Install the plugin (see above)
195+
2. Install [Defold](https://defold.com/)
196+
3. Start Defold and open a project
197+
4. Open Neovim in the projects directory and run `:SetupDefold`
198+
5. Done, when you try to open files via Defold now they should open in a Neovim window.
194199

195200
### Setup Debugging
196201

197202
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
198-
in your project.
199-
200-
The easiest way is using [defold-mobdebug](https://github.com/atomicptr/defold-mobdebug) in your project.
203+
in your project. This plugin is handling the installation of mobdap automatically, but you still need to add MobDebug in
204+
your project. The easiest way is using [defold-mobdebug](https://github.com/atomicptr/defold-mobdebug) in your project.
201205

202206
[(Read this)](https://github.com/atomicptr/defold-mobdebug?tab=readme-ov-file#installation)
203207

crates/core/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ reqwest = { version = "0.13.1", default-features = false, features = [
1717
] }
1818
rust-ini = "0.21.3"
1919
serde = { version = "1.0.228", features = ["derive"] }
20-
serde_json = "1.0.148"
20+
serde_json = "1.0.149"
2121
sha3 = "0.10.8"
2222
strum = { version = "0.27.2", features = ["derive"] }
2323
tracing = "0.1.44"
2424
which = "8.0.0"
25-
url = "2.5.7"
25+
url = "2.5.8"
2626
walkdir = "2.5.0"
27-
edn-rs = "0.18.0"
2827
serde_yaml = "0.9.34"
2928
textwrap = "0.16.2"
3029
fs_extra = "1.3.0"

crates/core/src/editor_config.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use anyhow::{Context, Result, bail};
2-
use edn_rs::Edn;
3-
use serde::Deserialize;
2+
use serde::{Deserialize, Serialize};
43
use std::{
54
fs,
65
path::{Path, PathBuf},
7-
str::FromStr,
86
};
97

10-
use crate::bridge;
8+
use crate::{bridge, editor};
119

1210
#[derive(Debug, Deserialize)]
1311
pub enum LauncherType {
@@ -141,42 +139,46 @@ fn create_runner_script(
141139
Ok(script_path)
142140
}
143141

144-
pub fn set_default_editor(plugin_root: &Path, launcher_settings: &LauncherSettings) -> Result<()> {
145-
if !plugin_root.exists() {
146-
bail!("plugin root '{}' could not be found", plugin_root.display());
147-
}
142+
#[derive(Serialize)]
143+
struct EditorConfig {
144+
#[serde(rename = "custom-editor")]
145+
custom_editor: String,
148146

149-
let config_dir = if cfg!(target_os = "macos") {
150-
// on macos defold stores prefs.editor_settings in ~/Library/Preferences
151-
dirs::preference_dir().context("could not find pref dir")?
152-
} else {
153-
dirs::config_dir().context("could not find config dir")?
154-
};
155-
let path = config_dir.join("Defold").join("prefs.editor_settings");
147+
#[serde(rename = "open-file")]
148+
open_file: String,
156149

157-
if !path.exists() {
158-
bail!(
159-
"prefs.editor_settings file {} could not be found",
160-
path.display()
161-
);
162-
}
150+
#[serde(rename = "open-file-at-line")]
151+
open_file_at_line: String,
152+
}
163153

164-
let data = fs::read_to_string(&path)?;
154+
pub fn set_default_editor(
155+
port: u16,
156+
plugin_root: &Path,
157+
launcher_settings: &LauncherSettings,
158+
) -> Result<()> {
159+
if !editor::is_editor_port(port) {
160+
bail!("No edito was found runnign at {port}");
161+
}
165162

166-
let mut config = Edn::from_str(&data).map_err(|err| anyhow::anyhow!(err.to_string()))?;
163+
if !plugin_root.exists() {
164+
bail!("plugin root '{}' could not be found", plugin_root.display());
165+
}
167166

168-
config[":code"][":custom-editor"] = Edn::Str(
169-
create_runner_script(plugin_root, launcher_settings)?
167+
let config = EditorConfig {
168+
custom_editor: create_runner_script(plugin_root, launcher_settings)?
170169
.to_str()
171170
.context("could not convert path to string")?
172171
.to_string(),
173-
);
174-
config[":code"][":open-file"] = Edn::Str("{file}".to_string());
175-
config[":code"][":open-file-at-line"] = Edn::Str("{file} {line}".to_string());
172+
open_file: "{file}".to_string(),
173+
open_file_at_line: "{file} {line}".to_string(),
174+
};
176175

177-
let config_str = &Edn::to_string(&config);
176+
let url = format!("http://localhost:{port}/prefs/code");
178177

179-
fs::write(path, config_str)?;
178+
reqwest::blocking::Client::new()
179+
.post(url)
180+
.json(&config)
181+
.send()?;
180182

181183
Ok(())
182184
}

crates/sidecar/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ fn send_command(_lua: &Lua, (port, cmd): (u16, String)) -> LuaResult<()> {
156156
#[instrument(level = "debug", err(Debug), skip_all)]
157157
fn set_default_editor(
158158
lua: &Lua,
159-
(plugin_root, launcher_settings): (String, LuaValue),
159+
(port, plugin_root, launcher_settings): (u16, String, LuaValue),
160160
) -> LuaResult<()> {
161161
let launcher_settings = lua.from_value(launcher_settings)?;
162-
editor_config::set_default_editor(&PathBuf::from(plugin_root), &launcher_settings)?;
162+
editor_config::set_default_editor(port, &PathBuf::from(plugin_root), &launcher_settings)?;
163163

164164
Ok(())
165165
}

lua/defold/init.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,16 @@ function M.setup(opts)
108108

109109
-- add setup defold command
110110
vim.api.nvim_create_user_command("SetupDefold", function()
111+
local project = require "defold.project"
112+
local port = project.editor_port()
113+
114+
if not port then
115+
log.error "Editor is not running, please make sure the editor for this project is running."
116+
return
117+
end
118+
111119
local sidecar = require "defold.sidecar"
112-
local ok, err = pcall(sidecar.set_default_editor, M.plugin_root(), M.config.launcher)
120+
local ok, err = pcall(sidecar.set_default_editor, port, M.plugin_root(), M.config.launcher)
113121
if not ok then
114122
log.error(string.format("Could not set default editor because: %s", err))
115123
end
@@ -161,10 +169,11 @@ function M.setup(opts)
161169

162170
vim.defer_fn(function()
163171
local project = require "defold.project"
172+
local port = project.editor_port()
164173

165-
if M.config.defold.set_default_editor then
174+
if M.config.defold.set_default_editor and port then
166175
local sidecar = require "defold.sidecar"
167-
local ok, err = pcall(sidecar.set_default_editor, M.plugin_root(), M.config.launcher)
176+
local ok, err = pcall(sidecar.set_default_editor, port, M.plugin_root(), M.config.launcher)
168177

169178
if not ok then
170179
log.error(string.format("Could not set default editor because: %s", err))

lua/defold/sidecar.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ package.cpath = package.cpath
183183
---@field is_editor_port function(port: integer): boolean
184184
---@field list_commands function(port: integer): table<string, string>
185185
---@field send_command function(port: integer, cmd: string)
186-
---@field set_default_editor function(plugin_root: string, launcher_config: LauncherSettings)
186+
---@field set_default_editor function(port: integer, plugin_root: string, launcher_config: LauncherSettings)
187187
---@field find_bridge_path function(plugin_root: string|nil): string
188188
---@field focus_neovim function(game_root: string)
189189
---@field focus_game function(game_root: string)

0 commit comments

Comments
 (0)