aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/syntax-highlighting/src/lib/state.cpp
blob: dca58b35b7c01b397db2df1fb78dcf49ba20a9b4 (plain)
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
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
    SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "state.h"
#include "state_p.h"

#include "context_p.h"

#include <QStringList>

using namespace KSyntaxHighlighting;

StateData *StateData::reset(State &state)
{
    auto *p = new StateData();
    state.d.reset(p);
    return p;
}

StateData *StateData::detach(State &state)
{
    state.d.detach();
    return state.d.data();
}

void StateData::push(Context *context, QStringList &&captures)
{
    Q_ASSERT(context);
    m_contextStack.push_back(StackValue{context, std::move(captures)});
}

bool StateData::pop(int popCount)
{
    // nop if nothing to pop
    if (popCount <= 0) {
        return true;
    }

    // keep the initial context alive in any case
    Q_ASSERT(!m_contextStack.empty());
    const bool initialContextSurvived = int(m_contextStack.size()) > popCount;
    m_contextStack.resize(std::max(1, int(m_contextStack.size()) - popCount));
    return initialContextSurvived;
}

State::State() = default;

State::State(State &&other) noexcept = default;

State::State(const State &other) noexcept = default;

State::~State() = default;

State &State::operator=(State &&other) noexcept = default;

State &State::operator=(const State &other) noexcept = default;

bool State::operator==(const State &other) const
{
    // use pointer equal as shortcut for shared states
    return (d == other.d) || (d && other.d && d->m_contextStack == other.d->m_contextStack && d->m_defId == other.d->m_defId);
}

bool State::operator!=(const State &other) const
{
    return !(*this == other);
}

bool State::indentationBasedFoldingEnabled() const
{
    if (!d || d->m_contextStack.empty()) {
        return false;
    }
    return d->m_contextStack.back().context->indentationBasedFoldingEnabled();
}

std::size_t KSyntaxHighlighting::qHash(const State &state, std::size_t seed)
{
    return state.d ? qHashMulti(seed, *state.d) : 0;
}