|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
| 6 | +import * as dom from '../../../../../base/browser/dom.js'; |
6 | 7 | import { ServicesAccessor } from '../../../../../editor/browser/editorExtensions.js';
|
7 | 8 | import { localize2 } from '../../../../../nls.js';
|
8 | 9 | import { Action2, MenuId, registerAction2 } from '../../../../../platform/actions/common/actions.js';
|
9 | 10 | import { IClipboardService } from '../../../../../platform/clipboard/common/clipboardService.js';
|
10 | 11 | import { CHAT_CATEGORY, stringifyItem } from './chatActions.js';
|
11 |
| -import { IChatWidgetService } from '../chat.js'; |
| 12 | +import { ChatTreeItem, IChatWidgetService } from '../chat.js'; |
12 | 13 | import { ChatContextKeys } from '../../common/chatContextKeys.js';
|
13 |
| -import { IChatRequestViewModel, IChatResponseViewModel, isRequestVM, isResponseVM } from '../../common/chatViewModel.js'; |
| 14 | +import { IChatRequestViewModel, IChatResponseViewModel, isChatTreeItem, isRequestVM, isResponseVM } from '../../common/chatViewModel.js'; |
14 | 15 |
|
15 | 16 | export function registerChatCopyActions() {
|
16 | 17 | registerAction2(class CopyAllAction extends Action2 {
|
@@ -60,15 +61,30 @@ export function registerChatCopyActions() {
|
60 | 61 | });
|
61 | 62 | }
|
62 | 63 |
|
63 |
| - run(accessor: ServicesAccessor, ...args: any[]) { |
64 |
| - const item = args[0]; |
65 |
| - if (!isRequestVM(item) && !isResponseVM(item)) { |
| 64 | + async run(accessor: ServicesAccessor, ...args: any[]) { |
| 65 | + const chatWidgetService = accessor.get(IChatWidgetService); |
| 66 | + const clipboardService = accessor.get(IClipboardService); |
| 67 | + |
| 68 | + const widget = chatWidgetService.lastFocusedWidget; |
| 69 | + let item: ChatTreeItem | undefined = args[0]; |
| 70 | + if (!isChatTreeItem(item)) { |
| 71 | + item = widget?.getFocus(); |
| 72 | + if (!item) { |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // If there is a text selection, and focus is inside the widget, copy the selected text. |
| 78 | + // Otherwise, context menu with no selection -> copy the full item |
| 79 | + const nativeSelection = dom.getActiveWindow().getSelection(); |
| 80 | + const selectedText = nativeSelection?.toString(); |
| 81 | + if (widget && selectedText && selectedText.length > 0 && dom.isAncestor(dom.getActiveElement(), widget.domNode)) { |
| 82 | + await clipboardService.writeText(selectedText); |
66 | 83 | return;
|
67 | 84 | }
|
68 | 85 |
|
69 |
| - const clipboardService = accessor.get(IClipboardService); |
70 | 86 | const text = stringifyItem(item, false);
|
71 |
| - clipboardService.writeText(text); |
| 87 | + await clipboardService.writeText(text); |
72 | 88 | }
|
73 | 89 | });
|
74 | 90 | }
|
0 commit comments