@@ -13,7 +13,7 @@ const get_global_contexts_file_path = (
1313 return path . join ( context . globalStorageUri . fsPath , GLOBAL_CONTEXTS_FILENAME )
1414}
1515
16- const load_all_global_contexts = (
16+ const load_all_global_contexts_data = (
1717 context : vscode . ExtensionContext
1818) : GlobalContextsData => {
1919 const file_path = get_global_contexts_file_path ( context )
@@ -32,7 +32,7 @@ export const load_contexts_for_workspace = (
3232 context : vscode . ExtensionContext ,
3333 workspace_root : string
3434) : SavedContext [ ] => {
35- const all_data = load_all_global_contexts ( context )
35+ const all_data = load_all_global_contexts_data ( context )
3636 return all_data [ workspace_root ] || [ ]
3737}
3838
@@ -48,7 +48,7 @@ export const save_contexts_for_workspace = (
4848 fs . mkdirSync ( dir_path , { recursive : true } )
4949 }
5050
51- const all_data = load_all_global_contexts ( context )
51+ const all_data = load_all_global_contexts_data ( context )
5252 all_data [ workspace_root ] = contexts
5353
5454 try {
@@ -58,3 +58,45 @@ export const save_contexts_for_workspace = (
5858 throw new Error ( 'Failed to save contexts to global storage.' )
5959 }
6060}
61+
62+ export const load_and_merge_global_contexts = (
63+ context : vscode . ExtensionContext
64+ ) : { merged : SavedContext [ ] ; context_to_roots : Map < string , string [ ] > } => {
65+ const workspace_folders = vscode . workspace . workspaceFolders || [ ]
66+ const should_prefix = workspace_folders . length > 1
67+ const contexts_by_name = new Map <
68+ string ,
69+ { paths : string [ ] ; roots : string [ ] }
70+ > ( )
71+
72+ for ( const folder of workspace_folders ) {
73+ const root = folder . uri . fsPath
74+ const contexts = load_contexts_for_workspace ( context , root )
75+
76+ for ( const c of contexts ) {
77+ if ( ! contexts_by_name . has ( c . name ) ) {
78+ contexts_by_name . set ( c . name , { paths : [ ] , roots : [ ] } )
79+ }
80+ const entry = contexts_by_name . get ( c . name ) !
81+
82+ const paths_to_add = should_prefix
83+ ? c . paths . map ( ( p ) => `${ folder . name } :${ p } ` )
84+ : c . paths
85+
86+ entry . paths . push ( ...paths_to_add )
87+ entry . roots . push ( root )
88+ }
89+ }
90+
91+ const merged : SavedContext [ ] = [ ]
92+ const context_to_roots = new Map < string , string [ ] > ( )
93+
94+ for ( const [ name , data ] of contexts_by_name . entries ( ) ) {
95+ merged . push ( { name, paths : data . paths } )
96+ context_to_roots . set ( name , data . roots )
97+ }
98+
99+ merged . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
100+
101+ return { merged, context_to_roots }
102+ }
0 commit comments