style: fix clippy collapsible_if warnings in nvim-core

This commit is contained in:
Riz Ashraf committed 2026-09-22 06:07:50 +01:00
1 parent 148025c5a7
commit b2f15de58c
8 files changed
+105 -89

No files matched your search

+92 -74
View File
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use serde_json::{Value, json};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -133,10 +133,10 @@ static NVIM_CONN: LazyLock<Arc<std::sync::Mutex<Option<mpsc::Sender<NvimRequest>
async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
{
let conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner());
if let Some(sender) = conn_lock.as_ref() {
if !sender.is_closed() {
return Ok(sender.clone());
}
if let Some(sender) = conn_lock.as_ref()
&& !sender.is_closed()
{
return Ok(sender.clone());
}
}
@@ -200,20 +200,21 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
Ok(val) => {
offset += cursor.position() as usize;
if let rmpv::Value::Array(ref arr) = val {
if arr.len() >= 4 && arr[0] == rmpv::Value::Integer(1.into()) {
let msgid = match &arr[1] {
rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0),
_ => 0,
};
if let rmpv::Value::Array(ref arr) = val
&& arr.len() >= 4
&& arr[0] == rmpv::Value::Integer(1.into())
{
let msgid = match &arr[1] {
rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0),
_ => 0,
};
if let Some(reply_sender) = pending_clone2
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&msgid)
{
let _ = reply_sender.send(Ok(val));
}
if let Some(reply_sender) = pending_clone2
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&msgid)
{
let _ = reply_sender.send(Ok(val));
}
}
if offset == resp_buf.len() {
@@ -225,11 +226,17 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
}
continue;
}
Err(e) if match &e {
rmpv::decode::Error::InvalidMarkerRead(io_err) => io_err.kind() == std::io::ErrorKind::UnexpectedEof,
rmpv::decode::Error::InvalidDataRead(io_err) => io_err.kind() == std::io::ErrorKind::UnexpectedEof,
_ => false,
} => {
Err(e)
if match &e {
rmpv::decode::Error::InvalidMarkerRead(io_err) => {
io_err.kind() == std::io::ErrorKind::UnexpectedEof
}
rmpv::decode::Error::InvalidDataRead(io_err) => {
io_err.kind() == std::io::ErrorKind::UnexpectedEof
}
_ => false,
} =>
{
resp_buf.drain(..offset);
offset = 0;
@@ -277,11 +284,11 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
});
let mut conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner());
if let Some(existing_sender) = conn_lock.as_ref() {
if !existing_sender.is_closed() {
// Another task established the connection while we were waiting
return Ok(existing_sender.clone());
}
if let Some(existing_sender) = conn_lock.as_ref()
&& !existing_sender.is_closed()
{
// Another task established the connection while we were waiting
return Ok(existing_sender.clone());
}
*conn_lock = Some(tx.clone());
Ok(tx)
@@ -348,21 +355,25 @@ async fn send_nvim_command(cmd: &str) -> Result<(), String> {
}
async fn get_nvim_active_buffer() -> Result<String, String> {
let result = call_nvim_method("nvim_buf_get_lines", vec![
rmpv::Value::Integer(0.into()),
rmpv::Value::Integer(0.into()),
rmpv::Value::Integer((-1).into()),
rmpv::Value::Boolean(true),
]).await?;
let result = call_nvim_method(
"nvim_buf_get_lines",
vec![
rmpv::Value::Integer(0.into()),
rmpv::Value::Integer(0.into()),
rmpv::Value::Integer((-1).into()),
rmpv::Value::Boolean(true),
],
)
.await?;
if let rmpv::Value::Array(lines) = result {
let mut text = String::new();
for line in lines {
if let rmpv::Value::String(s) = line {
if let Some(s) = s.as_str() {
text.push_str(s);
text.push('\n');
}
if let rmpv::Value::String(s) = line
&& let Some(s) = s.as_str()
{
text.push_str(s);
text.push('\n');
}
}
return Ok(text);
@@ -371,14 +382,14 @@ async fn get_nvim_active_buffer() -> Result<String, String> {
}
async fn get_nvim_cursor() -> Result<String, String> {
let result = call_nvim_method("nvim_win_get_cursor", vec![rmpv::Value::Integer(0.into())]).await?;
if let rmpv::Value::Array(pos) = result {
if pos.len() == 2 {
if let (rmpv::Value::Integer(row), rmpv::Value::Integer(col)) = (&pos[0], &pos[1]) {
return Ok(format!("Line: {row}, Column: {col}"));
}
}
let result =
call_nvim_method("nvim_win_get_cursor", vec![rmpv::Value::Integer(0.into())]).await?;
if let rmpv::Value::Array(pos) = result
&& pos.len() == 2
&& let (rmpv::Value::Integer(row), rmpv::Value::Integer(col)) = (&pos[0], &pos[1])
{
return Ok(format!("Line: {row}, Column: {col}"));
}
Err("Invalid response format".to_string())
}
@@ -395,15 +406,19 @@ async fn get_nvim_visual_selection() -> Result<String, String> {
end
"#;
let result = call_nvim_method("nvim_exec_lua", vec![
rmpv::Value::String(lua_script.into()),
rmpv::Value::Array(vec![]),
]).await?;
if let rmpv::Value::String(s) = result {
if let Some(text) = s.as_str() {
return Ok(text.to_string());
}
let result = call_nvim_method(
"nvim_exec_lua",
vec![
rmpv::Value::String(lua_script.into()),
rmpv::Value::Array(vec![]),
],
)
.await?;
if let rmpv::Value::String(s) = result
&& let Some(text) = s.as_str()
{
return Ok(text.to_string());
}
Err("Invalid response format".to_string())
}
@@ -423,11 +438,15 @@ async fn set_nvim_diagnostics(line: i64, message: &str) -> Result<(), String> {
"#
);
call_nvim_method("nvim_exec_lua", vec![
rmpv::Value::String(lua_script.into()),
rmpv::Value::Array(vec![]),
]).await?;
call_nvim_method(
"nvim_exec_lua",
vec![
rmpv::Value::String(lua_script.into()),
rmpv::Value::Array(vec![]),
],
)
.await?;
Ok(())
}
@@ -489,22 +508,21 @@ fn json_to_rmpv(val: &serde_json::Value) -> rmpv::Value {
}
}
serde_json::Value::String(s) => rmpv::Value::String(s.as_str().into()),
serde_json::Value::Array(arr) => {
rmpv::Value::Array(arr.iter().map(json_to_rmpv).collect())
}
serde_json::Value::Object(obj) => {
rmpv::Value::Map(obj.iter().map(|(k, v)| {
(rmpv::Value::String(k.as_str().into()), json_to_rmpv(v))
}).collect())
}
serde_json::Value::Array(arr) => rmpv::Value::Array(arr.iter().map(json_to_rmpv).collect()),
serde_json::Value::Object(obj) => rmpv::Value::Map(
obj.iter()
.map(|(k, v)| (rmpv::Value::String(k.as_str().into()), json_to_rmpv(v)))
.collect(),
),
}
}
async fn execute_nvim_lua_with_args(code: &str, args: Vec<rmpv::Value>) -> Result<String, String> {
let result = call_nvim_method("nvim_exec_lua", vec![
rmpv::Value::String(code.into()),
rmpv::Value::Array(args),
]).await?;
let result = call_nvim_method(
"nvim_exec_lua",
vec![rmpv::Value::String(code.into()), rmpv::Value::Array(args)],
)
.await?;
let json_res = rmpv_to_json(&result);
// If it's a simple string, return it unquoted, otherwise format it pretty
if let serde_json::Value::String(s) = json_res {