|
1
|
|
|
from coalib.misc.Decorators import (enforce_signature, |
|
2
|
|
|
generate_ordering, |
|
3
|
|
|
generate_repr) |
|
4
|
|
|
from coalib.results.TextPosition import TextPosition |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
@generate_repr("start", "end") |
|
8
|
|
|
@generate_ordering("start", "end") |
|
9
|
|
|
class TextRange: |
|
10
|
|
|
@enforce_signature |
|
11
|
|
|
def __init__(self, start: TextPosition, end: (TextPosition, None)=None): |
|
12
|
|
|
""" |
|
13
|
|
|
Creates a new TextRange. |
|
14
|
|
|
|
|
15
|
|
|
:param start: A TextPosition indicating the start of the range. |
|
16
|
|
|
Can't be `None`. |
|
17
|
|
|
:param end: A TextPosition indicating the end of the range. If |
|
18
|
|
|
`None` is given, the start object will be used |
|
19
|
|
|
here. |
|
20
|
|
|
:raises TypeError: Raised when |
|
21
|
|
|
- start is no TextPosition or None. |
|
22
|
|
|
- end is no TextPosition. |
|
23
|
|
|
:raises ValueError: Raised when end position is smaller than start |
|
24
|
|
|
position, because negative ranges are not allowed. |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
|
|
self._start = start |
|
28
|
|
|
self._end = end or start |
|
29
|
|
|
|
|
30
|
|
|
if self._end < start: |
|
31
|
|
|
raise ValueError("End position can't be less than start position.") |
|
32
|
|
|
|
|
33
|
|
|
@classmethod |
|
34
|
|
|
def from_values(cls, |
|
35
|
|
|
start_line=None, |
|
36
|
|
|
start_column=None, |
|
37
|
|
|
end_line=None, |
|
38
|
|
|
end_column=None): |
|
39
|
|
|
""" |
|
40
|
|
|
Creates a new TextRange. |
|
41
|
|
|
|
|
42
|
|
|
:param start_line: The line number of the start position. The first |
|
43
|
|
|
line is 1. |
|
44
|
|
|
:param start_column: The column number of the start position. The first |
|
45
|
|
|
column is 1. |
|
46
|
|
|
:param end_line: The line number of the end position. If this |
|
47
|
|
|
parameter is `None`, then the end position is set |
|
48
|
|
|
the same like start position and end_column gets |
|
49
|
|
|
ignored. |
|
50
|
|
|
:param end_column: The column number of the end position. |
|
51
|
|
|
:return: A TextRange. |
|
52
|
|
|
""" |
|
53
|
|
|
start = TextPosition(start_line, start_column) |
|
54
|
|
|
if end_line is None: |
|
55
|
|
|
end = None |
|
56
|
|
|
else: |
|
57
|
|
|
end = TextPosition(end_line, end_column) |
|
58
|
|
|
|
|
59
|
|
|
return cls(start, end) |
|
60
|
|
|
|
|
61
|
|
|
@property |
|
62
|
|
|
def start(self): |
|
63
|
|
|
return self._start |
|
64
|
|
|
|
|
65
|
|
|
@property |
|
66
|
|
|
def end(self): |
|
67
|
|
|
return self._end |
|
68
|
|
|
|