Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

coalib/results/SourceRange.py (33 issues)

1
from os.path import relpath
2
3
from coalib.misc.Decorators import enforce_signature, get_public_members
4
from coalib.results.SourcePosition import SourcePosition
5
from coalib.results.TextRange import TextRange
6
from coalib.results.AbsolutePosition import AbsolutePosition
7
8
9
class SourceRange(TextRange):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TextRange does not seem to be defined.
Loading history...
10
11
    @enforce_signature
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable enforce_signature does not seem to be defined.
Loading history...
12
    def __init__(self,
13
                 start: SourcePosition,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SourcePosition does not seem to be defined.
Loading history...
14
                 end: (SourcePosition, None)=None):
15
        """
16
        Creates a new SourceRange.
17
18
        :param start:       A SourcePosition indicating the start of the range.
19
        :param end:         A SourcePosition indicating the end of the range.
20
                            If ``None`` is given, the start object will be used
21
                            here. end must be in the same file and be greater
22
                            than start as negative ranges are not allowed.
23
        :raises TypeError:  Raised when
24
                            - start is no SourcePosition or None.
25
                            - end is no SourcePosition.
26
        :raises ValueError: Raised when file of start and end mismatch.
27
        """
28
        TextRange.__init__(self, start, end)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable start does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable end does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
29
30
        if self.start.file != self.end.file:
31
            raise ValueError("File of start and end position do not match.")
32
33
    @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
34
    def from_values(cls,
35
                    file,
36
                    start_line=None,
37
                    start_column=None,
38
                    end_line=None,
39
                    end_column=None):
40
        start = SourcePosition(file, start_line, start_column)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable start_column does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable start_line does not seem to be defined.
Loading history...
41
        if end_line or (end_column and end_column > start_column):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable end_column does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable end_line does not seem to be defined.
Loading history...
42
            end = SourcePosition(file, end_line if end_line else start_line,
43
                                 end_column)
44
        else:
45
            end = None
46
47
        return cls(start, end)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable end does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable start does not seem to be defined.
Loading history...
48
49
    @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
50
    def from_clang_range(cls, range):
51
        """
52
        Creates a SourceRange from a clang SourceRange object.
53
54
        :param range: A cindex.SourceRange object.
55
        """
56
        return cls.from_values(range.start.file.name,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable range does not seem to be defined.
Loading history...
57
                               range.start.line,
58
                               range.start.column,
59
                               range.end.line,
60
                               range.end.column)
61
62
    @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
63
    @enforce_signature
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable enforce_signature does not seem to be defined.
Loading history...
64
    def from_absolute_position(cls,
65
                               file: str,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
66
                               position_start: AbsolutePosition,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable AbsolutePosition does not seem to be defined.
Loading history...
67
                               position_end: (AbsolutePosition, None)=None):
68
        """
69
        Creates a SourceRange from a start and end positions.
70
71
        :param file:           Name of the file.
72
        :param position_start: Start of range given by AbsolutePosition.
73
        :param position_end:   End of range given by AbsolutePosition or None.
74
        """
75
        start = SourcePosition(file, position_start.line, position_start.column)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable position_start does not seem to be defined.
Loading history...
76
        end = None
77
        if position_end:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable position_end does not seem to be defined.
Loading history...
78
            end = SourcePosition(file, position_end.line, position_end.column)
79
        return cls(start, end)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable end does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable start does not seem to be defined.
Loading history...
80
81
    @property
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable property does not seem to be defined.
Loading history...
82
    def file(self):
83
        return self.start.file
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
84
85
    def expand(self, file_contents):
86
        """
87
        Passes a new SourceRange that covers the same area of a file as this
88
        one would. All values of None get replaced with absolute values.
89
90
        values of None will be interpreted as follows:
91
        self.start.line is None:   -> 1
92
        self.start.column is None: -> 1
93
        self.end.line is None:     -> last line of file
94
        self.end.column is None:   -> last column of self.end.line
95
96
        :param file_contents: File contents of the applicable file
97
        :return:              TextRange with absolute values
98
        """
99
        tr = TextRange.expand(self, file_contents)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file_contents does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
100
101
        return SourceRange.from_values(self.file,
102
                                       tr.start.line,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable tr does not seem to be defined.
Loading history...
103
                                       tr.start.column,
104
                                       tr.end.line,
105
                                       tr.end.column)
106
107
    def __json__(self, use_relpath=False):
108
        _dict = get_public_members(self)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
109
        if use_relpath:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable use_relpath does not seem to be defined.
Loading history...
110
            _dict['file'] = relpath(_dict['file'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _dict does not seem to be defined.
Loading history...
111
        return _dict
112