use anyhow::{Context, Result};
use serde_json::{Map, Value};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
const USER_DIRS: &[(&str, &str)] = &[
("VS Code", "Code"),
("VS Code Insiders", "Code - Insiders"),
("VSCodium", "VSCodium"),
("Cursor", "Cursor"),
("Windsurf", "Windsurf"),
];
pub fn discover_profiles() -> Vec<(String, PathBuf)> {
let mut roots: Vec<PathBuf> = Vec::new();
if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
roots.push(home.join("Library").join("Application Support"));
roots.push(home.join(".config"));
}
if let Some(appdata) = std::env::var_os("APPDATA").map(PathBuf::from) {
roots.push(appdata);
}
let mut found = Vec::new();
for (label, dir) in USER_DIRS {
for root in &roots {
let candidate = root.join(dir).join("User");
if candidate.is_dir()
&& !found
.iter()
.any(|(_, p): &(String, PathBuf)| p == &candidate)
{
found.push(((*label).to_string(), candidate));
}
}
}
found
}
struct SettingMap {
vscode: &'static str,
croft: &'static str,
convert: fn(&Value) -> Option<Value>,
}
fn as_bool(v: &Value) -> Option<Value> {
v.as_bool().map(Value::from)
}
fn negated(v: &Value) -> Option<Value> {
v.as_bool().map(|b| Value::from(!b))
}
fn off_means_disabled(v: &Value) -> Option<Value> {
match v {
Value::Bool(b) => Some(Value::from(!*b)),
Value::String(s) => Some(Value::from(s == "off")),
_ => None,
}
}
fn never_means_disabled(v: &Value) -> Option<Value> {
v.as_str().map(|s| Value::from(s == "never"))
}
fn as_whitespace_mode(v: &Value) -> Option<Value> {
let s = v.as_str()?;
let mapped = match s {
"none" | "selection" | "all" => s,
"boundary" | "trailing" => "none",
_ => return None,
};
Some(Value::from(mapped))
}
fn as_scrollback(v: &Value) -> Option<Value> {
v.as_u64().map(Value::from)
}
const SETTINGS: &[SettingMap] = &[
SettingMap {
vscode: "editor.formatOnSave",
croft: "format_on_save",
convert: as_bool,
},
SettingMap {
vscode: "editor.formatOnType",
croft: "format_on_type",
convert: as_bool,
},
SettingMap {
vscode: "editor.bracketPairColorization.enabled",
croft: "disable_bracket_colors",
convert: negated,
},
SettingMap {
vscode: "editor.guides.indentation",
croft: "disable_indent_guides",
convert: negated,
},
SettingMap {
vscode: "editor.inlayHints.enabled",
croft: "disable_inlay_hints",
convert: off_means_disabled,
},
SettingMap {
vscode: "editor.autoClosingBrackets",
croft: "disable_auto_close_pairs",
convert: never_means_disabled,
},
SettingMap {
vscode: "debug.inlineValues",
croft: "disable_inline_values",
convert: off_means_disabled,
},
SettingMap {
vscode: "editor.renderWhitespace",
croft: "render_whitespace",
convert: as_whitespace_mode,
},
SettingMap {
vscode: "terminal.integrated.scrollback",
croft: "terminal_scrollback",
convert: as_scrollback,
},
SettingMap {
vscode: "terminal.integrated.copyOnSelection",
croft: "copy_on_select",
convert: as_bool,
},
];
const COMMANDS: &[(&str, &str)] = &[
("workbench.action.files.save", "save_file"),
("workbench.action.quickOpen", "quick_open"),
("workbench.action.gotoSymbol", "go_to_symbol"),
("workbench.action.showAllSymbols", "go_to_workspace_symbol"),
("workbench.action.closeActiveEditor", "close_editor"),
(
"workbench.action.reopenClosedEditor",
"reopen_closed_editor",
),
("workbench.action.splitEditor", "split_editor"),
(
"workbench.action.toggleSidebarVisibility",
"toggle_side_bar",
),
(
"workbench.action.toggleAuxiliaryBar",
"toggle_secondary_side_bar",
),
(
"workbench.action.terminal.toggleTerminal",
"toggle_terminal",
),
("workbench.action.terminal.new", "new_terminal"),
("workbench.action.toggleZenMode", "toggle_zen_mode"),
("workbench.view.explorer", "show_explorer"),
("workbench.view.search", "show_search"),
("workbench.view.scm", "show_source_control"),
("workbench.view.debug", "show_run_debug"),
("workbench.view.testing", "show_testing"),
("workbench.view.extensions", "show_extensions"),
("workbench.action.openSettings", "open_settings"),
("workbench.action.openSettingsJson", "open_settings_json"),
(
"workbench.action.openGlobalKeybindingsFile",
"open_keybindings_json",
),
(
"workbench.action.openGlobalKeybindings",
"keyboard_shortcuts",
),
("editor.action.formatDocument", "format_document"),
("editor.action.formatSelection", "format_selection"),
("editor.action.commentLine", "toggle_line_comment"),
("editor.action.blockComment", "toggle_block_comment"),
("editor.action.quickFix", "quick_fix"),
("editor.action.peekDefinition", "peek_definition"),
("editor.action.startFindReplaceAction", "replace_in_file"),
("editor.action.moveLinesUpAction", "move_line_up"),
("editor.action.moveLinesDownAction", "move_line_down"),
("editor.action.deleteLines", "delete_line"),
("editor.action.joinLines", "join_lines"),
("editor.action.transformToUppercase", "transform_upper"),
("editor.action.transformToLowercase", "transform_lower"),
("editor.action.transformToTitlecase", "transform_title"),
(
"editor.action.trimTrailingWhitespace",
"trim_trailing_whitespace",
),
("editor.action.indentationToSpaces", "indentation_to_spaces"),
("editor.action.indentationToTabs", "indentation_to_tabs"),
("editor.action.insertCursorAbove", "add_cursor_above"),
("editor.action.insertCursorBelow", "add_cursor_below"),
(
"editor.action.addSelectionToNextFindMatch",
"add_selection_to_next_match",
),
("editor.action.smartSelect.expand", "expand_selection"),
("editor.action.smartSelect.shrink", "shrink_selection"),
("editor.action.jumpToBracket", "jump_to_bracket"),
("editor.action.selectToBracket", "select_to_bracket"),
("editor.foldAll", "fold_all"),
("editor.unfoldAll", "unfold_all"),
("editor.toggleFold", "toggle_fold"),
("markdown.showPreview", "toggle_markdown_preview"),
("workbench.action.tasks.build", "run_build_task"),
("workbench.action.tasks.runTask", "run_task"),
("workbench.action.tasks.reRunTask", "rerun_last_task"),
("workbench.action.debug.start", "start_debugging"),
("workbench.action.debug.stop", "stop_debugging"),
("workbench.action.debug.restart", "restart_debugging"),
("workbench.action.debug.pause", "pause_debugging"),
("workbench.action.debug.stepOver", "step_over"),
("editor.debug.action.toggleBreakpoint", "toggle_breakpoint"),
("undo", "undo"),
("redo", "redo"),
("references-view.showCallHierarchy", "show_incoming_calls"),
("workbench.action.navigateBack", "navigate_back"),
("workbench.action.navigateForward", "navigate_forward"),
];
const EXTENSION_DIRS: &[(&str, &str)] = &[
("Code", ".vscode"),
("Code - Insiders", ".vscode-insiders"),
("VSCodium", ".vscode-oss"),
("Cursor", ".cursor"),
("Windsurf", ".windsurf"),
];
pub fn theme_extension_dirs_under(home: &Path, user_dir: &Path) -> Vec<PathBuf> {
let product = user_dir
.parent()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str());
if let Some(dot) = product.and_then(|p| {
EXTENSION_DIRS
.iter()
.find(|(name, _)| *name == p)
.map(|(_, dot)| *dot)
}) {
return vec![home.join(dot).join("extensions")];
}
EXTENSION_DIRS
.iter()
.map(|(_, dot)| home.join(dot).join("extensions"))
.collect()
}
pub fn theme_extension_dirs(user_dir: &Path) -> Vec<PathBuf> {
let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE"));
match home.map(PathBuf::from) {
Some(home) => theme_extension_dirs_under(&home, user_dir),
None => Vec::new(),
}
}
fn extension_order(dir: &Path) -> (String, std::cmp::Reverse<Vec<u64>>) {
let name = dir.file_name().and_then(|n| n.to_str()).unwrap_or_default();
let (stem, version) = name.rsplit_once('-').unwrap_or((name, ""));
let parts: Vec<u64> = version
.split('.')
.map(|p| {
p.trim_end_matches(|c: char| !c.is_ascii_digit())
.parse()
.unwrap_or(0)
})
.collect();
(stem.to_string(), std::cmp::Reverse(parts))
}
pub fn find_installed_themes(label: &str, extension_dirs: &[PathBuf]) -> Vec<PathBuf> {
let mut found = Vec::new();
for dir in extension_dirs {
let Ok(entries) = std::fs::read_dir(dir) else {
continue;
};
let mut extensions: Vec<PathBuf> =
entries.filter_map(|e| e.ok().map(|e| e.path())).collect();
extensions.sort_by_key(|p| extension_order(p));
for ext in extensions {
let Ok(raw) = std::fs::read_to_string(ext.join("package.json")) else {
continue;
};
let Ok(pkg) = serde_json::from_str::<Value>(&raw) else {
continue;
};
let Some(themes) = pkg["contributes"]["themes"].as_array() else {
continue;
};
for theme in themes {
let named =
theme["id"].as_str() == Some(label) || theme["label"].as_str() == Some(label);
let Some(rel) = theme["path"].as_str().filter(|_| named) else {
continue;
};
let rel = Path::new(rel);
let escapes = rel.is_absolute()
|| rel
.components()
.any(|c| matches!(c, std::path::Component::ParentDir));
if !escapes {
found.push(ext.join(rel));
}
}
}
}
found
}
#[derive(Debug, Clone)]
pub struct ThemeSource {
pub label: String,
pub path: PathBuf,
pub converted: crate::vscode_theme::Converted,
}
#[derive(Debug, Default)]
pub struct Report {
pub theme: Option<ThemeSource>,
pub settings: BTreeMap<String, Value>,
pub unmapped_settings: Vec<String>,
pub keybindings: Vec<(String, String)>,
pub dropped_keybindings: Vec<String>,
pub snippets: Map<String, Value>,
pub conflicts: Vec<String>,
pub refusals: Vec<String>,
pub warnings: Vec<String>,
}
impl Report {
pub fn is_empty(&self) -> bool {
self.settings.is_empty() && self.keybindings.is_empty() && self.snippets.is_empty()
}
}
fn read_jsonc(path: &Path) -> Result<Option<Value>> {
if !path.is_file() {
return Ok(None);
}
let raw =
std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
let stripped = crate::tasks::strip_jsonc(&raw);
if stripped.trim().is_empty() {
return Ok(None);
}
let value =
serde_json::from_str(&stripped).with_context(|| format!("parsing {}", path.display()))?;
Ok(Some(value))
}
pub fn map_settings(
obj: &Map<String, Value>,
) -> (BTreeMap<String, Value>, Vec<String>, Vec<String>) {
let mut settings: BTreeMap<String, Value> = BTreeMap::new();
let mut unmapped: Vec<String> = Vec::new();
let mut warnings: Vec<String> = Vec::new();
for (key, value) in obj {
match SETTINGS.iter().find(|m| m.vscode == key) {
Some(map) => match (map.convert)(value) {
Some(converted) => {
if key == "editor.renderWhitespace" && converted != *value {
warnings.push(format!(
"editor.renderWhitespace {value} has no croft mode; using {converted}"
));
}
settings.insert(map.croft.to_string(), converted);
}
None => unmapped.push(format!(
"{key} (croft has {}, but not for this value: {value})",
map.croft
)),
},
None => {
if let Some(mode) = value.as_str().filter(|_| key == "files.autoSave") {
let (delay, focus) = match mode {
"afterDelay" => (true, false),
"onFocusChange" | "onWindowChange" => (false, true),
"off" => (false, false),
_ => {
unmapped.push(format!("{key} (unrecognised value: {value})"));
continue;
}
};
settings.insert(String::from("auto_save"), Value::from(delay));
settings.insert(
String::from("auto_save_on_focus_change"),
Value::from(focus),
);
continue;
}
unmapped.push(key.clone());
}
}
}
unmapped.sort();
(settings, unmapped, warnings)
}
pub fn convert_settings(doc: &Value, report: &mut Report) {
let Some(obj) = doc.as_object() else {
report
.warnings
.push(String::from("settings.json is not a JSON object; skipped"));
return;
};
let (settings, unmapped, warnings) = map_settings(obj);
report.settings.extend(settings);
report.unmapped_settings.extend(unmapped);
report.warnings.extend(warnings);
report.unmapped_settings.sort();
}
pub fn convert_keybindings(doc: &Value, report: &mut Report) {
let Some(rows) = doc.as_array() else {
report.warnings.push(String::from(
"keybindings.json is not a JSON array; skipped",
));
return;
};
let mut seen: BTreeMap<String, String> = BTreeMap::new();
for row in rows {
let Some(command) = row.get("command").and_then(Value::as_str) else {
continue;
};
let Some(key) = row.get("key").and_then(Value::as_str) else {
continue;
};
if let Some(removed) = command.strip_prefix('-') {
report.dropped_keybindings.push(format!(
"{key}: unbinding {removed} has no croft equivalent"
));
continue;
}
if key.split_whitespace().count() > 1 {
report
.dropped_keybindings
.push(format!("{key}: croft has no multi-key chord sequences"));
continue;
}
match COMMANDS.iter().find(|(vs, _)| *vs == command) {
Some((_, croft)) => {
seen.insert(key.to_string(), (*croft).to_string());
}
None => report
.dropped_keybindings
.push(format!("{key}: no croft command matches {command}")),
}
}
report.keybindings = seen.into_iter().collect();
report.dropped_keybindings.sort();
}
pub fn convert_snippets(doc: &Value, language: Option<&str>, report: &mut Report) {
let Some(obj) = doc.as_object() else {
report.warnings.push(String::from(
"a snippets file is not a JSON object; skipped",
));
return;
};
for (name, body) in obj {
let Some(entry) = body.as_object() else {
continue;
};
if entry.get("prefix").is_none() || entry.get("body").is_none() {
report
.warnings
.push(format!("snippet {name:?} has no prefix or body; skipped"));
continue;
}
let mut out = entry.clone();
if let Some(lang) = language {
out.entry("scope")
.or_insert_with(|| Value::from(lang.to_string()));
}
report.snippets.insert(name.clone(), Value::Object(out));
}
}
fn resolve_color_theme(label: &str, extension_dirs: &[PathBuf], report: &mut Report) {
let mut matches = find_installed_themes(label, extension_dirs).into_iter();
let Some(path) = matches.next() else {
report.warnings.push(format!(
"workbench.colorTheme {label:?} is not an installed extension theme (a built-in VS \
Code theme lives in the app, not the extensions directory); to bring it over, run \
`croft theme-import <theme.json>` on its file"
));
return;
};
let alternates: Vec<String> = matches.map(|p| p.display().to_string()).collect();
if !alternates.is_empty() {
report.warnings.push(format!(
"workbench.colorTheme {label:?} matches more than one installed theme; took {} \
and ignored {}",
path.display(),
alternates.join(", ")
));
}
match crate::vscode_theme::convert_file(&path, None) {
Ok(converted) => {
report
.settings
.insert(String::from("theme"), Value::from(converted.id.clone()));
report
.unmapped_settings
.retain(|k| k != "workbench.colorTheme");
report.theme = Some(ThemeSource {
label: label.to_string(),
path,
converted,
});
}
Err(err) => report.warnings.push(format!(
"workbench.colorTheme {label:?} was found at {} but could not be converted: {err:#}",
path.display()
)),
}
}
pub fn scan_profile(dir: &Path) -> Result<Report> {
scan_profile_with_extensions(dir, &theme_extension_dirs(dir))
}
pub fn scan_profile_with_extensions(dir: &Path, extension_dirs: &[PathBuf]) -> Result<Report> {
let mut report = Report::default();
if let Some(doc) = read_jsonc(&dir.join("settings.json"))? {
convert_settings(&doc, &mut report);
if let Some(label) = doc["workbench.colorTheme"].as_str() {
resolve_color_theme(label, extension_dirs, &mut report);
}
}
if let Some(doc) = read_jsonc(&dir.join("keybindings.json"))? {
convert_keybindings(&doc, &mut report);
}
let snippets_dir = dir.join("snippets");
if snippets_dir.is_dir() {
let mut entries: Vec<PathBuf> = std::fs::read_dir(&snippets_dir)
.with_context(|| format!("reading {}", snippets_dir.display()))?
.filter_map(|e| e.ok().map(|e| e.path()))
.collect();
entries.sort();
for path in entries {
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
let language = match ext {
"json" => path.file_stem().and_then(|s| s.to_str()),
"code-snippets" => None,
_ => continue,
};
if let Some(doc) = read_jsonc(&path)? {
convert_snippets(&doc, language, &mut report);
}
}
}
Ok(report)
}
fn carries_jsonc_extras(path: &Path) -> bool {
let Ok(raw) = std::fs::read_to_string(path) else {
return false;
};
raw != crate::tasks::strip_jsonc(&raw)
}
fn read_or_refuse(path: &Path, report: &mut Report) -> (Option<Value>, bool) {
match read_jsonc(path) {
Ok(v) => (v, false),
Err(e) => {
let name = path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| path.display().to_string());
report.refusals.push(format!(
"{name} could not be read ({e}), so it was left untouched"
));
(None, true)
}
}
}
pub fn apply(report: &mut Report) -> Result<Vec<PathBuf>> {
apply_into_dirs(
&crate::prefs::config_dir(),
&crate::lsp::manifest::user_extensions_dir(),
report,
)
}
#[cfg(test)]
pub fn apply_into(dir: &Path, report: &mut Report) -> Result<Vec<PathBuf>> {
apply_into_dirs(dir, &dir.join("extensions"), report)
}
pub fn apply_into_dirs(
dir: &Path,
extensions_dir: &Path,
report: &mut Report,
) -> Result<Vec<PathBuf>> {
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
let mut written = Vec::new();
if let Some(theme) = &report.theme {
let installed = crate::vscode_theme::install_into(extensions_dir, &theme.converted)
.with_context(|| format!("installing theme {:?}", theme.label))?;
written.push(installed.path);
}
let mut wrong_shape;
if !report.settings.is_empty() {
let path = dir.join("config.json");
if carries_jsonc_extras(&path) {
report.refusals.push(String::from(
"config.json: left untouched, because rewriting it would drop its comments or hand formatting. Add the entries above by hand.",
));
} else {
let (doc, unusable) = read_or_refuse(&path, report);
wrong_shape = unusable;
let mut existing: Map<String, Value> = match doc {
Some(Value::Object(m)) => m,
Some(_) => {
report.refusals.push(String::from(
"config.json is not a JSON object, so it was left untouched",
));
wrong_shape = true;
Map::new()
}
None => Map::new(),
};
let mut changed = false;
for (key, value) in &report.settings {
if let Some(current) = existing.get(key) {
if current != value {
report.conflicts.push(if key == "theme" && report.theme.is_some() {
format!(
"config.json theme: kept {current}; the converted theme {value} \
is installed and selectable under the gear menu, set \
theme = {value} to switch"
)
} else {
format!("config.json {key}: kept {current}, VS Code had {value}")
});
}
continue;
}
existing.insert(key.clone(), value.clone());
changed = true;
}
if changed && !wrong_shape {
write_json(&path, &Value::Object(existing))?;
written.push(path);
}
}
}
if !report.keybindings.is_empty() {
let path = dir.join("keybindings.json");
if carries_jsonc_extras(&path) {
report.refusals.push(String::from(
"keybindings.json: left untouched, because rewriting it would drop its comments or hand formatting. Add the entries above by hand.",
));
} else {
let (doc, unusable) = read_or_refuse(&path, report);
wrong_shape = unusable;
let mut rows: Vec<Value> = match doc {
Some(Value::Array(a)) => a,
Some(_) => {
report.refusals.push(String::from(
"keybindings.json is not a JSON array, so it was left untouched",
));
wrong_shape = true;
Vec::new()
}
None => Vec::new(),
};
let bound: Vec<String> = rows
.iter()
.filter_map(|r| r.get("key").and_then(Value::as_str).map(str::to_string))
.collect();
let mut changed = false;
for (key, command) in &report.keybindings {
if bound.iter().any(|k| k == key) {
report
.conflicts
.push(format!("keybindings.json {key}: already bound, left alone"));
continue;
}
let mut row = Map::new();
row.insert(String::from("key"), Value::from(key.clone()));
row.insert(String::from("command"), Value::from(command.clone()));
rows.push(Value::Object(row));
changed = true;
}
if changed && !wrong_shape {
write_json(&path, &Value::Array(rows))?;
written.push(path);
}
}
}
if !report.snippets.is_empty() {
let path = dir.join("snippets.json");
if carries_jsonc_extras(&path) {
report.refusals.push(String::from(
"snippets.json: left untouched, because rewriting it would drop its comments or hand formatting. Add the entries above by hand.",
));
} else {
let (doc, unusable) = read_or_refuse(&path, report);
wrong_shape = unusable;
let mut existing: Map<String, Value> = match doc {
Some(Value::Object(m)) => m,
Some(_) => {
report.refusals.push(String::from(
"snippets.json is not a JSON object, so it was left untouched",
));
wrong_shape = true;
Map::new()
}
None => Map::new(),
};
let mut changed = false;
for (name, entry) in &report.snippets {
if existing.contains_key(name) {
report.conflicts.push(format!(
"snippets.json {name:?}: already defined, left alone"
));
continue;
}
existing.insert(name.clone(), entry.clone());
changed = true;
}
if changed && !wrong_shape {
write_json(&path, &Value::Object(existing))?;
written.push(path);
}
}
}
report.conflicts.sort();
report.refusals.sort();
Ok(written)
}
fn write_json(path: &Path, value: &Value) -> Result<()> {
let text = serde_json::to_string_pretty(value)?;
std::fs::write(path, format!("{text}\n")).with_context(|| format!("writing {}", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn settings_map_only_where_croft_has_a_real_equivalent() {
let doc = json!({
"editor.formatOnSave": true,
"editor.bracketPairColorization.enabled": false,
"editor.autoClosingBrackets": "never",
"editor.renderWhitespace": "all",
"terminal.integrated.scrollback": 20000,
"editor.fontFamily": "Fira Code",
"workbench.iconTheme": "material-icon-theme"
});
let mut report = Report::default();
convert_settings(&doc, &mut report);
assert_eq!(report.settings["format_on_save"], json!(true));
assert_eq!(
report.settings["disable_bracket_colors"],
json!(true),
"VS Code enables the feature, croft names the negative"
);
assert_eq!(report.settings["disable_auto_close_pairs"], json!(true));
assert_eq!(report.settings["render_whitespace"], json!("all"));
assert_eq!(report.settings["terminal_scrollback"], json!(20000));
assert!(
report
.unmapped_settings
.contains(&String::from("editor.fontFamily"))
);
assert!(
report
.unmapped_settings
.contains(&String::from("workbench.iconTheme"))
);
}
#[test]
fn auto_save_modes_map_onto_both_croft_toggles() {
for (mode, delay, focus) in [
("afterDelay", true, false),
("onFocusChange", false, true),
("onWindowChange", false, true),
("off", false, false),
] {
let mut report = Report::default();
convert_settings(&json!({ "files.autoSave": mode }), &mut report);
assert_eq!(report.settings["auto_save"], json!(delay), "{mode}");
assert_eq!(
report.settings["auto_save_on_focus_change"],
json!(focus),
"{mode}"
);
}
}
#[test]
fn every_mapped_setting_is_a_real_croft_pref() {
let defaults =
serde_json::to_value(crate::prefs::Prefs::default()).expect("Prefs serialises");
let known = defaults.as_object().expect("Prefs is a JSON object");
let mut emitted: Vec<&str> = SETTINGS.iter().map(|m| m.croft).collect();
emitted.push("auto_save");
emitted.push("auto_save_on_focus_change");
for key in emitted {
assert!(
known.contains_key(key),
"{key} is not a field of Prefs, so croft would ignore it"
);
}
}
#[test]
fn a_converted_whitespace_mode_round_trips_through_croft() {
use crate::widgets::editor::WhitespaceMode;
for (vscode, expected) in [
("none", "none"),
("all", "all"),
("selection", "selection"),
("boundary", "none"),
("trailing", "none"),
] {
let mut report = Report::default();
convert_settings(&json!({ "editor.renderWhitespace": vscode }), &mut report);
let stored = report.settings["render_whitespace"].as_str().unwrap();
assert_eq!(stored, expected, "{vscode} converted to {stored}");
assert_eq!(
WhitespaceMode::from_pref(stored).pref_id(),
stored,
"{stored:?} does not survive croft's own parse: from_pref sends \
anything unrecognised to Selection, so a wrong id can look \
correct while depending on that fallback"
);
}
}
#[test]
fn every_written_keybinding_is_one_croft_can_load() {
let doc = json!([
{ "key": "ctrl+shift+p", "command": "workbench.action.quickOpen" },
{ "key": "ctrl+k z", "command": "workbench.action.toggleZenMode" },
{ "key": "f12", "command": "workbench.action.tasks.build" }
]);
let mut report = Report::default();
convert_keybindings(&doc, &mut report);
assert!(
report
.dropped_keybindings
.iter()
.any(|d| d.contains("ctrl+k z")),
"a chord sequence croft cannot express must be reported, not \
written: {:?}",
report.dropped_keybindings
);
let rows: Vec<Value> = report
.keybindings
.iter()
.map(|(key, command)| json!({ "key": key, "command": command }))
.collect();
let json = serde_json::to_string(&Value::Array(rows)).unwrap();
let (_keymap, warnings) = crate::keymap::Keymap::resolve(&json);
assert!(
warnings.is_empty(),
"croft rejected rows this import would have written: {warnings:?}"
);
}
#[test]
fn keybindings_convert_by_table_and_name_what_they_drop() {
let doc = json!([
{ "key": "ctrl+shift+p", "command": "workbench.action.quickOpen" },
{ "key": "ctrl+k z", "command": "workbench.action.toggleZenMode" },
{ "key": "ctrl+alt+q", "command": "some.extension.command" },
{ "key": "ctrl+b", "command": "-workbench.action.toggleSidebarVisibility" }
]);
let mut report = Report::default();
convert_keybindings(&doc, &mut report);
assert_eq!(
report.keybindings,
vec![(String::from("ctrl+shift+p"), String::from("quick_open"))],
"only rows croft can both understand and PARSE are written: \
`ctrl+k z` is a chord sequence croft cannot express"
);
assert_eq!(
report.dropped_keybindings.len(),
3,
"an unknown command, an unbinding and a chord sequence are all \
reported: {:?}",
report.dropped_keybindings
);
assert!(
report
.dropped_keybindings
.iter()
.any(|d| d.contains("some.extension.command")),
"a chord croft cannot honour must be NAMED, not bound to something \
that merely sounds similar"
);
}
#[test]
fn every_mapped_command_id_is_a_real_croft_command() {
for (vscode, croft) in COMMANDS {
assert!(
crate::widgets::command_palette::Command::from_id(croft).is_some(),
"{vscode} maps to {croft}, which is not a croft command id"
);
}
}
#[test]
fn no_keyboard_shortcut_maps_to_a_pointer_driven_command() {
for (vscode, croft) in COMMANDS {
assert!(
!croft.starts_with("mouse_"),
"{vscode} maps to {croft}, which acts on the last POINTER \
position: a chord bound to it fires wherever the mouse \
happens to be, not at the caret"
);
}
}
#[test]
fn the_workspace_layer_and_the_importer_agree_on_a_settings_file() {
let doc = json!({
"files.autoSave": "onWindowChange",
"editor.formatOnSave": true,
"editor.renderWhitespace": "boundary",
"terminal.integrated.scrollback": 9000
});
let obj = doc.as_object().unwrap();
let (shared, _unmapped, _warnings) = map_settings(obj);
assert_eq!(
shared["auto_save_on_focus_change"],
json!(true),
"onWindowChange saves when the window loses focus, which is what \
croft's focus-change toggle means"
);
for key in [
"auto_save_on_focus_change",
"format_on_save",
"render_whitespace",
] {
assert!(
crate::config_layers::WORKSPACE_ALLOWED_KEYS.contains(&key),
"{key} should reach a workspace layer"
);
}
assert!(
!crate::config_layers::WORKSPACE_ALLOWED_KEYS.contains(&"terminal_scrollback"),
"a workspace must not set the scrollback, so the filter must drop it"
);
}
#[test]
fn a_commented_config_file_is_never_rewritten() {
let dir = tempfile::TempDir::new().unwrap();
let keybindings = dir.path().join("keybindings.json");
std::fs::write(&keybindings, crate::keymap::TEMPLATE).unwrap();
let before = std::fs::read_to_string(&keybindings).unwrap();
assert!(
before.contains("//"),
"croft's own template carries comments, which is the whole point"
);
let mut report = Report::default();
report
.keybindings
.push((String::from("ctrl+shift+p"), String::from("quick_open")));
report
.snippets
.insert(String::from("S"), json!({ "prefix": "s", "body": "x" }));
report
.settings
.insert(String::from("format_on_save"), json!(true));
let written = apply_into(dir.path(), &mut report).expect("apply runs");
assert_eq!(
std::fs::read_to_string(&keybindings).unwrap(),
before,
"the commented file must be byte-for-byte untouched"
);
assert!(
!written.contains(&keybindings),
"and must not be reported as written"
);
assert!(
report
.refusals
.iter()
.any(|c| c.contains("keybindings.json") && c.contains("comments")),
"the user must be told why, got {:?}",
report.refusals
);
assert!(
written.iter().any(|p| p.ends_with("config.json")),
"an uncommented file must still be merged: {written:?}"
);
}
#[test]
fn a_wrong_shaped_config_file_is_reported_rather_than_overwritten() {
let dir = tempfile::TempDir::new().unwrap();
let cases = [
("config.json", "[1, 2, 3]\n"),
("keybindings.json", "{ \"not\": \"an array\" }\n"),
("snippets.json", "\"a bare string\"\n"),
];
for (name, body) in cases {
std::fs::write(dir.path().join(name), body).unwrap();
}
let mut report = Report::default();
report
.settings
.insert(String::from("format_on_save"), json!(true));
report
.keybindings
.push((String::from("ctrl+shift+p"), String::from("quick_open")));
report
.snippets
.insert(String::from("S"), json!({ "prefix": "s", "body": "x" }));
let written = apply_into(dir.path(), &mut report).expect("apply runs");
assert!(
written.is_empty(),
"nothing may be written over a wrong-shaped file: {written:?}"
);
for (name, body) in cases {
assert_eq!(
std::fs::read_to_string(dir.path().join(name)).unwrap(),
body,
"{name} must be byte-for-byte untouched"
);
assert!(
report.refusals.iter().any(|c| c.starts_with(name)),
"{name} must be reported as a refusal, got {:?}",
report.refusals
);
}
}
#[test]
fn an_unparsable_file_is_refused_without_stopping_the_others() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(dir.path().join("config.json"), "{ \"a\": }\n").unwrap();
let mut report = Report::default();
report
.settings
.insert(String::from("format_on_save"), json!(true));
report
.snippets
.insert(String::from("S"), json!({ "prefix": "s", "body": "x" }));
let written = apply_into(dir.path(), &mut report)
.expect("an unreadable file must not fail the whole import");
assert!(
written.iter().any(|p| p.ends_with("snippets.json")),
"the other files must still be written: {written:?}"
);
assert!(
!written.iter().any(|p| p.ends_with("config.json")),
"and the unreadable one must not be"
);
assert!(
report
.refusals
.iter()
.any(|r| r.starts_with("config.json") && r.contains("could not be read")),
"the user must be told which file and why, got {:?}",
report.refusals
);
assert_eq!(
std::fs::read_to_string(dir.path().join("config.json")).unwrap(),
"{ \"a\": }\n",
"and it must be left byte-for-byte alone"
);
}
#[test]
fn a_refusal_is_reported_apart_from_a_conflict() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(dir.path().join("keybindings.json"), crate::keymap::TEMPLATE).unwrap();
std::fs::write(
dir.path().join("config.json"),
r#"{ "format_on_save": false }"#,
)
.unwrap();
let mut report = Report::default();
report
.settings
.insert(String::from("format_on_save"), json!(true));
report
.keybindings
.push((String::from("ctrl+shift+p"), String::from("quick_open")));
apply_into(dir.path(), &mut report).expect("apply runs");
assert!(
report
.conflicts
.iter()
.all(|c| !c.contains("left untouched")),
"a refusal must not appear as a conflict: {:?}",
report.conflicts
);
assert!(
report
.refusals
.iter()
.any(|r| r.contains("keybindings.json")),
"the refusal belongs in its own list: {:?}",
report.refusals
);
assert!(
report
.conflicts
.iter()
.any(|c| c.contains("format_on_save")),
"and the real conflict stays a conflict: {:?}",
report.conflicts
);
}
#[test]
fn a_wrong_shaped_file_does_not_stop_the_other_two() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(dir.path().join("config.json"), "[1]\n").unwrap();
let mut report = Report::default();
report
.settings
.insert(String::from("format_on_save"), json!(true));
report
.snippets
.insert(String::from("S"), json!({ "prefix": "s", "body": "x" }));
let written = apply_into(dir.path(), &mut report).expect("apply runs");
assert!(
written.iter().any(|p| p.ends_with("snippets.json")),
"the snippets file is fine and must still be written: {written:?}"
);
assert!(!written.iter().any(|p| p.ends_with("config.json")));
}
#[test]
fn a_conflict_message_names_the_value_it_kept() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(
dir.path().join("config.json"),
r#"{ "format_on_save": false }"#,
)
.unwrap();
std::fs::write(
dir.path().join("snippets.json"),
r#"{ "Main": { "prefix": "m", "body": "mine" } }"#,
)
.unwrap();
let mut report = Report::default();
report
.settings
.insert(String::from("format_on_save"), json!(true));
report.snippets.insert(
String::from("Main"),
json!({ "prefix": "m", "body": "theirs" }),
);
apply_into(dir.path(), &mut report).expect("apply runs");
let joined = report.conflicts.join("\n");
assert!(
joined.contains("format_on_save") && joined.contains("false"),
"the settings conflict must name the key and the kept value: {joined}"
);
assert!(
joined.contains("Main"),
"the snippet conflict must name the snippet: {joined}"
);
assert!(
!joined.contains('{') && !joined.contains('}'),
"no message may print a literal placeholder: {joined}"
);
}
#[test]
fn snippets_take_their_language_from_the_file_name() {
let doc = json!({
"Print": { "prefix": "log", "body": "print($1)$0" },
"Scoped": { "prefix": "x", "body": "y", "scope": "rust" },
"Broken": { "prefix": "no body" }
});
let mut report = Report::default();
convert_snippets(&doc, Some("python"), &mut report);
assert_eq!(report.snippets["Print"]["scope"], json!("python"));
assert_eq!(
report.snippets["Scoped"]["scope"],
json!("rust"),
"an explicit scope wins over the file name"
);
assert!(!report.snippets.contains_key("Broken"));
assert_eq!(report.warnings.len(), 1);
}
#[test]
fn a_global_snippets_file_keeps_its_own_scope() {
let doc = json!({ "Any": { "prefix": "a", "body": "b" } });
let mut report = Report::default();
convert_snippets(&doc, None, &mut report);
assert!(report.snippets["Any"].get("scope").is_none());
}
#[test]
fn a_profile_directory_converts_end_to_end() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(
dir.path().join("settings.json"),
r#"{
// VS Code writes JSONC here
"editor.formatOnSave": true,
"editor.fontSize": 13,
}"#,
)
.unwrap();
std::fs::write(
dir.path().join("keybindings.json"),
r#"[{ "key": "ctrl+s", "command": "workbench.action.files.save" }]"#,
)
.unwrap();
std::fs::create_dir(dir.path().join("snippets")).unwrap();
std::fs::write(
dir.path().join("snippets").join("rust.json"),
r##"{ "Test": { "prefix": "tst", "body": "#[test]" } }"##,
)
.unwrap();
let report = scan_profile(dir.path()).expect("the profile scans");
assert_eq!(report.settings["format_on_save"], json!(true));
assert_eq!(
report.unmapped_settings,
vec![String::from("editor.fontSize")]
);
assert_eq!(
report.keybindings,
vec![(String::from("ctrl+s"), String::from("save_file"))]
);
assert_eq!(report.snippets["Test"]["scope"], json!("rust"));
}
fn fixture_extensions(root: &Path, label: &str) -> PathBuf {
let ext = root.join("extensions").join("acme.fixture-theme-1.2.0");
std::fs::create_dir_all(ext.join("themes")).unwrap();
std::fs::write(
ext.join("package.json"),
format!(
r##"{{ "name": "fixture-theme", "contributes": {{ "themes": [
{{ "label": "{label}", "uiTheme": "vs-dark", "path": "./themes/fixture.json" }}
] }} }}"##
),
)
.unwrap();
std::fs::write(
ext.join("themes").join("fixture.json"),
format!(
r##"{{ "name": "{label}", "type": "dark", "colors": {{ "editor.background": "#101820" }} }}"##
),
)
.unwrap();
root.join("extensions")
}
#[test]
fn an_installed_vscode_theme_is_converted_and_selected() {
let dir = tempfile::TempDir::new().unwrap();
let profile = dir.path().join("User");
std::fs::create_dir_all(&profile).unwrap();
std::fs::write(
profile.join("settings.json"),
r#"{ "workbench.colorTheme": "Fixture Dark", "editor.formatOnSave": true }"#,
)
.unwrap();
let extensions = fixture_extensions(dir.path(), "Fixture Dark");
let mut report =
scan_profile_with_extensions(&profile, &[extensions]).expect("the profile scans");
let theme = report.theme.as_ref().expect("the theme was resolved");
assert_eq!(theme.label, "Fixture Dark");
assert_eq!(theme.converted.id, "fixture-dark");
assert_eq!(
report.settings["theme"],
json!("fixture-dark"),
"croft's theme setting follows the converted id"
);
assert!(
!report
.unmapped_settings
.iter()
.any(|k| k == "workbench.colorTheme"),
"a resolved theme is mapped, not listed as missing: {:?}",
report.unmapped_settings
);
let croft = tempfile::TempDir::new().unwrap();
let croft_ext = croft.path().join("extensions");
let written = apply_into_dirs(croft.path(), &croft_ext, &mut report).expect("apply runs");
let manifest = croft_ext.join("theme-fixture-dark").join("extension.toml");
assert!(manifest.is_file(), "the converted theme was installed");
assert!(
written.contains(&manifest),
"and reported as written: {written:?}"
);
let config: Value = serde_json::from_str(
&std::fs::read_to_string(croft.path().join("config.json")).unwrap(),
)
.unwrap();
assert_eq!(config["theme"], json!("fixture-dark"));
}
#[test]
fn a_built_in_or_absent_theme_stays_unmapped_with_the_reason() {
let dir = tempfile::TempDir::new().unwrap();
let profile = dir.path().join("User");
std::fs::create_dir_all(&profile).unwrap();
std::fs::write(
profile.join("settings.json"),
r#"{ "workbench.colorTheme": "Dark+" }"#,
)
.unwrap();
let extensions = fixture_extensions(dir.path(), "Something Else");
let report =
scan_profile_with_extensions(&profile, &[extensions]).expect("the profile scans");
assert!(report.theme.is_none());
assert!(!report.settings.contains_key("theme"));
assert_eq!(
report.unmapped_settings,
vec![String::from("workbench.colorTheme")]
);
assert!(
report
.warnings
.iter()
.any(|w| w.contains("Dark+") && w.contains("theme-import")),
"the warning names the theme and the way to import it by hand: {:?}",
report.warnings
);
}
#[test]
fn a_theme_that_does_not_convert_is_a_warning_not_a_failure() {
let dir = tempfile::TempDir::new().unwrap();
let profile = dir.path().join("User");
std::fs::create_dir_all(&profile).unwrap();
std::fs::write(
profile.join("settings.json"),
r#"{ "workbench.colorTheme": "Broken", "editor.formatOnSave": true }"#,
)
.unwrap();
let extensions = fixture_extensions(dir.path(), "Broken");
std::fs::write(
extensions
.join("acme.fixture-theme-1.2.0")
.join("themes")
.join("fixture.json"),
"{ not json",
)
.unwrap();
let report =
scan_profile_with_extensions(&profile, &[extensions]).expect("the profile scans");
assert!(report.theme.is_none());
assert_eq!(report.settings["format_on_save"], json!(true));
assert!(
report
.warnings
.iter()
.any(|w| w.contains("could not be converted")),
"{:?}",
report.warnings
);
}
#[test]
fn an_existing_theme_setting_is_kept_and_the_conflict_says_how_to_switch() {
let dir = tempfile::TempDir::new().unwrap();
let profile = dir.path().join("User");
std::fs::create_dir_all(&profile).unwrap();
std::fs::write(
profile.join("settings.json"),
r#"{ "workbench.colorTheme": "Fixture Dark" }"#,
)
.unwrap();
let extensions = fixture_extensions(dir.path(), "Fixture Dark");
let mut report =
scan_profile_with_extensions(&profile, &[extensions]).expect("the profile scans");
let croft = tempfile::TempDir::new().unwrap();
std::fs::write(croft.path().join("config.json"), r#"{ "theme": "nord" }"#).unwrap();
let croft_ext = croft.path().join("extensions");
let written = apply_into_dirs(croft.path(), &croft_ext, &mut report).expect("apply runs");
assert!(
croft_ext
.join("theme-fixture-dark")
.join("extension.toml")
.is_file()
);
assert!(
!written.contains(&croft.path().join("config.json")),
"config.json is untouched"
);
let config: Value = serde_json::from_str(
&std::fs::read_to_string(croft.path().join("config.json")).unwrap(),
)
.unwrap();
assert_eq!(config["theme"], json!("nord"));
assert!(
report
.conflicts
.iter()
.any(|c| c.contains("installed and selectable") && c.contains("fixture-dark")),
"{:?}",
report.conflicts
);
}
#[test]
fn the_newest_installed_version_wins_and_an_escaping_path_is_skipped() {
let dir = tempfile::TempDir::new().unwrap();
let root = dir.path().join("extensions");
for (version, file) in [
("1.9.0", "old.json"),
("1.10.0", "new.json"),
("1.2.0", "older.json"),
] {
let ext = root.join(format!("acme.fixture-theme-{version}"));
std::fs::create_dir_all(ext.join("themes")).unwrap();
std::fs::write(
ext.join("package.json"),
format!(
r##"{{ "contributes": {{ "themes": [ {{ "label": "Fixture Dark", "uiTheme": "vs-dark", "path": "./themes/{file}" }} ] }} }}"##
),
)
.unwrap();
std::fs::write(
ext.join("themes").join(file),
r##"{ "name": "Fixture Dark", "type": "dark", "colors": {} }"##,
)
.unwrap();
}
let found = find_installed_themes("Fixture Dark", std::slice::from_ref(&root));
let names: Vec<String> = found
.iter()
.map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
.collect();
assert_eq!(names, ["new.json", "old.json", "older.json"]);
let escaping = root.join("evil.theme-1.0.0");
std::fs::create_dir_all(&escaping).unwrap();
std::fs::write(
escaping.join("package.json"),
r##"{ "contributes": { "themes": [ { "label": "Evil", "path": "../../../etc/passwd" }, { "label": "Evil", "path": "/etc/passwd" } ] } }"##,
)
.unwrap();
assert!(find_installed_themes("Evil", &[root]).is_empty());
}
#[test]
fn extension_dirs_follow_the_product_of_the_user_directory() {
let home = Path::new("/home/t");
for (user_dir, expected) in [
("/home/t/.config/Code/User", ".vscode/extensions"),
(
"/home/t/.config/Code - Insiders/User",
".vscode-insiders/extensions",
),
("/home/t/.config/VSCodium/User", ".vscode-oss/extensions"),
("/home/t/.config/Cursor/User", ".cursor/extensions"),
("/home/t/.config/Windsurf/User", ".windsurf/extensions"),
] {
assert_eq!(
theme_extension_dirs_under(home, Path::new(user_dir)),
vec![home.join(expected)],
"{user_dir}"
);
}
let all = theme_extension_dirs_under(home, Path::new("/tmp/somewhere"));
assert_eq!(all[0], home.join(".vscode/extensions"));
assert_eq!(all.len(), 5);
}
#[test]
fn an_empty_profile_directory_is_not_a_failure() {
let dir = tempfile::TempDir::new().unwrap();
let report = scan_profile(dir.path()).expect("an empty profile is fine");
assert!(report.is_empty());
}
}