diff --git a/Cargo.lock b/Cargo.lock index 8b19cd0c07..0e01cf43a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5116,6 +5116,7 @@ dependencies = [ "ide-text", "ide-vfs", "parking_lot 0.12.5", + "quick-xml 0.42.0", "semver", "serde", "serde_json", diff --git a/crates/ide-host/src/menu.rs b/crates/ide-host/src/menu.rs index 65eeca5483..d80bb212cd 100644 --- a/crates/ide-host/src/menu.rs +++ b/crates/ide-host/src/menu.rs @@ -277,6 +277,7 @@ pub fn spec(os: Os) -> Vec { act("file.exportSettings", "Export Settings…"), act("file.importSettings", "Import Settings…"), act("file.importJetBrainsSettings", "Import Settings from JetBrains IDE…"), + act("file.importJetBrainsRecentProjects", "Import Recent Projects from JetBrains IDE…"), act("file.syncSettings", "Sync Settings Now"), ], ), diff --git a/crates/ide-workspace/Cargo.toml b/crates/ide-workspace/Cargo.toml index f3e8d275e5..f4d324b079 100644 --- a/crates/ide-workspace/Cargo.toml +++ b/crates/ide-workspace/Cargo.toml @@ -24,6 +24,7 @@ globset.workspace = true uuid.workspace = true zip.workspace = true encoding_rs.workspace = true +quick-xml = "0.42" [target.'cfg(windows)'.dependencies] winreg = "0.56" diff --git a/crates/ide-workspace/src/recent.rs b/crates/ide-workspace/src/recent.rs index 7db02c3cd7..5a027f892b 100644 --- a/crates/ide-workspace/src/recent.rs +++ b/crates/ide-workspace/src/recent.rs @@ -162,6 +162,127 @@ fn is_missing(path: &Path) -> bool { path.is_absolute() && !text.contains("://") && !wsl && !path.exists() } +fn folder_name(path: &Path) -> String { + path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| path.display().to_string()) +} + +/// What importing a JetBrains IDE's recent projects did (or, as a preview, would do). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct JetBrainsRecentImport { + /// Projects added to the recent projects. + pub imported: u32, + /// Projects left out because their folder no longer exists. + pub missing: u32, + /// Project groups added. + pub groups: u32, +} + +/// A recent project as a JetBrains IDE keeps it in `options/recentProjects.xml`. +#[derive(Debug, Default, PartialEq)] +struct JetBrainsRecent { + path: String, + name: Option, + opened_at_ms: f64, + /// Opened but not listed (LightEdit, a file opened alone): the IDE does not show it. + hidden: bool, +} + +#[derive(Debug, Default, PartialEq)] +struct JetBrainsGroup { + name: String, + paths: Vec, + expanded: bool, +} + +/// The `RecentProjectsManager` state: `additionalInfo` entries (path → `RecentProjectMetaInfo`) and +/// `groups` (`ProjectGroup` with `name`, `expanded` and its `projects`). +fn parse_jetbrains_recent(xml: &str) -> (Vec, Vec) { + use quick_xml::{Reader, XmlVersion, events::Event}; + let mut reader = Reader::from_str(xml); + let (mut projects, mut groups) = (Vec::new(), Vec::new()); + let mut project: Option = None; + let mut group: Option = None; + loop { + let (tag, empty) = match reader.read_event() { + Ok(Event::Start(tag)) => (tag, false), + Ok(Event::Empty(tag)) => (tag, true), + Ok(Event::End(tag)) => { + match tag.local_name().as_ref() { + "entry" => projects.extend(project.take()), + "ProjectGroup" => groups.extend(group.take()), + _ => {} + } + continue; + } + Ok(Event::Eof) | Err(_) => break, + _ => continue, + }; + let attr = |key: &str| { + tag.attributes() + .flatten() + .find(|a| a.key.as_ref() == key) + .and_then(|a| a.normalized_value(XmlVersion::Implicit1_0).ok().map(|v| v.into_owned())) + }; + match tag.local_name().as_ref() { + "entry" if !empty && group.is_none() => { + project = attr("key").map(|path| JetBrainsRecent { path, ..Default::default() }); + } + "RecentProjectMetaInfo" => { + if let Some(p) = &mut project { + p.hidden = attr("hidden").as_deref() == Some("true"); + } + } + "ProjectGroup" if !empty => group = Some(JetBrainsGroup::default()), + "option" => { + let (name, value) = (attr("name"), attr("value")); + if let Some(g) = &mut group { + match (name.as_deref(), value) { + (Some("name"), Some(v)) => g.name = v, + (Some("expanded"), Some(v)) => g.expanded = v == "true", + // `