File indexing completed on 2024-05-12 04:02:20

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 #include "state.h"
0009 #include "state_p.h"
0010 
0011 #include "context_p.h"
0012 
0013 #include <QStringList>
0014 
0015 using namespace KSyntaxHighlighting;
0016 
0017 StateData *StateData::reset(State &state)
0018 {
0019     auto *p = new StateData();
0020     state.d.reset(p);
0021     return p;
0022 }
0023 
0024 StateData *StateData::detach(State &state)
0025 {
0026     state.d.detach();
0027     return state.d.data();
0028 }
0029 
0030 void StateData::push(Context *context, QStringList &&captures)
0031 {
0032     Q_ASSERT(context);
0033     m_contextStack.push_back(StackValue{context, std::move(captures)});
0034 }
0035 
0036 bool StateData::pop(int popCount)
0037 {
0038     // nop if nothing to pop
0039     if (popCount <= 0) {
0040         return true;
0041     }
0042 
0043     // keep the initial context alive in any case
0044     Q_ASSERT(!m_contextStack.empty());
0045     const bool initialContextSurvived = int(m_contextStack.size()) > popCount;
0046     m_contextStack.resize(std::max(1, int(m_contextStack.size()) - popCount));
0047     return initialContextSurvived;
0048 }
0049 
0050 State::State() = default;
0051 
0052 State::State(State &&other) noexcept = default;
0053 
0054 State::State(const State &other) noexcept = default;
0055 
0056 State::~State() = default;
0057 
0058 State &State::operator=(State &&other) noexcept = default;
0059 
0060 State &State::operator=(const State &other) noexcept = default;
0061 
0062 bool State::operator==(const State &other) const
0063 {
0064     // use pointer equal as shortcut for shared states
0065     return (d == other.d) || (d && other.d && d->m_contextStack == other.d->m_contextStack && d->m_defId == other.d->m_defId);
0066 }
0067 
0068 bool State::operator!=(const State &other) const
0069 {
0070     return !(*this == other);
0071 }
0072 
0073 bool State::indentationBasedFoldingEnabled() const
0074 {
0075     if (!d || d->m_contextStack.empty()) {
0076         return false;
0077     }
0078     return d->m_contextStack.back().context->indentationBasedFoldingEnabled();
0079 }
0080 
0081 std::size_t KSyntaxHighlighting::qHash(const State &state, std::size_t seed)
0082 {
0083     return state.d ? qHashMulti(seed, *state.d) : 0;
0084 }