Skip to content

Commit 0f7d99c

Browse files
committed
Enhance context restoration and saving flows by adding edit and delete functionality, improving navigation, and optimizing source selection
- in such case quick pick shoundn't even show-up - Variable 'action_result' is used before being assigned. - once saved, re-open the quick pick so we can see the created entry - in 'save' flow, allow renaming - place number of files before number of tokens
1 parent eb1123c commit 0f7d99c

9 files changed

Lines changed: 423 additions & 52 deletions

File tree

‎apps/editor/src/commands/context-restoration/restore/restore-file-selection-command.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,37 @@ export const restore_file_selection_command = (params: {
1818
while (show_main_menu) {
1919
show_main_menu = false
2020

21-
const source = await select_context_source({
21+
const { source, skipped_menu } = await select_context_source({
2222
extension_context: params.extension_context,
23-
title: t('command.context-restoration.source.title')
23+
title: t('command.context-restoration.source.title'),
24+
mode: 'restore'
2425
})
2526

2627
if (!source) return
2728

29+
let action_result: 'back' | void = undefined
30+
2831
if (source == 'internal') {
29-
const result = await restore_from_workspace_state({
32+
action_result = await restore_from_workspace_state({
3033
workspace_provider: params.workspace_provider,
3134
extension_context: params.extension_context,
32-
on_context_selected: params.on_context_selected
35+
on_context_selected: params.on_context_selected,
36+
show_back_button: !skipped_menu
3337
})
34-
if (result == 'back') show_main_menu = true
3538
} else if (source == 'file') {
36-
const result = await restore_from_json_file({
39+
action_result = await restore_from_json_file({
3740
workspace_provider: params.workspace_provider,
3841
extension_context: params.extension_context,
39-
on_context_selected: params.on_context_selected
42+
on_context_selected: params.on_context_selected,
43+
show_back_button: !skipped_menu
4044
})
41-
if (result == 'back') show_main_menu = true
45+
}
46+
47+
if (action_result == 'back') {
48+
if (skipped_menu) {
49+
return
50+
}
51+
show_main_menu = true
4252
}
4353
}
4454
}

‎apps/editor/src/commands/context-restoration/restore/restore-from-json-file.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const restore_from_json_file = async (params: {
2020
workspace_provider: WorkspaceProvider
2121
extension_context: vscode.ExtensionContext
2222
on_context_selected: () => void
23+
show_back_button?: boolean
2324
}): Promise<'back' | void> => {
2425
try {
2526
let { merged: file_contexts, context_to_roots } =
@@ -69,12 +70,17 @@ export const restore_from_json_file = async (params: {
6970
return items
7071
}
7172

73+
const back_or_close_button =
74+
params.show_back_button !== false
75+
? vscode.QuickInputButtons.Back
76+
: { iconPath: new vscode.ThemeIcon('close'), tooltip: 'Close' }
77+
7278
const quick_pick = vscode.window.createQuickPick<any>()
7379
quick_pick.title = t('command.context-restoration.select-saved.title')
7480
quick_pick.placeholder = t(
7581
'command.context-restoration.select-saved.file'
7682
)
77-
quick_pick.buttons = [vscode.QuickInputButtons.Back]
83+
quick_pick.buttons = [back_or_close_button]
7884
quick_pick.items = await create_items()
7985

8086
if (name_to_highlight) {
@@ -95,7 +101,7 @@ export const restore_from_json_file = async (params: {
95101
}
96102

97103
quick_pick.onDidTriggerButton(async (button) => {
98-
if (button === vscode.QuickInputButtons.Back) {
104+
if (button === back_or_close_button) {
99105
quick_pick.hide()
100106
resolve_once('back')
101107
}

‎apps/editor/src/commands/context-restoration/restore/restore-from-workspace-state.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const restore_from_workspace_state = async (params: {
1919
workspace_provider: WorkspaceProvider
2020
extension_context: vscode.ExtensionContext
2121
on_context_selected: () => void
22+
show_back_button?: boolean
2223
}): Promise<'back' | void> => {
2324
try {
2425
const refresh_contexts = () =>
@@ -70,12 +71,17 @@ export const restore_from_workspace_state = async (params: {
7071
return items
7172
}
7273

74+
const back_or_close_button =
75+
params.show_back_button !== false
76+
? vscode.QuickInputButtons.Back
77+
: { iconPath: new vscode.ThemeIcon('close'), tooltip: 'Close' }
78+
7379
const quick_pick = vscode.window.createQuickPick()
7480
quick_pick.title = t('command.context-restoration.select-saved.title')
7581
quick_pick.placeholder = t(
7682
'command.context-restoration.select-saved.workspace'
7783
)
78-
quick_pick.buttons = [vscode.QuickInputButtons.Back]
84+
quick_pick.buttons = [back_or_close_button]
7985
quick_pick.items = await create_quick_pick_items(internal_contexts)
8086

8187
let active_dialog_count = 0
@@ -90,7 +96,7 @@ export const restore_from_workspace_state = async (params: {
9096

9197
disposables.push(
9298
quick_pick.onDidTriggerButton((button) => {
93-
if (button === vscode.QuickInputButtons.Back) {
99+
if (button === back_or_close_button) {
94100
did_trigger_back = true
95101
quick_pick.hide()
96102
resolve('back')

‎apps/editor/src/commands/context-restoration/save/save-file-selection-command.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,30 @@ export const save_file_selection_command = (params: {
2626
while (show_main_menu) {
2727
show_main_menu = false
2828

29-
const source = await select_context_source({
29+
const { source } = await select_context_source({
3030
extension_context: params.extension_context,
31-
title: t('command.context-restoration.destination.title')
31+
title: t('command.context-restoration.destination.title'),
32+
mode: 'save'
3233
})
3334

3435
if (!source) return
3536

37+
let action_result: 'back' | void = undefined
38+
3639
if (source == 'internal') {
37-
const result = await save_to_workspace_state({
40+
action_result = await save_to_workspace_state({
3841
workspace_provider: params.workspace_provider,
3942
extension_context: params.extension_context
4043
})
41-
if (result == 'back') show_main_menu = true
4244
} else if (source == 'file') {
43-
const result = await save_to_json_file({
45+
action_result = await save_to_json_file({
4446
workspace_provider: params.workspace_provider,
4547
extension_context: params.extension_context
4648
})
47-
if (result == 'back') show_main_menu = true
49+
}
50+
51+
if (action_result == 'back') {
52+
show_main_menu = true
4853
}
4954
}
5055
}

‎apps/editor/src/commands/context-restoration/save/save-to-json-file.ts‎

Lines changed: 151 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,23 @@ export const save_to_json_file = async (params: {
2020
workspace_provider: WorkspaceProvider
2121
extension_context: vscode.ExtensionContext
2222
}): Promise<'back' | void> => {
23-
const { merged: file_contexts, context_to_roots } =
23+
let { merged: file_contexts, context_to_roots } =
2424
await load_and_merge_file_contexts()
2525
const LABEL_SAVE_NEW_CONTEXT = t(
2626
'command.context-restoration.save-new-context.label'
2727
)
2828

29+
const edit_button = {
30+
iconPath: new vscode.ThemeIcon('edit'),
31+
tooltip: t('command.context-restoration.action.rename')
32+
}
33+
const delete_button = {
34+
iconPath: new vscode.ThemeIcon('trash'),
35+
tooltip: t('command.context-restoration.action.delete')
36+
}
37+
38+
let name_to_highlight: string | undefined
39+
2940
while (true) {
3041
const items: any[] = []
3142
items.push({ label: LABEL_SAVE_NEW_CONTEXT })
@@ -46,7 +57,8 @@ export const save_to_json_file = async (params: {
4657
items.push({
4758
label: context.name,
4859
description,
49-
context
60+
context,
61+
buttons: [edit_button, delete_button]
5062
})
5163
}
5264
}
@@ -57,12 +69,38 @@ export const save_to_json_file = async (params: {
5769
quick_pick.items = items
5870
quick_pick.buttons = [vscode.QuickInputButtons.Back]
5971

72+
if (name_to_highlight) {
73+
const active_item = quick_pick.items.find(
74+
(i: any) => i.label == name_to_highlight
75+
)
76+
if (active_item) quick_pick.activeItems = [active_item]
77+
name_to_highlight = undefined
78+
}
79+
6080
const selection = await new Promise<any>((resolve) => {
81+
let is_resolved = false
82+
const resolve_once = (val: any) => {
83+
if (!is_resolved) {
84+
is_resolved = true
85+
resolve(val)
86+
}
87+
}
88+
6189
quick_pick.onDidTriggerButton((btn) => {
62-
if (btn === vscode.QuickInputButtons.Back) resolve('back')
90+
if (btn === vscode.QuickInputButtons.Back) {
91+
quick_pick.hide()
92+
resolve_once('back')
93+
}
94+
})
95+
quick_pick.onDidTriggerItemButton((e) => {
96+
quick_pick.hide()
97+
resolve_once({ ...e.item, triggeredButton: e.button })
6398
})
64-
quick_pick.onDidAccept(() => resolve(quick_pick.selectedItems[0]))
65-
quick_pick.onDidHide(() => resolve(undefined))
99+
quick_pick.onDidAccept(() => {
100+
quick_pick.hide()
101+
resolve_once(quick_pick.selectedItems[0])
102+
})
103+
quick_pick.onDidHide(() => resolve_once(undefined))
66104
quick_pick.show()
67105
})
68106

@@ -71,6 +109,102 @@ export const save_to_json_file = async (params: {
71109
if (!selection) return
72110
if (selection === 'back') return 'back'
73111

112+
if (selection.triggeredButton) {
113+
const item = selection
114+
const old_name = item.context.name
115+
116+
if (selection.triggeredButton === edit_button) {
117+
const input_box = vscode.window.createInputBox()
118+
input_box.title = t('command.context-restoration.rename.title')
119+
input_box.prompt = t('command.context-restoration.rename.prompt')
120+
input_box.value = old_name
121+
const new_name = await new Promise<string | undefined>((resolve) => {
122+
let accepted = false
123+
const disposables: vscode.Disposable[] = []
124+
const validate = (value: string) => {
125+
const trimmed = value.trim()
126+
if (!trimmed) {
127+
input_box.validationMessage = t(
128+
'command.context-restoration.rename.empty'
129+
)
130+
return false
131+
}
132+
if (
133+
file_contexts.find(
134+
(c) => c.name === trimmed && c.name !== old_name
135+
)
136+
) {
137+
input_box.validationMessage = t(
138+
'command.context-restoration.rename.exists'
139+
)
140+
return false
141+
}
142+
input_box.validationMessage = ''
143+
return true
144+
}
145+
disposables.push(
146+
input_box.onDidChangeValue(validate),
147+
input_box.onDidAccept(() => {
148+
if (!validate(input_box.value)) return
149+
accepted = true
150+
resolve(input_box.value.trim())
151+
input_box.hide()
152+
}),
153+
input_box.onDidHide(() => {
154+
if (!accepted) resolve(undefined)
155+
disposables.forEach((d) => d.dispose())
156+
input_box.dispose()
157+
})
158+
)
159+
input_box.show()
160+
})
161+
162+
if (new_name && new_name !== old_name) {
163+
const roots_to_update = context_to_roots.get(old_name) || []
164+
for (const root of roots_to_update) {
165+
const p = get_contexts_file_path(root)
166+
let contexts = load_contexts_from_file(p)
167+
let changed = false
168+
contexts = contexts.map((c) => {
169+
if (c.name === old_name) {
170+
changed = true
171+
return { ...c, name: new_name }
172+
}
173+
return c
174+
})
175+
if (changed) await save_contexts_to_file({ contexts, file_path: p })
176+
}
177+
const reloaded = await load_and_merge_file_contexts()
178+
file_contexts = reloaded.merged
179+
context_to_roots = reloaded.context_to_roots
180+
}
181+
name_to_highlight =
182+
new_name && new_name != old_name ? new_name : old_name
183+
} else if (selection.triggeredButton === delete_button) {
184+
const choice = await vscode.window.showInformationMessage(
185+
t('command.context-restoration.delete.prompt', { name: old_name }),
186+
{ modal: true },
187+
t('command.context-restoration.delete.action')
188+
)
189+
190+
if (choice == t('command.context-restoration.delete.action')) {
191+
const roots_to_update = context_to_roots.get(old_name) || []
192+
for (const root of roots_to_update) {
193+
const p = get_contexts_file_path(root)
194+
let contexts = load_contexts_from_file(p)
195+
const original_len = contexts.length
196+
contexts = contexts.filter((c) => c.name !== old_name)
197+
if (contexts.length !== original_len)
198+
await save_contexts_to_file({ contexts, file_path: p })
199+
}
200+
const reloaded = await load_and_merge_file_contexts()
201+
file_contexts = reloaded.merged
202+
context_to_roots = reloaded.context_to_roots
203+
}
204+
}
205+
continue
206+
}
207+
74208
const checked_files = params.workspace_provider.get_checked_files()
75209
const files_by_workspace = group_files_by_workspace(checked_files)
76210

@@ -111,7 +245,12 @@ export const save_to_json_file = async (params: {
111245
vscode.window.showInformationMessage(
112246
dictionary.information_message.CONTEXT_SAVED_SUCCESSFULLY
113247
)
114-
return
248+
249+
const reloaded = await load_and_merge_file_contexts()
250+
file_contexts = reloaded.merged
251+
context_to_roots = reloaded.context_to_roots
252+
253+
continue
115254
}
116255

117256
if (selection.context) {
@@ -159,7 +298,12 @@ export const save_to_json_file = async (params: {
159298
vscode.window.showInformationMessage(
160299
dictionary.information_message.CONTEXT_UPDATED_SUCCESSFULLY
161300
)
162-
return
301+
302+
const reloaded = await load_and_merge_file_contexts()
303+
file_contexts = reloaded.merged
304+
context_to_roots = reloaded.context_to_roots
305+
306+
continue
163307
} else {
164308
continue
165309
}

0 commit comments

Comments
 (0)