Skip to content

Commit 332e5ae

Browse files
Made position comparable (#378)
1 parent 6eeb011 commit 332e5ae

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Protocol/Models/Position.cs

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace OmniSharp.Extensions.LanguageServer.Protocol.Models
66
{
77
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
8-
public class Position : IEquatable<Position>
8+
public class Position : IEquatable<Position>, IComparable<Position>, IComparable
99
{
1010
public Position()
1111
{
@@ -34,6 +34,21 @@ public bool Equals(Position other) =>
3434
Line == other.Line &&
3535
Character == other.Character;
3636

37+
public int CompareTo(Position other)
38+
{
39+
if (ReferenceEquals(this, other)) return 0;
40+
if (ReferenceEquals(null, other)) return 1;
41+
var lineComparison = Line.CompareTo(other.Line);
42+
return lineComparison != 0 ? lineComparison : Character.CompareTo(other.Character);
43+
}
44+
45+
public int CompareTo(object obj)
46+
{
47+
if (ReferenceEquals(null, obj)) return 1;
48+
if (ReferenceEquals(this, obj)) return 0;
49+
return obj is Position other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(Position)}");
50+
}
51+
3752
public override int GetHashCode()
3853
{
3954
var hashCode = 1927683087;
@@ -48,6 +63,14 @@ public override int GetHashCode()
4863

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

66+
public static bool operator <(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) < 0;
67+
68+
public static bool operator >(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) > 0;
69+
70+
public static bool operator <=(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) <= 0;
71+
72+
public static bool operator >=(Position left, Position right) => Comparer<Position>.Default.Compare(left, right) >= 0;
73+
5174
private string DebuggerDisplay => $"(line: {Line}, char: {Character})";
5275

5376
/// <inheritdoc />

test/Lsp.Tests/Models/PositionTests.cs

+32
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,37 @@ public void SimpleTest(string expected)
2020
var deresult = new Serializer(ClientVersion.Lsp3).DeserializeObject<Position>(expected);
2121
deresult.Should().BeEquivalentTo(model);
2222
}
23+
24+
[Fact]
25+
public void Is_Sortable_By_Character()
26+
{
27+
var a = new Position(1, 1);
28+
var b = new Position(1, 2);
29+
var c = new Position(1, 1);
30+
31+
a.Should().BeLessThan(b);
32+
b.Should().BeGreaterThan(a);
33+
34+
a.CompareTo(c).Should().Be(0);
35+
36+
a.Should().BeLessOrEqualTo(c);
37+
a.Should().BeGreaterOrEqualTo(c);
38+
}
39+
40+
[Fact]
41+
public void Is_Sortable_By_Line()
42+
{
43+
var a = new Position(1, 1);
44+
var b = new Position(2, 1);
45+
var c = new Position(1, 1);
46+
47+
a.Should().BeLessThan(b);
48+
b.Should().BeGreaterThan(a);
49+
50+
a.CompareTo(c).Should().Be(0);
51+
52+
a.Should().BeLessOrEqualTo(c);
53+
a.Should().BeGreaterOrEqualTo(c);
54+
}
2355
}
2456
}

0 commit comments

Comments
 (0)