44
55using System ;
66using System . Composition ;
7- using System . Diagnostics . CodeAnalysis ;
87using System . Threading ;
98using System . Threading . Tasks ;
109using System . Windows ;
1615using Microsoft . CodeAnalysis ;
1716using Microsoft . CodeAnalysis . Editor . Host ;
1817using Microsoft . CodeAnalysis . Editor . Shared . Utilities ;
19- using Microsoft . CodeAnalysis . ErrorReporting ;
2018using Microsoft . CodeAnalysis . Host ;
2119using Microsoft . CodeAnalysis . Host . Mef ;
2220using Microsoft . CodeAnalysis . Navigation ;
@@ -59,8 +57,6 @@ internal sealed partial class SemanticSearchToolWindowImpl(
5957 VisualStudioWorkspace workspace ,
6058 IStreamingFindUsagesPresenter resultsPresenter ,
6159 ITextUndoHistoryRegistry undoHistoryRegistry ,
62- ISemanticSearchCopilotService copilotService ,
63- Lazy < ISemanticSearchCopilotUIProvider > copilotUIProvider , // lazy to avoid loading Microsoft.VisualStudio.LanguageServices.ExternalAccess.Copilot
6460 IVsService < SVsUIShell , IVsUIShell > vsUIShellProvider ) : IDisposable
6561{
6662 private const int ToolBarHeight = 26 ;
@@ -120,9 +116,7 @@ private async Task<FrameworkElement> CreateContentAsync(CancellationToken cancel
120116
121117 var vsUIShell = await vsUIShellProvider . GetValueAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
122118
123- var copilotUI = CreateCopilotUI ( ) ;
124-
125- var textViewHost = CreateTextViewHost ( vsUIShell , copilotUI ) ;
119+ var textViewHost = CreateTextViewHost ( vsUIShell ) ;
126120 var textViewControl = textViewHost . HostControl ;
127121 _textView = textViewHost . TextView ;
128122 _textBuffer = textViewHost . TextView . TextBuffer ;
@@ -167,11 +161,6 @@ private async Task<FrameworkElement> CreateContentAsync(CancellationToken cancel
167161
168162 toolWindowGrid . Children . Add ( toolbarGrid ) ;
169163
170- if ( copilotUI != null )
171- {
172- toolWindowGrid . Children . Add ( copilotUI . Control ) ;
173- }
174-
175164 toolWindowGrid . Children . Add ( textViewControl ) ;
176165 toolbarGrid . Children . Add ( executeButton ) ;
177166 toolbarGrid . Children . Add ( cancelButton ) ;
@@ -181,12 +170,6 @@ private async Task<FrameworkElement> CreateContentAsync(CancellationToken cancel
181170 Grid . SetRow ( toolbarGrid , 0 ) ;
182171 Grid . SetColumn ( toolbarGrid , 0 ) ;
183172
184- if ( copilotUI != null )
185- {
186- Grid . SetRow ( copilotUI . Control , 1 ) ;
187- Grid . SetColumn ( copilotUI . Control , 0 ) ;
188- }
189-
190173 Grid . SetRow ( textViewControl , 2 ) ;
191174 Grid . SetColumn ( textViewControl , 0 ) ;
192175
@@ -205,106 +188,6 @@ private async Task<FrameworkElement> CreateContentAsync(CancellationToken cancel
205188 return toolWindowGrid ;
206189 }
207190
208- private CopilotUI ? CreateCopilotUI ( )
209- {
210- if ( ! copilotUIProvider . Value . IsAvailable || ! copilotService . IsAvailable )
211- {
212- return null ;
213- }
214-
215- if ( ! globalOptions . GetOption ( SemanticSearchFeatureFlag . PromptEnabled ) )
216- {
217- return null ;
218- }
219-
220- var outerGrid = new Grid ( )
221- {
222- Background = ( Brush ) Application . Current . FindResource ( CommonControlsColors . TextBoxBackgroundBrushKey ) ,
223- } ;
224-
225- ImageThemingUtilities . SetImageBackgroundColor ( outerGrid , ( Color ) Application . Current . Resources [ CommonDocumentColors . PageBackgroundColorKey ] ) ;
226- ThemedDialogStyleLoader . SetUseDefaultThemedDialogStyles ( outerGrid , true ) ;
227-
228- // [ prompt border | empty ]
229- outerGrid . ColumnDefinitions . Add ( new ColumnDefinition { Width = GridLength . Auto } ) ;
230- outerGrid . ColumnDefinitions . Add ( new ColumnDefinition { Width = GridLength . Auto } ) ;
231-
232- var promptGrid = new Grid ( ) ;
233-
234- // [ input | panel ]
235- promptGrid . ColumnDefinitions . Add ( new ColumnDefinition { MaxWidth = 600 , Width = GridLength . Auto } ) ;
236- promptGrid . ColumnDefinitions . Add ( new ColumnDefinition { Width = GridLength . Auto } ) ;
237-
238- var promptTextBox = copilotUIProvider . Value . GetTextBox ( ) ;
239-
240- var panel = new StackPanel ( )
241- {
242- Orientation = Orientation . Horizontal ,
243- HorizontalAlignment = HorizontalAlignment . Left ,
244- VerticalAlignment = VerticalAlignment . Bottom ,
245- Margin = new Thickness ( 8 , 8 , 0 , 8 ) ,
246- } ;
247-
248- Grid . SetColumn ( promptTextBox . Control , 0 ) ;
249- promptGrid . Children . Add ( promptTextBox . Control ) ;
250-
251- Grid . SetColumn ( panel , 1 ) ;
252- promptGrid . Children . Add ( panel ) ;
253-
254- var promptGridBorder = new Border
255- {
256- Name = "PromptBorder" ,
257- BorderBrush = ( Brush ) Application . Current . Resources [ EnvironmentColors . SystemHighlightBrushKey ] ,
258- BorderThickness = new Thickness ( 1 ) ,
259- Child = promptGrid
260- } ;
261-
262- Grid . SetColumn ( promptGridBorder , 0 ) ;
263- outerGrid . Children . Add ( promptGridBorder ) ;
264-
265- // ComboBox for model selection
266- var modelPicker = new ComboBox
267- {
268- SelectedIndex = 0 ,
269- HorizontalAlignment = HorizontalAlignment . Right ,
270- VerticalAlignment = VerticalAlignment . Top ,
271- Margin = new Thickness ( 4 , 0 , 4 , 0 ) ,
272- Height = 24 ,
273- IsEditable = false ,
274- IsReadOnly = true ,
275- BorderThickness = new Thickness ( 0 ) ,
276- MinHeight = 24 ,
277- VerticalContentAlignment = VerticalAlignment . Top ,
278- TabIndex = 1 ,
279- Style = ( Style ) Application . Current . FindResource ( VsResourceKeys . ComboBoxStyleKey )
280- } ;
281-
282- modelPicker . Items . Add ( "gpt-4o" ) ;
283- modelPicker . Items . Add ( "gpt-4o-mini" ) ;
284- modelPicker . Items . Add ( "o1" ) ;
285- modelPicker . Items . Add ( "o1-ga" ) ;
286- modelPicker . Items . Add ( "o1-mini" ) ;
287-
288- panel . Children . Add ( modelPicker ) ;
289-
290- var submitButton = CreateButton (
291- KnownMonikers . Send ,
292- automationName : "Generate query" ,
293- acceleratorKey : "Ctrl+Enter" ,
294- toolTip : "Generate query" ) ;
295-
296- panel . Children . Add ( submitButton ) ;
297-
298- submitButton . Click += ( _ , _ ) => SubmitCopilotQuery ( promptTextBox . Text , modelPicker . Text ) ;
299-
300- return new CopilotUI ( )
301- {
302- Control = outerGrid ,
303- Input = promptTextBox ,
304- ModelPicker = modelPicker ,
305- } ;
306- }
307-
308191 private static Button CreateButton (
309192 Imaging . Interop . ImageMoniker moniker ,
310193 string automationName ,
@@ -383,7 +266,7 @@ private static ControlTemplate CreateButtonTemplate()
383266 """ , context ) ;
384267 }
385268
386- private IWpfTextViewHost CreateTextViewHost ( IVsUIShell vsUIShell , CopilotUI ? copilotUI )
269+ private IWpfTextViewHost CreateTextViewHost ( IVsUIShell vsUIShell )
387270 {
388271 Contract . ThrowIfFalse ( threadingContext . JoinableTaskContext . IsOnMainThread ) ;
389272
@@ -422,7 +305,7 @@ private IWpfTextViewHost CreateTextViewHost(IVsUIShell vsUIShell, CopilotUI? cop
422305
423306 ErrorHandler . ThrowOnFailure ( windowFrame . SetProperty ( ( int ) __VSFPROPID . VSFPROPID_ViewHelper , textViewAdapter ) ) ;
424307
425- _ = new CommandFilter ( this , textViewAdapter , copilotUI ) ;
308+ _ = new CommandFilter ( this , textViewAdapter ) ;
426309
427310 return textViewHost ;
428311 }
@@ -448,62 +331,6 @@ private void UpdateUIState()
448331 _cancelButton . IsEnabled = isExecuting ;
449332 }
450333
451- private void SubmitCopilotQuery ( string input , string model )
452- {
453- Contract . ThrowIfFalse ( threadingContext . JoinableTaskContext . IsOnMainThread ) ;
454- Contract . ThrowIfNull ( _textBuffer ) ;
455- Contract . ThrowIfNull ( copilotService ) ;
456-
457- // TODO: hook up cancel button for copilot queries
458- var cancellationSource = new CancellationTokenSource ( ) ;
459-
460- // TODO: fade out current content and show overlay spinner
461-
462- var completionToken = _asyncListener . BeginAsyncOperation ( nameof ( SemanticSearchToolWindow ) + "." + nameof ( SubmitCopilotQuery ) ) ;
463- _ = ExecuteAsync ( cancellationSource . Token ) . ReportNonFatalErrorAsync ( ) . CompletesAsyncOperation ( completionToken ) ;
464-
465- async Task ExecuteAsync ( CancellationToken cancellationToken )
466- {
467- await TaskScheduler . Default ;
468-
469- SemanticSearchCopilotGeneratedQuery query ;
470-
471- // TODO: generate list from SemanticSearch.ReferenceAssemblies:
472- var codeAnalysisVersion = new Version ( 4 , 14 , 0 ) ;
473- var sdkVersion = new Version ( 9 , 0 , 0 ) ;
474-
475- var context = new SemanticSearchCopilotContext ( )
476- {
477- ModelName = model ,
478- AvailablePackages =
479- [
480- ( "Microsoft.CodeAnalysis" , codeAnalysisVersion ) ,
481- ( "Microsoft.CodeAnalysis.CSharp" , codeAnalysisVersion ) ,
482- ( "System.Collections.Immutable" , sdkVersion ) ,
483- ( "System.Collections" , sdkVersion ) ,
484- ( "System.Linq" , sdkVersion ) ,
485- ( "System.Runtime" , sdkVersion ) ,
486- ]
487- } ;
488-
489- try
490- {
491- query = await copilotService . TryGetQueryAsync ( input , context , cancellationToken ) . ConfigureAwait ( false ) ;
492- }
493- catch ( Exception e ) when ( FatalError . ReportAndPropagateUnlessCanceled ( e , cancellationToken , ErrorSeverity . Critical ) )
494- {
495- return ;
496- }
497- catch ( OperationCanceledException )
498- {
499- return ;
500- }
501-
502- await threadingContext . JoinableTaskFactory . SwitchToMainThreadAsync ( CancellationToken . None ) ;
503- SetEditorText ( query . Text ) ;
504- }
505- }
506-
507334 /// <summary>
508335 /// Replaces current text buffer content with specified <paramref name="text"/>. Allow using Ctrl+Z to revert to the previous content.
509336 /// Must be invoked on UI thread.
@@ -613,36 +440,19 @@ public NavigableLocation GetNavigableLocation(TextSpan textSpan)
613440 return true ;
614441 } ) ;
615442
616- private sealed class CopilotUI
617- {
618- public required FrameworkElement Control { get ; init ; }
619- public required ITextBoxControl Input { get ; init ; }
620- public required ComboBox ModelPicker { get ; init ; }
621- }
622-
623443 private sealed class CommandFilter : IOleCommandTarget
624444 {
625445 private readonly SemanticSearchToolWindowImpl _window ;
626446 private readonly IOleCommandTarget _editorCommandTarget ;
627- private readonly CopilotUI ? _copilotUI ;
628447
629- public CommandFilter ( SemanticSearchToolWindowImpl window , IVsTextView textView , CopilotUI ? copilotUI )
448+ public CommandFilter ( SemanticSearchToolWindowImpl window , IVsTextView textView )
630449 {
631450 _window = window ;
632- _copilotUI = copilotUI ;
633451 ErrorHandler . ThrowOnFailure ( textView . AddCommandFilter ( this , out _editorCommandTarget ) ) ;
634452 }
635453
636- [ MemberNotNullWhen ( true , nameof ( _copilotUI ) ) ]
637- private bool HasCopilotInputFocus
638- => _copilotUI ? . Input . View . HasAggregateFocus == true ;
639-
640454 public int QueryStatus ( ref Guid pguidCmdGroup , uint cCmds , OLECMD [ ] prgCmds , IntPtr pCmdText )
641- {
642- var target = HasCopilotInputFocus ? _copilotUI . Input . CommandTarget : _editorCommandTarget ;
643-
644- return target . QueryStatus ( ref pguidCmdGroup , cCmds , prgCmds , pCmdText ) ;
645- }
455+ => _editorCommandTarget . QueryStatus ( ref pguidCmdGroup , cCmds , prgCmds , pCmdText ) ;
646456
647457 public int Exec ( ref Guid pguidCmdGroup , uint nCmdID , uint nCmdexecopt , IntPtr pvaIn , IntPtr pvaOut )
648458 {
@@ -651,12 +461,6 @@ public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pv
651461 switch ( ( VSConstants . VSStd2KCmdID ) nCmdID )
652462 {
653463 case VSConstants . VSStd2KCmdID . OPENLINEABOVE :
654- if ( HasCopilotInputFocus )
655- {
656- _window . SubmitCopilotQuery ( _copilotUI . Input . Text , _copilotUI . ModelPicker . Text ) ;
657- return VSConstants . S_OK ;
658- }
659-
660464 if ( ! _window . IsExecutingUIState ( ) )
661465 {
662466 _window . RunQuery ( ) ;
@@ -676,8 +480,7 @@ public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pv
676480 }
677481 }
678482
679- var target = HasCopilotInputFocus ? _copilotUI . Input . CommandTarget : _editorCommandTarget ;
680- return target . Exec ( ref pguidCmdGroup , nCmdID , nCmdexecopt , pvaIn , pvaOut ) ;
483+ return _editorCommandTarget . Exec ( ref pguidCmdGroup , nCmdID , nCmdexecopt , pvaIn , pvaOut ) ;
681484 }
682485 }
683486}
0 commit comments