use std::path::PathBuf;
use itertools::Itertools;
use crate::highlighting::KakHighlightRange;
#[derive(Debug, Eq, PartialEq)]
pub enum Response {
StatusChanged { status: String },
Init {
cmd_fifo_path: PathBuf,
buf_fifo_path: PathBuf,
},
Deinit,
FiletypeSupported {
supported: bool,
remove_default_highlighter: bool,
},
Highlights {
timestamp: u64,
ranges: Vec<KakHighlightRange>,
},
}
impl Response {
pub fn status(status: impl Into<String>) -> Self {
Response::StatusChanged {
status: status.into(),
}
}
pub fn to_kak_cmd(&self, client: Option<&str>) -> Option<String> {
let kak_cmd = match self {
Response::StatusChanged { status, .. } => {
format!("info %{{{status}}}",)
}
Response::Init {
cmd_fifo_path,
buf_fifo_path,
} => [
format!(
"set-option global kts_cmd_fifo_path {path}",
path = cmd_fifo_path.display()
),
format!(
"set-option global kts_buf_fifo_path {path}",
path = buf_fifo_path.display()
),
"kak-tree-sitter-req-enable".to_owned(),
]
.join("\n"),
Response::Deinit => "kak-tree-sitter-deinit".to_owned(),
Response::FiletypeSupported {
supported,
remove_default_highlighter,
} => {
if *supported {
[
Some("kak-tree-sitter-highlight-enable"),
remove_default_highlighter
.then_some(r#"try %{ remove-highlighter "window/%opt{filetype}" }"#),
]
.into_iter()
.flatten()
.join("\n")
} else {
String::new()
}
}
Response::Highlights { timestamp, ranges } => {
let ranges_str = ranges
.iter()
.map(KakHighlightRange::to_kak_range_str)
.join(" ");
format!(
"{range_specs} {timestamp} {ranges_str}",
range_specs = "set buffer kts_highlighter_ranges",
)
}
};
if kak_cmd.is_empty() {
return None;
}
let prefix = if let Some(client) = client {
format!("-try-client {client} ")
} else {
String::new()
};
Some(format!("eval -no-hooks {prefix}%{{{kak_cmd}}}"))
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct ConnectedResponse {
pub session: String,
pub client: Option<String>,
pub resp: Response,
}
impl ConnectedResponse {
pub fn new(
session: impl Into<String>,
client: impl Into<Option<String>>,
resp: Response,
) -> Self {
let session = session.into();
let client = client.into();
Self {
session,
client,
resp,
}
}
}