Skip to content

Commit cf2d5dd

Browse files
committed
Refactor ranges and tasks storage to use a unified GlobalWorkspaceStorage class
- `apps/editor/src/utils/ranges-utils.ts` and `apps/editor/src/utils/tasks-utils.ts` should be merged into generic global-workspace-storage.ts (accepts file name it saves to etc.)
1 parent cac03f3 commit cf2d5dd

10 files changed

Lines changed: 175 additions & 269 deletions

File tree

‎apps/editor/src/commands/set-ranges-command.ts‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { LAST_RANGES_SAVE_LOCATION_STATE_KEY } from '../constants/state-keys'
99
import { t } from '../i18n'
1010
import { get_error_message } from '@/utils/get-error-message'
1111
import { is_binary_file } from '../utils/is-binary'
12-
import { RangesUtils } from '../utils/ranges-utils'
12+
import { RangesStorage } from '../utils/global-workspace-storage'
1313
import { normalize_path } from '../utils/normalize-path'
1414

1515
export const set_ranges_command = (
@@ -71,7 +71,7 @@ export const set_ranges_command = (
7171
}
7272
} catch {}
7373

74-
const all_global_ranges = RangesUtils.load_all(extension_context)
74+
const all_global_ranges = RangesStorage.load_all(extension_context)
7575
const global_ranges_for_workspace =
7676
all_global_ranges[workspace_folder.uri.fsPath] || {}
7777

@@ -203,10 +203,7 @@ export const set_ranges_command = (
203203
delete updated_all[workspace_folder.uri.fsPath]
204204
}
205205
}
206-
RangesUtils.save_all({
207-
extension_context,
208-
ranges: updated_all
209-
})
206+
RangesStorage.save_all(extension_context, updated_all)
210207
changed = true
211208
}
212209

@@ -283,10 +280,7 @@ export const set_ranges_command = (
283280
delete updated_all[workspace_folder.uri.fsPath]
284281
}
285282
}
286-
RangesUtils.save_all({
287-
extension_context,
288-
ranges: updated_all
289-
})
283+
RangesStorage.save_all(extension_context, updated_all)
290284
}
291285
} catch (error) {
292286
vscode.window.showErrorMessage(
@@ -303,10 +297,7 @@ export const set_ranges_command = (
303297
updated_all[workspace_folder.uri.fsPath][relative_path] =
304298
formatted_range
305299

306-
RangesUtils.save_all({
307-
extension_context,
308-
ranges: updated_all
309-
})
300+
RangesStorage.save_all(extension_context, updated_all)
310301

311302
if (file_ranges[relative_path]) {
312303
delete file_ranges[relative_path]

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Logger } from '@shared/utils/logger'
1212
import { display_token_count } from '@shared/utils/display-token-count'
1313
import { TokenCalculator } from './modules/token-calculator'
1414
import { normalize_path } from '@/utils/normalize-path'
15-
import { RangesUtils } from '@/utils/ranges-utils'
15+
import { RangesStorage } from '@/utils/global-workspace-storage'
1616

1717
export interface IWorkspaceProvider {
1818
get_workspace_roots(): string[]
@@ -148,9 +148,9 @@ export class WorkspaceProvider
148148
this._ranges_watcher.onDidChange(() => this.load_all_ranges())
149149
this._ranges_watcher.onDidDelete(() => this.load_all_ranges())
150150

151-
const global_ranges_file_path = RangesUtils.get_file_path({
152-
extension_context: this._extension_context
153-
})
151+
const global_ranges_file_path = RangesStorage.get_file_path(
152+
this._extension_context
153+
)
154154
this._global_ranges_watcher = vscode.workspace.createFileSystemWatcher(
155155
new vscode.RelativePattern(
156156
vscode.Uri.file(path.dirname(global_ranges_file_path)),
@@ -832,7 +832,7 @@ export class WorkspaceProvider
832832
}
833833
}
834834

835-
const all_global_ranges = RangesUtils.load_all(this._extension_context)
835+
const all_global_ranges = RangesStorage.load_all(this._extension_context)
836836
for (const workspace_root of this._workspace_roots) {
837837
const global_ranges_for_workspace =
838838
all_global_ranges[workspace_root] || {}

‎apps/editor/src/migrations/migrate-ranges-to-global-storage.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as fs from 'fs'
33
import * as path from 'path'
44
import { Logger } from '@shared/utils/logger'
55
import { get_error_message } from '@/utils/get-error-message'
6-
import { RangesUtils } from '@/utils/ranges-utils'
6+
import { RangesStorage } from '@/utils/global-workspace-storage'
7+
import { normalize_path } from '@/utils/normalize-path'
78
import { RANGES_STATE_KEY } from '@/constants/state-keys'
89

910
const MIGRATION_ID = 'ranges-to-global-storage-migration-20261108'
@@ -22,7 +23,7 @@ export async function migrate_ranges_to_global_storage(
2223
)
2324

2425
if (state_ranges && Object.keys(state_ranges).length > 0) {
25-
const all_global_ranges = RangesUtils.load_all(extension_context)
26+
const all_global_ranges = RangesStorage.load_all(extension_context)
2627

2728
const workspace_folders = vscode.workspace.workspaceFolders || []
2829
const workspace_roots = workspace_folders.map((f) => f.uri.fsPath)
@@ -61,14 +62,11 @@ export async function migrate_ranges_to_global_storage(
6162
if (!all_global_ranges[resolved.root]) {
6263
all_global_ranges[resolved.root] = {}
6364
}
64-
all_global_ranges[resolved.root][resolved.rel] = range
65+
all_global_ranges[resolved.root][normalize_path(resolved.rel)] = range
6566
}
6667
}
6768

68-
RangesUtils.save_all({
69-
extension_context,
70-
ranges: all_global_ranges
71-
})
69+
RangesStorage.save_all(extension_context, all_global_ranges)
7270
}
7371

7472
await extension_context.workspaceState.update(MIGRATION_ID, true)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import * as vscode from 'vscode'
4+
import { Task } from '@shared/types/task'
5+
6+
export class GlobalWorkspaceStorage<T> {
7+
constructor(private filename: string) {}
8+
9+
public get_file_path(extension_context: vscode.ExtensionContext): string {
10+
return path.join(extension_context.globalStorageUri.fsPath, this.filename)
11+
}
12+
13+
public load_all(
14+
extension_context: vscode.ExtensionContext
15+
): Record<string, T> {
16+
const file_path = this.get_file_path(extension_context)
17+
try {
18+
if (fs.existsSync(file_path)) {
19+
const content = fs.readFileSync(file_path, 'utf8')
20+
return JSON.parse(content)
21+
}
22+
} catch (error) {
23+
console.error(
24+
`Error loading global workspace storage (${this.filename}):`,
25+
error
26+
)
27+
}
28+
return {}
29+
}
30+
31+
public save_all(
32+
extension_context: vscode.ExtensionContext,
33+
data: Record<string, T>
34+
): void {
35+
const file_path = this.get_file_path(extension_context)
36+
const dir_path = path.dirname(file_path)
37+
38+
if (!fs.existsSync(dir_path)) {
39+
fs.mkdirSync(dir_path, { recursive: true })
40+
}
41+
42+
try {
43+
fs.writeFileSync(file_path, JSON.stringify(data, null, 2), 'utf8')
44+
} catch (error) {
45+
console.error(
46+
`Failed to save to global workspace storage (${this.filename}):`,
47+
error
48+
)
49+
}
50+
}
51+
}
52+
53+
export const RangesStorage = new GlobalWorkspaceStorage<Record<string, string>>(
54+
'ranges.json'
55+
)
56+
export const TasksStorage = new GlobalWorkspaceStorage<Task[]>('tasks.json')

‎apps/editor/src/utils/ranges-utils.ts‎

Lines changed: 0 additions & 76 deletions
This file was deleted.

‎apps/editor/src/utils/tasks-utils.ts‎

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)