Skip to content

Commit 6a2af93

Browse files
committed
Hide checkboxes for empty directories by tracking directory file counts
- empty folders should not be selectable - is this situation, parent should be selected └── apps └── editor └── src └── context ├── context-initialization.ts ├── helpers │ ├── get-all-workspace-files.ts │ └── get-selected-files.ts ├── providers │ ├── open-editors │ │ └── open-editors-provider.ts │ ├── selected-files │ │ └── selected-files-provider.ts │ └── workspace │ ├── modules │ │ ├── index.ts │ │ └── token-calculator.ts │ └── workspace-provider.ts ├── shared-context-state.ts └── utils └── shrink-file ├── index.ts ├── languages │ ├── c-style.ts │ ├── css.ts │ ├── html.ts │ ├── jsx.ts │ ├── python.ts │ ├── ruby.ts │ └── sql.ts ├── shrink-file.spec.ts ├── shrink-file.ts └── test-cases ├── c-style │ ├── input.txt │ └── output.txt ├── css │ ├── input.txt │ └── output.txt ├── html │ ├── input.txt │ └── output.txt ├── jsx │ ├── input.txt │ └── output.txt ├── python │ ├── input.txt │ └── output.txt ├── ruby │ ├── input.txt │ └── output.txt ├── scss │ ├── input.txt │ └── output.txt └── sql ├── input.txt └── output.txt
1 parent bafe235 commit 6a2af93

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎apps/editor/src/context/providers/workspace/modules/token-calculator.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class TokenCalculator implements vscode.Disposable {
3030
private _file_shrink_token_counts: Map<string, number> = new Map()
3131
private _directory_token_counts: Map<string, number> = new Map()
3232
private _directory_shrink_token_counts: Map<string, number> = new Map()
33+
private _directory_file_counts: Map<string, number> = new Map()
3334
private _directory_selected_token_counts: Map<string, number> = new Map()
3435
private _directory_selected_shrink_token_counts: Map<string, number> =
3536
new Map()
@@ -358,6 +359,7 @@ export class TokenCalculator implements vscode.Disposable {
358359
while (dir_path.startsWith(workspace_root)) {
359360
this._directory_token_counts.delete(dir_path)
360361
this._directory_shrink_token_counts.delete(dir_path)
362+
this._directory_file_counts.delete(dir_path)
361363
this._directory_selected_token_counts.delete(dir_path)
362364
this._directory_selected_shrink_token_counts.delete(dir_path)
363365
dir_path = path.dirname(dir_path)
@@ -367,6 +369,7 @@ export class TokenCalculator implements vscode.Disposable {
367369
public invalidate_directory_counts(dir_path: string) {
368370
this._directory_token_counts.delete(dir_path)
369371
this._directory_shrink_token_counts.delete(dir_path)
372+
this._directory_file_counts.delete(dir_path)
370373
this._directory_selected_token_counts.delete(dir_path)
371374
this._directory_selected_shrink_token_counts.delete(dir_path)
372375
}
@@ -381,6 +384,7 @@ export class TokenCalculator implements vscode.Disposable {
381384
this._file_shrink_token_counts.clear()
382385
this._directory_token_counts.clear()
383386
this._directory_shrink_token_counts.clear()
387+
this._directory_file_counts.clear()
384388
this._directory_selected_token_counts.clear()
385389
this._directory_selected_shrink_token_counts.clear()
386390
}
@@ -540,22 +544,24 @@ export class TokenCalculator implements vscode.Disposable {
540544

541545
public async calculate_directory_tokens(
542546
dir_path: string
543-
): Promise<{ total: number; shrink: number }> {
547+
): Promise<{ total: number; shrink: number; file_count: number }> {
544548
if (
545549
this._directory_token_counts.has(dir_path) &&
546-
this._directory_shrink_token_counts.has(dir_path)
550+
this._directory_shrink_token_counts.has(dir_path) &&
551+
this._directory_file_counts.has(dir_path)
547552
) {
548553
return {
549554
total: this._directory_token_counts.get(dir_path)!,
550-
shrink: this._directory_shrink_token_counts.get(dir_path)!
555+
shrink: this._directory_shrink_token_counts.get(dir_path)!,
556+
file_count: this._directory_file_counts.get(dir_path)!
551557
}
552558
}
553559

554560
try {
555561
const workspace_root =
556562
this._provider.get_workspace_root_for_file(dir_path)
557563
if (!workspace_root) {
558-
return { total: 0, shrink: 0 }
564+
return { total: 0, shrink: 0, file_count: 0 }
559565
}
560566

561567
const relative_dir_path = path.relative(workspace_root, dir_path)
@@ -566,14 +572,16 @@ export class TokenCalculator implements vscode.Disposable {
566572
) {
567573
this._directory_token_counts.set(dir_path, 0)
568574
this._directory_shrink_token_counts.set(dir_path, 0)
569-
return { total: 0, shrink: 0 }
575+
this._directory_file_counts.set(dir_path, 0)
576+
return { total: 0, shrink: 0, file_count: 0 }
570577
}
571578

572579
const entries = await fs.promises.readdir(dir_path, {
573580
withFileTypes: true
574581
})
575582
let total_tokens = 0
576583
let total_shrink_tokens = 0
584+
let file_count = 0
577585

578586
for (const entry of entries) {
579587
if (entry.isSymbolicLink()) continue
@@ -604,24 +612,27 @@ export class TokenCalculator implements vscode.Disposable {
604612
const counts = await this.calculate_directory_tokens(full_path)
605613
total_tokens += counts.total
606614
total_shrink_tokens += counts.shrink
615+
file_count += counts.file_count
607616
} else if (entry.isFile()) {
608617
const counts = await this.calculate_file_tokens(full_path)
609618
total_tokens += counts.total
610619
total_shrink_tokens += counts.shrink
620+
file_count += 1
611621
}
612622
}
613623

614624
this._directory_token_counts.set(dir_path, total_tokens)
615625
this._directory_shrink_token_counts.set(dir_path, total_shrink_tokens)
626+
this._directory_file_counts.set(dir_path, file_count)
616627

617-
return { total: total_tokens, shrink: total_shrink_tokens }
628+
return { total: total_tokens, shrink: total_shrink_tokens, file_count }
618629
} catch (error) {
619630
Logger.error({
620631
function_name: 'calculate_directory_tokens',
621632
message: `Error calculating tokens for directory ${dir_path}`,
622633
data: error
623634
})
624-
return { total: 0, shrink: 0 }
635+
return { total: 0, shrink: 0, file_count: 0 }
625636
}
626637
}
627638

‎apps/editor/src/context/providers/workspace/workspace-provider.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,16 @@ export class WorkspaceProvider
512512

513513
async getTreeItem(element: FileItem): Promise<vscode.TreeItem> {
514514
const key = element.resourceUri.fsPath
515-
const checkbox_state =
515+
let checkbox_state: vscode.TreeItemCheckboxState | undefined =
516516
this._checked_items.get(key) ?? vscode.TreeItemCheckboxState.Unchecked
517517

518+
if (element.isDirectory) {
519+
const tokens = await this._token_calculator.calculate_directory_tokens(key)
520+
if (tokens.file_count === 0) {
521+
checkbox_state = undefined
522+
}
523+
}
524+
518525
element.checkboxState = checkbox_state
519526

520527
const total_token_count = element.tokenCount
@@ -705,6 +712,13 @@ export class WorkspaceProvider
705712
const selected_tokens =
706713
await this._token_calculator.calculate_directory_selected_tokens(root)
707714

715+
let checkbox_state: vscode.TreeItemCheckboxState | undefined =
716+
this._checked_items.get(root) ?? vscode.TreeItemCheckboxState.Unchecked
717+
718+
if (total_tokens.file_count === 0) {
719+
checkbox_state = undefined
720+
}
721+
708722
items.push(
709723
new FileItem(
710724
name,
@@ -713,8 +727,7 @@ export class WorkspaceProvider
713727
? this._selected_files_view_collapsible_state
714728
: this._workspace_view_collapsible_state,
715729
true,
716-
this._checked_items.get(root) ??
717-
vscode.TreeItemCheckboxState.Unchecked,
730+
checkbox_state,
718731
false,
719732
false,
720733
total_tokens.total,
@@ -944,6 +957,13 @@ export class WorkspaceProvider
944957

945958
const range = this._file_ranges.get(full_path)
946959

960+
let final_checkbox_state: vscode.TreeItemCheckboxState | undefined =
961+
checkbox_state
962+
963+
if (is_directory && (tokens as any).file_count === 0) {
964+
final_checkbox_state = undefined
965+
}
966+
947967
const item = new FileItem(
948968
entry.name,
949969
uri,
@@ -953,7 +973,7 @@ export class WorkspaceProvider
953973
: this._workspace_view_collapsible_state
954974
: vscode.TreeItemCollapsibleState.None,
955975
is_directory,
956-
checkbox_state,
976+
final_checkbox_state,
957977
false,
958978
false,
959979
tokens.total,
@@ -1070,6 +1090,16 @@ export class WorkspaceProvider
10701090
continue
10711091
}
10721092

1093+
if (entry.isDirectory()) {
1094+
const tokens =
1095+
await this._token_calculator.calculate_directory_tokens(
1096+
sibling_path
1097+
)
1098+
if (tokens.file_count === 0) {
1099+
continue
1100+
}
1101+
}
1102+
10731103
has_non_ignored_child = true
10741104
const state =
10751105
this._checked_items.get(sibling_path) ??

0 commit comments

Comments
 (0)