From bc3bed8267ef7cd6abe66799686b82fd2fab0363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= Date: Sat, 7 Dec 2019 17:11:34 +0900 Subject: [PATCH 1/3] types: add __repr__ and __eq__ support to Range and Location This helps debugging and simplify testing, where we can just assert that ranges or locations are equals. --- pygls/types.py | 25 ++++++++++++++++++++--- tests/test_types.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/test_types.py diff --git a/pygls/types.py b/pygls/types.py index b2cd2ed9..19f7c282 100644 --- a/pygls/types.py +++ b/pygls/types.py @@ -686,6 +686,15 @@ def __init__(self, uri: str, range: 'Range'): self.uri = uri self.range = range + def __eq__(self, other): + return ( + isinstance(other, Location) + and self.uri == other.uri + and self.range == other.range) + + def __repr__(self): + return "{}:{}".format(self.uri, self.range) + class LocationLink: def __init__(self, @@ -753,9 +762,10 @@ def __init__(self, line: int = 0, character: int = 0): self.character = character def __eq__(self, other): - if self.line == other.line and self.character == other.character: - return True - return False + return ( + isinstance(other, Position) + and self.line == other.line + and self.character == other.character) def __ge__(self, other): line_gt = self.line > other.line @@ -824,6 +834,15 @@ def __init__(self, start: Position, end: Position): self.start = start self.end = end + def __eq__(self, other): + return ( + isinstance(other, Range) + and self.start == other.start + and self.end == other.end) + + def __repr__(self): + return '{}-{}'.format(self.start, self.end) + class ReferenceContext: def __init__(self, include_declaration: bool): diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 00000000..87283144 --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,49 @@ +############################################################################ +# Original work Copyright 2018 Palantir Technologies, Inc. # +# Original work licensed under the MIT License. # +# See ThirdPartyNotices.txt in the project root for license information. # +# All modifications Copyright (c) Open Law Library. All rights reserved. # +# # +# Licensed under the Apache License, Version 2.0 (the "License") # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http: // www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +############################################################################ +from pygls.types import (Position, Range, Location) + + +def test_position(): + assert Position(1, 2) == Position(1, 2) + assert Position(1, 2) != Position(2, 2) + assert Position(1, 2) <= Position(2, 2) + assert Position(2, 2) >= Position(2, 0) + assert Position(1, 2) != 'something else' + assert "1:2" == repr(Position(1, 2)) + + +def test_range(): + assert Range(Position(1, 2), Position(3, 4)) \ + == Range(Position(1, 2), Position(3, 4)) + assert Range(Position(0, 2), Position(3, 4)) \ + != Range(Position(1, 2), Position(3, 4)) + assert Range(Position(0, 2), Position(3, 4)) != 'something else' + assert "1:2-3:4" == repr(Range(Position(1, 2), Position(3, 4))) + + +def test_location(): + assert Location(uri="file:///document.txt", range=Range(Position(1, 2), Position(3, 4))) \ + == Location(uri="file:///document.txt", range=Range(Position(1, 2), Position(3, 4))) + assert Location(uri="file:///document.txt", range=Range(Position(1, 2), Position(3, 4))) \ + != Location(uri="file:///another.txt", range=Range(Position(1, 2), Position(3, 4))) + assert Location(uri="file:///document.txt", range=Range(Position(1, 2), Position(3, 4))) \ + != 'something else' + assert "file:///document.txt:1:2-3:4" == repr(Location( + uri="file:///document.txt", + range=Range(Position(1, 2), Position(3, 4)))) From 583a022b427225bbfa91bc337d309353ab13d1a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= Date: Wed, 8 Jan 2020 10:08:42 +0900 Subject: [PATCH 2/3] add a changelog entry for #90 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 602dba1e..7c7cc499 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning][semver]. ## [Unreleased] +### Added + +- Add comparisons and repr support to Range and Location types ([#90]) + +[#90]: https://github.com/openlawlibrary/pygls/pull/90 + ## [0.8.1] - 09/05/2019 ### Changed From 57ef303d8f38f72865655f6795b37e6da1fb4aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= Date: Wed, 8 Jan 2020 10:09:00 +0900 Subject: [PATCH 3/3] add myself to CONTRIBUTORS --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index b2c181fd..21372a2b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -3,6 +3,7 @@ - [@augb](https://github.com/augb) - [Daniel Elero](https://github.com/danixeee) - [Denis Loginov](https://github.com/dinvlad) +- [Jérome Perrin](https://github.com/perrinjerome) - [Max O'Cull](https://github.com/Maxattax97) - [Samuel Roeca](https://github.com/pappasam) - [Tomoya Tanjo](https://github.com/tom-tan)