|
1
|
|
|
from coalib.misc.Decorators import enforce_signature |
|
2
|
|
|
from coalib.results.SourcePosition import SourcePosition |
|
3
|
|
|
from coalib.results.TextRange import TextRange |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class SourceRange(TextRange): |
|
7
|
|
|
@enforce_signature |
|
8
|
|
|
def __init__(self, |
|
9
|
|
|
start: SourcePosition, |
|
10
|
|
|
end: (SourcePosition, None)=None): |
|
11
|
|
|
""" |
|
12
|
|
|
Creates a new SourceRange. |
|
13
|
|
|
|
|
14
|
|
|
:param start: A SourcePosition indicating the start of the range. |
|
15
|
|
|
:param end: A SourcePosition indicating the end of the range. |
|
16
|
|
|
If `None` is given, the start object will be used |
|
17
|
|
|
here. end must be in the same file and be greater |
|
18
|
|
|
than start as negative ranges are not allowed. |
|
19
|
|
|
:raises TypeError: Raised when |
|
20
|
|
|
- start is no SourcePosition or None. |
|
21
|
|
|
- end is no SourcePosition. |
|
22
|
|
|
:raises ValueError: Raised when file of start and end mismatch. |
|
23
|
|
|
""" |
|
24
|
|
|
TextRange.__init__(self, start, end) |
|
25
|
|
|
|
|
26
|
|
|
if self.start.file != self.end.file: |
|
27
|
|
|
raise ValueError("File of start and end position do not match.") |
|
28
|
|
|
|
|
29
|
|
|
@classmethod |
|
30
|
|
|
def from_values(cls, |
|
31
|
|
|
file, |
|
32
|
|
|
start_line=None, |
|
33
|
|
|
start_column=None, |
|
34
|
|
|
end_line=None, |
|
35
|
|
|
end_column=None): |
|
36
|
|
|
start = SourcePosition(file, start_line, start_column) |
|
37
|
|
|
if not end_line: |
|
38
|
|
|
end = None |
|
39
|
|
|
else: |
|
40
|
|
|
end = SourcePosition(file, end_line, end_column) |
|
41
|
|
|
|
|
42
|
|
|
return cls(start, end) |
|
43
|
|
|
|
|
44
|
|
|
@classmethod |
|
45
|
|
|
def from_clang_range(cls, range): |
|
46
|
|
|
""" |
|
47
|
|
|
Creates a SourceRange from a clang SourceRange object. |
|
48
|
|
|
|
|
49
|
|
|
:param range: A cindex.SourceRange object. |
|
50
|
|
|
""" |
|
51
|
|
|
return cls.from_values(range.start.file.name.decode(), |
|
52
|
|
|
range.start.line, |
|
53
|
|
|
range.start.column, |
|
54
|
|
|
range.end.line, |
|
55
|
|
|
range.end.column) |
|
56
|
|
|
|
|
57
|
|
|
@property |
|
58
|
|
|
def file(self): |
|
59
|
|
|
return self.start.file |
|
60
|
|
|
|
|
61
|
|
|
def overlaps(self, other): |
|
62
|
|
|
return self.start <= other.end and self.end >= other.start |
|
63
|
|
|
|