Skip to content

Commit 276404b

Browse files
committed
Implement the duplicateWorkspace command to save current context state checked files/websites before duplicating the workspace and restore this state in the new window
1 parent 4431b5a commit 276404b

5 files changed

Lines changed: 112 additions & 9 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as vscode from 'vscode'
2+
import { WorkspaceProvider } from '../context/providers/workspace-provider'
3+
import { WebsitesProvider } from '../context/providers/websites-provider'
4+
import {
5+
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY,
6+
type DuplicateWorkspaceContext
7+
} from '../constants/state-keys'
8+
9+
export function duplicate_workspace_command(
10+
workspace_provider: WorkspaceProvider,
11+
websites_provider: WebsitesProvider,
12+
context: vscode.ExtensionContext
13+
) {
14+
return vscode.commands.registerCommand(
15+
'codeWebChat.duplicateWorkspace',
16+
async () => {
17+
const checked_files = workspace_provider.get_all_checked_paths()
18+
const checked_websites = websites_provider
19+
.get_checked_websites()
20+
.map((w) => w.url)
21+
const workspace_root_folders =
22+
vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath) ??
23+
[]
24+
25+
const context_to_save: DuplicateWorkspaceContext = {
26+
checked_files,
27+
checked_websites,
28+
workspace_root_folders,
29+
timestamp: Date.now()
30+
}
31+
32+
await context.globalState.update(
33+
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY,
34+
context_to_save
35+
)
36+
37+
vscode.commands.executeCommand(
38+
'workbench.action.duplicateWorkspaceInNewWindow'
39+
)
40+
}
41+
)
42+
}

‎packages/vscode/src/commands/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './apply-chat-response-command'
22
export * from './close-all-editors-command'
33
export * from './checkpoints-command'
44
export * from './close-editor-command'
5+
export * from './duplicate-workspace-command'
56
export * from './code-completion-commands'
67
export * from './delete-command'
78
export * from './new-file-command'

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export const LAST_REFACTOR_INSTRUCTION_STATE_KEY = 'last-refactor-instruction'
3131
export const CHECKPOINTS_STATE_KEY = 'checkpoints'
3232
export const TEMPORARY_CHECKPOINT_STATE_KEY = 'temporary-checkpoint'
3333

34+
export const DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY =
35+
'duplicate-workspace-context'
36+
3437
export const CHAT_EDIT_FORMAT_STATE_KEY = 'chat-edit-format'
3538
export const API_EDIT_FORMAT_STATE_KEY = 'api-edit-format'
3639
export const WEB_MODE_STATE_KEY = 'web-mode'
@@ -67,6 +70,13 @@ export const get_last_selected_preset_key = (web_mode: string) =>
6770
export const get_last_selected_group_state_key = (web_mode: string) =>
6871
`last-selected-group-${web_mode}`
6972

73+
export interface DuplicateWorkspaceContext {
74+
checked_files: string[]
75+
checked_websites: string[]
76+
timestamp: number
77+
workspace_root_folders: string[]
78+
}
79+
7080
export interface HistoryEntry {
7181
text: string
7282
createdAt: number

‎packages/vscode/src/context/context-initialization.ts‎

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { marked } from 'marked'
99
import { EventEmitter } from 'events'
1010
import { apply_context_command } from '../commands/apply-context-command/apply-context-command'
1111
import { dictionary } from '@shared/constants/dictionary'
12+
import {
13+
CONTEXT_CHECKED_PATHS_STATE_KEY,
14+
CONTEXT_CHECKED_URLS_STATE_KEY,
15+
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY,
16+
type DuplicateWorkspaceContext
17+
} from '../constants/state-keys'
1218
import { ContextProvider } from './providers/context-provider'
1319

1420
export const token_count_emitter = new EventEmitter()
@@ -20,13 +26,60 @@ const round_token_count_for_badge = (count: number): number => {
2026
return Math.floor(count / 1000) * 1000
2127
}
2228

23-
export const context_initialization = (
29+
const restore_duplicated_workspace_context = async (
2430
context: vscode.ExtensionContext
25-
): {
31+
) => {
32+
const duplicated_context = context.globalState.get<DuplicateWorkspaceContext>(
33+
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY
34+
)
35+
36+
if (duplicated_context?.timestamp) {
37+
const now = Date.now()
38+
const age = now - duplicated_context.timestamp
39+
40+
if (age <= 60000) {
41+
const current_workspace_folders =
42+
vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath) ??
43+
[]
44+
45+
const sorted_current_folders = [...current_workspace_folders].sort()
46+
const sorted_saved_folders = [
47+
...(duplicated_context.workspace_root_folders ?? [])
48+
].sort()
49+
50+
const are_workspaces_the_same =
51+
sorted_current_folders.length == sorted_saved_folders.length &&
52+
sorted_current_folders.every(
53+
(value, index) => value == sorted_saved_folders[index]
54+
)
55+
if (are_workspaces_the_same) {
56+
await context.workspaceState.update(
57+
CONTEXT_CHECKED_PATHS_STATE_KEY,
58+
duplicated_context.checked_files
59+
)
60+
await context.workspaceState.update(
61+
CONTEXT_CHECKED_URLS_STATE_KEY,
62+
duplicated_context.checked_websites
63+
)
64+
}
65+
}
66+
67+
await context.globalState.update(
68+
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY,
69+
undefined
70+
)
71+
}
72+
}
73+
74+
export const context_initialization = async (
75+
context: vscode.ExtensionContext
76+
): Promise<{
2677
workspace_provider: WorkspaceProvider
2778
open_editors_provider: OpenEditorsProvider
2879
websites_provider: WebsitesProvider
29-
} => {
80+
}> => {
81+
await restore_duplicated_workspace_context(context)
82+
3083
let was_above_threshold = false
3184

3285
const workspace_folders = vscode.workspace.workspaceFolders ?? []

‎packages/vscode/src/extension.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
new_file_command,
1818
open_file_from_workspace_command,
1919
new_folder_command,
20+
duplicate_workspace_command,
2021
rename_command,
2122
delete_command,
2223
save_context_command,
@@ -31,7 +32,7 @@ let websocket_server_instance: WebSocketManager | null = null
3132

3233
export async function activate(context: vscode.ExtensionContext) {
3334
const { workspace_provider, open_editors_provider, websites_provider } =
34-
context_initialization(context)
35+
await context_initialization(context)
3536

3637
websocket_server_instance = new WebSocketManager(context, websites_provider)
3738

@@ -89,11 +90,7 @@ export async function activate(context: vscode.ExtensionContext) {
8990
delete_command(),
9091
save_context_command(workspace_provider, context),
9192
...checkpoints_command(workspace_provider, context),
92-
vscode.commands.registerCommand('codeWebChat.duplicateWorkspace', () => {
93-
vscode.commands.executeCommand(
94-
'workbench.action.duplicateWorkspaceInNewWindow'
95-
)
96-
}),
93+
duplicate_workspace_command(workspace_provider, websites_provider, context),
9794
open_url_command({
9895
command: 'codeWebChat.openRepository',
9996
url: 'https://github.com/robertpiosik/CodeWebChat'

0 commit comments

Comments
 (0)