12
12
13
13
namespace Microsoft . PowerShell . EditorServices . Services . PowerShellContext
14
14
{
15
+ using System . Collections . Generic ;
15
16
using System . Management . Automation ;
16
17
using Microsoft . Extensions . Logging ;
17
18
@@ -44,6 +45,15 @@ internal class PSReadLinePromptContext : IPromptContext
44
45
return [Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]
45
46
}" ;
46
47
48
+ private static ExecutionOptions s_psrlExecutionOptions = new ExecutionOptions
49
+ {
50
+ WriteErrorsToHost = false ,
51
+ WriteOutputToHost = false ,
52
+ InterruptCommandPrompt = false ,
53
+ AddToHistory = false ,
54
+ IsReadLine = true ,
55
+ } ;
56
+
47
57
private readonly PowerShellContextService _powerShellContext ;
48
58
49
59
private readonly PromptNest _promptNest ;
@@ -68,7 +78,6 @@ internal PSReadLinePromptContext(
68
78
_consoleReadLine = new ConsoleReadLine ( powerShellContext ) ;
69
79
_readLineProxy = readLineProxy ;
70
80
71
-
72
81
_readLineProxy . OverrideReadKey (
73
82
intercept => ConsoleProxy . SafeReadKey (
74
83
intercept ,
@@ -81,6 +90,7 @@ internal static bool TryGetPSReadLineProxy(
81
90
out PSReadLineProxy readLineProxy )
82
91
{
83
92
readLineProxy = null ;
93
+ logger . LogTrace ( "Attempting to load PSReadLine" ) ;
84
94
using ( var pwsh = PowerShell . Create ( ) )
85
95
{
86
96
pwsh . Runspace = runspace ;
@@ -91,18 +101,20 @@ internal static bool TryGetPSReadLineProxy(
91
101
92
102
if ( psReadLineType == null )
93
103
{
104
+ logger . LogWarning ( "PSReadLine unable to be loaded: {Reason}" , pwsh . HadErrors ? pwsh . Streams . Error [ 0 ] . ToString ( ) : "<Unknown reason>" ) ;
94
105
return false ;
95
106
}
96
107
97
108
try
98
109
{
99
110
readLineProxy = new PSReadLineProxy ( psReadLineType , logger ) ;
100
111
}
101
- catch ( InvalidOperationException )
112
+ catch ( InvalidOperationException e )
102
113
{
103
114
// The Type we got back from PowerShell doesn't have the members we expected.
104
115
// Could be an older version, a custom build, or something a newer version with
105
116
// breaking changes.
117
+ logger . LogWarning ( "PSReadLine unable to be loaded: {Reason}" , e ) ;
106
118
return false ;
107
119
}
108
120
}
@@ -119,38 +131,27 @@ public async Task<string> InvokeReadLineAsync(bool isCommandLine, CancellationTo
119
131
throw new TaskCanceledException ( ) ;
120
132
}
121
133
122
- try
134
+ if ( ! isCommandLine )
123
135
{
124
- if ( ! isCommandLine )
125
- {
126
- return await _consoleReadLine . InvokeLegacyReadLineAsync (
127
- isCommandLine : false ,
128
- _readLineCancellationSource . Token ) . ConfigureAwait ( false ) ;
129
- }
136
+ return await _consoleReadLine . InvokeLegacyReadLineAsync (
137
+ isCommandLine : false ,
138
+ _readLineCancellationSource . Token ) . ConfigureAwait ( false ) ;
139
+ }
130
140
131
- var result = ( await _powerShellContext . ExecuteCommandAsync < string > (
132
- new PSCommand ( )
133
- . AddScript ( ReadLineScript )
134
- . AddArgument ( _readLineCancellationSource . Token ) ,
135
- errorMessages : null ,
136
- new ExecutionOptions ( )
137
- {
138
- WriteErrorsToHost = false ,
139
- WriteOutputToHost = false ,
140
- InterruptCommandPrompt = false ,
141
- AddToHistory = false ,
142
- IsReadLine = isCommandLine
143
- } ) . ConfigureAwait ( false ) )
144
- . FirstOrDefault ( ) ;
141
+ var readLineCommand = new PSCommand ( )
142
+ . AddScript ( ReadLineScript )
143
+ . AddArgument ( _readLineCancellationSource . Token ) ;
145
144
146
- return cancellationToken . IsCancellationRequested
147
- ? string . Empty
148
- : result ;
149
- }
150
- finally
151
- {
152
- _readLineCancellationSource = null ;
153
- }
145
+ IEnumerable < string > readLineResults = await _powerShellContext . ExecuteCommandAsync < string > (
146
+ readLineCommand ,
147
+ errorMessages : null ,
148
+ s_psrlExecutionOptions ) . ConfigureAwait ( false ) ;
149
+
150
+ string line = readLineResults . FirstOrDefault ( ) ;
151
+
152
+ return cancellationToken . IsCancellationRequested
153
+ ? string . Empty
154
+ : line ;
154
155
}
155
156
156
157
public void AbortReadLine ( )
0 commit comments