Skip to content

Commit 064eb77

Browse files
author
Kapil Borle
committed
Add documentation to Range class
1 parent cb6f9c5 commit 064eb77

1 file changed

Lines changed: 59 additions & 17 deletions

File tree

Engine/Range.cs

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,42 @@
33

44
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
55
{
6-
// TODO doc
6+
/// <summary>
7+
/// Class to represent range in text. Range is represented as a pair of positions, [Start, End).
8+
/// </summary>
79
public class Range
810
{
9-
public Position Start { get; }
10-
public Position End { get; }
11+
/// <summary>
12+
/// Constructs a Range object to represent a range of text.
13+
/// </summary>
14+
/// <param name="start">The start position of the text.</param>
15+
/// <param name="end">The end position of the text, such that range is [start, end).</param>
1116
public Range(Position start, Position end)
1217
{
13-
if (start > end)
14-
{
15-
throw new ArgumentException(String.Format(
16-
CultureInfo.CurrentCulture,
17-
Strings.RangeStartPosGreaterThanEndPos));
18-
}
19-
2018
Start = new Position(start);
2119
End = new Position(end);
20+
ValidatePositions();
2221
}
2322

23+
/// <summary>
24+
/// Constructs a Range object to represent a range of text.
25+
/// </summary>
26+
/// <param name="startLineNumber">1-based line number on which the text starts.</param>
27+
/// <param name="startColumnNumber">1-based offset on start line at which the text starts. This includes the first character of the text.</param>
28+
/// <param name="endLineNumber">1-based line number on which the text ends.</param>
29+
/// <param name="endColumnNumber">1-based offset on end line at which the text ends. This offset value is 1 more than the offset of the last character of the text. </param>
2430
public Range(int startLineNumber, int startColumnNumber, int endLineNumber, int endColumnNumber)
2531
: this(
2632
new Position(startLineNumber, startColumnNumber),
2733
new Position(endLineNumber, endColumnNumber))
2834
{
29-
35+
ValidatePositions();
3036
}
3137

38+
/// <summary>
39+
/// Copy constructor
40+
/// </summary>
41+
/// <param name="range">A Range object</param>
3242
public Range(Range range)
3343
{
3444
if (range == null)
@@ -40,13 +50,25 @@ public Range(Range range)
4050
End = new Position(range.End);
4151
}
4252

43-
public Range Shift(int lineDelta, int columnDelta)
44-
{
45-
var newStart = Start.Shift(lineDelta, columnDelta);
46-
var newEnd = End.Shift(lineDelta, Start.Line == End.Line ? columnDelta : 0);
47-
return new Range(newStart, newEnd);
48-
}
53+
/// <summary>
54+
/// Start position of the range.
55+
/// </summary>
56+
public Position Start { get; }
4957

58+
/// <summary>
59+
/// End position of the range.
60+
///
61+
/// This position does not contain the last character of the range, but instead is the position
62+
/// right after the last character in the range.
63+
/// </summary>
64+
public Position End { get; }
65+
66+
/// <summary>
67+
/// Normalize a range with respect to the input position.
68+
/// </summary>
69+
/// <param name="refPosition">Reference position.</param>
70+
/// <param name="range">Range to be normalized.</param>
71+
/// <returns>Range object with normalized positions.</returns>
5072
public static Range Normalize(Position refPosition, Range range)
5173
{
5274
if (refPosition == null)
@@ -70,5 +92,25 @@ public static Range Normalize(Position refPosition, Range range)
7092
1 - refPosition.Line,
7193
range.Start.Line == refPosition.Line ? 1 - refPosition.Column : 0);
7294
}
95+
96+
/// <summary>
97+
/// Returns a new range object with shifted positions.
98+
/// </summary>
99+
public Range Shift(int lineDelta, int columnDelta)
100+
{
101+
var newStart = Start.Shift(lineDelta, columnDelta);
102+
var newEnd = End.Shift(lineDelta, Start.Line == End.Line ? columnDelta : 0);
103+
return new Range(newStart, newEnd);
104+
}
105+
106+
private void ValidatePositions()
107+
{
108+
if (Start > End)
109+
{
110+
throw new ArgumentException(String.Format(
111+
CultureInfo.CurrentCulture,
112+
Strings.RangeStartPosGreaterThanEndPos));
113+
}
114+
}
73115
}
74116
}

0 commit comments

Comments
 (0)