Skip to content

Made position comparable #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Protocol/Models/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace OmniSharp.Extensions.LanguageServer.Protocol.Models
{
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
public class Position : IEquatable<Position>
public class Position : IEquatable<Position>, IComparable<Position>, IComparable
{
public Position()
{
Expand Down Expand Up @@ -34,6 +34,21 @@ public bool Equals(Position other) =>
Line == other.Line &&
Character == other.Character;

public int CompareTo(Position other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
var lineComparison = Line.CompareTo(other.Line);
return lineComparison != 0 ? lineComparison : Character.CompareTo(other.Character);
}

public int CompareTo(object obj)
{
if (ReferenceEquals(null, obj)) return 1;
if (ReferenceEquals(this, obj)) return 0;
return obj is Position other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(Position)}");
}

public override int GetHashCode()
{
var hashCode = 1927683087;
Expand All @@ -48,6 +63,14 @@ public override int GetHashCode()

public static implicit operator Position((int line, int character) value) => new Position(value.line, value.character);

public static bool operator <(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) < 0;

public static bool operator >(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) > 0;

public static bool operator <=(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) <= 0;

public static bool operator >=(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) >= 0;

private string DebuggerDisplay => $"(line: {Line}, char: {Character})";

/// <inheritdoc />
Expand Down
32 changes: 32 additions & 0 deletions test/Lsp.Tests/Models/PositionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,37 @@ public void SimpleTest(string expected)
var deresult = new Serializer(ClientVersion.Lsp3).DeserializeObject<Position>(expected);
deresult.Should().BeEquivalentTo(model);
}

[Fact]
public void Is_Sortable_By_Character()
{
var a = new Position(1, 1);
var b = new Position(1, 2);
var c = new Position(1, 1);

a.Should().BeLessThan(b);
b.Should().BeGreaterThan(a);

a.CompareTo(c).Should().Be(0);

a.Should().BeLessOrEqualTo(c);
a.Should().BeGreaterOrEqualTo(c);
}

[Fact]
public void Is_Sortable_By_Line()
{
var a = new Position(1, 1);
var b = new Position(2, 1);
var c = new Position(1, 1);

a.Should().BeLessThan(b);
b.Should().BeGreaterThan(a);

a.CompareTo(c).Should().Be(0);

a.Should().BeLessOrEqualTo(c);
a.Should().BeGreaterOrEqualTo(c);
}
}
}