From 1a6c60d1c8e8d5642a4a1e9a040639a34fbf6d12 Mon Sep 17 00:00:00 2001 From: Linus Heckemann Date: Fri, 13 Mar 2026 14:25:16 +0100 Subject: [PATCH 1/2] feat(ServerTextPosition): implement comparison operators --- pygls/workspace/position_codec.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygls/workspace/position_codec.py b/pygls/workspace/position_codec.py index 40fb9c91..094365d9 100644 --- a/pygls/workspace/position_codec.py +++ b/pygls/workspace/position_codec.py @@ -25,7 +25,7 @@ log = logging.getLogger(__name__) -@dataclass +@dataclass(order=True) class ServerTextPosition: line: int character: int From 77155f449a25a7b396cd01ae9ca90cb66aa9f440 Mon Sep 17 00:00:00 2001 From: Linus Heckemann Date: Fri, 13 Mar 2026 15:13:53 +0100 Subject: [PATCH 2/2] feat(ServerTextRange): add utility functions for overlap and inclusion --- pygls/workspace/position_codec.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pygls/workspace/position_codec.py b/pygls/workspace/position_codec.py index 094365d9..b8affc45 100644 --- a/pygls/workspace/position_codec.py +++ b/pygls/workspace/position_codec.py @@ -18,7 +18,7 @@ ############################################################################ import logging from dataclasses import dataclass -from typing import Optional, Union, Sequence +from typing import Optional, Union, Sequence, Any from lsprotocol import types @@ -42,6 +42,25 @@ class ServerTextRange: def __repr__(self): return f"{self.start}-{self.end}" + def __contains__(self, position: Any) -> bool: + if not isinstance(position, ServerTextPosition): + raise TypeError("ServerTextRanges can only contain ServerTextPositions.") + return self.start <= position <= self.end + + def includes(self, inner: "ServerTextRange") -> bool: + """ + Returns whether `inner` is entirely contained within self, i.e. all + positions in `inner` are also in `self`. + """ + return self.start <= inner.start and inner.end <= self.end + + def overlaps(self, other: "ServerTextRange") -> bool: + """ + Returns whether `self` and `other` overlap, i.e. any positions exist that + are included in both self and other. + """ + return self.start <= other.end and other.start <= self.end + class UnitCounter: def code_units_for_char(self, char: str) -> int: