@@ -26,6 +26,7 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
26
26
{
27
27
private const string defaultSettingsPreset = "CodeFormatting" ;
28
28
private Settings inputSettings ;
29
+ private Range range ;
29
30
30
31
/// <summary>
31
32
/// The script text to be formated.
@@ -43,19 +44,10 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
43
44
[ ValidateNotNull ]
44
45
public object Settings { get ; set ; } = defaultSettingsPreset ;
45
46
46
- #if DEBUG
47
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 ;
58
-
48
+ [ ValidateNotNull ]
49
+ public object Range { get ; set ; }
50
+ #if DEBUG
59
51
/// <summary>
60
52
/// Attaches to an instance of a .Net debugger
61
53
/// </summary>
@@ -85,6 +77,22 @@ protected override void BeginProcessing()
85
77
}
86
78
#endif
87
79
80
+ if ( Range != null )
81
+ {
82
+ try
83
+ {
84
+ range = GetRange ( Range ) ;
85
+ }
86
+ catch ( Exception e )
87
+ {
88
+ this . ThrowTerminatingError ( new ErrorRecord (
89
+ e ,
90
+ "RANGE_ERROR" ,
91
+ ErrorCategory . InvalidArgument ,
92
+ Range ) ) ;
93
+ }
94
+ }
95
+
88
96
try
89
97
{
90
98
inputSettings = PSSASettings . Create ( Settings , this . MyInvocation . PSScriptRoot , this ) ;
@@ -93,7 +101,7 @@ protected override void BeginProcessing()
93
101
{
94
102
this . ThrowTerminatingError ( new ErrorRecord (
95
103
e ,
96
- "SETTNGS_ERROR " ,
104
+ "SETTINGS_ERROR " ,
97
105
ErrorCategory . InvalidData ,
98
106
Settings ) ) ;
99
107
}
@@ -114,18 +122,30 @@ protected override void ProcessRecord()
114
122
{
115
123
// todo add tests to check range formatting
116
124
string formattedScriptDefinition ;
117
- #if DEBUG
118
- var range = Range ;
119
- if ( this . ParameterSetName . Equals ( "NoRange" ) )
125
+ formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , range , this ) ;
126
+ this . WriteObject ( formattedScriptDefinition ) ;
127
+ }
128
+
129
+ private static Range GetRange ( object rangeObj )
130
+ {
131
+ var range = rangeObj as Range ;
132
+ if ( range == null )
120
133
{
121
- range = new Range ( StartLineNumber , StartColumnNumber , EndLineNumber , EndColumnNumber ) ;
122
- }
134
+ var intArr = rangeObj as int [ ] ;
135
+ if ( intArr == null )
136
+ {
137
+ throw new ArgumentException ( "Argument should be of type Range or int[]." ) ;
138
+ }
123
139
124
- formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , range , this ) ;
125
- #endif // DEBUG
140
+ if ( intArr . Length != 4 )
141
+ {
142
+ throw new ArgumentException ( "Integer array should be of length 4." ) ;
143
+ }
126
144
127
- formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , null , this ) ;
128
- this . WriteObject ( formattedScriptDefinition ) ;
145
+ range = new Range ( intArr [ 0 ] , intArr [ 1 ] , intArr [ 2 ] , intArr [ 3 ] ) ;
146
+ }
147
+
148
+ return range ;
129
149
}
130
150
131
151
private void ValidateInputSettings ( )
0 commit comments