@@ -12,8 +12,21 @@ import { localize } from 'vs/nls';
1212import { Action2 , MenuId , registerAction2 } from 'vs/platform/actions/common/actions' ;
1313import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService' ;
1414import { INTERACTIVE_SESSION_CATEGORY } from 'vs/workbench/contrib/interactiveSession/browser/actions/interactiveSessionActions' ;
15+ import { interactiveSessionResponseHasProviderId } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionContextKeys' ;
16+ import { IInteractiveSessionService , IInteractiveSessionUserActionEvent } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionService' ;
17+ import { IInteractiveResponseViewModel } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionViewModel' ;
1518import { IEditorService } from 'vs/workbench/services/editor/common/editorService' ;
1619
20+ export interface IInteractiveSessionCodeBlockActionContext {
21+ code : string ;
22+ codeBlockIndex : number ;
23+ element : IInteractiveResponseViewModel ;
24+ }
25+
26+ function isCodeBlockActionContext ( thing : unknown ) : thing is IInteractiveSessionCodeBlockActionContext {
27+ return typeof thing === 'object' && thing !== null && 'code' in thing && 'element' in thing ;
28+ }
29+
1730export function registerInteractiveSessionCodeBlockActions ( ) {
1831 registerAction2 ( class CopyCodeBlockAction extends Action2 {
1932 constructor ( ) {
@@ -28,19 +41,30 @@ export function registerInteractiveSessionCodeBlockActions() {
2841 icon : Codicon . copy ,
2942 menu : {
3043 id : MenuId . InteractiveSessionCodeBlock ,
44+ when : interactiveSessionResponseHasProviderId ,
3145 group : 'navigation' ,
3246 }
3347 } ) ;
3448 }
3549
3650 run ( accessor : ServicesAccessor , ...args : any [ ] ) {
37- const code = args [ 0 ] ;
38- if ( typeof code !== 'string' ) {
51+ const context = args [ 0 ] ;
52+ if ( ! isCodeBlockActionContext ( context ) ) {
3953 return ;
4054 }
4155
4256 const clipboardService = accessor . get ( IClipboardService ) ;
43- clipboardService . writeText ( code ) ;
57+ clipboardService . writeText ( context . code ) ;
58+
59+ const interactiveSessionService = accessor . get ( IInteractiveSessionService ) ;
60+ interactiveSessionService . notifyUserAction ( < IInteractiveSessionUserActionEvent > {
61+ providerId : context . element . providerId ,
62+ action : {
63+ kind : 'copy' ,
64+ responseId : context . element . providerResponseId ,
65+ codeBlockIndex : context . codeBlockIndex ,
66+ }
67+ } ) ;
4468 }
4569 } ) ;
4670
@@ -56,18 +80,20 @@ export function registerInteractiveSessionCodeBlockActions() {
5680 category : INTERACTIVE_SESSION_CATEGORY ,
5781 menu : {
5882 id : MenuId . InteractiveSessionCodeBlock ,
83+ when : interactiveSessionResponseHasProviderId ,
5984 }
6085 } ) ;
6186 }
6287
6388 async run ( accessor : ServicesAccessor , ...args : any [ ] ) {
64- const code = args [ 0 ] ;
65- if ( typeof code !== 'string' ) {
89+ const context = args [ 0 ] ;
90+ if ( ! isCodeBlockActionContext ( context ) ) {
6691 return ;
6792 }
6893
6994 const editorService = accessor . get ( IEditorService ) ;
7095 const bulkEditService = accessor . get ( IBulkEditService ) ;
96+ const interactiveSessionService = accessor . get ( IInteractiveSessionService ) ;
7197 const activeEditorControl = editorService . activeTextEditorControl ;
7298 if ( isCodeEditor ( activeEditorControl ) ) {
7399 const activeModel = activeEditorControl . getModel ( ) ;
@@ -78,9 +104,18 @@ export function registerInteractiveSessionCodeBlockActions() {
78104 const activeSelection = activeEditorControl . getSelection ( ) ?? new Range ( activeModel . getLineCount ( ) , 1 , activeModel . getLineCount ( ) , 1 ) ;
79105 await bulkEditService . apply ( [ new ResourceTextEdit ( activeModel . uri , {
80106 range : activeSelection ,
81- text : code ,
107+ text : context . code ,
82108 insertAsSnippet : true ,
83109 } ) ] ) ;
110+
111+ interactiveSessionService . notifyUserAction ( < IInteractiveSessionUserActionEvent > {
112+ providerId : context . element . providerId ,
113+ action : {
114+ kind : 'insert' ,
115+ responseId : context . element . providerResponseId ,
116+ codeBlockIndex : context . codeBlockIndex ,
117+ }
118+ } ) ;
84119 }
85120 }
86121 } ) ;
0 commit comments