use crate::ported::bindings::vim::{vim_command_exists, MatcherInfo};
use serde_json::{json, Value};
pub fn parse_coc_status(coc_status: &str) -> (i32, i32) {
let mut errors_count = 0;
let mut warnings_count = 0;
if coc_status.is_empty() {
return (errors_count, warnings_count);
}
for item in coc_status.split(' ') {
let bytes = item.as_bytes();
if bytes.is_empty() {
continue;
}
if bytes[0] == b'E' {
if let Ok(n) = item[1..].parse() {
errors_count = n;
}
} else if bytes[0] == b'W' {
if let Ok(n) = item[1..].parse() {
warnings_count = n;
}
}
}
(errors_count, warnings_count)
}
pub fn coc(_segment_info: &MatcherInfo, _pl: &()) -> Vec<Value> {
let mut segments = Vec::new();
if !vim_command_exists("CocCommand") {
return segments;
}
let coc_status = "";
let (errors_count, warnings_count) = parse_coc_status(coc_status);
if errors_count > 0 {
segments.push(json!({
"contents": format!("E:{}", errors_count),
"highlight_groups": ["coc:error", "error"],
}));
}
if warnings_count > 0 {
segments.push(json!({
"contents": format!("W:{}", warnings_count),
"highlight_groups": ["coc:warning", "warning"],
}));
}
segments
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_coc_status_empty_returns_zeros() {
assert_eq!(parse_coc_status(""), (0, 0));
}
#[test]
fn parse_coc_status_parses_errors_only() {
assert_eq!(parse_coc_status("E5"), (5, 0));
}
#[test]
fn parse_coc_status_parses_warnings_only() {
assert_eq!(parse_coc_status("W3"), (0, 3));
}
#[test]
fn parse_coc_status_parses_both() {
assert_eq!(parse_coc_status("E2 W7"), (2, 7));
assert_eq!(parse_coc_status("W3 E12"), (12, 3));
}
#[test]
fn parse_coc_status_ignores_unknown_prefix() {
assert_eq!(parse_coc_status("I9"), (0, 0));
}
#[test]
fn coc_returns_empty_without_vim() {
let info = MatcherInfo::default();
assert!(coc(&info, &()).is_empty());
}
}