@@ -78,58 +78,66 @@ public static async Task<CommandCompletion> GetCompletionsAsync(
7878 IScriptPosition cursorPosition = s_clonePositionWithNewOffset ( scriptAst . Extent . StartScriptPosition , fileOffset ) ;
7979 Stopwatch stopwatch = new ( ) ;
8080 logger . LogTrace ( $ "Getting completions at offset { fileOffset } (line: { cursorPosition . LineNumber } , column: { cursorPosition . ColumnNumber } )") ;
81-
82- CommandCompletion commandCompletion = await executionService . ExecuteDelegateAsync (
83- representation : "CompleteInput" ,
84- new ExecutionOptions { Priority = ExecutionPriority . Next } ,
85- ( pwsh , _ ) =>
86- {
87- stopwatch . Start ( ) ;
88-
89- // If the current runspace is not out of process, then we call TabExpansion2 so
90- // that we have the ability to issue pipeline stop requests on cancellation.
91- if ( executionService is PsesInternalHost psesInternalHost
92- && ! psesInternalHost . Runspace . RunspaceIsRemote )
93- {
94- IReadOnlyList < CommandCompletion > completionResults = new SynchronousPowerShellTask < CommandCompletion > (
95- logger ,
96- psesInternalHost ,
97- new PSCommand ( )
98- . AddCommand ( "TabExpansion2" )
99- . AddParameter ( "ast" , scriptAst )
100- . AddParameter ( "tokens" , currentTokens )
101- . AddParameter ( "positionOfCursor" , cursorPosition ) ,
102- executionOptions : null ,
103- cancellationToken )
104- . ExecuteAndGetResult ( cancellationToken ) ;
105-
106- if ( completionResults is { Count : > 0 } )
81+ CommandCompletion commandCompletion = null ;
82+ try
83+ {
84+ commandCompletion = await executionService . ExecuteDelegateAsync (
85+ representation : "CompleteInput" ,
86+ new ExecutionOptions { Priority = ExecutionPriority . Next } ,
87+ ( pwsh , _ ) =>
10788 {
108- return completionResults [ 0 ] ;
109- }
110-
111- return null ;
112- }
113-
114- // If the current runspace is out of process, we can't call TabExpansion2
115- // because the output will be serialized.
116- return CommandCompletion . CompleteInput (
117- scriptAst ,
118- currentTokens ,
119- cursorPosition ,
120- options : null ,
121- powershell : pwsh ) ;
122- } ,
123- cancellationToken ) . ConfigureAwait ( false ) ;
124-
125- stopwatch . Stop ( ) ;
126-
127- if ( commandCompletion is null )
89+ stopwatch . Start ( ) ;
90+
91+ // If the current runspace is not out of process, then we call TabExpansion2 so
92+ // that we have the ability to issue pipeline stop requests on cancellation.
93+ if ( executionService is PsesInternalHost psesInternalHost
94+ && ! psesInternalHost . Runspace . RunspaceIsRemote )
95+ {
96+ IReadOnlyList < CommandCompletion > completionResults = new SynchronousPowerShellTask < CommandCompletion > (
97+ logger ,
98+ psesInternalHost ,
99+ new PSCommand ( )
100+ . AddCommand ( "TabExpansion2" )
101+ . AddParameter ( "ast" , scriptAst )
102+ . AddParameter ( "tokens" , currentTokens )
103+ . AddParameter ( "positionOfCursor" , cursorPosition ) ,
104+ executionOptions : new PowerShellExecutionOptions ( )
105+ {
106+ ThrowOnError = true
107+ } ,
108+ cancellationToken )
109+ . ExecuteAndGetResult ( cancellationToken ) ;
110+
111+ if ( completionResults is { Count : > 0 } )
112+ {
113+ return completionResults [ 0 ] ;
114+ }
115+ }
116+ return null ;
117+
118+ // If the current runspace is out of process, we can't call TabExpansion2
119+ // because the output will be serialized.
120+ return CommandCompletion . CompleteInput (
121+ scriptAst ,
122+ currentTokens ,
123+ cursorPosition ,
124+ options : null ,
125+ powershell : pwsh ) ;
126+ } ,
127+ cancellationToken ) . ConfigureAwait ( false ) ;
128+
129+ }
130+ catch ( WildcardPatternException )
131+ {
132+ // This hits when you press Ctrl+Space inside empty square brackets []
133+ }
134+ catch ( RuntimeException ex )
128135 {
129- logger . LogError ( "Error Occurred in TabExpansion2" ) ;
136+ logger . LogError ( $ "Error Occurred in TabExpansion2: { ex . ErrorRecord } " , ex . ErrorRecord . Exception ) ;
130137 }
131- else
138+ finally
132139 {
140+ stopwatch . Stop ( ) ;
133141 logger . LogTrace (
134142 "IntelliSense completed in {elapsed}ms - WordToComplete: \" {word}\" MatchCount: {count}" ,
135143 stopwatch . ElapsedMilliseconds ,
0 commit comments