-
Notifications
You must be signed in to change notification settings - Fork 28
/
neovim.rs
152 lines (128 loc) · 3.93 KB
/
neovim.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::env;
use std::str;
use anyhow::bail;
use anyhow::Result;
use async_trait::async_trait;
use base64::engine::general_purpose::STANDARD as b64;
use base64::Engine;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use crate::domain::models::AcceptType;
use crate::domain::models::Editor;
use crate::domain::models::EditorContext;
use crate::domain::models::EditorName;
fn base64_to_string<'de, D: Deserializer<'de>>(deserializer: D) -> Result<String, D::Error> {
let val = match serde::de::Deserialize::deserialize(deserializer)? {
serde_json::Value::String(s) => s,
_ => return Err(serde::de::Error::custom("Wrong type, expected string")),
};
let b64_res = b64.decode(val).unwrap();
let str_res = str::from_utf8(&b64_res).unwrap().to_string();
return Ok(str_res);
}
#[derive(Debug, Deserialize, Serialize)]
struct ContextResponse {
file_path: String,
language: String,
#[serde(deserialize_with = "base64_to_string")]
code: String,
start_line: i64,
end_line: Option<i64>,
}
#[derive(Debug, Serialize)]
struct SubmitChangesRequest {
accept_type: String,
file_path: String,
code: String,
start_line: i64,
end_line: Option<i64>,
}
impl From<ContextResponse> for EditorContext {
fn from(val: ContextResponse) -> Self {
return EditorContext {
file_path: val.file_path,
language: val.language,
code: val.code,
start_line: val.start_line,
end_line: val.end_line,
};
}
}
async fn run_lua_command(func: &str) -> Result<String> {
let nvim_server_path = env::var("NVIM")?;
let lua_func = format!("v:lua.{func}");
let args = vec![
"--headless",
"--server",
&nvim_server_path,
"--remote-expr",
&lua_func,
];
let stdout = Command::new("nvim")
.args(args.clone())
.output()
.await?
.stdout;
let res = String::from_utf8(stdout)?;
tracing::error!(args = ?args, res = ?res, "Neovim requeest/response");
return Ok(res);
}
#[derive(Default)]
pub struct Neovim {}
#[async_trait]
impl Editor for Neovim {
fn name(&self) -> EditorName {
return EditorName::Neovim;
}
#[allow(clippy::implicit_return)]
async fn health_check(&self) -> Result<()> {
if env::var("NVIM").is_err() {
bail!("Not running within a Neovim terminal")
}
return Ok(());
}
#[allow(clippy::implicit_return)]
async fn get_context(&self) -> Result<Option<EditorContext>> {
let json_str = run_lua_command("oatmeal_get_context()").await?;
if json_str.trim() == "[]" {
return Ok(None);
}
let ctx: ContextResponse = serde_json::from_str(&json_str)?;
return Ok(Some(ctx.into()));
}
#[allow(clippy::implicit_return)]
async fn clear_context(&self) -> Result<()> {
run_lua_command("oatmeal_clear_context()").await?;
return Ok(());
}
#[allow(clippy::implicit_return)]
async fn send_codeblock<'a>(
&self,
context: EditorContext,
codeblock: String,
accept_type: AcceptType,
) -> Result<()> {
let req = SubmitChangesRequest {
accept_type: accept_type.to_string(),
file_path: context.file_path,
code: codeblock,
start_line: context.start_line,
end_line: context.end_line,
};
let json_str = serde_json::to_string(&req)?;
let temp_file_path = env::temp_dir().join("oatmeal-context");
let mut file = File::create(&temp_file_path).await?;
file.write_all(json_str.as_bytes()).await?;
file.sync_all().await?;
run_lua_command(&format!(
"oatmeal_submit_changes(\"{}\")",
temp_file_path.display()
))
.await?;
return Ok(());
}
}