use crate::config_layers::LayerKind;
use serde_json::Value;
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Kind {
Bool,
Choice(Vec<String>),
Number,
Text,
Json,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Row {
pub key: String,
pub value: Value,
pub kind: Kind,
pub layer: LayerKind,
}
pub fn choices(key: &str) -> Option<Vec<String>> {
let fixed: &[&str] = match key {
"problems_scope" => &["whole_project", "open_files"],
"problems_project_scope" => &["auto", "on", "off"],
"diff_ignore_whitespace" => &["off", "leading", "all"],
"render_whitespace" => &["selection", "all", "none"],
"word_based_suggestions" => &[
"matchingDocuments",
"currentDocument",
"allDocuments",
"off",
],
"theme" => {
return Some(
crate::theme::Theme::all()
.iter()
.map(|t| t.id().to_string())
.collect(),
);
}
_ => return None,
};
Some(fixed.iter().map(|c| c.to_string()).collect())
}
pub fn rows(prefs: &crate::prefs::Prefs, provenance: &BTreeMap<String, LayerKind>) -> Vec<Row> {
let Ok(Value::Object(map)) = serde_json::to_value(prefs) else {
return Vec::new();
};
let mut out: Vec<Row> = map
.into_iter()
.map(|(key, value)| {
let kind = match &value {
Value::Bool(_) => Kind::Bool,
Value::Number(_) => Kind::Number,
Value::String(_) => choices(&key).map(Kind::Choice).unwrap_or(Kind::Text),
_ => Kind::Json,
};
let layer = crate::config_layers::layer_of(provenance, &key);
Row {
key,
value,
kind,
layer,
}
})
.collect();
out.sort_by(|a, b| a.key.cmp(&b.key));
out
}
pub fn display(value: &Value) -> String {
match value {
Value::String(s) if s.is_empty() => String::from("(default)"),
Value::String(s) => s.clone(),
other => other.to_string(),
}
}
pub fn matches(row: &Row, query: &str) -> bool {
let hay = format!(
"{} {} {}",
row.key,
row.key.replace('_', " "),
display(&row.value)
)
.to_lowercase();
query
.split_whitespace()
.all(|w| hay.contains(&w.to_lowercase()))
}
pub fn next_value(row: &Row) -> Option<Value> {
match (&row.kind, &row.value) {
(Kind::Bool, Value::Bool(b)) => Some(Value::Bool(!b)),
(Kind::Choice(c), v) if !c.is_empty() => {
let now = v.as_str().unwrap_or_default();
let at = c.iter().position(|x| x == now).unwrap_or(0);
Some(Value::String(c[(at + 1) % c.len()].clone()))
}
_ => None,
}
}
pub fn set_key(text: &str, key: &str, value: Value) -> Result<String, String> {
let mut map = if text.trim().is_empty() {
serde_json::Map::new()
} else {
match serde_json::from_str::<Value>(text) {
Ok(Value::Object(m)) => m,
Ok(_) => return Err(String::from("the settings file is not a JSON object")),
Err(e) => return Err(format!("the settings file does not parse: {e}")),
}
};
map.insert(key.to_string(), value);
serde_json::to_string_pretty(&Value::Object(map))
.map(|s| s + "\n")
.map_err(|e| e.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn rows_cover_every_setting_with_its_kind_and_layer() {
let prefs = crate::prefs::Prefs {
auto_save: true,
problems_scope: String::from("open_files"),
..Default::default()
};
let provenance = BTreeMap::from([(String::from("auto_save"), LayerKind::Workspace)]);
let all = rows(&prefs, &provenance);
let expected = serde_json::to_value(&prefs)
.unwrap()
.as_object()
.unwrap()
.len();
assert_eq!(all.len(), expected, "one row per setting");
let keys: Vec<&str> = all.iter().map(|r| r.key.as_str()).collect();
let mut sorted = keys.clone();
sorted.sort();
assert_eq!(keys, sorted, "in key order");
let get = |k: &str| all.iter().find(|r| r.key == k).unwrap();
assert_eq!(get("auto_save").kind, Kind::Bool);
assert_eq!(get("auto_save").value, json!(true));
assert_eq!(get("auto_save").layer, LayerKind::Workspace);
assert_eq!(get("copy_on_select").layer, LayerKind::Default);
assert_eq!(
get("problems_scope").kind,
Kind::Choice(vec!["whole_project".into(), "open_files".into()])
);
assert!(matches!(get("theme").kind, Kind::Choice(ref c) if c.len() > 1));
assert_eq!(get("explorer_views").kind, Kind::Json);
}
#[test]
fn values_display_on_one_line() {
assert_eq!(display(&json!("open_files")), "open_files");
assert_eq!(display(&json!("")), "(default)");
assert_eq!(display(&json!(true)), "true");
assert_eq!(display(&json!(12)), "12");
assert_eq!(display(&json!({"a": 1})), "{\"a\":1}");
}
fn row(key: &str, value: Value, kind: Kind) -> Row {
Row {
key: key.into(),
value,
kind,
layer: LayerKind::Default,
}
}
#[test]
fn search_reads_underscores_as_spaces_and_matches_values() {
let r = row("format_on_save", json!(true), Kind::Bool);
assert!(matches(&r, "format save"));
assert!(matches(&r, "FORMAT_ON"));
assert!(matches(&r, "true"));
assert!(!matches(&r, "format type"));
}
#[test]
fn enter_flips_a_bool_and_steps_a_choice_wrapping_round() {
assert_eq!(
next_value(&row("a", json!(false), Kind::Bool)),
Some(json!(true))
);
let c = Kind::Choice(vec!["auto".into(), "on".into(), "off".into()]);
assert_eq!(
next_value(&row("p", json!("on"), c.clone())),
Some(json!("off"))
);
assert_eq!(
next_value(&row("p", json!("off"), c.clone())),
Some(json!("auto"))
);
assert_eq!(next_value(&row("p", json!(""), c)), Some(json!("on")));
assert_eq!(next_value(&row("n", json!(3), Kind::Number)), None);
}
#[test]
fn set_key_writes_one_key_and_keeps_the_rest() {
let out = set_key(
"{\n \"theme\": \"dark\",\n \"auto_save\": false\n}",
"auto_save",
json!(true),
)
.unwrap();
let v: Value = serde_json::from_str(&out).unwrap();
assert_eq!(v, json!({"theme": "dark", "auto_save": true}));
assert_eq!(
serde_json::from_str::<Value>(&set_key("", "k", json!(1)).unwrap()).unwrap(),
json!({"k": 1})
);
assert!(set_key("[1, 2]", "k", json!(1)).is_err());
assert!(set_key("{ broken", "k", json!(1)).is_err());
}
}