Skip to content

Commit 8b9efcb

Browse files
committed
Add sync button to update saved contexts with currently selected files
1 parent c4d6c66 commit 8b9efcb

2 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎packages/vscode/src/commands/apply-context-command/sources/json-file-source.ts‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export const handle_json_file_source = async (
7272
await load_and_merge_file_contexts()
7373
const workspace_folders = vscode.workspace.workspaceFolders || []
7474

75+
const sync_button = {
76+
iconPath: new vscode.ThemeIcon('sync'),
77+
tooltip: 'Update with currently selected files'
78+
}
79+
7580
const edit_button = {
7681
iconPath: new vscode.ThemeIcon('edit'),
7782
tooltip: 'Rename'
@@ -81,7 +86,7 @@ export const handle_json_file_source = async (
8186
tooltip: 'Delete'
8287
}
8388
const open_file_button = {
84-
iconPath: new vscode.ThemeIcon('file'),
89+
iconPath: new vscode.ThemeIcon('go-to-file'),
8590
tooltip: 'Open contexts.json'
8691
}
8792

@@ -123,7 +128,7 @@ export const handle_json_file_source = async (
123128
label: context.name,
124129
description,
125130
context: context,
126-
buttons: [edit_button, delete_button]
131+
buttons: [sync_button, edit_button, delete_button]
127132
})
128133
}
129134
}
@@ -224,6 +229,53 @@ export const handle_json_file_source = async (
224229
const item = selection
225230
const old_name = item.context.name
226231

232+
if (selection.triggeredButton === sync_button) {
233+
const context_name = item.context.name
234+
const checked_files = workspace_provider.get_checked_files()
235+
const files_by_workspace = group_files_by_workspace(checked_files)
236+
237+
const current_roots = context_to_roots.get(context_name) || []
238+
const all_roots = new Set([
239+
...current_roots,
240+
...files_by_workspace.keys()
241+
])
242+
243+
for (const root of all_roots) {
244+
const files = files_by_workspace.get(root) || []
245+
const p = get_contexts_file_path(root)
246+
247+
if (files.length === 0 && !fs.existsSync(p)) continue
248+
249+
if (files.length > 0) {
250+
const dir = path.dirname(p)
251+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
252+
}
253+
254+
let contexts = load_contexts_from_file(p)
255+
contexts = contexts.filter((c) => c.name !== context_name)
256+
257+
if (files.length > 0) {
258+
const condensed_paths = condense_paths(
259+
files,
260+
root,
261+
workspace_provider
262+
)
263+
const relative_paths = condensed_paths.map((p) =>
264+
p.replace(/\\/g, '/')
265+
)
266+
contexts.unshift({
267+
name: context_name,
268+
paths: relative_paths
269+
})
270+
}
271+
await save_contexts_to_file(contexts, p)
272+
}
273+
vscode.window.showInformationMessage(
274+
dictionary.information_message.CONTEXT_SAVED_SUCCESSFULLY
275+
)
276+
return
277+
}
278+
227279
if (selection.triggeredButton === edit_button) {
228280
active_dialog_count++
229281
const input = vscode.window.createInputBox()

‎packages/vscode/src/commands/apply-context-command/sources/workspace-state-source.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export const handle_workspace_state_source = async (
3535
workspace_root
3636
)
3737

38+
const sync_button = {
39+
iconPath: new vscode.ThemeIcon('sync'),
40+
tooltip: 'Update with currently selected files'
41+
}
42+
3843
const edit_button = {
3944
iconPath: new vscode.ThemeIcon('edit'),
4045
tooltip: 'Rename'
@@ -60,7 +65,7 @@ export const handle_workspace_state_source = async (
6065
})
6166

6267
contexts.forEach((context, index) => {
63-
const buttons = [edit_button, delete_button]
68+
const buttons = [sync_button, edit_button, delete_button]
6469

6570
const description = `${context.paths.length} ${
6671
context.paths.length == 1 ? 'path' : 'paths'
@@ -205,6 +210,43 @@ export const handle_workspace_state_source = async (
205210
index: number
206211
}
207212

213+
if (event.button === sync_button) {
214+
const context = item.context
215+
const checked_files = workspace_provider.get_checked_files()
216+
const files_by_workspace = group_files_by_workspace(checked_files)
217+
let all_prefixed_paths: string[] = []
218+
219+
for (const [root, files] of files_by_workspace.entries()) {
220+
if (files.length === 0) continue
221+
const condensed_paths = condense_paths(
222+
files,
223+
root,
224+
workspace_provider
225+
)
226+
const relative_paths = condensed_paths.map((p) =>
227+
p.replace(/\\/g, '/')
228+
)
229+
const prefixed_paths = add_workspace_prefix(relative_paths, root)
230+
all_prefixed_paths = [...all_prefixed_paths, ...prefixed_paths]
231+
}
232+
all_prefixed_paths.sort((a, b) => a.localeCompare(b))
233+
234+
const updated_contexts = internal_contexts.map((c) =>
235+
c.name === context.name ? { ...c, paths: all_prefixed_paths } : c
236+
)
237+
save_contexts_for_workspace(
238+
extension_context,
239+
workspace_root,
240+
updated_contexts
241+
)
242+
vscode.window.showInformationMessage(
243+
dictionary.information_message.CONTEXT_SAVED_SUCCESSFULLY
244+
)
245+
quick_pick.hide()
246+
resolve(undefined)
247+
return
248+
}
249+
208250
if (event.button === edit_button) {
209251
active_dialog_count++
210252
const input_box = vscode.window.createInputBox()

0 commit comments

Comments
 (0)