12
12
13
13
using System ;
14
14
using System . Globalization ;
15
+ using System . Linq ;
15
16
using System . Management . Automation ;
16
17
17
18
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Commands
@@ -26,6 +27,7 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
26
27
{
27
28
private const string defaultSettingsPreset = "CodeFormatting" ;
28
29
private Settings inputSettings ;
30
+ private Range range ;
29
31
30
32
/// <summary>
31
33
/// The script text to be formated.
@@ -43,19 +45,20 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
43
45
[ ValidateNotNull ]
44
46
public object Settings { get ; set ; } = defaultSettingsPreset ;
45
47
46
- #if DEBUG
47
- [ Parameter ( Mandatory = false ) ]
48
- public Range Range { get ; set ; }
49
-
50
- [ Parameter ( Mandatory = false , ParameterSetName = "NoRange" ) ]
51
- public int StartLineNumber { get ; set ; } = - 1 ;
52
- [ Parameter ( Mandatory = false , ParameterSetName = "NoRange" ) ]
53
- public int StartColumnNumber { get ; set ; } = - 1 ;
54
- [ Parameter ( Mandatory = false , ParameterSetName = "NoRange" ) ]
55
- public int EndLineNumber { get ; set ; } = - 1 ;
56
- [ Parameter ( Mandatory = false , ParameterSetName = "NoRange" ) ]
57
- public int EndColumnNumber { get ; set ; } = - 1 ;
48
+ /// <summary>
49
+ /// The range within which formatting should take place.
50
+ ///
51
+ /// The parameter is an array of integers of length 4 such that the first, second, third and last
52
+ /// elements correspond to the start line number, start column number, end line number and
53
+ /// end column number. These numbers must be greater than 0.
54
+ /// </summary>
55
+ /// <returns></returns>
56
+ [ Parameter ( Mandatory = false , Position = 3 ) ]
57
+ [ ValidateNotNull ]
58
+ [ ValidateCount ( 4 , 4 ) ]
59
+ public int [ ] Range { get ; set ; }
58
60
61
+ #if DEBUG
59
62
/// <summary>
60
63
/// Attaches to an instance of a .Net debugger
61
64
/// </summary>
@@ -85,6 +88,7 @@ protected override void BeginProcessing()
85
88
}
86
89
#endif
87
90
91
+ this . range = Range == null ? null : new Range ( Range [ 0 ] , Range [ 1 ] , Range [ 2 ] , Range [ 3 ] ) ;
88
92
try
89
93
{
90
94
inputSettings = PSSASettings . Create ( Settings , this . MyInvocation . PSScriptRoot , this ) ;
@@ -93,7 +97,7 @@ protected override void BeginProcessing()
93
97
{
94
98
this . ThrowTerminatingError ( new ErrorRecord (
95
99
e ,
96
- "SETTNGS_ERROR " ,
100
+ "SETTINGS_ERROR " ,
97
101
ErrorCategory . InvalidData ,
98
102
Settings ) ) ;
99
103
}
@@ -114,17 +118,7 @@ protected override void ProcessRecord()
114
118
{
115
119
// todo add tests to check range formatting
116
120
string formattedScriptDefinition ;
117
- #if DEBUG
118
- var range = Range ;
119
- if ( this . ParameterSetName . Equals ( "NoRange" ) )
120
- {
121
- range = new Range ( StartLineNumber , StartColumnNumber , EndLineNumber , EndColumnNumber ) ;
122
- }
123
-
124
121
formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , range , this ) ;
125
- #endif // DEBUG
126
-
127
- formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , null , this ) ;
128
122
this . WriteObject ( formattedScriptDefinition ) ;
129
123
}
130
124
0 commit comments