Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClaudeCodeControl.UserInput.cs
More file actions
865 lines (758 loc) · 37 KB
/
Copy pathClaudeCodeControl.UserInput.cs
File metadata and controls
865 lines (758 loc) · 37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/* *******************************************************************************************************************
* Application: ClaudeCodeExtension
*
* Autor: Daniel Carvalho Liedke / Claude Code
*
* Copyright © Daniel Carvalho Liedke 2026
* Usage and reproduction in any manner whatsoever without the written permission of Daniel Carvalho Liedke is strictly forbidden.
*
* Purpose: User input handling - keyboard events, send button, and prompt submission
*
* *******************************************************************************************************************/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Microsoft.VisualStudio.Shell;
namespace ClaudeCodeVS
{
public partial class ClaudeCodeControl
{
#region Prompt History Fields
/// <summary>
/// Current index in the prompt history (-1 means not navigating history)
/// </summary>
private int _historyIndex = -1;
/// <summary>
/// Temporary storage for current text when navigating history
/// </summary>
private string _tempCurrentText = string.Empty;
/// <summary>
/// Temporary storage for current attached file paths when navigating history
/// </summary>
private List<string> _tempCurrentFiles = new List<string>();
/// <summary>
/// Maximum number of prompts to keep in history
/// </summary>
private const int MaxHistorySize = 50;
/// <summary>
/// Re-entrancy guard for prompt submission. A single send takes ~2 seconds (focus +
/// paste delays) and the prompt/attachments aren't cleared until it finishes, so a second
/// click or Enter during that window would re-send the same prompt (and re-attach the same
/// files). Set synchronously at the very top of SendButton_Click before any await and reset
/// in finally, so a concurrent UI-thread invocation is rejected. See issue #63.
/// </summary>
private bool _isSendingPrompt;
#endregion
#region Send Button and Prompt Submission
/// <summary>
/// Handles send button click - sends the prompt to the terminal
/// </summary>
#pragma warning disable VSTHRD100 // Avoid async void methods
private async void SendButton_Click(object sender, RoutedEventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
{
// Re-entrancy guard: reject a second click/Enter while a send is already in flight.
// Checked and set synchronously before any await (UI thread), so a concurrent
// invocation can't re-send the same prompt and re-attach the same files. See issue #63.
if (_isSendingPrompt)
{
return;
}
try
{
string prompt = PromptTextBox.Text.Trim();
bool hasFiles = attachedImagePaths.Any();
// Allow sending if there's text OR attached files
if (string.IsNullOrEmpty(prompt) && !hasFiles)
{
MessageBox.Show("Please enter a prompt.", "No Prompt", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
_isSendingPrompt = true;
if (SendPromptButton != null) SendPromptButton.IsEnabled = false;
StringBuilder fullPrompt = new StringBuilder();
// Check if CURRENTLY RUNNING provider is WSL-based (not CodexNative, CursorAgentNative).
// Hoisted out of the hasFiles branch so the large-prompt-as-file path can use it too.
bool isWSLProvider = _currentRunningProvider == AiProvider.Codex ||
_currentRunningProvider == AiProvider.ClaudeCodeWSL ||
_currentRunningProvider == AiProvider.CursorAgent ||
_currentRunningProvider == AiProvider.Windsurf;
// If files are attached, include their paths in the prompt
if (hasFiles)
{
// Create a unique directory under ClaudeCodeVS_Session for this prompt with files
string promptDirectory = null;
try
{
promptDirectory = Path.Combine(Path.GetTempPath(), "ClaudeCodeVS_Session", Guid.NewGuid().ToString());
Directory.CreateDirectory(promptDirectory);
}
catch (Exception ex)
{
Debug.WriteLine($"Error creating temp directory: {ex.Message}");
promptDirectory = null;
}
fullPrompt.AppendLine("Files attached:");
foreach (string filePath in attachedImagePaths)
{
try
{
string displayPath;
// Try to copy file to temp directory for persistence
if (promptDirectory != null && File.Exists(filePath))
{
string fileName = Path.GetFileName(filePath);
string tempPath = GetUniquePromptAttachmentPath(promptDirectory, fileName);
File.Copy(filePath, tempPath, false);
displayPath = isWSLProvider ? ConvertToWslPath(tempPath) : tempPath;
}
else
{
// Use original path if copy fails or file doesn't exist
displayPath = isWSLProvider ? ConvertToWslPath(filePath) : filePath;
}
fullPrompt.AppendLine($" - {displayPath}");
Debug.WriteLine($"File attached to prompt: {filePath}");
}
catch (Exception ex)
{
// Always include the file path even if copy fails
Debug.WriteLine($"Error processing file {filePath}: {ex.Message}");
try
{
string displayPath = isWSLProvider ? ConvertToWslPath(filePath) : filePath;
fullPrompt.AppendLine($" - {displayPath}");
}
catch
{
// Last resort: use the raw path
fullPrompt.AppendLine($" - {filePath}");
}
}
}
fullPrompt.AppendLine();
}
// Add user's prompt text (if any)
if (!string.IsNullOrEmpty(prompt))
{
fullPrompt.AppendLine(prompt);
}
// Add to prompt history (before clearing) - only if there's text
if (!string.IsNullOrEmpty(prompt))
{
AddToPromptHistory(prompt, attachedImagePaths.ToList());
}
// Ensure tracking is active and reset baseline before sending prompt
await EnsureDiffTrackingStartedAsync(false);
// Auto-open changes view if enabled and project is in git
if (_settings != null && _settings.AutoOpenChangesOnPrompt && !string.IsNullOrEmpty(_gitRepositoryRoot))
{
await AutoOpenChangesViewAsync();
}
// Send to terminal
string finalPrompt = fullPrompt.ToString();
string textToSend = finalPrompt;
// "Disable clipboard" mode (issue #61): never touch the clipboard. Always write the
// prompt to a temp file and inject only a short reference via simulated keystrokes, so
// an app holding the clipboard can't break the send. Only available with conhost
// (Command Prompt) — Windows Terminal (_wtTabBarHeight > 0) doesn't accept the posted
// WM_CHAR keystrokes, so fall back to the normal clipboard paste path there.
// PI is excluded: its TUI reacts to per-character WM_CHAR by flooding the input with
// cursor-position responses and crashing (issue #82), so it must use the clipboard
// paste path regardless — the prompt is still written to a file so the pasted
// reference stays short.
bool disableClipboardRequested = _settings != null
&& _settings.DisableClipboardSend
&& _wtTabBarHeight == 0;
bool isPiProvider = _currentRunningProvider == AiProvider.Pi;
// Reasonix is a TUI like PI — per-character keystrokes flood it, so keep it on the
// clipboard/WM_COMMAND paste path even when "Disable clipboard" is on.
bool isReasonix = _currentRunningProvider == AiProvider.Reasonix;
bool clipboardFree = disableClipboardRequested && !isPiProvider && !isReasonix;
// Save the prompt to a temp file and send only a short reference when either:
// • "Disable clipboard" is on (always, so the keystroke/paste payload stays short), or
// • "Send large prompts as file" is on and the prompt exceeds the ~1 KB conhost
// paste-buffer threshold (avoids front-truncation of large pastes, see issue #48).
// In both cases the "Files attached:" list is preserved by living inside the file.
const int LargePromptThresholdChars = 1024;
bool writeToFile = !string.IsNullOrEmpty(finalPrompt)
&& (disableClipboardRequested
|| (_settings != null
&& _settings.SendLargePromptsAsFile
&& finalPrompt.Length > LargePromptThresholdChars));
if (writeToFile)
{
try
{
string sessionDir = Path.Combine(Path.GetTempPath(), "ClaudeCodeVS_Session", Guid.NewGuid().ToString());
Directory.CreateDirectory(sessionDir);
string promptFile = Path.Combine(sessionDir, $"prompt-{DateTime.Now:yyyyMMdd-HHmmss}.md");
File.WriteAllText(promptFile, finalPrompt, new UTF8Encoding(false));
string displayPath = isWSLProvider ? ConvertToWslPath(promptFile) : promptFile;
textToSend = $"Read and follow: {displayPath}";
Debug.WriteLine($"Prompt ({finalPrompt.Length} chars) saved to: {promptFile}");
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to save prompt to file, falling back to inline send: {ex.Message}");
// Fall back to inline send (keystrokes or paste, depending on mode)
textToSend = finalPrompt;
}
}
Debug.WriteLine($"Sending prompt to terminal ({textToSend.Length} chars): {textToSend.Substring(0, Math.Min(200, textToSend.Length))}...");
if (clipboardFree)
{
await SendTextViaKeystrokesAsync(textToSend);
}
else
{
await SendTextToTerminalAsync(textToSend);
}
// Clear prompt and images
PromptTextBox.Clear();
ClearAttachedImages();
// Reset image counter after sending prompt
imageCounter = 1;
// Reset history navigation
_historyIndex = -1;
_tempCurrentText = string.Empty;
// Refresh inline usage bars (throttled internally)
_ = RefreshInlineUsageAsync();
// Arm the "On Agent Finish" watcher (Claude Code only; no-op when disabled)
_ = ArmAgentCompletionWatcherAsync();
}
catch (Exception ex)
{
MessageBox.Show($"Error sending prompt: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
// Always release the re-entrancy guard and re-enable the button, even on error.
_isSendingPrompt = false;
if (SendPromptButton != null) SendPromptButton.IsEnabled = true;
}
}
#endregion
#region Keyboard Input Handling
/// <summary>
/// Handles KeyDown event for the prompt textbox.
/// When Send-with-Enter is enabled, Enter sends the prompt;
/// Shift+Enter or Ctrl+Enter inserts a newline.
/// When disabled, Enter inserts a newline (default TextBox behavior).
/// </summary>
private void PromptTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && _settings?.SendWithEnter != false)
{
// Plain Enter sends the prompt (modifier cases handled in PreviewKeyDown)
e.Handled = true;
SendButton_Click(sender, null);
}
}
/// <summary>
/// Handles PreviewKeyDown event for the prompt textbox
/// Catches Enter before TextBox processes it, and handles Ctrl+V for image paste, Ctrl+Up/Down for history
/// </summary>
private void PromptTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
// Force cursor visible — "Hide pointer while typing" calls SetCursor(NULL) which WPF
// only counteracts via WM_SETCURSOR (i.e. on mouse move). While typing non-stop with
// the mouse stationary, WM_SETCURSOR never fires, so we must call SetCursor directly.
SetCursor(LoadCursor(IntPtr.Zero, new IntPtr(IDC_IBEAM)));
// Note prompt typing so the "On Agent Finish" watcher pauses its console read (its
// AttachConsole can bounce focus out of the prompt mid-keystroke), and keep the WPF
// focus guard alive so the cross-process terminal can't steal focus while the user
// types the next prompt during active generation.
_lastPromptKeyUtc = DateTime.UtcNow;
EnsureWpfPromptFocusGuardRunning();
// When the "@" file/folder picker is open, let it consume navigation/commit keys
// (Up/Down/Enter/Tab/Esc) before history navigation or send-on-Enter runs.
if (HandleAtMentionKey(e)) return;
// Handle Ctrl+Up/Down for prompt history navigation
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (e.Key == Key.Up)
{
NavigateHistoryUp();
e.Handled = true;
return;
}
else if (e.Key == Key.Down)
{
NavigateHistoryDown();
e.Handled = true;
return;
}
}
if (e.Key == Key.Enter)
{
bool sendWithEnter = _settings?.SendWithEnter != false;
bool sendWithCtrlEnter = _settings?.SendWithCtrlEnter == true;
bool shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
bool ctrl = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
if (sendWithEnter)
{
if (shift || ctrl)
{
// Shift+Enter or Ctrl+Enter: insert newline at caret
int caret = PromptTextBox.CaretIndex;
PromptTextBox.SelectedText = "\n";
PromptTextBox.CaretIndex = caret + 1;
e.Handled = true;
return;
}
// Plain Enter: send prompt
e.Handled = true;
SendButton_Click(sender, null);
return;
}
if (sendWithCtrlEnter && ctrl)
{
// Ctrl+Enter sends; plain/Shift+Enter fall through to the default newline.
// Guards against accidentally sending an incomplete prompt with a stray Enter.
e.Handled = true;
SendButton_Click(sender, null);
return;
}
// Plain Enter (and Shift+Enter): let TextBox insert a newline by default.
}
// Preserve paste-image shortcut even with new behavior
if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (TryPasteImage())
{
e.Handled = true;
}
}
}
#endregion
#region Prompt Font Zoom
/// <summary>
/// Handles Ctrl+Scroll on the prompt textbox to increase/decrease font size
/// </summary>
private void PromptTextBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control)
{
double newSize = PromptTextBox.FontSize + (e.Delta > 0 ? 1 : -1);
newSize = Math.Max(8, Math.Min(24, newSize));
PromptTextBox.FontSize = newSize;
if (_settings != null)
{
_settings.PromptFontSize = newSize;
SaveSettings();
}
e.Handled = true;
}
}
#endregion
#region Prompt History Navigation
/// <summary>
/// Adds a prompt to the history and saves settings
/// </summary>
/// <param name="prompt">The prompt text to add</param>
/// <param name="filePaths">The file paths attached to this prompt</param>
private void AddToPromptHistory(string prompt, List<string> filePaths)
{
if (string.IsNullOrWhiteSpace(prompt))
return;
// Ensure settings and history are initialized
if (_settings == null)
_settings = new ClaudeCodeSettings();
if (_settings.PromptHistory == null)
_settings.PromptHistory = new System.Collections.Generic.List<PromptHistoryEntry>();
// Remove duplicate if it exists (same text)
_settings.PromptHistory.RemoveAll(e => e.Text == prompt);
// Add to end (most recent)
_settings.PromptHistory.Add(new PromptHistoryEntry
{
Text = prompt,
FilePaths = filePaths != null ? new List<string>(filePaths) : new List<string>()
});
// Keep only the last MaxHistorySize items
if (_settings.PromptHistory.Count > MaxHistorySize)
{
_settings.PromptHistory.RemoveAt(0);
}
// Save to settings file
SaveSettings();
}
/// <summary>
/// Navigates up in the prompt history (to older prompts)
/// </summary>
private void NavigateHistoryUp()
{
if (_settings?.PromptHistory == null || _settings.PromptHistory.Count == 0)
return;
// First time navigating? Save current text and files
if (_historyIndex == -1)
{
_tempCurrentText = PromptTextBox.Text;
_tempCurrentFiles = attachedImagePaths.ToList();
_historyIndex = _settings.PromptHistory.Count;
}
// Move to previous item (if possible)
if (_historyIndex > 0)
{
_historyIndex--;
var entry = _settings.PromptHistory[_historyIndex];
PromptTextBox.Text = entry.Text;
PromptTextBox.SelectionStart = PromptTextBox.Text.Length;
RestoreFilesFromHistory(entry.FilePaths);
}
}
/// <summary>
/// Navigates down in the prompt history (to newer prompts)
/// </summary>
private void NavigateHistoryDown()
{
if (_settings?.PromptHistory == null || _historyIndex == -1)
return;
// Move to next item
_historyIndex++;
// If we've gone past the end, restore the temp text and files
if (_historyIndex >= _settings.PromptHistory.Count)
{
PromptTextBox.Text = _tempCurrentText;
RestoreFilesFromHistory(_tempCurrentFiles);
_historyIndex = -1;
_tempCurrentText = string.Empty;
_tempCurrentFiles = new List<string>();
}
else
{
var entry = _settings.PromptHistory[_historyIndex];
PromptTextBox.Text = entry.Text;
RestoreFilesFromHistory(entry.FilePaths);
}
PromptTextBox.SelectionStart = PromptTextBox.Text.Length;
}
private static string GetUniquePromptAttachmentPath(string promptDirectory, string fileName)
{
string safeFileName = string.IsNullOrWhiteSpace(fileName) ? "attachment" : fileName;
string candidate = Path.Combine(promptDirectory, safeFileName);
if (!File.Exists(candidate))
{
return candidate;
}
string nameWithoutExtension = Path.GetFileNameWithoutExtension(safeFileName);
string extension = Path.GetExtension(safeFileName);
for (int index = 2; ; index++)
{
candidate = Path.Combine(promptDirectory, $"{nameWithoutExtension}_{index}{extension}");
if (!File.Exists(candidate))
{
return candidate;
}
}
}
/// <summary>
/// Clears the prompt history
/// </summary>
private void ClearPromptHistory()
{
if (_settings == null)
_settings = new ClaudeCodeSettings();
_settings.PromptHistory?.Clear();
_historyIndex = -1;
_tempCurrentText = string.Empty;
_tempCurrentFiles = new List<string>();
SaveSettings();
MessageBox.Show("Prompt history cleared.", "History Cleared", MessageBoxButton.OK, MessageBoxImage.Information);
}
/// <summary>
/// Handles context menu click to clear prompt history
/// </summary>
private void ClearPromptHistoryMenuItem_Click(object sender, RoutedEventArgs e)
{
ClearPromptHistory();
}
#endregion
#region Editor Selection Integration
/// <summary>
/// Language identifier mapping from file extensions to markdown code fence language IDs
/// </summary>
private static readonly Dictionary<string, string> _languageMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ ".cs", "csharp" }, { ".vb", "vb" }, { ".fs", "fsharp" },
{ ".py", "python" }, { ".js", "javascript" }, { ".ts", "typescript" },
{ ".jsx", "jsx" }, { ".tsx", "tsx" },
{ ".java", "java" }, { ".kt", "kotlin" }, { ".scala", "scala" },
{ ".cpp", "cpp" }, { ".cc", "cpp" }, { ".cxx", "cpp" },
{ ".c", "c" }, { ".h", "c" }, { ".hpp", "cpp" },
{ ".go", "go" }, { ".rs", "rust" }, { ".swift", "swift" },
{ ".rb", "ruby" }, { ".php", "php" }, { ".lua", "lua" },
{ ".r", "r" }, { ".m", "objectivec" }, { ".mm", "objectivec" },
{ ".html", "html" }, { ".htm", "html" }, { ".css", "css" },
{ ".scss", "scss" }, { ".less", "less" }, { ".sass", "sass" },
{ ".xml", "xml" }, { ".xaml", "xml" }, { ".json", "json" },
{ ".yaml", "yaml" }, { ".yml", "yaml" }, { ".toml", "toml" },
{ ".sql", "sql" }, { ".sh", "bash" }, { ".bash", "bash" },
{ ".ps1", "powershell" }, { ".psm1", "powershell" },
{ ".bat", "batch" }, { ".cmd", "batch" },
{ ".md", "markdown" }, { ".rst", "rst" },
{ ".dart", "dart" }, { ".ex", "elixir" }, { ".exs", "elixir" },
{ ".zig", "zig" }, { ".nim", "nim" }, { ".v", "v" },
};
/// <summary>
/// Gets the markdown language identifier for a file extension
/// </summary>
private static string GetLanguageIdFromExtension(string extension)
{
if (string.IsNullOrEmpty(extension))
return string.Empty;
return _languageMap.TryGetValue(extension, out string langId) ? langId : string.Empty;
}
private static bool TryGetRelativePathUnderDirectory(string fullPath, string directory, out string relativePath)
{
relativePath = null;
if (string.IsNullOrEmpty(fullPath) || string.IsNullOrEmpty(directory))
{
return false;
}
try
{
string normalizedFullPath = Path.GetFullPath(fullPath);
string normalizedDirectory = Path.GetFullPath(directory)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) +
Path.DirectorySeparatorChar;
if (!normalizedFullPath.StartsWith(normalizedDirectory, StringComparison.OrdinalIgnoreCase))
{
return false;
}
relativePath = normalizedFullPath.Substring(normalizedDirectory.Length);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Handles the grab selection toolbar button click.
/// Gets the current editor selection and inserts it into the prompt.
/// </summary>
private void GrabSelectionButton_Click(object sender, RoutedEventArgs e)
{
try
{
ThreadHelper.ThrowIfNotOnUIThread();
var dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
if (dte?.ActiveDocument == null)
{
MessageBox.Show("No active document open in the editor.",
"No Document", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var selection = dte.ActiveDocument.Selection as EnvDTE.TextSelection;
if (selection == null || string.IsNullOrEmpty(selection.Text))
{
MessageBox.Show("No text selected in the active editor.\nPlease select some code first.",
"No Selection", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
string code = selection.Text;
string filePath = dte.ActiveDocument.FullName;
int startLine = selection.TopLine;
int endLine = selection.BottomLine;
InsertCodeSnippetIntoPrompt(code, filePath, startLine, endLine);
}
catch (Exception ex)
{
Debug.WriteLine($"Error grabbing editor selection: {ex.Message}");
}
}
/// <summary>
/// Inserts a formatted code snippet into the prompt text box without sending.
/// Called from the toolbar button and the editor context menu command.
/// </summary>
public void InsertCodeSnippetIntoPrompt(string code, string filePath, int startLine, int endLine)
{
try
{
// Make path relative to workspace if possible
string displayPath = filePath;
if (TryGetRelativePathUnderDirectory(filePath, _lastWorkspaceDirectory, out string relativePath))
{
displayPath = relativePath;
}
// Get language identifier from file extension
string extension = Path.GetExtension(filePath);
string langId = GetLanguageIdFromExtension(extension);
// Build the formatted snippet
var snippet = new StringBuilder();
// Add separator if prompt already has text
string currentText = PromptTextBox.Text;
if (!string.IsNullOrEmpty(currentText) && !currentText.EndsWith("\n") && !currentText.EndsWith("\r"))
{
snippet.AppendLine();
}
// File header with line info
if (startLine == endLine)
{
snippet.AppendLine($"File: {displayPath} (line {startLine})");
}
else
{
snippet.AppendLine($"File: {displayPath} (lines {startLine}-{endLine})");
}
// Code fence with language (skipped when reference-only mode is on)
if (_settings?.SendSelectionReferenceOnly != true)
{
snippet.AppendLine($"```{langId}");
snippet.AppendLine(code.TrimEnd('\r', '\n'));
snippet.AppendLine("```");
snippet.AppendLine();
}
// Insert at current cursor position or append
int caretIndex = PromptTextBox.CaretIndex;
if (caretIndex >= 0 && caretIndex < currentText.Length && !string.IsNullOrEmpty(currentText))
{
PromptTextBox.Text = currentText.Insert(caretIndex, snippet.ToString());
PromptTextBox.CaretIndex = caretIndex + snippet.Length;
}
else
{
PromptTextBox.Text = currentText + snippet.ToString();
PromptTextBox.CaretIndex = PromptTextBox.Text.Length;
}
// Focus the prompt for the user to type their question
PromptTextBox.Focus();
}
catch (Exception ex)
{
Debug.WriteLine($"Error inserting code snippet: {ex.Message}");
}
}
/// <summary>
/// Maintainer toggle for the optional "📥 Paste from Clipboard" entry in the attach
/// dropdown menu. When false (default) the entry is hidden in <see cref="ClaudeCodeControl"/>'s
/// constructor. Flip this to true to expose the feature to users.
///
/// The entry pastes short clipboard text inline into the prompt, but for clipboard
/// content above the conhost truncation threshold it saves the text to a temp file
/// and attaches it instead — a workaround for the conhost INPUT_RECORD buffer
/// overflow that drops the front of large pastes (see issue #48).
/// </summary>
private const bool EnablePasteFromClipboardMenu = false;
/// <summary>
/// Handles the "Paste from Clipboard" menu item.
/// Short clipboard text is inserted into the prompt textbox at the caret;
/// large text is written to a temp file and attached (same path as the
/// regular Attach File flow), avoiding conhost paste-buffer truncation.
/// </summary>
private void PasteFromClipboardMenuItem_Click(object sender, RoutedEventArgs e)
{
try
{
ThreadHelper.ThrowIfNotOnUIThread();
// 1) File-drop list: clipboard holds a list of file paths (e.g. files copied
// from Explorer). Attach them directly without copying.
if (Clipboard.ContainsFileDropList())
{
var files = Clipboard.GetFileDropList();
int added = 0;
foreach (string path in files)
{
if (!string.IsNullOrEmpty(path) && (File.Exists(path) || Directory.Exists(path)))
{
attachedImagePaths.Add(path);
added++;
}
}
if (added > 0)
{
UpdateImageDropDisplay();
PromptTextBox.Focus();
Debug.WriteLine($"Attached {added} file(s) from clipboard file drop list");
return;
}
}
// 2) Image content: reuse the existing image-paste pipeline which saves the
// bitmap as PNG into the temp image directory and attaches it.
// TryPasteImage() intentionally skips when text is also present (Excel etc.),
// so falls through to the text branch below in that case.
if (TryPasteImage())
{
PromptTextBox.Focus();
return;
}
// 3) Text content: small text goes inline at the caret, large text is saved
// to a temp file and attached (avoids conhost paste-buffer truncation).
if (Clipboard.ContainsText())
{
string clipboardText;
try
{
clipboardText = Clipboard.GetText();
}
catch (Exception ex)
{
MessageBox.Show($"Could not read clipboard: {ex.Message}",
"Clipboard Error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (string.IsNullOrEmpty(clipboardText))
{
MessageBox.Show("Clipboard text is empty.",
"Nothing to Paste", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
// Same threshold as SendButton_Click — below it inline pastes are safe,
// above it conhost would truncate the front.
const int LargePasteThresholdChars = 1024;
if (clipboardText.Length <= LargePasteThresholdChars)
{
// Short paste: insert at caret position.
string currentText = PromptTextBox.Text ?? string.Empty;
int caretIndex = PromptTextBox.CaretIndex;
if (caretIndex >= 0 && caretIndex < currentText.Length && !string.IsNullOrEmpty(currentText))
{
PromptTextBox.Text = currentText.Insert(caretIndex, clipboardText);
PromptTextBox.CaretIndex = caretIndex + clipboardText.Length;
}
else
{
PromptTextBox.Text = currentText + clipboardText;
PromptTextBox.CaretIndex = PromptTextBox.Text.Length;
}
PromptTextBox.Focus();
return;
}
// Large paste: write to a session temp file and attach it.
try
{
string sessionDir = Path.Combine(Path.GetTempPath(), "ClaudeCodeVS_Session", Guid.NewGuid().ToString());
Directory.CreateDirectory(sessionDir);
string pasteFile = Path.Combine(sessionDir, $"paste-{DateTime.Now:yyyyMMdd-HHmmss}.txt");
File.WriteAllText(pasteFile, clipboardText, new UTF8Encoding(false));
attachedImagePaths.Add(pasteFile);
UpdateImageDropDisplay();
PromptTextBox.Focus();
Debug.WriteLine($"Pasted clipboard ({clipboardText.Length} chars) attached as file: {pasteFile}");
}
catch (Exception ex)
{
MessageBox.Show($"Could not save clipboard to file: {ex.Message}",
"Paste Failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
return;
}
// 4) Nothing usable.
MessageBox.Show("Clipboard does not contain text, an image, or a file list.",
"Nothing to Paste", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
Debug.WriteLine($"Error in PasteFromClipboardMenuItem_Click: {ex.Message}");
}
}
#endregion
}
}
You can’t perform that action at this time.
