Skip to content

Commit 0a984af

Browse files
committed
Refactor context loading to use shared utility functions for workspace state and file-based contexts
1 parent 285e3e2 commit 0a984af

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

‎packages/vscode/src/utils/replace-saved-context-placeholder.ts‎

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import * as vscode from 'vscode'
22
import * as path from 'path'
33
import * as fs from 'fs'
44
import { SavedContext } from '@/types/context'
5-
import { SAVED_CONTEXTS_STATE_KEY } from '@/constants/state-keys'
65
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
76
import { dictionary } from '@shared/constants/dictionary'
87
import { resolve_glob_patterns } from '@/commands/apply-context-command/helpers/applying'
8+
import { load_contexts_for_workspace } from '@/commands/apply-context-command/helpers/saving/global-storage-utils'
9+
import {
10+
get_contexts_file_path,
11+
load_contexts_from_file
12+
} from '@/commands/apply-context-command/helpers/saving/context-file-utils'
913

1014
async function get_file_content_as_xml(
1115
file_path: string,
@@ -43,37 +47,17 @@ const get_context = (
4347
workspace_root: string
4448
): SavedContext | undefined => {
4549
if (source == 'WorkspaceState') {
46-
const internal_contexts: SavedContext[] =
47-
context.workspaceState.get(SAVED_CONTEXTS_STATE_KEY, []) || []
50+
const internal_contexts = load_contexts_for_workspace(
51+
context,
52+
workspace_root
53+
)
4854
return internal_contexts.find((c) => c.name == name)
4955
} else {
5056
// JSON
51-
const contexts_file_path = path.join(
52-
workspace_root,
53-
'.vscode',
54-
'contexts.json'
55-
)
56-
if (fs.existsSync(contexts_file_path)) {
57-
try {
58-
const content = fs.readFileSync(contexts_file_path, 'utf8')
59-
const parsed = JSON.parse(content)
60-
if (Array.isArray(parsed)) {
61-
const file_contexts = parsed.filter(
62-
(item): item is SavedContext =>
63-
typeof item == 'object' &&
64-
item !== null &&
65-
typeof item.name == 'string' &&
66-
Array.isArray(item.paths) &&
67-
item.paths.every((p: any) => typeof p == 'string')
68-
)
69-
return file_contexts.find((c) => c.name == name)
70-
}
71-
} catch (e) {
72-
/* ignore */
73-
}
74-
}
57+
const contexts_file_path = get_contexts_file_path(workspace_root)
58+
const file_contexts = load_contexts_from_file(contexts_file_path)
59+
return file_contexts.find((c) => c.name == name)
7560
}
76-
return undefined
7761
}
7862

7963
export const replace_saved_context_placeholder = async (params: {

0 commit comments

Comments
 (0)