Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(explorer): draw the tree again when files change on disk

rokartur0608e22ed54a
diff --git a/native/crates/app/src/workbench/code_lens/tests.rs b/native/crates/app/src/workbench/code_lens/tests.rs
index 62372b9e929..4f2dd83d983 100644
--- a/native/crates/app/src/workbench/code_lens/tests.rs
+++ b/native/crates/app/src/workbench/code_lens/tests.rs
@@ -17,7 +17,8 @@ fn open(cx: &mut TestAppContext) -> (gpui::Entity<Workbench>, &mut VisualTestCon
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
- let dir = std::env::temp_dir().join(format!("code-gpui-code-lens-{}", std::process::id()));
+ // A folder per test: one test's b.ts would show in the explorer of another running at once.
+ let dir = std::env::temp_dir().join(format!("code-gpui-code-lens-{}-{:?}", std::process::id(), std::thread::current().id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("a.ts"), "function a() {}\n function b() {}\nc\n").unwrap();
let workspace = config::workspace::Workspace::folder(dir.clone());
diff --git a/native/crates/app/src/workbench/explorer_actions/tests.rs b/native/crates/app/src/workbench/explorer_actions/tests.rs
index 1aa8a63bd14..8a222ecc1dd 100644
--- a/native/crates/app/src/workbench/explorer_actions/tests.rs
+++ b/native/crates/app/src/workbench/explorer_actions/tests.rs
@@ -550,6 +550,18 @@ fn closing_the_window_stores_the_tree_view_state_for_the_next_open(cx: &mut Test
std::fs::remove_dir_all(&dir).unwrap();
}
+/// explorerService.ts `onDidChangeFiles`: a file added on disk to a shown folder draws in the cached side bar.
+#[gpui::test]
+fn a_file_added_on_disk_draws_in_the_tree(cx: &mut TestAppContext) {
+ let (dir, workbench, cx) = explorer("explorer-added-on-disk", cx);
+ std::fs::write(dir.join("b.txt"), "b").unwrap();
+ let added = vec![files::watcher::Change { path: dir.join("b.txt"), kind: files::watcher::ChangeType::Added }];
+ workbench.update(cx, |workbench, cx| workbench.files_changed(added, cx));
+ cx.run_until_parked();
+ assert_eq!(cx.update(|window, cx| window.retained_frame_difference(cx)), None);
+ std::fs::remove_dir_all(&dir).unwrap();
+}
+
/// explorerView.ts: the Explorer shown at startup runs `selectActiveFile(true)`, so a restored editor's file is
/// selected and its folders expand.
#[gpui::test]
diff --git a/native/crates/app/src/workbench/local_history_actions/tests.rs b/native/crates/app/src/workbench/local_history_actions/tests.rs
index 6b2986d7996..8aebf77ed19 100644
--- a/native/crates/app/src/workbench/local_history_actions/tests.rs
+++ b/native/crates/app/src/workbench/local_history_actions/tests.rs
@@ -23,9 +23,11 @@ fn open<'a>(cx: &'a mut TestAppContext, name: &str) -> (gpui::Entity<Workbench>,
let dir = std::env::temp_dir().join(format!("code-gpui-local-history-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
- let file = dir.join("notes.txt");
+ // The user data beside the folder, as in the app: history written into it would show in the explorer undrawn.
+ std::fs::create_dir_all(dir.join("folder")).unwrap();
+ let file = dir.join("folder/notes.txt");
std::fs::write(&file, "saved").unwrap();
- let workspace = config::workspace::Workspace::folder(dir.clone());
+ let workspace = config::workspace::Workspace::folder(dir.join("folder"));
let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), Some(workspace.clone()))));
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx);
@@ -45,7 +47,11 @@ fn run(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, command:
}
fn history_home(file: &Path) -> PathBuf {
- file.parent().unwrap().join("User/History")
+ test_dir(file).join("User/History")
+}
+
+fn test_dir(file: &Path) -> &Path {
+ file.parent().unwrap().parent().unwrap()
}
/// Creates one entry named `source` through Create Entry and returns the file's entries.
@@ -81,7 +87,7 @@ fn find_entry_to_restore_compares_the_entry_with_the_file(cx: &mut TestAppContex
assert_eq!(original.document().name(), "notes.txt");
assert_eq!(workbench.readonly(LOCAL_HISTORY_SCHEME, &diff.original.path), Some(Readonly::Provider));
});
- std::fs::remove_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::remove_dir_all(test_dir(&file)).unwrap();
}
#[gpui::test]
@@ -116,7 +122,7 @@ fn the_timeline_lists_the_entries_and_their_commands_run(cx: &mut TestAppContext
let folder = local_history::entries_folder(&history_home(&file), &uri_string("file", &file));
let sources = || local_history::read_entries(&folder, &file).unwrap().into_iter().map(|entry| entry.source.unwrap_or_default()).collect::<Vec<_>>();
assert_eq!(sources(), ["First", "Renamed"]);
- std::fs::remove_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::remove_dir_all(test_dir(&file)).unwrap();
}
/// localHistoryTimeline.ts `provideTimeline`: a resource of an extension's file system provider shows the entries of
@@ -132,7 +138,7 @@ fn a_file_system_provider_resource_shows_the_entries_of_its_file(cx: &mut TestAp
let request = exthost::session::ForwardedRequest { req: 1, proxy: "workbench", method: "fileSystemProviderChanged".into(), args: vec![serde_json::json!("memfs"), serde_json::json!(true)] };
workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_host_request(request, window, cx)));
assert_eq!(workbench.read_with(cx, |workbench, _| labels(workbench)), ["First"]);
- std::fs::remove_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::remove_dir_all(test_dir(&file)).unwrap();
}
/// workingCopyHistoryTracker.ts `resolveSourceFromUndoRedo`: a save after typing is "File Saved" (two merge into one
@@ -150,7 +156,7 @@ fn a_save_after_an_undo_is_an_undo_redo_entry(cx: &mut TestAppContext) {
let folder = local_history::entries_folder(&history_home(&file), &uri_string("file", &file));
let sources = local_history::read_entries(&folder, &file).unwrap().into_iter().map(|entry| entry.source).collect::<Vec<_>>();
assert_eq!(sources, [None, Some("undoRedo.source".to_owned()), None]);
- std::fs::remove_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::remove_dir_all(test_dir(&file)).unwrap();
}
/// workingCopyHistoryTracker.ts `resolveSourceFromUndoRedo`: a save after a labelled bulk edit (rename.ts
@@ -167,5 +173,5 @@ fn a_save_after_a_workspace_edit_is_named_by_its_label(cx: &mut TestAppContext)
let folder = local_history::entries_folder(&history_home(&file), &uri_string("file", &file));
let sources = local_history::read_entries(&folder, &file).unwrap().into_iter().map(|entry| entry.source).collect::<Vec<_>>();
assert_eq!(sources, [Some("Renaming saved to kept".to_owned()), Some("Workspace Edit".to_owned())]);
- std::fs::remove_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::remove_dir_all(test_dir(&file)).unwrap();
}
diff --git a/native/crates/app/src/workbench/text_files.rs b/native/crates/app/src/workbench/text_files.rs
index 892c17ffcf3..ce45d50f017 100644
--- a/native/crates/app/src/workbench/text_files.rs
+++ b/native/crates/app/src/workbench/text_files.rs
@@ -799,6 +799,14 @@ impl Workbench {
/// `workbench.editor.closeOnFileDelete`; a file added back ends the orphan state.
pub(super) fn files_changed(&mut self, changes: Vec<Change>, cx: &mut Context<Self>) {
self.search_editor_files_changed(&changes, cx);
+ // explorerService.ts `onDidChangeFiles`: the tree reads the disk as it draws, so a file added to or deleted
+ // from a folder it shows draws it again.
+ if let Some(tree) = self.folder.as_ref() {
+ let shown = |folder: &Path| tree.is_expanded(folder) || tree.workspace.folders.iter().any(|root| root.path == folder);
+ if changes.iter().any(|change| change.kind != ChangeType::Updated && change.path.parent().is_some_and(shown)) {
+ cx.notify();
+ }
+ }
// FileUserDataProvider.handleFileChanges: a disk change reaches the `vscode-userdata` model of the path too.
let resources = changes.into_iter().flat_map(|change| ["file", files::user_data::SCHEME].map(|scheme| (scheme, change.clone())));
for (scheme, change) in resources {