|
1 | 1 | import * as vscode from 'vscode' |
2 | 2 | import { execSync } from 'child_process' |
3 | 3 | import { dictionary } from '@shared/constants/dictionary' |
4 | | -import { LAST_SELECTED_REPOSITORY_IN_SYMBOLS_QUCK_PICK_STATE_KEY } from '@/constants/state-keys' |
| 4 | +import { |
| 5 | + LAST_SELECTED_REPOSITORY_IN_SYMBOLS_QUCK_PICK_STATE_KEY, |
| 6 | + LAST_SELECTED_COMMIT_REFERENCE_ACTION_STATE_KEY |
| 7 | +} from '@/constants/state-keys' |
5 | 8 | import { GIT_LOG_SINCE_DURATION } from '@/constants/values' |
6 | 9 |
|
7 | 10 | export const handle_commit_item = async ( |
8 | | - extension_context: vscode.ExtensionContext, |
9 | | - symbol: 'Commit' |
| 11 | + extension_context: vscode.ExtensionContext |
10 | 12 | ): Promise<string | 'continue' | undefined> => { |
11 | 13 | try { |
12 | 14 | const workspace_folders = vscode.workspace.workspaceFolders |
@@ -136,56 +138,133 @@ export const handle_commit_item = async ( |
136 | 138 | } |
137 | 139 | } |
138 | 140 |
|
139 | | - const quick_pick = vscode.window.createQuickPick() |
140 | | - quick_pick.items = commit_items |
141 | | - quick_pick.placeholder = 'Select a commit to reference' |
142 | | - quick_pick.title = 'Commits' |
143 | | - quick_pick.buttons = [vscode.QuickInputButtons.Back] |
144 | | - quick_pick.matchOnDetail = true |
145 | | - |
146 | | - const selected_commit = await new Promise< |
147 | | - vscode.QuickPickItem | 'back' | undefined |
148 | | - >((resolve) => { |
149 | | - let is_accepted = false |
150 | | - let did_trigger_back = false |
151 | | - const disposables: vscode.Disposable[] = [] |
152 | | - |
153 | | - disposables.push( |
154 | | - quick_pick.onDidTriggerButton((button) => { |
155 | | - if (button === vscode.QuickInputButtons.Back) { |
156 | | - did_trigger_back = true |
| 141 | + let go_back_to_folders = false |
| 142 | + |
| 143 | + while (true) { |
| 144 | + const quick_pick = vscode.window.createQuickPick() |
| 145 | + quick_pick.items = commit_items |
| 146 | + quick_pick.placeholder = 'Select a commit to reference' |
| 147 | + quick_pick.title = 'Commits' |
| 148 | + quick_pick.buttons = [vscode.QuickInputButtons.Back] |
| 149 | + quick_pick.matchOnDetail = true |
| 150 | + |
| 151 | + const selected_commit = await new Promise< |
| 152 | + vscode.QuickPickItem | 'back' | undefined |
| 153 | + >((resolve) => { |
| 154 | + let is_accepted = false |
| 155 | + let did_trigger_back = false |
| 156 | + const disposables: vscode.Disposable[] = [] |
| 157 | + |
| 158 | + disposables.push( |
| 159 | + quick_pick.onDidTriggerButton((button) => { |
| 160 | + if (button === vscode.QuickInputButtons.Back) { |
| 161 | + did_trigger_back = true |
| 162 | + quick_pick.hide() |
| 163 | + resolve('back') |
| 164 | + } |
| 165 | + }), |
| 166 | + quick_pick.onDidAccept(() => { |
| 167 | + is_accepted = true |
| 168 | + resolve(quick_pick.selectedItems[0]) |
157 | 169 | quick_pick.hide() |
158 | | - resolve('back') |
159 | | - } |
160 | | - }), |
161 | | - quick_pick.onDidAccept(() => { |
162 | | - is_accepted = true |
163 | | - resolve(quick_pick.selectedItems[0]) |
164 | | - quick_pick.hide() |
165 | | - }), |
166 | | - quick_pick.onDidHide(() => { |
167 | | - if (!is_accepted && !did_trigger_back) { |
168 | | - resolve(undefined) |
169 | | - } |
170 | | - disposables.forEach((d) => d.dispose()) |
171 | | - quick_pick.dispose() |
172 | | - }) |
| 170 | + }), |
| 171 | + quick_pick.onDidHide(() => { |
| 172 | + if (!is_accepted && !did_trigger_back) { |
| 173 | + resolve(undefined) |
| 174 | + } |
| 175 | + disposables.forEach((d) => d.dispose()) |
| 176 | + quick_pick.dispose() |
| 177 | + }) |
| 178 | + ) |
| 179 | + quick_pick.show() |
| 180 | + }) |
| 181 | + |
| 182 | + if (!selected_commit || selected_commit == 'back') { |
| 183 | + if (git_folders.length > 1) { |
| 184 | + go_back_to_folders = true |
| 185 | + break |
| 186 | + } |
| 187 | + return 'continue' |
| 188 | + } |
| 189 | + |
| 190 | + const action_quick_pick = vscode.window.createQuickPick() |
| 191 | + const action_items = [ |
| 192 | + { |
| 193 | + label: 'Diff with commit message' |
| 194 | + }, |
| 195 | + { |
| 196 | + label: 'Commit message only' |
| 197 | + } |
| 198 | + ] |
| 199 | + action_quick_pick.items = action_items |
| 200 | + action_quick_pick.placeholder = 'Select what to include' |
| 201 | + action_quick_pick.title = 'Commit Reference Options' |
| 202 | + action_quick_pick.buttons = [vscode.QuickInputButtons.Back] |
| 203 | + |
| 204 | + const last_action = extension_context.workspaceState.get<string>( |
| 205 | + LAST_SELECTED_COMMIT_REFERENCE_ACTION_STATE_KEY |
173 | 206 | ) |
174 | | - quick_pick.show() |
175 | | - }) |
| 207 | + if (last_action) { |
| 208 | + const active = action_items.find((item) => item.label === last_action) |
| 209 | + if (active) action_quick_pick.activeItems = [active] |
| 210 | + } |
176 | 211 |
|
177 | | - if (!selected_commit || selected_commit == 'back') { |
178 | | - if (git_folders.length > 1) { |
| 212 | + const selected_action = await new Promise< |
| 213 | + vscode.QuickPickItem | 'back' | undefined |
| 214 | + >((resolve) => { |
| 215 | + let is_accepted = false |
| 216 | + let did_trigger_back = false |
| 217 | + const disposables: vscode.Disposable[] = [] |
| 218 | + |
| 219 | + disposables.push( |
| 220 | + action_quick_pick.onDidTriggerButton((button) => { |
| 221 | + if (button === vscode.QuickInputButtons.Back) { |
| 222 | + did_trigger_back = true |
| 223 | + action_quick_pick.hide() |
| 224 | + resolve('back') |
| 225 | + } |
| 226 | + }), |
| 227 | + action_quick_pick.onDidAccept(() => { |
| 228 | + is_accepted = true |
| 229 | + resolve(action_quick_pick.selectedItems[0]) |
| 230 | + action_quick_pick.hide() |
| 231 | + }), |
| 232 | + action_quick_pick.onDidHide(() => { |
| 233 | + if (!is_accepted && !did_trigger_back) { |
| 234 | + resolve(undefined) |
| 235 | + } |
| 236 | + disposables.forEach((d) => d.dispose()) |
| 237 | + action_quick_pick.dispose() |
| 238 | + }) |
| 239 | + ) |
| 240 | + action_quick_pick.show() |
| 241 | + }) |
| 242 | + |
| 243 | + if (selected_action == 'back') { |
179 | 244 | continue |
180 | 245 | } |
181 | | - return 'continue' |
182 | | - } |
183 | 246 |
|
184 | | - if (selected_commit) { |
| 247 | + if (!selected_action) { |
| 248 | + return 'continue' |
| 249 | + } |
| 250 | + |
| 251 | + await extension_context.workspaceState.update( |
| 252 | + LAST_SELECTED_COMMIT_REFERENCE_ACTION_STATE_KEY, |
| 253 | + selected_action.label |
| 254 | + ) |
| 255 | + |
| 256 | + const symbol = |
| 257 | + selected_action.label === 'Commit message only' |
| 258 | + ? 'CommitMessage' |
| 259 | + : 'Commit' |
185 | 260 | const message = (selected_commit.detail || '').replace(/"/g, '\\"') |
186 | 261 | return `#${symbol}(${selected_folder.name}:${selected_commit.label} "${message}")` |
187 | 262 | } |
188 | 263 |
|
| 264 | + if (go_back_to_folders) { |
| 265 | + continue |
| 266 | + } |
| 267 | + |
189 | 268 | return 'continue' |
190 | 269 | } |
191 | 270 | } catch (error) { |
|
0 commit comments