use super::*;
pub(super) fn import_vscode(from: Option<PathBuf>, dry_run: bool) -> Result<()> {
let (dir, reading) = match from {
Some(dir) => {
if !dir.is_dir() {
anyhow::bail!(
"{} is not a directory. --from wants a VS Code user directory \
(the one holding settings.json), e.g. ~/.config/Code/User",
dir.display()
);
}
let user = crate::import_vscode::user_dir_in(&dir);
let reading = (user != dir).then(|| format!("Reading {}", user.display()));
(user, reading)
}
None => {
let found = crate::import_vscode::discover_profiles();
let Some((label, dir)) = found.into_iter().next() else {
anyhow::bail!(
"no VS Code user directory found. Pass one with --from \
(e.g. --from ~/.config/Code/User)"
);
};
let reading = format!("Reading {label}: {}", dir.display());
(dir, Some(reading))
}
};
let mut report = crate::import_vscode::scan_profile(&dir)?;
let applied = (!dry_run && !report.is_empty())
.then(|| crate::import_vscode::apply(&mut report))
.transpose()?;
if let Some(reading) = reading {
println!("{reading}");
}
println!(
"\n{} setting{} mapped, {} keybinding{} mapped, {} snippet{}",
report.settings.len(),
plural(report.settings.len()),
report.keybindings.len(),
plural(report.keybindings.len()),
report.snippets.len(),
plural(report.snippets.len()),
);
for (key, value) in &report.settings {
println!(" {key} = {value}");
}
for (key, command, when) in &report.keybindings {
match when {
Some(when) => println!(" {key} -> {command} (when {when})"),
None => println!(" {key} -> {command}"),
}
}
if let Some(theme) = &report.theme {
println!(
"\nworkbench.colorTheme {:?} converts to croft theme {:?} (from {})",
theme.label,
theme.converted.id,
theme.path.display()
);
for note in &theme.converted.notes {
println!(" - {note}");
}
}
if !report.unmapped_settings.is_empty() {
println!(
"\n{} setting{} croft has no equivalent for:",
report.unmapped_settings.len(),
plural(report.unmapped_settings.len())
);
for key in &report.unmapped_settings {
println!(" {key}");
}
}
if !report.dropped_keybindings.is_empty() {
println!(
"\n{} keybinding{} not carried over:",
report.dropped_keybindings.len(),
plural(report.dropped_keybindings.len())
);
for row in &report.dropped_keybindings {
println!(" {row}");
}
}
for warning in &report.warnings {
println!(" warning: {warning}");
}
let mut ids = crate::vscode_extensions::installed_via_code();
let listed = !ids.is_empty();
if let Ok(cwd) = std::env::current_dir() {
ids.extend(crate::vscode_extensions::workspace_recommendations(&cwd));
}
let compared = crate::vscode_extensions::compare(ids);
if !compared.is_empty() {
println!(
"\n{} VS Code extension{} ({}):",
compared.len(),
plural(compared.len()),
if listed {
"installed, plus this workspace's recommendations"
} else {
"this workspace's recommendations; `code` is not on PATH"
}
);
for row in &compared {
use crate::vscode_extensions::Status;
let what = match row.mapping {
Some(m) if m.status == Status::Builtin => {
format!("built in ({}): {}", m.croft, m.note)
}
Some(m) if m.status == Status::Installable => {
format!("installable ({}): {}", m.croft, m.note)
}
Some(m) => format!("no equivalent: {}", m.note),
None => String::from("no equivalent: not in croft's table"),
};
println!(" {} -> {what}", row.id);
}
println!(" Open the Extensions panel (Cmd+Shift+X) to add an installable one.");
}
let Some(written) = applied else {
if dry_run {
println!("\nDry run: nothing was written.");
} else {
println!("\nNothing to import.");
}
return Ok(());
};
if !report.conflicts.is_empty() {
println!(
"\n{} value{} croft already had, left alone:",
report.conflicts.len(),
plural(report.conflicts.len())
);
for conflict in &report.conflicts {
println!(" {conflict}");
}
}
if !report.refusals.is_empty() {
println!(
"\n{} file{} croft declined to write:",
report.refusals.len(),
plural(report.refusals.len())
);
for refusal in &report.refusals {
println!(" {refusal}");
}
}
if written.is_empty() {
if report.refusals.is_empty() {
println!("\nEverything was already in place; nothing changed.");
} else {
println!("\nNothing was written: see the refusals above.");
}
} else {
println!("\nWrote:");
for path in &written {
println!(" {}", path.display());
}
println!("\nRestart croft to pick the new configuration up.");
}
Ok(())
}
pub(super) fn plural(n: usize) -> &'static str {
if n == 1 { "" } else { "s" }
}