File: rangeinrevision.h

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (134 lines) | stat: -rw-r--r-- 3,989 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
    SPDX-FileCopyrightText: 2010 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#ifndef KDEVPLATFORM_RANGEINREVISION_H
#define KDEVPLATFORM_RANGEINREVISION_H

#include <language/languageexport.h>
#include "cursorinrevision.h"

#include <KTextEditor/Range>

#include <QDebug>

namespace KDevelop {
/**
 * Represents a range (start- and end cursor) within a text document.
 *
 * In KDevelop, this object is used when referencing a ranges that do _not_ point into the
 * most current document revision. Therefore, before applying such a range in the text
 * documents, it has to be translated into the current document revision explicitly, thereby replaying
 * eventual changes (see DUChainBase::translate...)
 */

class KDEVPLATFORMLANGUAGE_EXPORT RangeInRevision
{
public:

    CursorInRevision start, end;

    RangeInRevision(const CursorInRevision& _start, const CursorInRevision& _end) : start(_start)
        , end(_end)
    {
    }

    RangeInRevision(const CursorInRevision& _start, int length) : start(_start)
        , end(_start.line, _start.column + length)
    {
    }

    RangeInRevision()
    {
    }

    RangeInRevision(int sLine, int sCol, int eLine, int eCol) : start(sLine, sCol)
        , end(eLine, eCol)
    {
    }

    static RangeInRevision invalid()
    {
        return RangeInRevision(-1, -1, -1, -1);
    }

    bool isValid() const
    {
        return start.column != -1 || start.line != -1 || end.column != -1 || end.line != -1;
    }

    bool isEmpty() const
    {
        return start == end;
    }

    enum ContainsBehavior {
        Default = 0,
        IncludeBackEdge = 1
    };
    /**
     * Checks if @p position is contained within this range (i.e. >= start and < end)
     * If @p cb is IncludeBackEdge, also checks that @p position == end
     */
    bool contains(const CursorInRevision& position, ContainsBehavior cb = Default) const
    {
        return (position >= start && position < end) || (cb == IncludeBackEdge && position == end);
    }

    bool contains(const RangeInRevision& range) const
    {
        return range.start >= start && range.end <= end;
    }

    bool operator ==(const RangeInRevision& rhs) const
    {
        return start == rhs.start && end == rhs.end;
    }

    bool operator !=(const RangeInRevision& rhs) const
    {
        return !(*this == rhs);
    }

    bool operator <(const RangeInRevision& rhs) const
    {
        return start < rhs.start;
    }

    /// @warning Using this is wrong in most cases! If you want
    ///  to transform this range to the current revision, you should do a proper
    ///  mapping instead through @ref KDevelop::DUChainBase or @ref KDevelop::RevisionReference
    ///  or @ref KDevelop::DocumentChangeTracker
    KTextEditor::Range castToSimpleRange() const
    {
        return KTextEditor::Range(start.castToSimpleCursor(), end.castToSimpleCursor());
    }

    /// @warning Using this is wrong in most cases! If you want
    ///  to transform this range to the current revision, you should do a proper
    ///  mapping instead through @ref KDevelop::DUChainBase or @ref KDevelop::RevisionReference
    ///  or @ref KDevelop::DocumentChangeTracker
    static RangeInRevision castFromSimpleRange(const KTextEditor::Range& range)
    {
        return RangeInRevision(range.start().line(), range.start().column(), range.end().line(), range.end().column());
    }
    ///qDebug() stream operator.  Writes this range to the debug output in a nicely formatted way.
    inline friend QDebug operator<<(QDebug s, const RangeInRevision& range)
    {
        s.nospace() << '[' << range.start << ", " << range.end << ']';
        return s.space();
    }
};

inline size_t qHash(const KDevelop::RangeInRevision& range)
{
    return qHash(range.start) + qHash(range.end) * 41;
}
} // namespace KDevelop

Q_DECLARE_TYPEINFO(KDevelop::RangeInRevision, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(KDevelop::RangeInRevision)

#endif