Skip to content
Draft
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
29 changes: 5 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
62 changes: 32 additions & 30 deletions crates/core/src/editor_config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -83,13 +81,13 @@
const RUN_SCRIPT: &str = include_str!("../assets/run_linux.sh");

#[cfg(target_os = "macos")]
const RUN_SCRIPT: &'static str = include_str!("../assets/run_macos.sh");

Check warning on line 84 in crates/core/src/editor_config.rs

View workflow job for this annotation

GitHub Actions / Test on macos-latest

constants have by default a `'static` lifetime

Check warning on line 84 in crates/core/src/editor_config.rs

View workflow job for this annotation

GitHub Actions / Test on macos-15-intel

constants have by default a `'static` lifetime

#[cfg(target_os = "windows")]
const RUN_SCRIPT: &'static str = include_str!("../assets/run_windows.bat");

Check warning on line 87 in crates/core/src/editor_config.rs

View workflow job for this annotation

GitHub Actions / Test on windows-latest

constants have by default a `'static` lifetime

#[cfg(target_os = "windows")]
const SCRIPT_EXT: &'static str = "bat";

Check warning on line 90 in crates/core/src/editor_config.rs

View workflow job for this annotation

GitHub Actions / Test on windows-latest

constants have by default a `'static` lifetime

#[cfg(not(target_os = "windows"))]
const SCRIPT_EXT: &str = "sh";
Expand Down Expand Up @@ -141,42 +139,46 @@
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(())
}
4 changes: 2 additions & 2 deletions crates/sidecar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
15 changes: 12 additions & 3 deletions lua/defold/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion lua/defold/sidecar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ package.cpath = package.cpath
---@field is_editor_port function(port: integer): boolean
---@field list_commands function(port: integer): table<string, string>
---@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)
Expand Down
Loading