//! LightEdit: a window editing loose files without a project. mod common; use ide_core::{ChangeSet, TextChange}; use ide_host::{Host, catalogue::TrustClass, ops}; use std::sync::Arc; use std::time::Duration; fn host(base: &std::path::Path) -> Arc { Arc::new(Host::new(common::host_config(base))) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn edits_loose_files_without_project_services() { let tmp = tempfile::tempdir().unwrap(); let tmp_root = dunce::canonicalize(tmp.path()).unwrap(); let (a_dir, b_dir) = (tmp_root.join("a"), tmp_root.join("b")); std::fs::create_dir_all(a_dir.join(".git")).unwrap(); std::fs::create_dir_all(&b_dir).unwrap(); std::fs::write(a_dir.join("one.py"), "print(1)\n").unwrap(); std::fs::write(b_dir.join("two.rs"), "fn main() {}\n").unwrap(); let host = host(&tmp_root.join("app")); let light = host.open_light("main", &a_dir.join("one.py")).unwrap(); assert!(light.light); assert_eq!(light.root, a_dir); assert!(host.open_light("main", &b_dir).is_err(), "a folder is not opened for LightEdit"); let doc = ops::doc::doc_open(&host, "main", a_dir.join("one.py")).await.unwrap(); assert!(doc.text.starts_with("print")); let changes = ChangeSet { changes: vec![TextChange { from: 0, to: 0, insert: "#\n".into() }] }; let version = ops::doc::doc_edit(&host, "main", doc.id, doc.version, changes).await.unwrap(); assert!(version > doc.version); ops::doc::doc_save(&host, "main", doc.id).await.unwrap(); assert_eq!(std::fs::read_to_string(a_dir.join("one.py")).unwrap(), "#\nprint(1)\n"); // More files join the same session, wherever they are. let again = host.open_light("main", &b_dir.join("two.rs")).unwrap(); assert!(Arc::ptr_eq(&light, &again)); ops::doc::doc_open(&host, "main", b_dir.join("two.rs")).await.unwrap(); assert!(light.index.wait_idle(Duration::from_secs(10))); assert_eq!(light.index.status().files, 0, "nothing is indexed"); assert!(light.lsp.servers().is_empty(), "no language servers"); assert!(light.repos().is_empty(), "no repositories"); // Opening the folder as a project replaces the LightEdit session. let project = host.open_project("main", &a_dir).unwrap(); assert!(!project.light); assert!(!Arc::ptr_eq(&project, &light)); } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn light_windows_ignore_project_scoped_settings() { let tmp = tempfile::tempdir().unwrap(); let dir = dunce::canonicalize(tmp.path()).unwrap().join("loose"); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("notes.txt"), "x\n").unwrap(); let host = host(&tmp.path().join("app")); host.open_light("main", &dir.join("notes.txt")).unwrap(); let applied = ops::project::set_settings( &host, "main", ide_workspace::settings::SettingsScope::Project, "general.projectName", "\"loose\"", ) .unwrap(); assert!(!applied.project_scope); assert!(!dir.join(".openidea").exists(), "no project settings next to the file"); } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn light_windows_stay_restricted_when_folder_trust_changes() { let tmp = tempfile::tempdir().unwrap(); let root = tmp.path().join("loose"); let other = tmp.path().join("other"); std::fs::create_dir_all(&root).unwrap(); std::fs::create_dir_all(&other).unwrap(); std::fs::write(root.join("one.py"), "print(1)\n").unwrap(); std::fs::write(other.join("two.js"), "const value = 1;\n").unwrap(); let host = host(&tmp.path().join("app")); host.global_settings .set(ide_workspace::SettingsScope::Global, "languages.prettier.prefer", serde_json::json!(true)) .unwrap(); let light = host.open_light("main", &root.join("one.py")).unwrap(); assert!(!light.is_trusted()); assert!(light.require_trust().is_err()); host.set_project_trust(&root, true, false).await.unwrap(); assert!(!light.is_trusted()); let trusted_folder = host.open_light("second", &root.join("one.py")).unwrap(); assert!(!trusted_folder.is_trusted()); let again = host.open_light("main", &other.join("two.js")).unwrap(); assert!(Arc::ptr_eq(&light, &again)); assert!(!again.is_trusted()); host.set_project_trust(&root, true, true).await.unwrap(); assert!(!light.is_trusted()); assert!(!trusted_folder.is_trusted()); for class in [TrustClass::OutsideRestricted, TrustClass::Trusted] { assert!(host.authorize("main", class).is_err(), "{class:?}"); } assert!(host.authorize("main", TrustClass::Always).is_ok()); let doc = ops::doc::doc_open(&host, "main", other.join("two.js")).await.unwrap(); let error = ops::lsp::lsp_format( &host, "main", doc.id, None, ide_lsp::types::FormatOptions { tab_size: 2, insert_spaces: true, on_save: false, tool: None }, ) .await .unwrap_err(); assert_eq!(error.kind, ide_core::error::ErrorKind::Unavailable); assert!(error.message.contains("Restricted Mode")); host.set_project_trust(&root, false, false).await.unwrap(); assert!(!light.is_trusted()); assert!(!trusted_folder.is_trusted()); host.set_project_trust(&root, true, false).await.unwrap(); let project = host.open_project("main", &root).unwrap(); assert!(!project.light); assert!(project.is_trusted()); assert!(project.require_trust().is_ok()); host.shutdown().await; } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn light_notebooks_open_without_interpreter_detection_or_execution() { let tmp = tempfile::tempdir().unwrap(); let root = tmp.path().join("loose"); let bin = root.join(".venv").join(if cfg!(windows) { "Scripts" } else { "bin" }); std::fs::create_dir_all(&bin).unwrap(); std::fs::write(root.join(".venv/pyvenv.cfg"), "home = python\n").unwrap(); #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; let python = bin.join("python"); std::fs::write(&python, "#!/bin/sh\nprintf probed > \"$0.probed\"\nprintf 'Python 3.12.0\\n'\n").unwrap(); std::fs::set_permissions(&python, std::fs::Permissions::from_mode(0o755)).unwrap(); } let file = root.join("analysis.ipynb"); std::fs::write( &file, r#"{"cells":[{"id":"cell","cell_type":"code","source":["print(1)"],"metadata":{},"outputs":[],"execution_count":null}],"metadata":{},"nbformat":4,"nbformat_minor":5}"#, ) .unwrap(); let host = host(&tmp.path().join("app")); host.open_light("main", &file).unwrap(); let notebook = ops::notebook::notebook_open(&host, "main", file.to_string_lossy().into()).await.unwrap(); assert!(ops::notebook::notebook_save(&host, "main", notebook.id.clone()).await.is_ok()); let error = ops::sdk::sdk_detect(&host, "main", ide_sdk::SdkKind::Python).await.unwrap_err(); assert!(error.message.contains("Restricted Mode")); assert!(ops::sdk::sdk_python_effective(&host, "main", None).await.is_err()); assert!(ops::sdk::sdk_locate_tools(&host, "main", vec!["prettier".into()], Some(root)).await.is_err()); let error = ops::notebook::notebook_restart(&host, "main", notebook.id.clone()).await.unwrap_err(); assert!(error.message.contains("Restricted Mode")); let error = ops::notebook::notebook_execute( &host, "main", notebook.id, vec![notebook.cells[0].id.clone()], None, ide_core::RequestId(849), ops::Channel::new(|_| {}), ) .await .unwrap_err(); assert!(error.message.contains("Restricted Mode")); assert!(!bin.join("python.probed").exists()); host.shutdown().await; } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn untitled_buffers_reach_the_disk_through_save_as() { let tmp = tempfile::tempdir().unwrap(); let tmp_root = dunce::canonicalize(tmp.path()).unwrap(); let dir = tmp_root.join("notes"); let host = host(&tmp_root.join("app")); // A window with no file (a welcome screen's File › New File): rooted at the // home folder, no watcher yet, doc commands work. let empty = host.open_light_empty("main").unwrap(); assert!(empty.light); let untitled = ops::doc::doc_open_untitled(&host, "main", None).await.unwrap(); assert_eq!(untitled.path, std::path::PathBuf::from("untitled:Untitled-1")); assert_eq!(untitled.language.as_str(), "plaintext"); let version = ops::doc::doc_edit(&host, "main", untitled.id, untitled.version, untitled_edit("# notes\n")).await.unwrap(); assert!(ops::doc::doc_save(&host, "main", untitled.id).await.is_ok(), "saving nowhere is a no-op"); assert!(host.project("main").unwrap().documents.is_dirty(untitled.id).unwrap()); // Save As: the buffer lands on the disk and turns into a file document. let target = dir.join("deep/notes.md"); let saved = ops::doc::doc_save_as(&host, "main", untitled.id, target.clone()).await.unwrap(); assert_eq!(saved.path, target); assert_eq!(saved.language.as_str(), "markdown"); assert!(!saved.dirty); // The platform's default line separator applies to new buffers. assert_eq!(std::fs::read_to_string(&target).unwrap().replace("\r\n", "\n"), "# notes\n"); ops::doc::doc_edit(&host, "main", untitled.id, version, untitled_edit("x\n")).await.unwrap(); ops::doc::doc_save(&host, "main", untitled.id).await.unwrap(); assert_eq!(std::fs::read_to_string(&target).unwrap().replace("\r\n", "\n"), "x\n# notes\n"); // The saved file's folder is followed for external changes now. host.shutdown().await; } fn untitled_edit(insert: &str) -> ChangeSet { ChangeSet { changes: vec![TextChange { from: 0, to: 0, insert: insert.into() }] } } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn untitled_buffers_follow_the_window_into_a_project() { let tmp = tempfile::tempdir().unwrap(); let tmp_root = dunce::canonicalize(tmp.path()).unwrap(); let host = host(&tmp_root.join("app")); // A LightEdit window with an edited untitled buffer (and a clean one). host.open_light_empty("main").unwrap(); let draft = ops::doc::doc_open_untitled(&host, "main", None).await.unwrap(); ops::doc::doc_edit(&host, "main", draft.id, draft.version, untitled_edit("{\"draft\": true}")).await.unwrap(); let clean = ops::doc::doc_open_untitled(&host, "main", None).await.unwrap(); // The window switches to a project: the buffers follow, contents and dirty flags kept. let project_root = tmp_root.join("proj"); std::fs::create_dir_all(&project_root).unwrap(); let project = host.open_project("main", &project_root).unwrap(); assert!(!project.light); let carried = ops::doc::doc_open_untitled(&host, "main", Some(draft.path.to_string_lossy().into_owned())).await.unwrap(); assert_eq!(carried.path, draft.path, "same key, fresh registry: a new doc id carries the content"); let snap = ops::doc::doc_snapshot(&host, "main", carried.id).await.unwrap(); assert_eq!(snap.text, "{\"draft\": true}"); assert!(host.project("main").unwrap().documents.is_dirty(carried.id).unwrap()); assert_eq!(snap.language.as_str(), "plaintext"); let clean_carried = ops::doc::doc_open_untitled(&host, "main", Some(clean.path.to_string_lossy().into_owned())).await.unwrap(); assert!(!host.project("main").unwrap().documents.is_dirty(clean_carried.id).unwrap()); host.shutdown().await; }