Skip to content

Commit 4f67d16

Browse files
committed
Persist selected unstaged files in workspace state during commit message generation
- commit message generation flow, current behavior: user selects files, then uses 'autofill in chatbot', then, re-launches the command and needs to select files again. Expected behavior: for whatever option used, selected files are remembered and restored upon re-launch of the command, and state cleared upon commiting. Make super the state object is keyed by root folder path. - If a user selects fileA.ts, cancels the flow, and later reverts/commits fileA.ts externally before modifying fileB.ts and fileC.ts, saved_selection_state[workspace_root] will still contain ['.../fileA.ts']. On the next run, items.filter((i) => current_selected_fs_paths.includes(i.fsPath)) will match nothing, opening the QuickPick with 0 files selected instead of defaulting to all files. └── apps └── editor └── src ├── commands │ └── generate-commit-message-commands │ ├── actions │ │ ├── get-prompt-data.ts │ │ └── run-generate-action.ts │ ├── generate-commit-message-commands.ts │ └── utils │ ├── build-commit-message-prompt.ts │ ├── generate-commit-message-with-api.ts │ ├── get-commit-message-config.ts │ └── strip-wrapping-quotes.ts ├── constants │ └── state-keys.ts ├── extension.ts ├── utils │ └── git-repository-utils.ts └── views └── prompt └── frontend └── Prompt.tsx
1 parent d1cebad commit 4f67d16

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎apps/editor/src/commands/generate-commit-message-commands/actions/run-generate-action.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
LAST_USE_CONTEXT_FILES_STATE_KEY,
1919
LAST_USED_AGENT_FOR_COMMIT_MESSAGE_STATE_KEY,
2020
LAST_SELECTED_WORKSPACE_FOR_COMMIT_MESSAGE_STATE_KEY,
21-
LAST_ACCEPTED_COMMIT_MESSAGE_STATE_KEY
21+
LAST_ACCEPTED_COMMIT_MESSAGE_STATE_KEY,
22+
LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY
2223
} from '@/constants/state-keys'
2324
import { get_prompt_data } from './get-prompt-data'
2425
import { display_token_count } from '@shared/utils/display-token-count'
@@ -63,11 +64,19 @@ export const run_generate_action = async (params: {
6364
let is_single_change_flow = false
6465
let show_quick_pick = false
6566
let current_action: string | undefined = undefined
66-
const selection_state: { files?: string[] } = {}
6767

6868
const repository = await get_repository_for_commit(params.source_control)
6969
if (!repository) return
7070

71+
const workspace_root = repository.rootUri.fsPath
72+
const saved_selection_state = params.extension_context.workspaceState.get<
73+
Record<string, string[]>
74+
>(LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY, {})
75+
76+
const selection_state: { files?: string[] } = {
77+
files: saved_selection_state[workspace_root]
78+
}
79+
7180
while (true) {
7281
const data = await get_prompt_data({
7382
repository,
@@ -85,6 +94,17 @@ export const run_generate_action = async (params: {
8594

8695
if (!data) return
8796

97+
if (data.was_empty_stage && selection_state.files) {
98+
const saved = params.extension_context.workspaceState.get<
99+
Record<string, string[]>
100+
>(LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY, {})
101+
saved[workspace_root] = selection_state.files
102+
await params.extension_context.workspaceState.update(
103+
LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY,
104+
saved
105+
)
106+
}
107+
88108
const {
89109
prompt_diffs_only,
90110
prompt_without_context,
@@ -702,7 +722,6 @@ export const run_generate_action = async (params: {
702722
}
703723
}
704724

705-
const workspace_root = repository.rootUri.fsPath
706725
const all_prompts =
707726
CommitMessageDetails.load_all(params.extension_context)[workspace_root] ||
708727
[]
@@ -1127,6 +1146,17 @@ export const run_generate_action = async (params: {
11271146
)
11281147
}
11291148

1149+
const saved = params.extension_context.workspaceState.get<
1150+
Record<string, string[]>
1151+
>(LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY, {})
1152+
if (saved[workspace_root]) {
1153+
delete saved[workspace_root]
1154+
await params.extension_context.workspaceState.update(
1155+
LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY,
1156+
saved
1157+
)
1158+
}
1159+
11301160
break
11311161
}
11321162
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ export const LAST_USED_AGENT_FOR_COMMIT_MESSAGE_STATE_KEY =
130130
'last-used-agent-for-commit-message'
131131
export const LAST_ACCEPTED_COMMIT_MESSAGE_STATE_KEY =
132132
'last-accepted-commit-message'
133+
export const LAST_SELECTED_UNSTAGED_FILES_FOR_COMMIT_MESSAGE_STATE_KEY =
134+
'last-selected-unstaged-files-for-commit-message'
133135

134136
export const LAST_INTELLIGENT_FILE_SEARCH_SHRINK_STATE_KEY =
135137
'last-intelligent-file-search-shrink'

‎apps/editor/src/utils/git-repository-utils.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,16 @@ export const prepare_staged_changes = async (params: {
251251
files_to_stage = params.repository.state.workingTreeChanges.map(
252252
(change: any) => change.uri.fsPath
253253
)
254+
if (params.selection_state) {
255+
params.selection_state.files = files_to_stage
256+
}
254257
} else if (params.repository.state.workingTreeChanges.length === 1) {
255258
files_to_stage = [
256259
params.repository.state.workingTreeChanges[0].uri.fsPath
257260
]
261+
if (params.selection_state) {
262+
params.selection_state.files = files_to_stage
263+
}
258264
} else {
259265
let prompts_for_repo: CommitMessageDetails.Prompt[] = []
260266
if (params.extension_context) {
@@ -443,6 +449,10 @@ export const prepare_staged_changes = async (params: {
443449
let current_selected_fs_paths =
444450
params.selection_state?.files || items.map((i) => i.fsPath)
445451

452+
if (!items.some((i) => current_selected_fs_paths.includes(i.fsPath))) {
453+
current_selected_fs_paths = items.map((i) => i.fsPath)
454+
}
455+
446456
while (true) {
447457
const selected = await new Promise<any[] | undefined | 'search'>(
448458
(resolve) => {

0 commit comments

Comments
 (0)