#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Scope {
User,
Workspace,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Applies {
Live,
NewPane,
NextStart,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Values {
Any,
OneOf(&'static [&'static str]),
OptionalOneOf(&'static [&'static str]),
OptionalText,
OptionalNumber,
Language,
Theme,
}
impl Values {
pub fn optional(self) -> bool {
matches!(
self,
Self::OptionalOneOf(_) | Self::OptionalText | Self::OptionalNumber | Self::Language
)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Setting {
pub key: &'static str,
pub scope: Scope,
pub applies: Applies,
pub values: Values,
}
const fn user(key: &'static str) -> Setting {
Setting {
key,
scope: Scope::User,
applies: Applies::Live,
values: Values::Any,
}
}
const fn workspace(key: &'static str) -> Setting {
Setting {
scope: Scope::Workspace,
..user(key)
}
}
impl Setting {
const fn values(self, values: Values) -> Self {
Self { values, ..self }
}
const fn applies(self, applies: Applies) -> Self {
Self { applies, ..self }
}
}
pub static SETTINGS: &[Setting] = &[
workspace("theme").values(Values::Theme),
user("osk_split"),
user("suppress_terminal_warning"),
workspace("explorer_views"),
user("sidebar_auto_hide"),
user("problems_scope").values(Values::OneOf(&["whole_project", "open_files"])),
user("problems_project_scope").values(Values::OneOf(&["auto", "on", "off"])),
user("build_column_unit").values(Values::OneOf(&["display", "byte", "char"])),
user("diff_ignore_whitespace").values(Values::OneOf(&["off", "leading", "all"])),
user("disabled_extensions").applies(Applies::NextStart),
user("tour_done").applies(Applies::NextStart),
user("mcp_consented"),
user("mcp_tool_fingerprints"),
user("layout").applies(Applies::NextStart),
workspace("format_on_save"),
workspace("default_formatter").values(Values::OptionalText),
workspace("language_formatters"),
workspace("insert_final_newline"),
workspace("trim_trailing_whitespace"),
workspace("trim_final_newlines"),
workspace("format_on_type"),
workspace("auto_save"),
workspace("auto_save_on_focus_change"),
workspace("disable_inline_blame"),
workspace("disable_auto_close_pairs"),
workspace("disable_paste_reindent"),
workspace("disable_quick_suggestions"),
workspace("disable_inline_values"),
workspace("disable_save_before_debug"),
user("vim_mode"),
user("multi_cursor_persist"),
workspace("render_whitespace").values(Values::OneOf(&["selection", "all", "none"])),
workspace("rulers"),
workspace("word_based_suggestions").values(Values::OneOf(&[
"matchingDocuments",
"currentDocument",
"allDocuments",
"off",
])),
workspace("disable_bracket_colors"),
workspace("disable_indent_guides"),
user("home_walks_wrapped_rows"),
workspace("line_numbers").values(Values::OneOf(&["on", "relative", "interval", "off"])),
workspace("disable_inlay_hints"),
user("disable_proactive_navigator"),
workspace("copy_on_select"),
workspace("files_exclude"),
workspace("search_exclude"),
workspace("disable_compact_folders"),
workspace("files_associations"),
workspace("languages"),
workspace("files_encoding"),
workspace("disable_log_highlight"),
user("disable_secret_redaction"),
user("host_accents"),
user("disable_remote_offer"),
user("remote_offer_excluded_hosts"),
user("config_sync_excluded_hosts"),
user("config_sync_excluded_files"),
user("fleet_groups"),
user("code_scanning").values(Values::OneOf(&["off", "on", "prompt"])),
user("lane_agent").values(Values::OptionalText),
user("color_depth").values(Values::OptionalOneOf(&["", "truecolor", "256", "16"])),
user("terminal_scrollback").applies(Applies::NewPane),
user("terminal_rewind_mb")
.applies(Applies::NewPane)
.values(Values::OptionalNumber),
user("terminal_persistent_panes").applies(Applies::NewPane),
user("terminal_multiline_paste_warning"),
user("review_ai_prefix").values(Values::OptionalText),
user("screen_reader"),
user("screen_reader_command").values(Values::OptionalText),
user("codeql_cli_path").values(Values::OptionalText),
user("locale")
.applies(Applies::NextStart)
.values(Values::Language),
user("notifications"),
];
pub fn setting(key: &str) -> Option<&'static Setting> {
SETTINGS.iter().find(|s| s.key == key)
}
pub fn workspace_may_set(key: &str) -> bool {
setting(key).is_some_and(|s| s.scope == Scope::Workspace)
}
pub fn applies_at_next_start(key: &str) -> bool {
setting(key).is_some_and(|s| s.applies == Applies::NextStart)
}
pub fn values(key: &str) -> Values {
setting(key).map_or(Values::Any, |s| s.values)
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
fn prefs_keys() -> BTreeSet<String> {
let serde_json::Value::Object(map) =
serde_json::to_value(crate::prefs::Prefs::default()).unwrap()
else {
panic!("Prefs serializes to an object");
};
let mut keys: BTreeSet<String> = map.into_iter().map(|(k, _)| k).collect();
keys.insert(String::from("languages"));
keys
}
#[test]
fn every_setting_has_one_row_and_every_row_is_a_setting() {
let rows: Vec<&str> = SETTINGS.iter().map(|s| s.key).collect();
let unique: BTreeSet<&str> = rows.iter().copied().collect();
assert_eq!(unique.len(), rows.len(), "a key with two rows");
let rows: BTreeSet<String> = unique.into_iter().map(String::from).collect();
let keys = prefs_keys();
let missing: Vec<_> = keys.difference(&rows).collect();
let extra: Vec<_> = rows.difference(&keys).collect();
assert!(missing.is_empty(), "settings with no row: {missing:?}");
assert!(extra.is_empty(), "rows for no setting: {extra:?}");
}
#[test]
fn optional_kinds_match_the_settings_that_are_unset_by_default() {
let serde_json::Value::Object(map) =
serde_json::to_value(crate::prefs::Prefs::default()).unwrap()
else {
panic!("Prefs serializes to an object");
};
for s in SETTINGS {
let optional = s.values.optional();
let unset = map.get(s.key).is_some_and(serde_json::Value::is_null);
assert_eq!(optional, unset, "{}", s.key);
}
}
#[test]
fn optional_choices_start_with_unset() {
for s in SETTINGS {
if let Values::OptionalOneOf(values) = s.values {
assert_eq!(values.first(), Some(&""), "{}", s.key);
}
}
}
fn settings_doc() -> String {
std::fs::read_to_string(
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("docs/SETTINGS.md"),
)
.unwrap()
}
fn between<'a>(doc: &'a str, from: &str, to: &str) -> &'a str {
let start = doc
.find(from)
.unwrap_or_else(|| panic!("docs/SETTINGS.md has no {from:?}"));
let rest = &doc[start..];
&rest[..rest
.find(to)
.unwrap_or_else(|| panic!("docs/SETTINGS.md has no {to:?} after {from:?}"))]
}
fn backticked_keys(text: &str) -> BTreeSet<String> {
text.split('`')
.skip(1)
.step_by(2)
.filter(|w| {
!w.is_empty()
&& w.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
})
.map(String::from)
.collect()
}
fn rows(keep: impl Fn(&Setting) -> bool) -> BTreeSet<String> {
SETTINGS
.iter()
.filter(|s| keep(s))
.map(|s| s.key.to_string())
.collect()
}
#[test]
fn the_docs_list_exactly_the_settings_a_workspace_may_set() {
let doc = settings_doc();
let section = between(&doc, "## What workspace layers may set", "\n## ");
let (allowed, refused) = section
.split_once("Everything else")
.expect("the section names what a workspace may not set");
assert_eq!(
backticked_keys(allowed),
rows(|s| s.scope == Scope::Workspace),
"docs/SETTINGS.md's allowlist and the table's Workspace rows differ"
);
let refused = backticked_keys(between(refused, "", "is ignored from workspace layers"));
assert!(refused.len() > 10, "fixture: {refused:?}");
let not_user: Vec<&String> = refused
.iter()
.filter(|k| setting(k).is_none_or(|s| s.scope != Scope::User))
.collect();
assert!(
not_user.is_empty(),
"docs/SETTINGS.md refuses these from a workspace, and no user-only row says so: {not_user:?}"
);
}
#[test]
fn the_docs_name_when_each_delayed_setting_applies() {
let doc = settings_doc();
assert_eq!(
backticked_keys(between(
&doc,
"read once, when croft starts",
"Settings (UI) says so"
)),
rows(|s| s.applies == Applies::NextStart),
);
assert_eq!(
backticked_keys(between(
&doc,
"re-merge on the spot.",
"apply to the next terminal pane"
)),
rows(|s| s.applies == Applies::NewPane),
);
}
}