1
- using System ;
1
+ /********************************************************************++
2
+ Copyright (c) Microsoft Corporation. All rights reserved.
3
+ --********************************************************************/
4
+
5
+ using System ;
6
+ using System . Diagnostics . CodeAnalysis ;
7
+ using System . Globalization ;
2
8
using System . Linq ;
3
9
using System . Management . Automation ;
4
10
using System . Management . Automation . Language ;
11
+ using System . Management . Automation . Runspaces ;
5
12
6
- namespace PSConsoleUtilities
13
+ namespace Microsoft . PowerShell
7
14
{
8
15
public partial class PSConsoleReadLine
9
16
{
10
17
/// <summary>
11
18
/// Insert the key
12
19
/// </summary>
20
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
13
21
public static void SelfInsert ( ConsoleKeyInfo ? key = null , object arg = null )
14
22
{
15
23
if ( ! key . HasValue )
@@ -55,6 +63,7 @@ public static void SelfInsert(ConsoleKeyInfo? key = null, object arg = null)
55
63
/// <summary>
56
64
/// Reverts all of the input to the current input.
57
65
/// </summary>
66
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
58
67
public static void RevertLine ( ConsoleKeyInfo ? key = null , object arg = null )
59
68
{
60
69
if ( _singleton . _statusIsErrorMessage )
@@ -75,6 +84,7 @@ public static void RevertLine(ConsoleKeyInfo? key = null, object arg = null)
75
84
/// Cancel the current input, leaving the input on the screen,
76
85
/// but returns back to the host so the prompt is evaluated again.
77
86
/// </summary>
87
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
78
88
public static void CancelLine ( ConsoleKeyInfo ? key = null , object arg = null )
79
89
{
80
90
_singleton . ClearStatusMessage ( false ) ;
@@ -109,6 +119,7 @@ public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
109
119
/// Like ForwardKillLine - deletes text from the point to the end of the line,
110
120
/// but does not put the deleted text in the kill ring.
111
121
/// </summary>
122
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
112
123
public static void ForwardDeleteLine ( ConsoleKeyInfo ? key = null , object arg = null )
113
124
{
114
125
var current = _singleton . _current ;
@@ -127,6 +138,7 @@ public static void ForwardDeleteLine(ConsoleKeyInfo? key = null, object arg = nu
127
138
/// Like BackwardKillLine - deletes text from the point to the start of the line,
128
139
/// but does not put the deleted text in the kill ring.
129
140
/// </summary>
141
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
130
142
public static void BackwardDeleteLine ( ConsoleKeyInfo ? key = null , object arg = null )
131
143
{
132
144
if ( _singleton . _current > 0 )
@@ -142,6 +154,7 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n
142
154
/// <summary>
143
155
/// Delete the character before the cursor.
144
156
/// </summary>
157
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
145
158
public static void BackwardDeleteChar ( ConsoleKeyInfo ? key = null , object arg = null )
146
159
{
147
160
if ( _singleton . _visualSelectionCommandCount > 0 )
@@ -191,6 +204,7 @@ private void DeleteCharImpl(bool orExit)
191
204
/// <summary>
192
205
/// Delete the character under the cursor.
193
206
/// </summary>
207
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
194
208
public static void DeleteChar ( ConsoleKeyInfo ? key = null , object arg = null )
195
209
{
196
210
_singleton . DeleteCharImpl ( orExit : false ) ;
@@ -199,6 +213,7 @@ public static void DeleteChar(ConsoleKeyInfo? key = null, object arg = null)
199
213
/// <summary>
200
214
/// Delete the character under the cursor, or if the line is empty, exit the process
201
215
/// </summary>
216
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
202
217
public static void DeleteCharOrExit ( ConsoleKeyInfo ? key = null , object arg = null )
203
218
{
204
219
_singleton . DeleteCharImpl ( orExit : true ) ;
@@ -223,6 +238,7 @@ private bool AcceptLineImpl(bool validate)
223
238
_emphasisStart = - 1 ;
224
239
_emphasisLength = 0 ;
225
240
241
+ var insertionPoint = _current ;
226
242
// Make sure cursor is at the end before writing the line
227
243
_current = _buffer . Length ;
228
244
@@ -239,6 +255,17 @@ private bool AcceptLineImpl(bool validate)
239
255
var errorMessage = Validate ( _ast ) ;
240
256
if ( ! string . IsNullOrWhiteSpace ( errorMessage ) )
241
257
{
258
+ // If there are more keys, assume the user pasted with a right click and
259
+ // we should insert a newline even though validation failed.
260
+ if ( _queuedKeys . Count > 0 )
261
+ {
262
+ // Validation may have moved the cursor. Because there are queued
263
+ // keys, we need to move the cursor back to the correct place, and
264
+ // ignore where validation put the cursor because the queued keys
265
+ // will be inserted in the wrong place.
266
+ SetCursorPosition ( insertionPoint ) ;
267
+ Insert ( '\n ' ) ;
268
+ }
242
269
_statusLinePrompt = "" ;
243
270
_statusBuffer . Append ( errorMessage ) ;
244
271
_statusIsErrorMessage = true ;
@@ -280,7 +307,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
280
307
if ( commandInfo == null && ! _singleton . UnresolvedCommandCouldSucceed ( commandName , _rootAst ) )
281
308
{
282
309
_singleton . _current = commandAst . CommandElements [ 0 ] . Extent . EndOffset ;
283
- detectedError = string . Format ( PSReadLineResources . CommandNotFoundError , commandName ) ;
310
+ detectedError = string . Format ( CultureInfo . CurrentCulture , PSReadLineResources . CommandNotFoundError , commandName ) ;
284
311
return AstVisitAction . StopVisit ;
285
312
}
286
313
}
@@ -415,6 +442,7 @@ static bool StaticParameterBindingSupported(CommandInfo commandInfo)
415
442
/// continuation prompt is displayed on the next line and PSReadline waits for
416
443
/// keys to edit the current input.
417
444
/// </summary>
445
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
418
446
public static void AcceptLine ( ConsoleKeyInfo ? key = null , object arg = null )
419
447
{
420
448
_singleton . AcceptLineImpl ( false ) ;
@@ -426,6 +454,7 @@ public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
426
454
/// continuation prompt is displayed on the next line and PSReadline waits for
427
455
/// keys to edit the current input.
428
456
/// </summary>
457
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
429
458
public static void ValidateAndAcceptLine ( ConsoleKeyInfo ? key = null , object arg = null )
430
459
{
431
460
_singleton . AcceptLineImpl ( true ) ;
@@ -435,6 +464,7 @@ public static void ValidateAndAcceptLine(ConsoleKeyInfo? key = null, object arg
435
464
/// Attempt to execute the current input. If it can be executed (like AcceptLine),
436
465
/// then recall the next item from history the next time Readline is called.
437
466
/// </summary>
467
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
438
468
public static void AcceptAndGetNext ( ConsoleKeyInfo ? key = null , object arg = null )
439
469
{
440
470
if ( _singleton . AcceptLineImpl ( false ) )
@@ -455,6 +485,7 @@ public static void AcceptAndGetNext(ConsoleKeyInfo? key = null, object arg = nul
455
485
/// keys to edit the current input. This is useful to enter multi-line input as
456
486
/// a single command even when a single line is complete input by itself.
457
487
/// </summary>
488
+ [ SuppressMessage ( "Microsoft.Design" , "CA1026:DefaultParametersShouldNotBeUsed" ) ]
458
489
public static void AddLine ( ConsoleKeyInfo ? key = null , object arg = null )
459
490
{
460
491
Insert ( '\n ' ) ;
0 commit comments