Skip to content

Commit 127b49e

Browse files
committed
Remove the redundant codeWebChat.undo command and integrate its functionality directly into the ViewProvider to centralize undo logic within the webview panel
1 parent 0181625 commit 127b49e

10 files changed

Lines changed: 111 additions & 131 deletions

File tree

‎packages/vscode/package.json‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@
251251
"command": "codeWebChat.delete",
252252
"title": "Delete"
253253
},
254-
{
255-
"command": "codeWebChat.undo",
256-
"title": "Undo Last Changes",
257-
"category": "Code Web Chat"
258-
},
259254
{
260255
"command": "codeWebChat.referenceInChat",
261256
"title": "Reference in Chat",
@@ -281,14 +276,6 @@
281276
],
282277
"menus": {
283278
"commandPalette": [
284-
{
285-
"command": "codeWebChat.applyChatResponse",
286-
"when": "false"
287-
},
288-
{
289-
"command": "codeWebChat.undo",
290-
"when": "false"
291-
},
292279
{
293280
"command": "codeWebChat.clearChecks",
294281
"when": "false"

‎packages/vscode/src/commands/apply-chat-response-command/apply-chat-response-command.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ export const apply_chat_response_command = (
732732
const editor = vscode.window.activeTextEditor
733733
if (editor) {
734734
const choice = await vscode.window.showWarningMessage(
735-
'No valid code blocks found in the clipboard text. Apply it to the active editor with the Intelligent Update API tool?',
735+
'No valid code blocks found in the clipboard text. Apply found text to the active editor with the Intelligent Update API tool?',
736736
{ modal: true },
737737
'Apply'
738738
)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export * from './open-url-command'
1111
export * from './reference-in-chat-command'
1212
export * from './feedback-command'
1313
export * from './rename-command'
14-
export * from './undo-command'
1514
export * from './save-all-command'
1615
export * from './save-context-command'
1716
export * from './apply-context-command'

‎packages/vscode/src/commands/undo-command.ts‎

Lines changed: 0 additions & 92 deletions
This file was deleted.

‎packages/vscode/src/extension.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
rename_command,
2121
delete_command,
2222
save_context_command,
23-
undo_command,
2423
reference_in_chat_command,
2524
open_url_command,
2625
feedback_command,
@@ -69,7 +68,6 @@ export async function activate(context: vscode.ExtensionContext) {
6968
),
7069
reference_in_chat_command(view_provider, workspace_provider),
7170
apply_chat_response_command(context, view_provider, workspace_provider),
72-
undo_command(context, view_provider.set_undo_button_state),
7371
...code_completion_commands(
7472
workspace_provider,
7573
open_editors_provider,

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
} from './message-handlers'
5656
import {
5757
API_EDIT_FORMAT_STATE_KEY,
58+
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
5859
API_MODE_STATE_KEY,
5960
CHAT_EDIT_FORMAT_STATE_KEY,
6061
INSTRUCTIONS_ASK_STATE_KEY,
@@ -64,23 +65,26 @@ import {
6465
get_last_group_or_preset_choice_state_key,
6566
get_last_selected_group_state_key,
6667
get_last_selected_preset_key,
68+
LAST_APPLIED_CHANGES_STATE_KEY,
69+
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
6770
LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY,
6871
LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY,
6972
RECENT_DONATIONS_VISIBLE_STATE_KEY,
7073
WEB_MODE_STATE_KEY
7174
} from '@/constants/state-keys'
72-
import { can_undo } from '@/commands/undo-command'
7375
import {
7476
config_preset_to_ui_format,
7577
ConfigPresetFormat
7678
} from '@/views/panel/backend/utils/preset-format-converters'
79+
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
7780
import { CHATBOTS } from '@shared/constants/chatbots'
7881
import { HOME_VIEW_TYPES, HomeViewType } from '../types/home-view-type'
7982
import { ApiMode, WebMode } from '@shared/types/modes'
8083
import { code_review_promise_resolve } from '@/commands/apply-chat-response-command/utils/review'
8184
import { Logger } from '@shared/utils/logger'
8285
import { CancelTokenSource } from 'axios'
8386
import { update_last_used_preset_or_group } from './message-handlers/update-last-used-preset-or-group'
87+
import { undo_files } from '@/commands/apply-chat-response-command/utils/file-operations'
8488
import { dictionary } from '@shared/constants/dictionary'
8589

8690
export class ViewProvider implements vscode.WebviewViewProvider {
@@ -363,6 +367,8 @@ export class ViewProvider implements vscode.WebviewViewProvider {
363367
await handle_duplicate_preset(this, message, webview_view)
364368
} else if (message.command == 'CREATE_PRESET') {
365369
await handle_create_preset(this)
370+
} else if (message.command == 'UNDO') {
371+
await this._handle_undo()
366372
} else if (message.command == 'EXECUTE_COMMAND') {
367373
await vscode.commands.executeCommand(message.command_id)
368374
} else if (message.command == 'EDIT_CONTEXT') {
@@ -479,7 +485,7 @@ export class ViewProvider implements vscode.WebviewViewProvider {
479485
setTimeout(() => {
480486
this.send_message({
481487
command: 'CAN_UNDO_CHANGED',
482-
can_undo: can_undo(this.context)
488+
can_undo: this._can_undo()
483489
})
484490
}, 1000)
485491
}
@@ -736,6 +742,81 @@ export class ViewProvider implements vscode.WebviewViewProvider {
736742
})
737743
}
738744

745+
private _can_undo(): boolean {
746+
const original_states = this.context.workspaceState.get<
747+
OriginalFileState[]
748+
>(LAST_APPLIED_CHANGES_STATE_KEY)
749+
return !!original_states && original_states.length > 0
750+
}
751+
752+
private async _handle_undo(): Promise<void> {
753+
const context = this.context
754+
const original_states = context.workspaceState.get<OriginalFileState[]>(
755+
LAST_APPLIED_CHANGES_STATE_KEY
756+
)
757+
const editor_state = context.workspaceState.get<
758+
| {
759+
file_path: string
760+
position: { line: number; character: number }
761+
}
762+
| undefined
763+
>(LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY)
764+
765+
if (!original_states || original_states.length == 0) {
766+
vscode.window.showInformationMessage(
767+
dictionary.information_message.NO_RECENT_CHANGES_TO_UNDO
768+
)
769+
return
770+
}
771+
772+
try {
773+
const success = await undo_files({ original_states })
774+
775+
if (!success) {
776+
return
777+
}
778+
779+
if (editor_state) {
780+
try {
781+
const uri = vscode.Uri.file(editor_state.file_path)
782+
const document = await vscode.workspace.openTextDocument(uri)
783+
const editor = await vscode.window.showTextDocument(document, {
784+
preview: false
785+
})
786+
const position = new vscode.Position(
787+
editor_state.position.line,
788+
editor_state.position.character
789+
)
790+
editor.selection = new vscode.Selection(position, position)
791+
editor.revealRange(
792+
new vscode.Range(position, position),
793+
vscode.TextEditorRevealType.InCenter
794+
)
795+
} catch (error) {
796+
console.error('Error restoring editor state:', error)
797+
}
798+
}
799+
800+
context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, null)
801+
context.workspaceState.update(
802+
LAST_APPLIED_CHANGES_EDITOR_STATE_STATE_KEY,
803+
null
804+
)
805+
context.workspaceState.update(
806+
LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY,
807+
null
808+
)
809+
this.set_undo_button_state(false)
810+
} catch (error: any) {
811+
console.error('Error during undo:', error)
812+
vscode.window.showErrorMessage(
813+
dictionary.error_message.FAILED_TO_UNDO_CHANGES(
814+
error.message || 'Unknown error'
815+
)
816+
)
817+
}
818+
}
819+
739820
private _get_html_for_webview(webview: vscode.Webview) {
740821
const resources_uri = webview.asWebviewUri(
741822
vscode.Uri.joinPath(this.extension_uri, 'resources')

‎packages/vscode/src/views/panel/frontend/Main/Main.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,12 @@ export const Main: React.FC<Props> = (props) => {
548548
})
549549
}
550550

551+
const handle_undo_click = () => {
552+
post_message(props.vscode, {
553+
command: 'UNDO'
554+
})
555+
}
556+
551557
const instructions =
552558
current_mode == 'ask'
553559
? props.ask_instructions
@@ -618,6 +624,7 @@ export const Main: React.FC<Props> = (props) => {
618624
on_create_preset={handle_create_preset}
619625
on_quick_action_click={handle_quick_action_click}
620626
on_commit_click={handle_commit_click}
627+
on_undo_click={handle_undo_click}
621628
has_active_editor={props.has_active_editor}
622629
has_active_selection={props.has_active_selection}
623630
has_changes_to_commit={has_changes_to_commit}

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.module.scss‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
height: 24px;
4545
padding: 0 var(--padding-5px);
4646
border-radius: var(--border-radius-4px);
47-
text-decoration: none;
4847
color: var(--vscode-foreground);
49-
letter-spacing: -0.5px;
5048

5149
&:disabled {
5250
opacity: var(--disabled-opacity);

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Props = {
3737
on_curly_braces_click: () => void
3838
on_quick_action_click: (command: string) => void
3939
on_commit_click: () => void
40+
on_undo_click: () => void
4041
is_connected: boolean
4142
presets: Preset[]
4243
configurations: ApiToolConfiguration[]
@@ -193,10 +194,6 @@ export const MainView: React.FC<Props> = (props) => {
193194
}, 500)
194195
}, [props.can_undo])
195196

196-
const handle_undo_click = () => {
197-
props.on_quick_action_click('codeWebChat.undo')
198-
}
199-
200197
const handle_commit_click = () => {
201198
if (!props.has_changes_to_commit) return
202199

@@ -425,32 +422,32 @@ export const MainView: React.FC<Props> = (props) => {
425422
</a>
426423
</div>
427424
<div className={styles.footer__right}>
425+
{props.home_view_type == HOME_VIEW_TYPES.WEB && (
426+
<button
427+
className={cn(
428+
styles.footer__button,
429+
styles['footer__button--outlined']
430+
)}
431+
onClick={handle_apply_click}
432+
title={'Integrate copied chat response or a single code block'}
433+
disabled={is_apply_disabled_temporarily}
434+
>
435+
Apply
436+
</button>
437+
)}
428438
<button
429439
className={cn(
430440
styles.footer__button,
431441
styles['footer__button--outlined']
432442
)}
433-
onClick={handle_undo_click}
443+
onClick={props.on_undo_click}
434444
title={
435445
'Restore saved state of the codebase after chat/API response integration'
436446
}
437447
disabled={!props.can_undo}
438448
>
439449
Undo
440450
</button>
441-
{props.home_view_type == HOME_VIEW_TYPES.WEB && (
442-
<button
443-
className={cn(
444-
styles.footer__button,
445-
styles['footer__button--outlined']
446-
)}
447-
onClick={handle_apply_click}
448-
title={'Integrate copied message or a code block'}
449-
disabled={is_apply_disabled_temporarily}
450-
>
451-
Apply clipboard
452-
</button>
453-
)}
454451
<button
455452
className={cn(
456453
styles.footer__button,

0 commit comments

Comments
 (0)