Skip to content

Commit d42b5cb

Browse files
committed
Refactor saved context loading to use shared utility functions
1 parent 7ed6a0a commit d42b5cb

1 file changed

Lines changed: 7 additions & 33 deletions

File tree

‎packages/vscode/src/views/panel/backend/message-handlers/handle-hash-sign-quick-pick.ts‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { PanelProvider } from '@/views/panel/backend/panel-provider'
22
import { MODE } from '@/views/panel/types/main-view-mode'
33
import * as vscode from 'vscode'
44
import { execSync } from 'child_process'
5-
import * as path from 'path'
6-
import * as fs from 'fs'
75
import { dictionary } from '@shared/constants/dictionary'
8-
import { SAVED_CONTEXTS_STATE_KEY } from '@/constants/state-keys'
9-
import { SavedContext } from '@/types/context'
6+
import { load_contexts_for_workspace } from '@/commands/apply-context-command/helpers/saving'
7+
import { load_and_merge_file_contexts } from '@/commands/apply-context-command/sources'
108

119
const selection_label = '$(list-flat) Selection'
1210
const changes_label = '$(git-pull-request-draft) Changes'
@@ -300,39 +298,15 @@ const handle_commit_item = async (
300298
const handle_saved_context_item = async (
301299
context: vscode.ExtensionContext
302300
): Promise<string | 'continue' | undefined> => {
303-
const workspace_root = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath
304-
if (!workspace_root) {
301+
const workspace_folders = vscode.workspace.workspaceFolders || []
302+
if (workspace_folders.length === 0) {
305303
vscode.window.showErrorMessage(dictionary.error_message.NO_WORKSPACE_ROOT)
306304
return undefined
307305
}
306+
const workspace_root = workspace_folders[0].uri.fsPath
308307

309-
const internal_contexts: SavedContext[] =
310-
context.workspaceState.get(SAVED_CONTEXTS_STATE_KEY, []) || []
311-
312-
const contexts_file_path = path.join(
313-
workspace_root,
314-
'.vscode',
315-
'contexts.json'
316-
)
317-
let file_contexts: SavedContext[] = []
318-
if (fs.existsSync(contexts_file_path)) {
319-
try {
320-
const content = fs.readFileSync(contexts_file_path, 'utf8')
321-
const parsed = JSON.parse(content)
322-
if (Array.isArray(parsed)) {
323-
file_contexts = parsed.filter(
324-
(item: any): item is SavedContext =>
325-
typeof item == 'object' &&
326-
item !== null &&
327-
typeof item.name == 'string' &&
328-
Array.isArray(item.paths) &&
329-
item.paths.every((p: any) => typeof p == 'string')
330-
)
331-
}
332-
} catch (e) {
333-
/* ignore */
334-
}
335-
}
308+
const internal_contexts = load_contexts_for_workspace(context, workspace_root)
309+
const { merged: file_contexts } = await load_and_merge_file_contexts()
336310

337311
const source_options: (vscode.QuickPickItem & {
338312
value: 'WorkspaceState' | 'JSON'

0 commit comments

Comments
 (0)