diff --git a/src/lib/abstracthighlighter.cpp b/src/lib/abstracthighlighter.cpp index f4e4ab4..f69944d 100644 --- a/src/lib/abstracthighlighter.cpp +++ b/src/lib/abstracthighlighter.cpp @@ -1,328 +1,328 @@ /* Copyright (C) 2016 Volker Krause Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "abstracthighlighter.h" #include "abstracthighlighter_p.h" #include "context_p.h" #include "definition_p.h" #include "foldingregion.h" #include "format.h" #include "repository.h" #include "rule_p.h" #include "state.h" #include "state_p.h" #include "ksyntaxhighlighting_logging.h" #include "theme.h" using namespace KSyntaxHighlighting; AbstractHighlighterPrivate::AbstractHighlighterPrivate() { } AbstractHighlighterPrivate::~AbstractHighlighterPrivate() { } void AbstractHighlighterPrivate::ensureDefinitionLoaded() { auto defData = DefinitionData::get(m_definition); if (Q_UNLIKELY(!m_definition.isValid() && defData->repo && !m_definition.name().isEmpty())) { qCDebug(Log) << "Definition became invalid, trying re-lookup."; m_definition = defData->repo->definitionForName(m_definition.name()); defData = DefinitionData::get(m_definition); } if (Q_UNLIKELY(!defData->repo && !defData->name.isEmpty())) qCCritical(Log) << "Repository got deleted while a highlighter is still active!"; if (m_definition.isValid()) defData->load(); } AbstractHighlighter::AbstractHighlighter() : d_ptr(new AbstractHighlighterPrivate) { } AbstractHighlighter::AbstractHighlighter(AbstractHighlighterPrivate *dd) : d_ptr(dd) { } AbstractHighlighter::~AbstractHighlighter() { delete d_ptr; } Definition AbstractHighlighter::definition() const { return d_ptr->m_definition; } void AbstractHighlighter::setDefinition(const Definition &def) { Q_D(AbstractHighlighter); d->m_definition = def; } Theme AbstractHighlighter::theme() const { Q_D(const AbstractHighlighter); return d->m_theme; } void AbstractHighlighter::setTheme(const Theme &theme) { Q_D(AbstractHighlighter); d->m_theme = theme; } /** * Returns the index of the first non-space character. If the line is empty, * or only contains white spaces, text.size() is returned. */ static inline int firstNonSpaceChar(const QString & text) { for (int i = 0; i < text.length(); ++i) { if (!text[i].isSpace()) { return i; } } return text.size(); } State AbstractHighlighter::highlightLine(const QString& text, const State &state) { Q_D(AbstractHighlighter); // verify definition, deal with no highlighting being enabled d->ensureDefinitionLoaded(); if (!d->m_definition.isValid()) { applyFormat(0, text.size(), Format()); return State(); } // verify/initialize state auto defData = DefinitionData::get(d->m_definition); auto newState = state; auto stateData = StateData::get(newState); const DefinitionRef currentDefRef(d->m_definition); if (!stateData->isEmpty() && (stateData->m_defRef != currentDefRef)) { qCDebug(Log) << "Got invalid state, resetting."; stateData->clear(); } if (stateData->isEmpty()) { stateData->push(defData->initialContext(), QStringList()); stateData->m_defRef = currentDefRef; } // process empty lines if (text.isEmpty()) { - while (!stateData->topContext()->lineEmptyContext().isStay()) - d->switchContext(stateData, stateData->topContext()->lineEmptyContext(), QStringList()); + while (!stateData->topContext()->lineEmptyContext().isStay()) { + if (!d->switchContext(stateData, stateData->topContext()->lineEmptyContext(), QStringList())) + break; + } auto context = stateData->topContext(); applyFormat(0, 0, context->attributeFormat()); return newState; } int offset = 0, beginOffset = 0; bool lineContinuation = false; QHash skipOffsets; /** * current active format * stored as pointer to avoid deconstruction/constructions inside the internal loop * the pointers are stable, the formats are either in the contexts or rules */ auto currentFormat = &stateData->topContext()->attributeFormat(); /** * cached first non-space character, needs to be computed if < 0 */ int firstNonSpace = -1; int lastOffset = offset; int endlessLoopingCounter = 0; do { /** * avoid that we loop endless for some broken hl definitions */ if (lastOffset == offset) { ++endlessLoopingCounter; if (endlessLoopingCounter > 1024) { qCDebug(Log) << "Endless state transitions, aborting highlighting of line."; break; } } else { // ensure we made progress, clear the endlessLoopingCounter Q_ASSERT(offset > lastOffset); lastOffset = offset; endlessLoopingCounter = 0; } /** * try to match all rules in the context in order of declaration in XML */ bool isLookAhead = false; int newOffset = 0; const Format *newFormat = nullptr; for (const auto &rule : stateData->topContext()->rules()) { /** * filter out rules that require a specific column */ if ((rule->requiredColumn() >= 0) && (rule->requiredColumn() != offset)) { continue; } /** * filter out rules that only match for leading whitespace */ if (rule->firstNonSpace()) { /** * compute the first non-space lazy * avoids computing it for contexts without any such rules */ if (firstNonSpace < 0) { firstNonSpace = firstNonSpaceChar(text); } /** * can we skip? */ if (offset > firstNonSpace) { continue; } } /** * shall we skip application of this rule? two cases: * - rule can't match at all => currentSkipOffset < 0 * - rule will only match for some higher offset => currentSkipOffset > offset */ const auto currentSkipOffset = skipOffsets.value(rule.get()); if (currentSkipOffset < 0 || currentSkipOffset > offset) continue; const auto newResult = rule->doMatch(text, offset, stateData->topCaptures()); newOffset = newResult.offset(); /** * update skip offset if new one rules out any later match or is larger than current one */ if (newResult.skipOffset() < 0 || newResult.skipOffset() > currentSkipOffset) skipOffsets.insert(rule.get(), newResult.skipOffset()); if (newOffset <= offset) continue; // apply folding if (rule->endRegion().isValid()) applyFolding(offset, newOffset - offset, rule->endRegion()); if (rule->beginRegion().isValid()) applyFolding(offset, newOffset - offset, rule->beginRegion()); if (rule->isLookAhead()) { Q_ASSERT(!rule->context().isStay()); d->switchContext(stateData, rule->context(), newResult.captures()); isLookAhead = true; break; } d->switchContext(stateData, rule->context(), newResult.captures()); newFormat = rule->attributeFormat().isValid() ? &rule->attributeFormat() : &stateData->topContext()->attributeFormat(); if (newOffset == text.size() && std::dynamic_pointer_cast(rule)) lineContinuation = true; break; } if (isLookAhead) continue; if (newOffset <= offset) { // no matching rule if (stateData->topContext()->fallthrough()) { d->switchContext(stateData, stateData->topContext()->fallthroughContext(), QStringList()); continue; } newOffset = offset + 1; newFormat = &stateData->topContext()->attributeFormat(); } /** * if we arrive here, some new format has to be set! */ Q_ASSERT(newFormat); /** * on format change, apply the last one and switch to new one */ if (newFormat != currentFormat && newFormat->id() != currentFormat->id()) { if (offset > 0) applyFormat(beginOffset, offset - beginOffset, *currentFormat); beginOffset = offset; currentFormat = newFormat; } /** * we must have made progress if we arrive here! */ Q_ASSERT(newOffset > offset); offset = newOffset; } while (offset < text.size()); if (beginOffset < offset) applyFormat(beginOffset, text.size() - beginOffset, *currentFormat); while (!stateData->topContext()->lineEndContext().isStay() && !lineContinuation) { if (!d->switchContext(stateData, stateData->topContext()->lineEndContext(), QStringList())) break; } return newState; } bool AbstractHighlighterPrivate::switchContext(StateData *data, const ContextSwitch &contextSwitch, const QStringList &captures) { - for (int i = 0; i < contextSwitch.popCount(); ++i) { - // don't pop the last context if we can't push one - if (data->size() == 1 && !contextSwitch.context()) - return false; - if (data->size() == 0) - break; - data->pop(); - } + // kill as many items as requested from the stack, will always keep the initial context alive! + const bool initialContextSurvived = data->pop(contextSwitch.popCount()); - if (contextSwitch.context()) + // if we have a new context to add, push it + // then we always "succeed" + if (contextSwitch.context()) { data->push(contextSwitch.context(), captures); + return true; + } - Q_ASSERT(!data->isEmpty()); - return true; + // else we abort, if we did try to pop the initial context + return initialContextSurvived; } void AbstractHighlighter::applyFolding(int offset, int length, FoldingRegion region) { Q_UNUSED(offset); Q_UNUSED(length); Q_UNUSED(region); } diff --git a/src/lib/state.cpp b/src/lib/state.cpp index f9abfa9..f970e13 100644 --- a/src/lib/state.cpp +++ b/src/lib/state.cpp @@ -1,114 +1,123 @@ /* Copyright (C) 2016 Volker Krause Copyright (C) 2018 Christoph Cullmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "state.h" #include "state_p.h" #include "context_p.h" #include using namespace KSyntaxHighlighting; StateData* StateData::get(State &state) { state.d.detach(); return state.d.data(); } bool StateData::isEmpty() const { return m_contextStack.isEmpty(); } void StateData::clear() { m_contextStack.clear(); } int StateData::size() const { return m_contextStack.size(); } void StateData::push(Context *context, const QStringList &captures) { Q_ASSERT(context); m_contextStack.push_back(qMakePair(context, captures)); } -void StateData::pop() +bool StateData::pop(int popCount) { - m_contextStack.pop_back(); + // nop if nothing to pop + if (popCount <= 0) { + return true; + } + + // keep the initial context alive in any case + Q_ASSERT(!isEmpty()); + const bool initialContextSurvived = m_contextStack.size() > popCount; + m_contextStack.resize(std::max(1, m_contextStack.size() - popCount)); + return initialContextSurvived; } Context* StateData::topContext() const { Q_ASSERT(!isEmpty()); return m_contextStack.last().first; } const QStringList &StateData::topCaptures() const { Q_ASSERT(!isEmpty()); return m_contextStack.last().second; } State::State() : d(new StateData) { } State::State(const State &other) : d(other.d) { } State::~State() { } State& State::operator=(const State &other) { d = other.d; return *this; } bool State::operator==(const State &other) const { // use pointer equal as shortcut for shared states return (d == other.d) || (d->m_contextStack == other.d->m_contextStack && d->m_defRef == other.d->m_defRef); } bool State::operator!=(const State &other) const { return !(*this == other); } bool State::indentationBasedFoldingEnabled() const { if (d->m_contextStack.isEmpty()) return false; return d->m_contextStack.last().first->indentationBasedFoldingEnabled(); } diff --git a/src/lib/state_p.h b/src/lib/state_p.h index eed129b..a99192b 100644 --- a/src/lib/state_p.h +++ b/src/lib/state_p.h @@ -1,73 +1,81 @@ /* Copyright (C) 2016 Volker Krause Copyright (C) 2018 Christoph Cullmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef KSYNTAXHIGHLIGHTING_STATE_P_H #define KSYNTAXHIGHLIGHTING_STATE_P_H #include #include #include "definitionref_p.h" QT_BEGIN_NAMESPACE class QStringList; QT_END_NAMESPACE namespace KSyntaxHighlighting { class Context; class StateData : public QSharedData { friend class State; friend class AbstractHighlighter; public: StateData() = default; static StateData* get(State &state); bool isEmpty() const; void clear(); int size() const; void push(Context *context, const QStringList &captures); - void pop(); + + /** + * Pop the number of elements given from the top of the current stack. + * Will not pop the initial element. + * @param popCount number of elements to pop + * @return false if one has tried to pop the initial context, else true + */ + bool pop(int popCount); + Context* topContext() const; const QStringList &topCaptures() const; private: /** * weak reference to the used definition to filter out invalid states */ DefinitionRef m_defRef; /** * the context stack combines the active context + valid captures */ QVector> m_contextStack; }; } #endif