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 | |
15 | using namespace KSyntaxHighlighting; |
16 | |
17 | StateData *StateData::reset(State &state) |
18 | { |
19 | auto *p = new StateData(); |
20 | state.d.reset(ptr: p); |
21 | return p; |
22 | } |
23 | |
24 | StateData *StateData::detach(State &state) |
25 | { |
26 | state.d.detach(); |
27 | return state.d.data(); |
28 | } |
29 | |
30 | void StateData::push(Context *context, QStringList &&captures) |
31 | { |
32 | Q_ASSERT(context); |
33 | m_contextStack.push_back(x: StackValue{.context: context, .captures: std::move(captures)}); |
34 | } |
35 | |
36 | bool 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 | |
50 | State::State() = default; |
51 | |
52 | State::State(State &&other) noexcept = default; |
53 | |
54 | State::State(const State &other) noexcept = default; |
55 | |
56 | State::~State() = default; |
57 | |
58 | State &State::operator=(State &&other) noexcept = default; |
59 | |
60 | State &State::operator=(const State &other) noexcept = default; |
61 | |
62 | bool 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 | |
68 | bool State::operator!=(const State &other) const |
69 | { |
70 | return !(*this == other); |
71 | } |
72 | |
73 | bool 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 | |
81 | std::size_t KSyntaxHighlighting::qHash(const State &state, std::size_t seed) |
82 | { |
83 | return state.d ? qHashMulti(seed, args: *state.d) : 0; |
84 | } |
85 |