77using System ;
88using System . Collections . Generic ;
99using System . Linq ;
10- using System . Management . Automation ;
11- using System . Management . Automation . Language ;
12- using System . Management . Automation . Runspaces ;
1310using System . Reflection ;
11+ using System . Threading ;
12+ using System . Threading . Tasks ;
1413
1514namespace Microsoft . PowerShell . EditorServices
1615{
16+ using System . Management . Automation ;
17+ using System . Management . Automation . Language ;
18+ using System . Management . Automation . Runspaces ;
19+
1720 /// <summary>
1821 /// Provides common operations for the syntax tree of a parsed script.
1922 /// </summary>
@@ -32,18 +35,22 @@ internal static class AstOperations
3235 /// <param name="fileOffset">
3336 /// The 1-based file offset at which a symbol will be located.
3437 /// </param>
35- /// <param name="runspace">
36- /// The Runspace to use for gathering completions.
38+ /// <param name="powerShellContext">
39+ /// The PowerShellContext to use for gathering completions.
40+ /// </param>
41+ /// <param name="cancellationToken">
42+ /// A CancellationToken to cancel completion requests.
3743 /// </param>
3844 /// <returns>
3945 /// A CommandCompletion instance that contains completions for the
4046 /// symbol at the given offset.
4147 /// </returns>
42- static public CommandCompletion GetCompletions (
48+ static public async Task < CommandCompletion > GetCompletions (
4349 Ast scriptAst ,
4450 Token [ ] currentTokens ,
4551 int fileOffset ,
46- Runspace runspace )
52+ PowerShellContext powerShellContext ,
53+ CancellationToken cancellationToken )
4754 {
4855 var type = scriptAst . Extent . StartScriptPosition . GetType ( ) ;
4956 var method =
@@ -73,20 +80,34 @@ static public CommandCompletion GetCompletions(
7380 cursorPosition . ColumnNumber ) ) ;
7481
7582 CommandCompletion commandCompletion = null ;
76- if ( runspace . RunspaceAvailability == RunspaceAvailability . Available )
83+ if ( powerShellContext . IsDebuggerStopped )
84+ {
85+ PSCommand command = new PSCommand ( ) ;
86+ command . AddCommand ( "TabExpansion2" ) ;
87+ command . AddParameter ( "Ast" , scriptAst ) ;
88+ command . AddParameter ( "Tokens" , currentTokens ) ;
89+ command . AddParameter ( "PositionOfCursor" , cursorPosition ) ;
90+ command . AddParameter ( "Options" , null ) ;
91+
92+ commandCompletion =
93+ ( await powerShellContext . ExecuteCommand < CommandCompletion > ( command , false , false ) )
94+ . FirstOrDefault ( ) ;
95+ }
96+ else if ( powerShellContext . CurrentRunspace . Runspace . RunspaceAvailability ==
97+ RunspaceAvailability . Available )
7798 {
78- using ( System . Management . Automation . PowerShell powerShell =
79- System . Management . Automation . PowerShell . Create ( ) )
99+ using ( RunspaceHandle runspaceHandle = await powerShellContext . GetRunspaceHandle ( cancellationToken ) )
100+ using ( PowerShell powerShell = PowerShell . Create ( ) )
80101 {
81- powerShell . Runspace = runspace ;
102+ powerShell . Runspace = runspaceHandle . Runspace ;
82103
83- commandCompletion =
104+ commandCompletion =
84105 CommandCompletion . CompleteInput (
85- scriptAst ,
86- currentTokens ,
87- cursorPosition ,
88- null ,
89- powerShell ) ;
106+ scriptAst ,
107+ currentTokens ,
108+ cursorPosition ,
109+ null ,
110+ powerShell ) ;
90111 }
91112 }
92113
0 commit comments