1/*
2 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
3 SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org>
4
5 SPDX-License-Identifier: MIT
6*/
7
8#include "state.h"
9#include "state_p.h"
10
11#include "context_p.h"
12
13#include <QStringList>
14
15using namespace KSyntaxHighlighting;
16
17StateData *StateData::reset(State &state)
18{
19 auto *p = new StateData();
20 state.d.reset(p);
21 return p;
22}
23
24StateData *StateData::detach(State &state)
25{
26 state.d.detach();
27 return state.d.data();
28}
29
30void StateData::push(Context *context, QStringList &&captures)
31{
32 Q_ASSERT(context);
33 m_contextStack.push_back(x: StackValue{context, std::move(captures)});
34}
35
36bool StateData::pop(int popCount)
37{
38 // nop if nothing to pop
39 if (popCount <= 0) {
40 return true;
41 }
42
43 // keep the initial context alive in any case
44 Q_ASSERT(!m_contextStack.empty());
45 const bool initialContextSurvived = int(m_contextStack.size()) > popCount;
46 m_contextStack.resize(new_size: std::max(a: 1, b: int(m_contextStack.size()) - popCount));
47 return initialContextSurvived;
48}
49
50State::State() = default;
51
52State::State(State &&other) noexcept = default;
53
54State::State(const State &other) = default;
55
56State::~State() = default;
57
58State &State::operator=(State &&other) noexcept = default;
59
60State &State::operator=(const State &other) = default;
61
62bool State::operator==(const State &other) const
63{
64 // use pointer equal as shortcut for shared states
65 return (d == other.d) || (d && other.d && d->m_contextStack == other.d->m_contextStack && d->m_defId == other.d->m_defId);
66}
67
68bool State::operator!=(const State &other) const
69{
70 return !(*this == other);
71}
72
73bool State::indentationBasedFoldingEnabled() const
74{
75 if (!d || d->m_contextStack.empty()) {
76 return false;
77 }
78 return d->m_contextStack.back().context->indentationBasedFoldingEnabled();
79}
80
81std::size_t KSyntaxHighlighting::qHash(const State &state, std::size_t seed)
82{
83 return state.d ? qHashMulti(seed, *state.d) : 0;
84}
85

source code of syntax-highlighting/src/lib/state.cpp