Clean up various ways of calling nvim (#3230)

Don't use deprecated functions, and use macros for cleaner syntax.
This commit is contained in:
fredizzimo 2025-10-02 19:46:59 +03:00 committed by GitHub
parent eba15553fb
commit 86c3639fcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 87 additions and 75 deletions

View file

@ -60,6 +60,7 @@ glamour = { version = "0.18.0", features = ["serde"] }
glutin = "0.32.3"
glutin-winit = "0.5.0"
image = { version = "0.25.5", default-features = false, features = ["ico"] }
indoc = "2.0.5"
itertools = "0.14.0"
log = "0.4.22"
lru = "0.16.0"
@ -92,7 +93,6 @@ winit = { version = "=0.30.12", features = ["serde"] }
xdg = "3.0.0"
[dev-dependencies]
indoc = "2.0.5"
scoped-env = "2.1.0"
serial_test = "3.2.0"

View file

@ -36,6 +36,36 @@ pub use ui_commands::{send_ui, start_ui_command_handler, ParallelCommand, Serial
const NEOVIM_REQUIRED_VERSION: &str = "0.10.0";
macro_rules! nvim_dict {
( $( $key:expr => $value:expr ),* $(,)? ) => {
vec![
$( (Value::from($key), Value::from($value)) ),*
]
};
}
pub(crate) use nvim_dict;
/// nvim_command_output is deprecated, so use our own version
async fn nvim_exec_output(
nvim: &Neovim<NeovimWriter>,
func: &str,
) -> Result<String, Box<CallError>> {
let result = nvim
.exec2(
func,
nvim_dict! {
"output" => true,
},
)
.await?;
Ok(result
.iter()
.find(|(k, _)| k.as_str() == Some("output"))
.and_then(|(_, v)| v.as_str())
.unwrap_or("")
.to_string())
}
pub struct NeovimRuntime {
pub runtime: Runtime,
}
@ -70,7 +100,7 @@ pub async fn show_error_message(
Value::String(error_msg_highlight.clone()),
]),
);
nvim.echo(prepared_lines, true, vec![]).await
nvim.echo(prepared_lines, true, nvim_dict! {}).await
}
async fn launch(
@ -85,11 +115,12 @@ async fn launch(
.context("Could not locate or start neovim process")?;
// Check the neovim version to ensure its high enough
match session
.neovim
.command_output(&format!("echo has('nvim-{NEOVIM_REQUIRED_VERSION}')"))
.await
.as_deref()
match nvim_exec_output(
&session.neovim,
&format!("echo has('nvim-{NEOVIM_REQUIRED_VERSION}')"),
)
.await
.as_deref()
{
Ok("1") => {} // This is just a guard
_ => {

View file

@ -1,8 +1,11 @@
use anyhow::{Context, Result};
use nvim_rs::Neovim;
use nvim_rs::{call_args, rpc::IntoVal, Neovim};
use rmpv::Value;
use super::api_info::{parse_api_info, ApiInformation};
use super::{
api_info::{parse_api_info, ApiInformation},
nvim_dict,
};
use crate::{
bridge::NeovimWriter,
settings::{SettingLocation, Settings},
@ -27,34 +30,25 @@ pub async fn setup_neovide_specific_state(
settings: &Settings,
) -> Result<()> {
// Set variable indicating to user config that neovide is being used.
nvim.set_var("neovide", Value::Boolean(true))
nvim.set_var("neovide", Value::from(true))
.await
.context("Could not communicate with neovim process")?;
nvim.command("runtime! ginit.vim")
nvim.exec2("runtime! ginit.vim", nvim_dict!())
.await
.context("Error encountered in ginit.vim ")?;
// Set details about the neovide version.
nvim.set_client_info(
"neovide",
vec![
(
Value::from("major"),
Value::from(env!("CARGO_PKG_VERSION_MAJOR")),
),
(
Value::from("minor"),
Value::from(env!("CARGO_PKG_VERSION_MINOR")),
),
(
Value::from("patch"),
Value::from(env!("CARGO_PKG_VERSION_PATCH")),
),
],
nvim_dict! {
"major" =>env!("CARGO_PKG_VERSION_MAJOR"),
"minor" =>env!("CARGO_PKG_VERSION_MINOR"),
"patch" =>env!("CARGO_PKG_VERSION_PATCH")
},
"ui",
vec![],
vec![],
nvim_dict! {},
nvim_dict! {},
)
.await
.context("Error setting client info")?;
@ -78,33 +72,19 @@ pub async fn setup_neovide_specific_state(
})
.collect::<Vec<_>>();
let args = Value::from(vec![
(
Value::from("neovide_channel_id"),
Value::from(api_information.channel),
),
(
Value::from("neovide_version"),
Value::from(crate_version!()),
),
(
Value::from("register_clipboard"),
Value::from(register_clipboard),
),
(
Value::from("register_right_click"),
Value::from(register_right_click),
),
(
Value::from("global_variable_settings"),
Value::from(global_variable_settings),
),
(Value::from("option_settings"), Value::from(option_settings)),
]);
nvim.execute_lua(INIT_LUA, vec![args])
.await
.context("Error when running Neovide init.lua")?;
nvim.exec_lua(
INIT_LUA,
call_args![nvim_dict! {
"neovide_channel_id" => api_information.channel,
"neovide_version" => crate_version!(),
"register_clipboard" => register_clipboard,
"register_right_click" => register_right_click,
"global_variable_settings" => global_variable_settings,
"option_settings" => option_settings,
}],
)
.await
.context("Error when running Neovide init.lua")?;
Ok(())
}

View file

@ -1,15 +1,15 @@
use std::sync::{Arc, OnceLock};
use log::trace;
use anyhow::{Context, Result};
use indoc::indoc;
use log::trace;
use nvim_rs::{call_args, error::CallError, rpc::model::IntoVal, Neovim, Value};
use strum::AsRefStr;
use tokio::sync::mpsc::unbounded_channel;
use super::{show_error_message, Settings};
use crate::{
bridge::NeovimWriter,
bridge::{nvim_dict, NeovimWriter},
cmd_line::CmdLineSettings,
profiling::{tracy_dynamic_zone, tracy_fiber_enter, tracy_fiber_leave},
utils::handle_wslpaths,
@ -155,21 +155,24 @@ async fn display_available_fonts(
].into_iter().map(|text| text.to_owned()).collect();
content.extend(fonts);
nvim.command("split").await?;
nvim.command("noswapfile hide enew").await?;
nvim.command("setlocal buftype=nofile").await?;
nvim.command("setlocal bufhidden=hide").await?;
nvim.command("\"setlocal nobuflisted").await?;
nvim.command("\"lcd ~").await?;
nvim.command("file scratch").await?;
nvim.exec2(
indoc! {"
split
noswapfile hide enew
setlocal buftype=nofile
setlocal bufhidden=hide
file scratch
nnoremap <buffer> <CR> <cmd>lua vim.opt.guifont=vim.fn.getline('.')<CR>,
"},
nvim_dict! {},
)
.await?;
let _ = nvim
.call(
"nvim_buf_set_lines",
call_args![0i64, 0i64, -1i64, false, content],
)
.await?;
nvim.command("nnoremap <buffer> <CR> <cmd>lua vim.opt.guifont=vim.fn.getline('.')<CR>")
.await?;
Ok(())
}
@ -186,9 +189,7 @@ impl ParallelCommand {
let _ = nvim
.exec_lua(
include_str!("../../lua/exit_handler.lua"),
vec![Value::Boolean(
settings.get::<CmdLineSettings>().server.is_some(),
)],
call_args![settings.get::<CmdLineSettings>().server.is_some()],
)
.await;
Ok(())
@ -205,20 +206,20 @@ impl ParallelCommand {
}
ParallelCommand::FileDrop(path) => nvim
.exec_lua(
&format!(
"neovide.private.dropfile([[{}]], {})",
"neovide.private.dropfile(...)",
call_args![
handle_wslpaths(vec![path], settings.get::<CmdLineSettings>().wsl)
.first()
.unwrap(),
.unwrap()
.to_string(),
settings.get::<CmdLineSettings>().tabs
),
Vec::new(),
],
)
.await
.map(|_| ()) // We don't care about the result
.context("FileDrop failed"),
ParallelCommand::SetBackground(background) => nvim
.command(format!("set background={background}").as_str())
.set_option_value("background", Value::from(background), nvim_dict! {})
.await
.context("SetBackground failed"),
ParallelCommand::DisplayAvailableFonts(fonts) => display_available_fonts(nvim, fonts)