41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import re
|
|
import sys
|
|
|
|
with open('C:\\Users\\reazul.ashraf\\workspace\\rust\\mcp-memory\\nvim-core\\src\\lib.rs', 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
macro_def = """
|
|
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;
|
|
};
|
|
}
|
|
"""
|
|
|
|
if "macro_rules! send_text_result" not in content:
|
|
idx = content.find("pub async fn run_mcp_loop")
|
|
content = content[:idx] + macro_def + "\n" + content[idx:]
|
|
|
|
# Find tools/call block
|
|
tools_call_idx = content.find('"tools/call" => {')
|
|
if tools_call_idx != -1:
|
|
block = content[tools_call_idx:]
|
|
|
|
# Replace the verbose blocks
|
|
pattern = r'send_response\(JsonRpcResponse \{\s*jsonrpc: "2\.0"\.to_string\(\),\s*id,\s*result: Some\(json!\(\{\s*"content": \[\{"type": "text", "text": (.*?)\}\]\s*\}\)\),\s*error: None,\s*\}\)\s*\.await;'
|
|
|
|
new_block = re.sub(pattern, r'send_text_result!(id.clone(), \1);', block)
|
|
|
|
content = content[:tools_call_idx] + new_block
|
|
|
|
with open('C:\\Users\\reazul.ashraf\\workspace\\rust\\mcp-memory\\nvim-core\\src\\lib.rs', 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
print("Patched nvim macro")
|