KSyntaxHighlighting

state.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Volker Krause <[email protected]>
3  SPDX-FileCopyrightText: 2018 Christoph Cullmann <[email protected]>
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::get(State &state)
18 {
19  // create state data on demand, to make default state construction cheap
20  if (!state.d) {
21  state.d = new StateData();
22  } else {
23  state.d.detach();
24  }
25  return state.d.data();
26 }
27 
28 bool StateData::isEmpty() const
29 {
30  return m_contextStack.isEmpty();
31 }
32 
33 void StateData::clear()
34 {
35  m_contextStack.clear();
36 }
37 
38 int StateData::size() const
39 {
40  return m_contextStack.size();
41 }
42 
43 void StateData::push(Context *context, const QStringList &captures)
44 {
45  Q_ASSERT(context);
46  m_contextStack.push_back(qMakePair(context, captures));
47 }
48 
49 bool StateData::pop(int popCount)
50 {
51  // nop if nothing to pop
52  if (popCount <= 0) {
53  return true;
54  }
55 
56  // keep the initial context alive in any case
57  Q_ASSERT(!isEmpty());
58  const bool initialContextSurvived = m_contextStack.size() > popCount;
59  m_contextStack.resize(std::max(1, int(m_contextStack.size()) - popCount));
60  return initialContextSurvived;
61 }
62 
63 Context *StateData::topContext() const
64 {
65  Q_ASSERT(!isEmpty());
66  return m_contextStack.last().first;
67 }
68 
69 const QStringList &StateData::topCaptures() const
70 {
71  Q_ASSERT(!isEmpty());
72  return m_contextStack.last().second;
73 }
74 
76 {
77 }
78 
79 State::State(const State &other)
80  : d(other.d)
81 {
82 }
83 
84 State::~State()
85 {
86 }
87 
88 State &State::operator=(const State &other)
89 {
90  d = other.d;
91  return *this;
92 }
93 
94 bool State::operator==(const State &other) const
95 {
96  // use pointer equal as shortcut for shared states
97  return (d == other.d) || (d && other.d && d->m_contextStack == other.d->m_contextStack && d->m_defId == other.d->m_defId);
98 }
99 
100 bool State::operator!=(const State &other) const
101 {
102  return !(*this == other);
103 }
104 
106 {
107  if (!d || d->m_contextStack.isEmpty()) {
108  return false;
109  }
110  return d->m_contextStack.last().first->indentationBasedFoldingEnabled();
111 }
Opaque handle to the state of the highlighting engine.
Definition: state.h:25
bool operator==(const State &other) const
Compares two states for equality.
Definition: state.cpp:94
bool indentationBasedFoldingEnabled() const
Returns whether or not indentation-based folding is enabled in this state.
Definition: state.cpp:105
bool operator!=(const State &other) const
Compares two states for inequality.
Definition: state.cpp:100
State()
Creates an initial state, ie.
Definition: state.cpp:75
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:09:17 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.