7
7
using System ;
8
8
using System . Collections . Generic ;
9
9
using System . Linq ;
10
- using System . Management . Automation ;
11
- using System . Management . Automation . Language ;
12
- using System . Management . Automation . Runspaces ;
13
10
using System . Reflection ;
11
+ using System . Threading ;
12
+ using System . Threading . Tasks ;
14
13
15
14
namespace Microsoft . PowerShell . EditorServices
16
15
{
16
+ using System . Management . Automation ;
17
+ using System . Management . Automation . Language ;
18
+ using System . Management . Automation . Runspaces ;
19
+
17
20
/// <summary>
18
21
/// Provides common operations for the syntax tree of a parsed script.
19
22
/// </summary>
@@ -32,18 +35,22 @@ internal static class AstOperations
32
35
/// <param name="fileOffset">
33
36
/// The 1-based file offset at which a symbol will be located.
34
37
/// </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.
37
43
/// </param>
38
44
/// <returns>
39
45
/// A CommandCompletion instance that contains completions for the
40
46
/// symbol at the given offset.
41
47
/// </returns>
42
- static public CommandCompletion GetCompletions (
48
+ static public async Task < CommandCompletion > GetCompletions (
43
49
Ast scriptAst ,
44
50
Token [ ] currentTokens ,
45
51
int fileOffset ,
46
- Runspace runspace )
52
+ PowerShellContext powerShellContext ,
53
+ CancellationToken cancellationToken )
47
54
{
48
55
var type = scriptAst . Extent . StartScriptPosition . GetType ( ) ;
49
56
var method =
@@ -73,20 +80,34 @@ static public CommandCompletion GetCompletions(
73
80
cursorPosition . ColumnNumber ) ) ;
74
81
75
82
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 )
77
98
{
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 ( ) )
80
101
{
81
- powerShell . Runspace = runspace ;
102
+ powerShell . Runspace = runspaceHandle . Runspace ;
82
103
83
- commandCompletion =
104
+ commandCompletion =
84
105
CommandCompletion . CompleteInput (
85
- scriptAst ,
86
- currentTokens ,
87
- cursorPosition ,
88
- null ,
89
- powerShell ) ;
106
+ scriptAst ,
107
+ currentTokens ,
108
+ cursorPosition ,
109
+ null ,
110
+ powerShell ) ;
90
111
}
91
112
}
92
113
0 commit comments