Skip to content

Commit 0265e10

Browse files
committed
Implement dual context state management for regular and find-relevant-files modes across workspace and open editors providers
- create separate context states for 'find relevant files' prompt type and all the others. - use state keys for frf - support frf in duplicate workspace - why these properties are optional? [Fragment] should not, dont worry about backward compatibility - when changing prompt types, the frf includes open editor automatically, should not.
1 parent 9760aa1 commit 0265e10

9 files changed

Lines changed: 266 additions & 45 deletions

File tree

‎apps/editor/src/commands/duplicate-workspace-command.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ export const duplicate_workspace_command = (
1515
return vscode.commands.registerCommand(
1616
'codeWebChat.duplicateWorkspace',
1717
async () => {
18-
const checked_files = workspace_provider.get_all_checked_paths()
19-
const checked_files_timestamps: Record<string, number> = {}
18+
const export_state = workspace_provider.get_export_state()
2019

21-
for (const file_path of checked_files) {
22-
const timestamp = workspace_provider.get_selection_timestamp(file_path)
23-
if (timestamp !== undefined) {
24-
checked_files_timestamps[file_path] = timestamp
25-
}
26-
}
20+
const checked_files = export_state.regular.checked_files
21+
const checked_files_timestamps = export_state.regular.checked_timestamps
22+
23+
const checked_files_frf = export_state.frf.checked_files
24+
const checked_files_timestamps_frf = export_state.frf.checked_timestamps
2725

2826
const workspace_root_folders =
2927
vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath) ??
@@ -49,6 +47,8 @@ export const duplicate_workspace_command = (
4947
const context_to_save: DuplicateWorkspaceContext = {
5048
checked_files,
5149
checked_files_timestamps,
50+
checked_files_frf,
51+
checked_files_timestamps_frf,
5252
workspace_root_folders,
5353
timestamp: Date.now(),
5454
open_editors,

‎apps/editor/src/constants/state-keys.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export const LAST_RANGES_SAVE_LOCATION_STATE_KEY = 'last-ranges-save-location'
1010
export const LAST_APPLY_CONTEXT_OPTION_STATE_KEY = 'last-apply-context-option'
1111
export const CONTEXT_CHECKED_PATHS_STATE_KEY = 'context-checked-paths'
1212
export const CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY = 'context-checked-timestamps'
13+
export const CONTEXT_CHECKED_PATHS_FRF_STATE_KEY = 'context-checked-paths-frf'
14+
export const CONTEXT_CHECKED_TIMESTAMPS_FRF_STATE_KEY =
15+
'context-checked-timestamps-frf'
1316
export const LAST_CONTEXT_MERGE_REPLACE_OPTION_STATE_KEY =
1417
'last-context-merge-replace-option'
1518
export const LAST_REFACTOR_INSTRUCTION_SOURCE_STATE_KEY =
@@ -85,7 +88,9 @@ export const LAST_SEARCH_FILES_FOR_CONTEXT_QUERY_STATE_KEY =
8588

8689
export type DuplicateWorkspaceContext = {
8790
checked_files: string[]
88-
checked_files_timestamps?: Record<string, number>
91+
checked_files_timestamps: Record<string, number>
92+
checked_files_frf: string[]
93+
checked_files_timestamps_frf: Record<string, number>
8994
timestamp: number
9095
workspace_root_folders: string[]
9196
open_editors?: { path: string; view_column?: number }[]

‎apps/editor/src/context/context-initialization.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { dictionary } from '@shared/constants/dictionary'
1212
import {
1313
CONTEXT_CHECKED_PATHS_STATE_KEY,
1414
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
15+
CONTEXT_CHECKED_PATHS_FRF_STATE_KEY,
16+
CONTEXT_CHECKED_TIMESTAMPS_FRF_STATE_KEY,
1517
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY,
1618
RANGES_STATE_KEY,
1719
type DuplicateWorkspaceContext
@@ -58,12 +60,20 @@ const restore_duplicated_workspace_context = async (
5860
CONTEXT_CHECKED_PATHS_STATE_KEY,
5961
duplicated_context.checked_files
6062
)
61-
if (duplicated_context.checked_files_timestamps) {
62-
await context.workspaceState.update(
63-
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
64-
duplicated_context.checked_files_timestamps
65-
)
66-
}
63+
64+
await context.workspaceState.update(
65+
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
66+
duplicated_context.checked_files_timestamps
67+
)
68+
await context.workspaceState.update(
69+
CONTEXT_CHECKED_PATHS_FRF_STATE_KEY,
70+
duplicated_context.checked_files_frf
71+
)
72+
await context.workspaceState.update(
73+
CONTEXT_CHECKED_TIMESTAMPS_FRF_STATE_KEY,
74+
duplicated_context.checked_files_timestamps_frf
75+
)
76+
6777
if (duplicated_context.ranges) {
6878
await context.workspaceState.update(
6979
RANGES_STATE_KEY,

‎apps/editor/src/context/providers/open-editors/open-editors-provider.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,21 @@ export class OpenEditorsProvider
1717
> = this._on_did_change_tree_data.event
1818

1919
private _workspace_roots: string[] = []
20-
private _checked_items: Map<string, vscode.TreeItemCheckboxState> = new Map()
20+
private _regular_checked_items = new Map<
21+
string,
22+
vscode.TreeItemCheckboxState
23+
>()
24+
private _frf_checked_items = new Map<string, vscode.TreeItemCheckboxState>()
25+
private _is_frf_mode = false
26+
private get _checked_items() {
27+
return this._is_frf_mode
28+
? this._frf_checked_items
29+
: this._regular_checked_items
30+
}
31+
private set _checked_items(val) {
32+
if (this._is_frf_mode) this._frf_checked_items = val
33+
else this._regular_checked_items = val
34+
}
2135
private _tab_change_handler: vscode.Disposable
2236
private _workspace_change_handler: vscode.Disposable
2337
private _initialized: boolean = false
@@ -38,6 +52,13 @@ export class OpenEditorsProvider
3852
}
3953
}
4054

55+
public switch_context_state(is_frf: boolean) {
56+
if (this._is_frf_mode == is_frf) return
57+
this._is_frf_mode = is_frf
58+
this.refresh()
59+
this._dispatch_change_events()
60+
}
61+
4162
constructor(params: {
4263
workspace_folders: readonly vscode.WorkspaceFolder[]
4364
workspace_provider: WorkspaceProvider

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

Lines changed: 120 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ignore, { Ignore } from 'ignore'
55
import {
66
CONTEXT_CHECKED_PATHS_STATE_KEY,
77
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
8+
CONTEXT_CHECKED_PATHS_FRF_STATE_KEY,
9+
CONTEXT_CHECKED_TIMESTAMPS_FRF_STATE_KEY,
810
RANGES_STATE_KEY
911
} from '@/constants/state-keys'
1012
import { IGNORE_PATTERNS } from '@/constants/ignore-patterns'
@@ -26,6 +28,13 @@ export interface IWorkspaceProvider {
2628
is_partially_checked(path: string): boolean
2729
get_checked_files(): string[]
2830
get_selection_timestamp(path: string): number | undefined
31+
get_export_state(): {
32+
regular: {
33+
checked_files: string[]
34+
checked_timestamps: Record<string, number>
35+
}
36+
frf: { checked_files: string[]; checked_timestamps: Record<string, number> }
37+
}
2938
}
3039

3140
export class WorkspaceProvider
@@ -43,8 +52,47 @@ export class WorkspaceProvider
4352
private _context: vscode.ExtensionContext
4453
private _workspace_roots: string[] = []
4554
private _workspace_names: string[] = []
46-
private _checked_items: Map<string, vscode.TreeItemCheckboxState> = new Map()
47-
private _checked_timestamps: Map<string, number> = new Map()
55+
56+
private _regular_state = {
57+
checked_items: new Map<string, vscode.TreeItemCheckboxState>(),
58+
checked_timestamps: new Map<string, number>(),
59+
partially_checked_dirs: new Set<string>()
60+
}
61+
private _frf_state = {
62+
checked_items: new Map<string, vscode.TreeItemCheckboxState>(),
63+
checked_timestamps: new Map<string, number>(),
64+
partially_checked_dirs: new Set<string>()
65+
}
66+
private _is_frf_mode: boolean = false
67+
68+
private get _checked_items() {
69+
return this._is_frf_mode
70+
? this._frf_state.checked_items
71+
: this._regular_state.checked_items
72+
}
73+
private set _checked_items(val) {
74+
if (this._is_frf_mode) this._frf_state.checked_items = val
75+
else this._regular_state.checked_items = val
76+
}
77+
private get _checked_timestamps() {
78+
return this._is_frf_mode
79+
? this._frf_state.checked_timestamps
80+
: this._regular_state.checked_timestamps
81+
}
82+
private set _checked_timestamps(val) {
83+
if (this._is_frf_mode) this._frf_state.checked_timestamps = val
84+
else this._regular_state.checked_timestamps = val
85+
}
86+
private get _partially_checked_dirs() {
87+
return this._is_frf_mode
88+
? this._frf_state.partially_checked_dirs
89+
: this._regular_state.partially_checked_dirs
90+
}
91+
private set _partially_checked_dirs(val) {
92+
if (this._is_frf_mode) this._frf_state.partially_checked_dirs = val
93+
else this._regular_state.partially_checked_dirs = val
94+
}
95+
4896
private _combined_gitignore = ignore()
4997
private _user_ignore_patterns: Ignore = ignore()
5098
private _user_allow_patterns: Ignore = ignore()
@@ -62,7 +110,6 @@ export class WorkspaceProvider
62110
private _opened_from_workspace_view: Set<string> = new Set()
63111
private _preview_tabs: Map<string, boolean> = new Map()
64112
private _tab_change_handler: vscode.Disposable
65-
private _partially_checked_dirs: Set<string> = new Set()
66113
private _file_workspace_map: Map<string, string> = new Map()
67114
public gitignore_initialization: Promise<void>
68115
public ranges_initialization: Promise<void>
@@ -91,6 +138,14 @@ export class WorkspaceProvider
91138
}
92139
}
93140

141+
public switch_context_state(is_frf: boolean) {
142+
if (this._is_frf_mode == is_frf) return
143+
this._is_frf_mode = is_frf
144+
this._token_calculator.clear_selected_counts()
145+
this.refresh()
146+
this._dispatch_change_events()
147+
}
148+
94149
constructor(params: {
95150
workspace_folders: readonly vscode.WorkspaceFolder[]
96151
context: vscode.ExtensionContext
@@ -153,29 +208,75 @@ export class WorkspaceProvider
153208

154209
private async _save_checked_files_state(): Promise<void> {
155210
const checked_paths = this.get_all_checked_paths()
211+
const paths_key = this._is_frf_mode
212+
? CONTEXT_CHECKED_PATHS_FRF_STATE_KEY
213+
: CONTEXT_CHECKED_PATHS_STATE_KEY
214+
const times_key = this._is_frf_mode
215+
? CONTEXT_CHECKED_TIMESTAMPS_FRF_STATE_KEY
216+
: CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY
217+
await this._context.workspaceState.update(paths_key, checked_paths)
156218
await this._context.workspaceState.update(
157-
CONTEXT_CHECKED_PATHS_STATE_KEY,
158-
checked_paths
159-
)
160-
await this._context.workspaceState.update(
161-
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
219+
times_key,
162220
Object.fromEntries(this._checked_timestamps)
163221
)
164222
}
165223

166224
public load_checked_files_state() {
167-
const persisted_checked_paths = this._context.workspaceState.get<string[]>(
168-
CONTEXT_CHECKED_PATHS_STATE_KEY
169-
)
170-
const persisted_timestamps = this._context.workspaceState.get<
171-
Record<string, number>
172-
>(CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY)
225+
const load = async () => {
226+
await this.gitignore_initialization
227+
const prev_mode = this._is_frf_mode
173228

174-
if (persisted_checked_paths && persisted_checked_paths.length > 0) {
175-
const timestamps_map = persisted_timestamps
176-
? new Map(Object.entries(persisted_timestamps))
177-
: undefined
178-
this.set_checked_files(persisted_checked_paths, timestamps_map)
229+
this._is_frf_mode = false
230+
const reg_paths = this._context.workspaceState.get<string[]>(
231+
CONTEXT_CHECKED_PATHS_STATE_KEY
232+
)
233+
const reg_times = this._context.workspaceState.get<
234+
Record<string, number>
235+
>(CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY)
236+
if (reg_paths && reg_paths.length > 0) {
237+
await this.set_checked_files(
238+
reg_paths,
239+
reg_times ? new Map(Object.entries(reg_times)) : undefined
240+
)
241+
}
242+
243+
this._is_frf_mode = true
244+
const frf_paths = this._context.workspaceState.get<string[]>(
245+
CONTEXT_CHECKED_PATHS_FRF_STATE_KEY
246+
)
247+
const frf_times = this._context.workspaceState.get<
248+
Record<string, number>
249+
>(CONTEXT_CHECKED_TIMESTAMPS_FRF_STATE_KEY)
250+
if (frf_paths && frf_paths.length > 0) {
251+
await this.set_checked_files(
252+
frf_paths,
253+
frf_times ? new Map(Object.entries(frf_times)) : undefined
254+
)
255+
}
256+
257+
this._is_frf_mode = prev_mode
258+
}
259+
load()
260+
}
261+
262+
public get_export_state() {
263+
return {
264+
regular: {
265+
checked_files: Array.from(this._regular_state.checked_items.entries())
266+
.filter(([, state]) => state == vscode.TreeItemCheckboxState.Checked)
267+
.map(([path]) => path),
268+
checked_timestamps: Object.fromEntries(
269+
this._regular_state.checked_timestamps
270+
)
271+
},
272+
frf: {
273+
checked_files: Array.from(this._frf_state.checked_items.entries())
274+
.filter(([, state]) => state == vscode.TreeItemCheckboxState.Checked)
275+
.map(([path]) => path),
276+
checked_timestamps: Object.fromEntries(
277+
this._frf_state.checked_timestamps
278+
)
279+
}
179280
}
180281
}
181282

0 commit comments

Comments
 (0)