Skip to content

Commit 0c83e39

Browse files
committed
Improve file creation handling to automatically check new files in checked directories and optimize updates
1 parent d8b862f commit 0c83e39

1 file changed

Lines changed: 70 additions & 15 deletions

File tree

packages/vscode/src/context/providers/workspace-provider.ts

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -500,27 +500,82 @@ export class WorkspaceProvider
500500
// Update the file to workspace mapping
501501
this.update_file_workspace_mapping()
502502

503-
// Refresh the tree view
503+
// Refresh the tree view first to update its structure
504504
await this.refresh()
505505

506-
// If a new file is created within a checked directory, check it automatically
507-
for (const [dir_path] of this.checked_items) {
508-
if (
509-
this.checked_items.get(dir_path) == vscode.TreeItemCheckboxState.Checked
510-
) {
511-
const workspace_root = this.get_workspace_root_for_file(dir_path)
512-
if (!workspace_root) continue
506+
let internal_check_state_changed = false
507+
// Take a snapshot of the checked items before potential modifications
508+
const initial_checked_items_state = new Map(this.checked_items.entries())
509+
510+
// If a new file is created within a checked directory, it should also be checked (if not excluded).
511+
// Iterate over a snapshot of items that were checked BEFORE this method's logic began fully.
512+
for (const [item_path_key, item_state_val] of initial_checked_items_state) {
513+
if (item_state_val === vscode.TreeItemCheckboxState.Checked) {
514+
// We are interested if item_path_key is a directory that could contain created_file_path,
515+
// or if created_file_path is null (less specific), we re-evaluate all checked directories.
516+
let needs_re_evaluation = false
517+
try {
518+
if (
519+
fs.existsSync(item_path_key) &&
520+
fs.lstatSync(item_path_key).isDirectory()
521+
) {
522+
if (created_file_path) {
523+
// Only process if item_path_key is a parent (or ancestor) of created_file_path
524+
if (created_file_path.startsWith(item_path_key + path.sep)) {
525+
needs_re_evaluation = true
526+
}
527+
} else {
528+
needs_re_evaluation = true // No specific file, re-evaluate all checked dirs
529+
}
530+
}
531+
} catch (e) {
532+
/* item_path_key might not be a dir or might be gone */
533+
}
513534

514-
const relative_path = path.relative(workspace_root, dir_path)
515-
const is_excluded = this.is_excluded(relative_path)
535+
if (needs_re_evaluation) {
536+
const workspace_root = this.get_workspace_root_for_file(item_path_key)
537+
if (!workspace_root) continue
516538

517-
await this.update_directory_check_state(
518-
dir_path,
519-
vscode.TreeItemCheckboxState.Checked,
520-
is_excluded
521-
)
539+
const relative_path = path.relative(workspace_root, item_path_key)
540+
const is_excluded = this.is_excluded(relative_path)
541+
542+
// This call directly modifies `this.checked_items` for children of item_path_key.
543+
await this.update_directory_check_state(
544+
item_path_key,
545+
vscode.TreeItemCheckboxState.Checked,
546+
is_excluded
547+
)
548+
}
522549
}
523550
}
551+
552+
// After potential modifications, check if the actual state of `this.checked_items` has changed.
553+
if (this.checked_items.size !== initial_checked_items_state.size) {
554+
internal_check_state_changed = true
555+
} else {
556+
for (const [key, value] of this.checked_items) {
557+
if (initial_checked_items_state.get(key) !== value) {
558+
internal_check_state_changed = true
559+
break
560+
}
561+
}
562+
// Also check if any keys were removed from initial_checked_items_state but not present in this.checked_items
563+
if (!internal_check_state_changed) {
564+
for (const key of initial_checked_items_state.keys()) {
565+
if (!this.checked_items.has(key)) {
566+
internal_check_state_changed = true
567+
break
568+
}
569+
}
570+
}
571+
}
572+
573+
if (internal_check_state_changed) {
574+
this._on_did_change_checked_files.fire()
575+
// Refresh again if this provider's UI also needs to reflect the new check states.
576+
// This ensures consistency within this provider's view.
577+
await this.refresh()
578+
}
524579
}
525580

526581
async refresh(): Promise<void> {

0 commit comments

Comments
 (0)