style: Optimize stub proxy logging and cleanup nvim-core JSON-RPC response boilerplate

This commit is contained in:
Riz Ashraf committed 2026-09-19 07:17:57 +01:00
1 parent 6011da95f6
commit 368f28e593
7 files changed
+113 -89

No files matched your search

+22 -70
View File
@@ -476,6 +476,20 @@ async fn execute_nvim_lua(code: &str) -> Result<String, String> {
}
Err("Invalid response".to_string())
}
macro_rules! send_text_result {
($id:expr, $text:expr) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id: $id,
result: Some(json!({
"content": [{"type": "text", "text": $text}]
})),
error: None,
}).await;
};
}
pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
if std::env::args().any(|arg| arg == "--version") {
println!(
@@ -649,14 +663,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
let cmd = format!("e {} | {} | normal! zz", escaped_file, line);
match send_nvim_command(&cmd).await {
Ok(_) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": format!("Successfully jumped to {} line {}", file, line)}]
})),
error: None,
}).await;
send_text_result!(id.clone(), format!("Successfully jumped to {} line {}", file, line));
}
Err(e) => send_error(id, -32603, &e).await,
}
@@ -666,43 +673,19 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
}
"nvim_get_active_buffer" => match get_nvim_active_buffer().await {
Ok(content) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": content}]
})),
error: None,
})
.await;
send_text_result!(id.clone(), content);
}
Err(e) => send_error(id, -32603, &e).await,
},
"nvim_get_cursor" => match get_nvim_cursor().await {
Ok(content) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": content}]
})),
error: None,
})
.await;
send_text_result!(id.clone(), content);
}
Err(e) => send_error(id, -32603, &e).await,
},
"nvim_get_visual_selection" => match get_nvim_visual_selection().await {
Ok(content) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": content}]
})),
error: None,
})
.await;
send_text_result!(id.clone(), content);
}
Err(e) => send_error(id, -32603, &e).await,
},
@@ -713,14 +696,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
) {
match set_nvim_diagnostics(line, message).await {
Ok(_) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": format!("Successfully set diagnostic on line {}", line)}]
})),
error: None,
}).await;
send_text_result!(id.clone(), format!("Successfully set diagnostic on line {}", line));
}
Err(e) => send_error(id, -32603, &e).await,
}
@@ -742,15 +718,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
"#;
match execute_nvim_lua(lua_code).await {
Ok(result) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": result}]
})),
error: None,
})
.await;
send_text_result!(id.clone(), result);
}
Err(e) => send_error(id, -32603, &e).await,
}
@@ -773,15 +741,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
"#;
match execute_nvim_lua(lua_code).await {
Ok(result) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": result}]
})),
error: None,
})
.await;
send_text_result!(id.clone(), result);
}
Err(e) => send_error(id, -32603, &e).await,
}
@@ -790,15 +750,7 @@ pub async fn run_mcp_loop(app_name: &str, app_version: &str) {
if let Some(code) = args.get("code").and_then(|v| v.as_str()) {
match execute_nvim_lua(code).await {
Ok(result) => {
send_response(JsonRpcResponse {
jsonrpc: "2.0".to_string(),
id,
result: Some(json!({
"content": [{"type": "text", "text": result}]
})),
error: None,
})
.await;
send_text_result!(id.clone(), result);
}
Err(e) => send_error(id, -32603, &e).await,
}