refactor: deduplicate lua execution match blocks in nvim-core using handle_lua_result macro
This commit is contained in:
1 parent
02f40604e1
commit
b8070de38c
1 file changed
+21
-54
+21
-54
@@ -38,6 +38,15 @@ pub async fn send_response(response: JsonRpcResponse) {
|
||||
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) {
|
||||
send_response(JsonRpcResponse {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
@@ -753,12 +762,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end
|
||||
return vim.fn.json_encode(result)
|
||||
";
|
||||
match execute_nvim_lua(lua_code).await {
|
||||
Ok(result) => {
|
||||
send_text_result!(id.clone(), result);
|
||||
}
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua(lua_code))
|
||||
}
|
||||
"nvim_get_diagnostics" => {
|
||||
let lua_code = r"
|
||||
@@ -776,12 +780,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end
|
||||
return vim.fn.json_encode(result)
|
||||
";
|
||||
match execute_nvim_lua(lua_code).await {
|
||||
Ok(result) => {
|
||||
send_text_result!(id.clone(), result);
|
||||
}
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua(lua_code))
|
||||
}
|
||||
|
||||
"nvim_open_file" => {
|
||||
@@ -794,10 +793,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end
|
||||
return 'Opened file ' .. args.file
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_open_buffer" => {
|
||||
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)
|
||||
return 'Opened buffer ' .. tostring(buf)
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_close_buffer" => {
|
||||
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 })
|
||||
return 'Closed buffer ' .. tostring(buf)
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_split_window" => {
|
||||
let args_rmp = json_to_rmpv(args);
|
||||
@@ -849,10 +839,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end
|
||||
return 'Split window created'
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_reload_buffer" => {
|
||||
let args_rmp = json_to_rmpv(args);
|
||||
@@ -868,20 +855,14 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end)
|
||||
return 'Reloaded buffer ' .. tostring(buf)
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_save_buffer" => {
|
||||
let code = "
|
||||
vim.cmd('write')
|
||||
return 'Saved current buffer'
|
||||
";
|
||||
match execute_nvim_lua(code).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua(code));
|
||||
}
|
||||
"nvim_set_quickfix" => {
|
||||
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')
|
||||
return 'Populated quickfix with ' .. tostring(#items) .. ' items'
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_highlight_lines" => {
|
||||
let args_rmp = json_to_rmpv(args);
|
||||
@@ -927,10 +905,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end
|
||||
return 'Highlighted lines ' .. tostring(args.start_line) .. ' to ' .. tostring(args.end_line)
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_get_messages" => {
|
||||
let args_rmp = json_to_rmpv(args);
|
||||
@@ -947,10 +922,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
end
|
||||
return msg
|
||||
";
|
||||
match execute_nvim_lua_with_args(code, vec![args_rmp]).await {
|
||||
Ok(res) => send_text_result!(id.clone(), res),
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua_with_args(code, vec![args_rmp]))
|
||||
}
|
||||
"nvim_get_viewport" => {
|
||||
let code = "
|
||||
@@ -995,12 +967,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
|
||||
return;
|
||||
}
|
||||
|
||||
match execute_nvim_lua(code).await {
|
||||
Ok(result) => {
|
||||
send_text_result!(id.clone(), result);
|
||||
}
|
||||
Err(e) => send_error(id, -32603, &e).await,
|
||||
}
|
||||
handle_lua_result!(id, execute_nvim_lua(code));
|
||||
} else {
|
||||
send_error(id, -32602, "Missing 'code'").await;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user