refactor: deduplicate lua execution match blocks in nvim-core using handle_lua_result macro

This commit is contained in:
Riz Ashraf committed 2026-09-22 05:57:50 +01:00
1 parent 02f40604e1
commit b8070de38c
1 file changed
+21 -54
+21 -54
View File
@@ -38,6 +38,15 @@ pub async fn send_response(response: JsonRpcResponse) {
let _ = stdout.flush().await; let _ = stdout.flush().await;
} }
macro_rules! handle_lua_result {
($id:expr, $result:expr) => {
match $result.await {
Ok(res) => send_text_result!($id.clone(), res),
Err(e) => send_error($id, -32603, &e).await,
}
};
}
pub async fn send_error(id: Value, code: i32, message: &str) { pub async fn send_error(id: Value, code: i32, message: &str) {
send_response(JsonRpcResponse { send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(), jsonrpc: "2.0".to_string(),
@@ -753,12 +762,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end end
return vim.fn.json_encode(result) return vim.fn.json_encode(result)
"; ";
match execute_nvim_lua(lua_code).await { handle_lua_result!(id, execute_nvim_lua(lua_code))
Ok(result) => {
send_text_result!(id.clone(), result);
}
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_get_diagnostics" => { "nvim_get_diagnostics" => {
let lua_code = r" let lua_code = r"
@@ -776,12 +780,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end end
return vim.fn.json_encode(result) return vim.fn.json_encode(result)
"; ";
match execute_nvim_lua(lua_code).await { handle_lua_result!(id, execute_nvim_lua(lua_code))
Ok(result) => {
send_text_result!(id.clone(), result);
}
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_open_file" => { "nvim_open_file" => {
@@ -794,10 +793,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end end
return 'Opened file ' .. args.file return 'Opened file ' .. args.file
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_open_buffer" => { "nvim_open_buffer" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -817,10 +813,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
vim.api.nvim_win_set_buf(0, buf) vim.api.nvim_win_set_buf(0, buf)
return 'Opened buffer ' .. tostring(buf) return 'Opened buffer ' .. tostring(buf)
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_close_buffer" => { "nvim_close_buffer" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -831,10 +824,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
vim.api.nvim_buf_delete(buf, { force = force }) vim.api.nvim_buf_delete(buf, { force = force })
return 'Closed buffer ' .. tostring(buf) return 'Closed buffer ' .. tostring(buf)
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_split_window" => { "nvim_split_window" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -849,10 +839,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end end
return 'Split window created' return 'Split window created'
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_reload_buffer" => { "nvim_reload_buffer" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -868,20 +855,14 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end) end)
return 'Reloaded buffer ' .. tostring(buf) return 'Reloaded buffer ' .. tostring(buf)
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_save_buffer" => { "nvim_save_buffer" => {
let code = " let code = "
vim.cmd('write') vim.cmd('write')
return 'Saved current buffer' return 'Saved current buffer'
"; ";
match execute_nvim_lua(code).await { handle_lua_result!(id, execute_nvim_lua(code));
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_set_quickfix" => { "nvim_set_quickfix" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -896,10 +877,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
vim.cmd('copen') vim.cmd('copen')
return 'Populated quickfix with ' .. tostring(#items) .. ' items' return 'Populated quickfix with ' .. tostring(#items) .. ' items'
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_highlight_lines" => { "nvim_highlight_lines" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -927,10 +905,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end end
return 'Highlighted lines ' .. tostring(args.start_line) .. ' to ' .. tostring(args.end_line) return 'Highlighted lines ' .. tostring(args.start_line) .. ' to ' .. tostring(args.end_line)
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_get_messages" => { "nvim_get_messages" => {
let args_rmp = json_to_rmpv(args); let args_rmp = json_to_rmpv(args);
@@ -947,10 +922,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
end end
return msg return msg
"; ";
match execute_nvim_lua_with_args(code, vec![args_rmp]).await { handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
Ok(res) => send_text_result!(id.clone(), res),
Err(e) => send_error(id, -32603, &e).await,
}
} }
"nvim_get_viewport" => { "nvim_get_viewport" => {
let code = " let code = "
@@ -995,12 +967,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
return; return;
} }
match execute_nvim_lua(code).await { handle_lua_result!(id, execute_nvim_lua(code));
Ok(result) => {
send_text_result!(id.clone(), result);
}
Err(e) => send_error(id, -32603, &e).await,
}
} else { } else {
send_error(id, -32602, "Missing 'code'").await; send_error(id, -32602, "Missing 'code'").await;
} }