Skip to content

Commit 8693524

Browse files
committed
Added CreateEntireFilePosition methods and allow SourcePosition to be overridden.
1 parent fd654bf commit 8693524

5 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/MrKWatkins.Ast.Tests/Position/BinaryFileTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ public void CreatePosition()
5555
position.StartIndex.Should().Be(1);
5656
position.Length.Should().Be(2);
5757
}
58+
59+
[Test]
60+
public void CreateEntireFilePosition()
61+
{
62+
var bytes = new byte[] { 1, 2, 3, 4, 5 };
63+
64+
var binaryFile = new BinaryFile("Test Filename", bytes);
65+
66+
var position = binaryFile.CreateEntireFilePosition();
67+
position.File.Should().BeSameAs(binaryFile);
68+
position.StartIndex.Should().Be(0);
69+
position.Length.Should().Be(5);
70+
}
5871

5972
[TestCaseSource(nameof(EqualityTestCases))]
6073
public void Equality(SourceFile x, object? y, bool expected) => AssertEqual(x, y, expected);

src/MrKWatkins.Ast.Tests/Position/TextFileTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ public void CreatePosition()
6464
position.Text.Should().Be("More");
6565
}
6666

67+
[Test]
68+
public void CreateEntireFilePosition()
69+
{
70+
const string text = "Some Text\nSome More Text";
71+
72+
var textFile = new TextFile("Test Filename", text);
73+
74+
var position = textFile.CreateEntireFilePosition();
75+
position.File.Should().BeSameAs(textFile);
76+
position.StartIndex.Should().Be(0);
77+
position.Length.Should().Be(24);
78+
position.StartLineIndex.Should().Be(0);
79+
position.StartLineNumber.Should().Be(1);
80+
position.StartColumnIndex.Should().Be(0);
81+
position.StartColumnNumber.Should().Be(1);
82+
position.StartLine.Should().Be("Some Text");
83+
position.Text.Should().Be(text);
84+
}
85+
6786
[TestCaseSource(nameof(EqualityTestCases))]
6887
public void Equality(SourceFile x, object? y, bool expected) => AssertEqual(x, y, expected);
6988

src/MrKWatkins.Ast/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected Node()
2828
/// <summary>
2929
/// The position of the node in the source code.
3030
/// </summary>
31-
public SourcePosition SourcePosition
31+
public virtual SourcePosition SourcePosition
3232
{
3333
get => Properties.GetOrDefault(nameof(SourcePosition), SourcePosition.None);
3434
set => Properties.Set(nameof(SourcePosition), value);

src/MrKWatkins.Ast/Position/BinaryFile.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public BinaryFile(string name, IReadOnlyList<byte> bytes)
5151
[Pure]
5252
public BinaryFilePosition CreatePosition(int startIndex, int length) =>
5353
new(this, startIndex, length);
54+
55+
/// <summary>
56+
/// Creates a <see cref="BinaryFilePosition" /> from this <see cref="BinaryFile" /> that represents the whole file.
57+
/// </summary>
58+
/// <returns>A new <see cref="BinaryFilePosition" /> instance.</returns>
59+
[Pure]
60+
public BinaryFilePosition CreateEntireFilePosition() => new(this, 0, Length);
5461

5562
[Pure]
5663
private static IReadOnlyList<byte> ReadStream(Stream stream)

src/MrKWatkins.Ast/Position/TextFile.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public TextFile(string name, string text)
5252
public IReadOnlyList<string> Lines { get; }
5353

5454
/// <summary>
55-
/// Creates a <see cref="TextFilePosition" /> from this <see cref="BinaryFile" />.
55+
/// Creates a <see cref="TextFilePosition" /> from this <see cref="TextFile" />.
5656
/// </summary>
5757
/// <param name="startIndex">The start index of the position in the file.</param>
5858
/// <param name="length">The length of the file.</param>
@@ -62,6 +62,13 @@ public TextFile(string name, string text)
6262
[Pure]
6363
public TextFilePosition CreatePosition(int startIndex, int length, int startLineIndex, int startColumnIndex) =>
6464
new(this, startIndex, length, startLineIndex, startColumnIndex);
65+
66+
/// <summary>
67+
/// Creates a <see cref="TextFilePosition" /> from this <see cref="TextFile" /> that represents the whole file.
68+
/// </summary>
69+
/// <returns>A new <see cref="TextFilePosition" /> instance.</returns>
70+
[Pure]
71+
public TextFilePosition CreateEntireFilePosition() => new(this, 0, Length, 0, 0);
6572

6673
[Pure]
6774
private static string ReadStream(Stream stream)

0 commit comments

Comments
 (0)