@@ -4,15 +4,15 @@ import * as vscode from 'vscode'
44import ignore , { Ignore } from 'ignore'
55import {
66 CONTEXT_SELECTED_PATHS_STATE_KEY ,
7- CONTEXT_SELECTED_TIMESTAMPS_STATE_KEY ,
8- RANGES_STATE_KEY
7+ CONTEXT_SELECTED_TIMESTAMPS_STATE_KEY
98} from '@/constants/state-keys'
109import { IGNORED_LOCK_FILES } from '@/constants/ignored-lock-files'
1110import { natural_sort } from '@/utils/natural-sort'
1211import { Logger } from '@shared/utils/logger'
1312import { display_token_count } from '@shared/utils/display-token-count'
1413import { TokenCalculator } from './modules/token-calculator'
1514import { normalize_path } from '@/utils/normalize-path'
15+ import { RangesUtils } from '@/utils/ranges-utils'
1616
1717export interface IWorkspaceProvider {
1818 get_workspace_roots ( ) : string [ ]
@@ -61,6 +61,7 @@ export class WorkspaceProvider
6161 private _user_allow_patterns : Ignore = ignore ( )
6262 private _watcher : vscode . FileSystemWatcher
6363 private _ranges_watcher : vscode . FileSystemWatcher
64+ private _global_ranges_watcher : vscode . FileSystemWatcher
6465 private _gitignore_watcher : vscode . FileSystemWatcher
6566 private _file_ranges : Map < string , string > = new Map ( )
6667 private _token_calculator : TokenCalculator
@@ -147,6 +148,19 @@ export class WorkspaceProvider
147148 this . _ranges_watcher . onDidChange ( ( ) => this . load_all_ranges ( ) )
148149 this . _ranges_watcher . onDidDelete ( ( ) => this . load_all_ranges ( ) )
149150
151+ const global_ranges_file_path = RangesUtils . get_file_path ( {
152+ extension_context : this . _extension_context
153+ } )
154+ this . _global_ranges_watcher = vscode . workspace . createFileSystemWatcher (
155+ new vscode . RelativePattern (
156+ vscode . Uri . file ( path . dirname ( global_ranges_file_path ) ) ,
157+ path . basename ( global_ranges_file_path )
158+ )
159+ )
160+ this . _global_ranges_watcher . onDidCreate ( ( ) => this . load_all_ranges ( ) )
161+ this . _global_ranges_watcher . onDidChange ( ( ) => this . load_all_ranges ( ) )
162+ this . _global_ranges_watcher . onDidDelete ( ( ) => this . load_all_ranges ( ) )
163+
150164 this . ranges_initialization = this . load_all_ranges ( )
151165
152166 this . load_selected_files_state ( )
@@ -263,6 +277,7 @@ export class WorkspaceProvider
263277 dispose ( ) {
264278 this . _watcher . dispose ( )
265279 this . _ranges_watcher . dispose ( )
280+ this . _global_ranges_watcher . dispose ( )
266281 this . _gitignore_watcher . dispose ( )
267282 this . _config_change_handler . dispose ( )
268283 this . _on_did_change_selected_files . dispose ( )
@@ -803,9 +818,12 @@ export class WorkspaceProvider
803818 try {
804819 const content = await fs . promises . readFile ( ranges_file_path , 'utf-8' )
805820 const ranges = JSON . parse ( content ) as Record < string , string >
806- for ( const [ relativePath , range ] of Object . entries ( ranges ) ) {
807- const absolutePath = path . join ( workspace_root , relativePath )
808- this . _file_ranges . set ( absolutePath , range )
821+ for ( const [ relative_path , range ] of Object . entries ( ranges ) ) {
822+ const absolute_path = path . join (
823+ workspace_root ,
824+ normalize_path ( relative_path )
825+ )
826+ this . _file_ranges . set ( absolute_path , range )
809827 }
810828 } catch ( e ) {
811829 if ( ( e as NodeJS . ErrnoException ) . code !== 'ENOENT' ) {
@@ -814,45 +832,27 @@ export class WorkspaceProvider
814832 }
815833 }
816834
817- const state_ranges = this . _extension_context . workspaceState . get <
818- Record < string , string >
819- > ( RANGES_STATE_KEY , { } )
820- for ( const [ key , range ] of Object . entries ( state_ranges ) ) {
821- const resolved_path = this . _resolve_state_path ( key )
822- if ( resolved_path && fs . existsSync ( resolved_path ) ) {
823- this . _file_ranges . set ( resolved_path , range )
835+ const all_global_ranges = RangesUtils . load_all ( this . _extension_context )
836+ for ( const workspace_root of this . _workspace_roots ) {
837+ const global_ranges_for_workspace =
838+ all_global_ranges [ workspace_root ] || { }
839+ for ( const [ rel_path , range ] of Object . entries (
840+ global_ranges_for_workspace
841+ ) ) {
842+ const absolute_path = path . join (
843+ workspace_root ,
844+ normalize_path ( rel_path )
845+ )
846+ if ( fs . existsSync ( absolute_path ) ) {
847+ this . _file_ranges . set ( absolute_path , range )
848+ }
824849 }
825850 }
826851
827852 this . _token_calculator . clear_caches ( )
828853 this . refresh ( )
829854 }
830855
831- private _resolve_state_path ( key : string ) : string | undefined {
832- if ( this . _workspace_roots . length === 1 ) {
833- return path . join ( this . _workspace_roots [ 0 ] , key )
834- }
835-
836- const parts = key . split ( ':' )
837- if ( parts . length > 1 ) {
838- const name = parts [ 0 ]
839- const rel_path = parts . slice ( 1 ) . join ( ':' )
840- const index = this . _workspace_names . indexOf ( name )
841- if ( index !== - 1 ) {
842- return path . join ( this . _workspace_roots [ index ] , rel_path )
843- }
844- }
845-
846- for ( const root of this . _workspace_roots ) {
847- const abs = path . join ( root , key )
848- if ( fs . existsSync ( abs ) ) {
849- return abs
850- }
851- }
852-
853- return undefined
854- }
855-
856856 get_range ( filePath : string ) : string | undefined {
857857 return this . _file_ranges . get ( filePath )
858858 }
0 commit comments