|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as path from 'path' |
| 4 | +import ignore, { Ignore } from 'ignore' |
| 5 | +import { |
| 6 | + CONTEXT_CHECKED_PATHS_STATE_KEY, |
| 7 | + CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY, |
| 8 | + RANGES_STATE_KEY |
| 9 | +} from '@/constants/state-keys' |
| 10 | +import { IGNORE_PATTERNS } from '@/constants/ignore-patterns' |
| 11 | +import { natural_sort } from '@/utils/natural-sort' |
| 12 | +import { dictionary } from '@shared/constants/dictionary' |
| 13 | +import { Logger } from '@shared/utils/logger' |
| 14 | +import { display_token_count } from '@/utils/display-token-count' |
| 15 | +import { TokenCalculator } from './modules/token-calculator' |
| 16 | + |
| 17 | +export interface IWorkspaceProvider { |
| 18 | + get_workspace_roots(): string[] |
| 19 | + get_workspace_root_for_file(file_path: string): string | undefined |
| 20 | + get_workspace_name(root_path: string): string |
| 21 | + get_range(filePath: string): string | undefined |
| 22 | + apply_range_to_content(content: string, range_string: string): string |
| 23 | + is_excluded(relative_path: string): boolean |
| 24 | + is_ignored_by_patterns(file_path: string): boolean |
| 25 | + get_check_state(path: string): vscode.TreeItemCheckboxState |
| 26 | + is_partially_checked(path: string): boolean |
| 27 | + get_checked_files(): string[] |
| 28 | + get_selection_timestamp(path: string): number | undefined |
| 29 | +} |
| 30 | + |
| 31 | +export class WorkspaceProvider |
| 32 | + implements |
| 33 | + vscode.TreeDataProvider<FileItem>, |
| 34 | + vscode.Disposable, |
| 35 | + IWorkspaceProvider |
| 36 | +{ |
| 37 | + private _on_did_change_tree_data: vscode.EventEmitter< |
| 38 | + FileItem | undefined | null | void |
| 39 | + > = new vscode.EventEmitter<FileItem | undefined | null | void>() |
| 40 | + readonly onDidChangeTreeData: vscode.Event< |
| 41 | + FileItem | undefined | null | void |
| 42 | + > = this._on_did_change_tree_data.event |
| 43 | + private _context: vscode.ExtensionContext |
| 44 | + private _workspace_roots: string[] = [] |
| 45 | + private _workspace_names: string[] = [] |
| 46 | + private _checked_items: Map<string, vscode.TreeItemCheckboxState> = new Map() |
| 47 | + private _checked_timestamps: Map<string, number> = new Map() |
| 48 | + private _combined_gitignore = ignore() |
| 49 | + private _user_ignore_patterns: Ignore = ignore() |
| 50 | + private _user_allow_patterns: Ignore = ignore() |
| 51 | + private _watcher: vscode.FileSystemWatcher |
| 52 | + private _ranges_watcher: vscode.FileSystemWatcher |
| 53 | + private _gitignore_watcher: vscode.FileSystemWatcher |
| 54 | + private _file_ranges: Map<string, string> = new Map() |
| 55 | + private _token_calculator: TokenCalculator |
| 56 | + |
| 57 | + private _config_change_handler: vscode.Disposable |
| 58 | + private _on_did_change_checked_files = new vscode.EventEmitter<void>() |
| 59 | + readonly onDidChangeCheckedFiles = this._on_did_change_checked_files.event |
| 60 | + private _refresh_timeout: NodeJS.Timeout | null = null |
| 61 | + // Track which files were opened from workspace view to prevent auto-checking |
| 62 | + private _opened_from_workspace_view: Set<string> = new Set() |
| 63 | + private _preview_tabs: Map<string, boolean> = new Map() |
| 64 | + private _tab_change_handler: vscode.Disposable |
| 65 | + private _partially_checked_dirs: Set<string> = new Set() |
| 66 | + private _file_workspace_map: Map<string, string> = new Map() |
| 67 | + public gitignore_initialization: Promise<void> |
| 68 | + public ranges_initialization: Promise<void> |
| 69 | + public use_compact_token_count: boolean = false |
| 70 | + private _context_view_collapsible_state: vscode.TreeItemCollapsibleState = |
| 71 | + vscode.TreeItemCollapsibleState.Expanded |
| 72 | + private _workspace_view_collapsible_state: vscode.TreeItemCollapsibleState = |
| 73 | + vscode.TreeItemCollapsibleState.Collapsed |
| 74 | + |
| 75 | + public set_context_view_collapsible_state( |
| 76 | + state: vscode.TreeItemCollapsibleState |
| 77 | + ) { |
| 78 | + this._context_view_collapsible_state = state |
| 79 | + } |
| 80 | + |
| 81 | + public set_workspace_view_collapsible_state( |
| 82 | + state: vscode.TreeItemCollapsibleState |
| 83 | + ) { |
| 84 | + this._workspace_view_collapsible_state = state |
| 85 | + } |
| 86 | + |
| 87 | + public set_use_compact_token_count(use_compact: boolean) { |
| 88 | + if (this.use_compact_token_count != use_compact) { |
| 89 | + this.use_compact_token_count = use_compact |
| 90 | + this.refresh() |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + constructor(params: { |
| 95 | + workspace_folders: vscode.WorkspaceFolder[] |
| 96 | + context: vscode.ExtensionContext |
| 97 | + }) { |
| 98 | + this._context = params.context |
| 99 | + this._workspace_roots = params.workspace_folders.map( |
| 100 | + (folder) => folder.uri.fsPath |
| 101 | + ) |
| 102 | + this._workspace_names = params.workspace_folders.map( |
| 103 | + (folder) => folder.name |
| 104 | + ) |
| 105 | + this.onDidChangeCheckedFiles(() => this._save_checked_files_state()) |
| 106 | + this._token_calculator = new TokenCalculator(this, this._context) |
| 107 | + this._load_ignore_patterns() |
| 108 | + |
| 109 | + this._watcher = vscode.workspace.createFileSystemWatcher('**/*') |
| 110 | + this._watcher.onDidCreate((uri) => this._handle_file_create(uri.fsPath)) |
| 111 | + this._watcher.onDidChange((uri) => this._on_file_system_changed(uri.fsPath)) |
| 112 | + this._watcher.onDidDelete((uri) => this._on_file_system_changed(uri.fsPath)) |
| 113 | + |
| 114 | + this._gitignore_watcher = vscode.workspace.createFileSystemWatcher( |
| 115 | + '**/{.gitignore,.cursorignore,.codeiumignore}' |
| 116 | + ) |
| 117 | + this._gitignore_watcher.onDidCreate(() => this._load_all_gitignore_files()) |
| 118 | + this._gitignore_watcher.onDidChange(() => this._load_all_gitignore_files()) |
| 119 | + this._gitignore_watcher.onDidDelete(() => this._load_all_gitignore_files()) |
| 120 | + |
| 121 | + this._config_change_handler = vscode.workspace.onDidChangeConfiguration( |
| 122 | + (event) => { |
| 123 | + if ( |
| 124 | + event.affectsConfiguration('codeWebChat.ignorePatterns') || |
| 125 | + event.affectsConfiguration('codeWebChat.allowPatterns') |
| 126 | + ) { |
| 127 | + this._load_ignore_patterns() |
| 128 | + this._uncheck_ignored_files() |
| 129 | + this.refresh() |
| 130 | + } |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + this._update_preview_tabs_state() |
| 135 | + |
| 136 | + this._tab_change_handler = vscode.window.tabGroups.onDidChangeTabs((e) => { |
| 137 | + this._handle_tab_changes(e) |
| 138 | + }) |
| 139 | + |
| 140 | + this.gitignore_initialization = this._load_all_gitignore_files() |
| 141 | + this._ranges_watcher = vscode.workspace.createFileSystemWatcher( |
| 142 | + '**/.vscode/ranges.json' |
| 143 | + ) |
| 144 | + this._ranges_watcher.onDidCreate(() => this.load_all_ranges()) |
| 145 | + this._ranges_watcher.onDidChange(() => this.load_all_ranges()) |
| 146 | + this._ranges_watcher.onDidDelete(() => this.load_all_ranges()) |
| 147 | + |
| 148 | + this.ranges_initialization = this.load_all_ranges() |
| 149 | + |
| 150 | + this.load_checked_files_state() |
| 151 | + } |
0 commit comments