Skip to content
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

types: add __repr__ and __eq__ support to Range and Location #90

Merged
merged 4 commits into from
Jan 8, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 22 additions & 3 deletions pygls/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -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))))