Skip to content

Commit a101343

Browse files
author
Kapil Borle
committed
Make Range parameter take Range type of int[] type arg
1 parent e212bc7 commit a101343

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
2626
{
2727
private const string defaultSettingsPreset = "CodeFormatting";
2828
private Settings inputSettings;
29+
private Range range;
2930

3031
/// <summary>
3132
/// The script text to be formated.
@@ -43,19 +44,10 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
4344
[ValidateNotNull]
4445
public object Settings { get; set; } = defaultSettingsPreset;
4546

46-
#if DEBUG
4747
[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
5951
/// <summary>
6052
/// Attaches to an instance of a .Net debugger
6153
/// </summary>
@@ -85,6 +77,22 @@ protected override void BeginProcessing()
8577
}
8678
#endif
8779

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+
8896
try
8997
{
9098
inputSettings = PSSASettings.Create(Settings, this.MyInvocation.PSScriptRoot, this);
@@ -93,7 +101,7 @@ protected override void BeginProcessing()
93101
{
94102
this.ThrowTerminatingError(new ErrorRecord(
95103
e,
96-
"SETTNGS_ERROR",
104+
"SETTINGS_ERROR",
97105
ErrorCategory.InvalidData,
98106
Settings));
99107
}
@@ -114,18 +122,30 @@ protected override void ProcessRecord()
114122
{
115123
// todo add tests to check range formatting
116124
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)
120133
{
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+
}
123139

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+
}
126144

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;
129149
}
130150

131151
private void ValidateInputSettings()

0 commit comments

Comments
 (0)