1212
1313using System ;
1414using System . Globalization ;
15+ using System . Linq ;
1516using System . Management . Automation ;
1617
1718namespace Microsoft . Windows . PowerShell . ScriptAnalyzer . Commands
@@ -26,6 +27,7 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
2627 {
2728 private const string defaultSettingsPreset = "CodeFormatting" ;
2829 private Settings inputSettings ;
30+ private Range range ;
2931
3032 /// <summary>
3133 /// The script text to be formated.
@@ -43,19 +45,20 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
4345 [ ValidateNotNull ]
4446 public object Settings { get ; set ; } = defaultSettingsPreset ;
4547
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 ; }
5860
61+ #if DEBUG
5962 /// <summary>
6063 /// Attaches to an instance of a .Net debugger
6164 /// </summary>
@@ -85,6 +88,7 @@ protected override void BeginProcessing()
8588 }
8689#endif
8790
91+ this . range = Range == null ? null : new Range ( Range [ 0 ] , Range [ 1 ] , Range [ 2 ] , Range [ 3 ] ) ;
8892 try
8993 {
9094 inputSettings = PSSASettings . Create ( Settings , this . MyInvocation . PSScriptRoot , this ) ;
@@ -93,7 +97,7 @@ protected override void BeginProcessing()
9397 {
9498 this . ThrowTerminatingError ( new ErrorRecord (
9599 e ,
96- "SETTNGS_ERROR " ,
100+ "SETTINGS_ERROR " ,
97101 ErrorCategory . InvalidData ,
98102 Settings ) ) ;
99103 }
@@ -114,17 +118,7 @@ protected override void ProcessRecord()
114118 {
115119 // todo add tests to check range formatting
116120 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-
124121 formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , range , this ) ;
125- #endif // DEBUG
126-
127- formattedScriptDefinition = Formatter . Format ( ScriptDefinition , inputSettings , null , this ) ;
128122 this . WriteObject ( formattedScriptDefinition ) ;
129123 }
130124
0 commit comments