diff --git a/src/lib/abstracthighlighter.cpp b/src/lib/abstracthighlighter.cpp index 430c7a3..9d14a4b 100644 --- a/src/lib/abstracthighlighter.cpp +++ b/src/lib/abstracthighlighter.cpp @@ -1,276 +1,277 @@ /* 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(); } /** * Returns the index of the first non-space character. If the line is empty, * or only contains white spaces, -1 is returned. */ static inline int firstNonSpaceChar(const QString & text) { for (int i = 0; i < text.length(); ++i) { if (!text[i].isSpace()) { return i; } } return -1; } 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; } 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); - if (stateData->m_defData && defData != stateData->m_defData) { + 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_defData = defData; + stateData->m_defRef = currentDefRef; } // process empty lines if (text.isEmpty()) { while (!stateData->topContext()->lineEmptyContext().isStay()) d->switchContext(stateData, stateData->topContext()->lineEmptyContext(), QStringList()); auto context = stateData->topContext(); applyFormat(0, 0, context->formatByName(context->attribute())); return newState; } Q_ASSERT(!stateData->isEmpty()); int firstNonSpace = firstNonSpaceChar(text); if (firstNonSpace < 0) { firstNonSpace = text.size(); } int offset = 0, beginOffset = 0; auto currentLookupContext = stateData->topContext(); auto currentFormat = currentLookupContext->attribute(); bool lineContinuation = false; QHash skipOffsets; do { bool isLookAhead = false; int newOffset = 0; QString newFormat; auto newLookupContext = currentLookupContext; foreach (const auto &rule, stateData->topContext()->rules()) { /** * 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; // filter out rules that only match for leading whitespace if (rule->firstNonSpace() && (offset > firstNonSpace)) { continue; } // filter out rules that require a specific column if ((rule->requiredColumn() >= 0) && (rule->requiredColumn() != offset)) { continue; } const auto newResult = rule->match(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; } newLookupContext = stateData->topContext(); d->switchContext(stateData, rule->context(), newResult.captures()); newFormat = rule->attribute().isEmpty() ? stateData->topContext()->attribute() : rule->attribute(); 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; newLookupContext = stateData->topContext(); newFormat = newLookupContext->attribute(); } if (newFormat != currentFormat /*|| currentLookupDef != newLookupDef*/) { if (offset > 0) applyFormat(beginOffset, offset - beginOffset, currentLookupContext->formatByName(currentFormat)); beginOffset = offset; currentFormat = newFormat; currentLookupContext = newLookupContext; } Q_ASSERT(newOffset > offset); offset = newOffset; } while (offset < text.size()); if (beginOffset < offset) applyFormat(beginOffset, text.size() - beginOffset, currentLookupContext->formatByName(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(); } if (contextSwitch.context()) data->push(contextSwitch.context(), captures); Q_ASSERT(!data->isEmpty()); return true; } void AbstractHighlighter::applyFolding(int offset, int length, FoldingRegion region) { Q_UNUSED(offset); Q_UNUSED(length); Q_UNUSED(region); } diff --git a/src/lib/definition.cpp b/src/lib/definition.cpp index 2bd2cb1..aabc146 100644 --- a/src/lib/definition.cpp +++ b/src/lib/definition.cpp @@ -1,785 +1,794 @@ /* Copyright (C) 2016 Volker Krause Copyright (C) 2018 Dominik Haumann 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 "definition.h" #include "definition_p.h" #include "definitionref_p.h" #include "context_p.h" #include "format.h" #include "format_p.h" #include "repository_p.h" #include "rule_p.h" #include "ksyntaxhighlighting_logging.h" #include "ksyntaxhighlighting_version.h" #include "xml_p.h" #include #include #include #include #include #include #include #include #include using namespace KSyntaxHighlighting; DefinitionData::DefinitionData() : wordDelimiters(QStringLiteral("\t !%&()*+,-./:;<=>?[\\]^{|}~")) // must be sorted! , wordWrapDelimiters(wordDelimiters) { } DefinitionData::~DefinitionData() { qDeleteAll(contexts); } DefinitionData* DefinitionData::get(const Definition &def) { return def.d.get(); } Definition::Definition() : d(new DefinitionData) { } Definition::Definition(const Definition &other) : d(other.d) { d->q = *this; } Definition::Definition(const std::shared_ptr &dd) : d(dd) { } Definition::~Definition() { } Definition& Definition::operator=(const Definition &rhs) { d = rhs.d; return *this; } bool Definition::operator==(const Definition &other) const { return d->fileName == other.d->fileName; } bool Definition::operator!=(const Definition& other) const { return d->fileName != other.d->fileName; } bool Definition::isValid() const { return d->repo && !d->fileName.isEmpty() && !d->name.isEmpty(); } QString Definition::filePath() const { return d->fileName; } QString Definition::name() const { return d->name; } QString Definition::translatedName() const { return QCoreApplication::instance()->translate("Language", d->name.toUtf8().constData()); } QString Definition::section() const { return d->section; } QString Definition::translatedSection() const { return QCoreApplication::instance()->translate("Language Section", d->section.toUtf8().constData()); } QVector Definition::mimeTypes() const { return d->mimetypes; } QVector Definition::extensions() const { return d->extensions; } int Definition::version() const { return d->version; } int Definition::priority() const { return d->priority; } bool Definition::isHidden() const { return d->hidden; } QString Definition::style() const { return d->style; } QString Definition::indenter() const { return d->indenter; } QString Definition::author() const { return d->author; } QString Definition::license() const { return d->license; } bool Definition::isWordDelimiter(QChar c) const { d->load(); return d->isWordDelimiter(c); } bool Definition::isWordWrapDelimiter(QChar c) const { d->load(); return std::binary_search(d->wordWrapDelimiters.constBegin(), d->wordWrapDelimiters.constEnd(), c); } bool Definition::foldingEnabled() const { d->load(); if (d->hasFoldingRegions || indentationBasedFoldingEnabled()) { return true; } // check included definitions for (const auto &def : includedDefinitions()) { if (def.foldingEnabled()) { d->hasFoldingRegions = true; break; } } return d->hasFoldingRegions; } bool Definition::indentationBasedFoldingEnabled() const { d->load(); return d->indentationBasedFolding; } QStringList Definition::foldingIgnoreList() const { d->load(); return d->foldingIgnoreList; } QStringList Definition::keywordLists() const { d->load(); return d->keywordLists.keys(); } QStringList Definition::keywordList(const QString& name) const { d->load(); return d->keywordList(name).keywords(); } QVector Definition::formats() const { d->load(); // sort formats so that the order matches the order of the itemDatas in the xml files. auto formatList = QVector::fromList(d->formats.values()); std::sort(formatList.begin(), formatList.end(), [](const KSyntaxHighlighting::Format & lhs, const KSyntaxHighlighting::Format & rhs){ return lhs.id() < rhs.id(); }); return formatList; } QVector Definition::includedDefinitions() const { d->load(); // init worklist and result used as guard with this definition QVector queue{*this}; QVector definitions{*this}; while (!queue.isEmpty()) { // Iterate all context rules to find associated Definitions. This will // automatically catch other Definitions referenced with IncludeRuldes or ContextSwitch. const auto definition = queue.takeLast(); for (const auto & context : definition.d->contexts) { // handle context switch attributes of this context itself for (const auto switchContext : {context->lineEndContext().context(), context->lineEmptyContext().context(), context->fallthroughContext().context()}) { if (switchContext) { if (!definitions.contains(switchContext->definition())) { queue.push_back(switchContext->definition()); definitions.push_back(switchContext->definition()); } } } // handle the embedded rules for (const auto &rule : context->rules()) { // handle include rules like inclusion if (!definitions.contains(rule->definition())) { queue.push_back(rule->definition()); definitions.push_back(rule->definition()); } // handle context switch context inclusion if (auto switchContext = rule->context().context()) { if (!definitions.contains(switchContext->definition())) { queue.push_back(switchContext->definition()); definitions.push_back(switchContext->definition()); } } } } } // remove the 1st entry, since it is this Definition definitions.pop_front(); return definitions; } QString Definition::singleLineCommentMarker() const { d->load(); return d->singleLineCommentMarker; } CommentPosition Definition::singleLineCommentPosition() const { d->load(); return d->singleLineCommentPosition; } QPair Definition::multiLineCommentMarker() const { d->load(); return { d->multiLineCommentStartMarker, d->multiLineCommentEndMarker }; } QVector> Definition::characterEncodings() const { d->load(); return d->characterEncodings; } Context* DefinitionData::initialContext() const { Q_ASSERT(!contexts.isEmpty()); return contexts.first(); } Context* DefinitionData::contextByName(const QString& name) const { foreach (auto context, contexts) { if (context->name() == name) return context; } return nullptr; } KeywordList DefinitionData::keywordList(const QString& name) const { return keywordLists.value(name); } bool DefinitionData::isWordDelimiter(QChar c) const { return std::binary_search(wordDelimiters.constBegin(), wordDelimiters.constEnd(), c); } Format DefinitionData::formatByName(const QString& name) const { const auto it = formats.constFind(name); if (it != formats.constEnd()) return it.value(); return Format(); } bool DefinitionData::isLoaded() const { return !contexts.isEmpty(); } bool DefinitionData::load() { if (fileName.isEmpty()) return false; if (isLoaded()) return true; QFile file(fileName); if (!file.open(QFile::ReadOnly)) return false; QXmlStreamReader reader(&file); while (!reader.atEnd()) { const auto token = reader.readNext(); if (token != QXmlStreamReader::StartElement) continue; if (reader.name() == QLatin1String("highlighting")) loadHighlighting(reader); else if (reader.name() == QLatin1String("general")) loadGeneral(reader); } for (auto it = keywordLists.begin(); it != keywordLists.end(); ++it) (*it).setCaseSensitivity(caseSensitive); foreach (auto context, contexts) { context->resolveContexts(); context->resolveIncludes(); } Q_ASSERT(std::is_sorted(wordDelimiters.constBegin(), wordDelimiters.constEnd())); return true; } void DefinitionData::clear() { // keep only name and repo, so we can re-lookup to make references persist over repo reloads keywordLists.clear(); qDeleteAll(contexts); contexts.clear(); formats.clear(); fileName.clear(); section.clear(); style.clear(); indenter.clear(); author.clear(); license.clear(); mimetypes.clear(); extensions.clear(); wordDelimiters = QStringLiteral("\t !%&()*+,-./:;<=>?[\\]^{|}~"); // must be sorted! wordWrapDelimiters = wordDelimiters; caseSensitive = Qt::CaseSensitive; version = 0.0f; priority = 0; hidden = false; } bool DefinitionData::loadMetaData(const QString& definitionFileName) { fileName = definitionFileName; QFile file(definitionFileName); if (!file.open(QFile::ReadOnly)) return false; QXmlStreamReader reader(&file); while (!reader.atEnd()) { const auto token = reader.readNext(); if (token != QXmlStreamReader::StartElement) continue; if (reader.name() == QLatin1String("language")) { return loadLanguage(reader); } } return false; } bool DefinitionData::loadMetaData(const QString &file, const QJsonObject &obj) { name = obj.value(QLatin1String("name")).toString(); section = obj.value(QLatin1String("section")).toString(); version = obj.value(QLatin1String("version")).toInt(); priority = obj.value(QLatin1String("priority")).toInt(); style = obj.value(QLatin1String("style")).toString(); author = obj.value(QLatin1String("author")).toString(); license = obj.value(QLatin1String("license")).toString(); indenter = obj.value(QLatin1String("indenter")).toString(); hidden = obj.value(QLatin1String("hidden")).toBool(); fileName = file; const auto exts = obj.value(QLatin1String("extensions")).toString(); foreach (const auto &ext, exts.split(QLatin1Char(';'), QString::SkipEmptyParts)) extensions.push_back(ext); const auto mts = obj.value(QLatin1String("mimetype")).toString(); foreach (const auto &mt, mts.split(QLatin1Char(';'), QString::SkipEmptyParts)) mimetypes.push_back(mt); return true; } bool DefinitionData::loadLanguage(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("language")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); if (!checkKateVersion(reader.attributes().value(QStringLiteral("kateversion")))) return false; name = reader.attributes().value(QStringLiteral("name")).toString(); section = reader.attributes().value(QStringLiteral("section")).toString(); // toFloat instead of toInt for backward compatibility with old Kate files version = reader.attributes().value(QStringLiteral("version")).toFloat(); priority = reader.attributes().value(QStringLiteral("priority")).toInt(); hidden = Xml::attrToBool(reader.attributes().value(QStringLiteral("hidden"))); style = reader.attributes().value(QStringLiteral("style")).toString(); indenter = reader.attributes().value(QStringLiteral("indenter")).toString(); author = reader.attributes().value(QStringLiteral("author")).toString(); license = reader.attributes().value(QStringLiteral("license")).toString(); const auto exts = reader.attributes().value(QStringLiteral("extensions")).toString(); foreach (const auto &ext, exts.split(QLatin1Char(';'), QString::SkipEmptyParts)) extensions.push_back(ext); const auto mts = reader.attributes().value(QStringLiteral("mimetype")).toString(); foreach (const auto &mt, mts.split(QLatin1Char(';'), QString::SkipEmptyParts)) mimetypes.push_back(mt); if (reader.attributes().hasAttribute(QStringLiteral("casesensitive"))) caseSensitive = Xml::attrToBool(reader.attributes().value(QStringLiteral("casesensitive"))) ? Qt::CaseSensitive : Qt::CaseInsensitive; return true; } void DefinitionData::loadHighlighting(QXmlStreamReader& reader) { Q_ASSERT(reader.name() == QLatin1String("highlighting")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: if (reader.name() == QLatin1String("list")) { KeywordList keywords; keywords.load(reader); keywordLists.insert(keywords.name(), keywords); } else if (reader.name() == QLatin1String("contexts")) { loadContexts(reader); reader.readNext(); } else if (reader.name() == QLatin1String("itemDatas")) { loadItemData(reader); } else { reader.readNext(); } break; case QXmlStreamReader::EndElement: return; default: reader.readNext(); break; } } } void DefinitionData::loadContexts(QXmlStreamReader& reader) { Q_ASSERT(reader.name() == QLatin1String("contexts")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: if (reader.name() == QLatin1String("context")) { auto context = new Context; context->setDefinition(q); context->load(reader); contexts.push_back(context); } reader.readNext(); break; case QXmlStreamReader::EndElement: return; default: reader.readNext(); break; } } } void DefinitionData::loadItemData(QXmlStreamReader& reader) { Q_ASSERT(reader.name() == QLatin1String("itemDatas")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: if (reader.name() == QLatin1String("itemData")) { Format f; auto formatData = FormatPrivate::detachAndGet(f); formatData->definition = q; formatData->load(reader); formatData->id = RepositoryPrivate::get(repo)->nextFormatId(); formats.insert(f.name(), f); reader.readNext(); } reader.readNext(); break; case QXmlStreamReader::EndElement: return; default: reader.readNext(); break; } } } void DefinitionData::loadGeneral(QXmlStreamReader& reader) { Q_ASSERT(reader.name() == QLatin1String("general")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); reader.readNext(); // reference counter to count XML child elements, to not return too early int elementRefCounter = 1; while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: ++elementRefCounter; if (reader.name() == QLatin1String("keywords")) { if (reader.attributes().hasAttribute(QStringLiteral("casesensitive"))) caseSensitive = Xml::attrToBool(reader.attributes().value(QStringLiteral("casesensitive"))) ? Qt::CaseSensitive : Qt::CaseInsensitive; // adapt sorted wordDelimiters wordDelimiters += reader.attributes().value(QStringLiteral("additionalDeliminator")); std::sort(wordDelimiters.begin(), wordDelimiters.end()); auto it = std::unique(wordDelimiters.begin(), wordDelimiters.end()); wordDelimiters.truncate(std::distance(wordDelimiters.begin(), it)); foreach (const auto c, reader.attributes().value(QLatin1String("weakDeliminator"))) wordDelimiters.remove(c); // adaptWordWrapDelimiters, and sort wordWrapDelimiters = reader.attributes().value(QStringLiteral("wordWrapDeliminator")).toString(); std::sort(wordWrapDelimiters.begin(), wordWrapDelimiters.end()); if (wordWrapDelimiters.isEmpty()) wordWrapDelimiters = wordDelimiters; } else if (reader.name() == QLatin1String("folding")) { if (reader.attributes().hasAttribute(QStringLiteral("indentationsensitive"))) indentationBasedFolding = Xml::attrToBool(reader.attributes().value(QStringLiteral("indentationsensitive"))); } else if (reader.name() == QLatin1String("emptyLines")) { loadFoldingIgnoreList(reader); } else if (reader.name() == QLatin1String("comments")) { loadComments(reader); } else if (reader.name() == QLatin1String("spellchecking")) { loadSpellchecking(reader); } else { reader.skipCurrentElement(); } reader.readNext(); break; case QXmlStreamReader::EndElement: --elementRefCounter; if (elementRefCounter == 0) return; reader.readNext(); break; default: reader.readNext(); break; } } } void DefinitionData::loadComments(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("comments")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); reader.readNext(); // reference counter to count XML child elements, to not return too early int elementRefCounter = 1; while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: ++elementRefCounter; if (reader.name() == QLatin1String("comment")) { const bool isSingleLine = reader.attributes().value(QStringLiteral("name")) == QStringLiteral("singleLine"); if (isSingleLine) { singleLineCommentMarker = reader.attributes().value(QStringLiteral("start")).toString(); const bool afterWhiteSpace = reader.attributes().value(QStringLiteral("position")).toString() == QStringLiteral("afterwhitespace"); singleLineCommentPosition = afterWhiteSpace ? CommentPosition::AfterWhitespace : CommentPosition::StartOfLine; } else { multiLineCommentStartMarker = reader.attributes().value(QStringLiteral("start")).toString(); multiLineCommentEndMarker = reader.attributes().value(QStringLiteral("end")).toString(); } } reader.readNext(); break; case QXmlStreamReader::EndElement: --elementRefCounter; if (elementRefCounter == 0) return; reader.readNext(); break; default: reader.readNext(); break; } } } void DefinitionData::loadFoldingIgnoreList(QXmlStreamReader& reader) { Q_ASSERT(reader.name() == QLatin1String("emptyLines")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); reader.readNext(); // reference counter to count XML child elements, to not return too early int elementRefCounter = 1; while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: ++elementRefCounter; if (reader.name() == QLatin1String("emptyLine")) { foldingIgnoreList << reader.attributes().value(QStringLiteral("regexpr")).toString(); } reader.readNext(); break; case QXmlStreamReader::EndElement: --elementRefCounter; if (elementRefCounter == 0) return; reader.readNext(); break; default: reader.readNext(); break; } } } void DefinitionData::loadSpellchecking(QXmlStreamReader &reader) { Q_ASSERT(reader.name() == QLatin1String("spellchecking")); Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); reader.readNext(); // reference counter to count XML child elements, to not return too early int elementRefCounter = 1; while (!reader.atEnd()) { switch (reader.tokenType()) { case QXmlStreamReader::StartElement: ++elementRefCounter; if (reader.name() == QLatin1String("encoding")) { const auto charRef = reader.attributes().value(QStringLiteral("char")); if (!charRef.isEmpty()) { const auto str = reader.attributes().value(QStringLiteral("string")).toString(); characterEncodings.push_back({ charRef[0], str }); } } reader.readNext(); break; case QXmlStreamReader::EndElement: --elementRefCounter; if (elementRefCounter == 0) return; reader.readNext(); break; default: reader.readNext(); break; } } } bool DefinitionData::checkKateVersion(const QStringRef& verStr) { const auto idx = verStr.indexOf(QLatin1Char('.')); if (idx <= 0) { qCWarning(Log) << "Skipping" << fileName << "due to having no valid kateversion attribute:" << verStr; return false; } const auto major = verStr.left(idx).toInt(); const auto minor = verStr.mid(idx + 1).toInt(); if (major > SyntaxHighlighting_VERSION_MAJOR || (major == SyntaxHighlighting_VERSION_MAJOR && minor > SyntaxHighlighting_VERSION_MINOR)) { qCWarning(Log) << "Skipping" << fileName << "due to being too new, version:" << verStr; return false; } return true; } quint16 DefinitionData::foldingRegionId(const QString &foldName) { hasFoldingRegions = true; return RepositoryPrivate::get(repo)->foldingRegionId(name, foldName); } DefinitionRef::DefinitionRef() { } DefinitionRef::DefinitionRef(const Definition &def) : d(def.d) { } DefinitionRef::~DefinitionRef() { } DefinitionRef& DefinitionRef::operator=(const Definition &def) { d = def.d; return *this; } Definition DefinitionRef::definition() const { if (!d.expired()) return Definition(d.lock()); return Definition(); } + +bool DefinitionRef::operator==(const DefinitionRef &other) const +{ + if (d.expired() != other.d.expired()) { + return false; + } + + return d.expired() || d.lock().get() == other.d.lock().get(); +} diff --git a/src/lib/definitionref_p.h b/src/lib/definitionref_p.h index abc69c9..08604a4 100644 --- a/src/lib/definitionref_p.h +++ b/src/lib/definitionref_p.h @@ -1,61 +1,74 @@ /* 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. */ #ifndef KSYNTAXHIGHLIGHTING_DEFINITIONREF_P_H #define KSYNTAXHIGHLIGHTING_DEFINITIONREF_P_H #include namespace KSyntaxHighlighting { class Definition; class DefinitionData; class DefinitionPrivate; /** Weak reference for Definition instances. * * This must be used when holding Definition instances * in objects hold directly or indirectly by Definition * to avoid reference count loops and thus memory leaks. * * @internal */ class DefinitionRef { public: DefinitionRef(); explicit DefinitionRef(const Definition &def); ~DefinitionRef(); DefinitionRef& operator=(const Definition &def); Definition definition() const; + /** + * Checks two definition references for equality. + */ + bool operator==(const DefinitionRef &other) const; + + /** + * Checks two definition references for inequality. + */ + bool operator!=(const DefinitionRef &other) const + { + return !(*this == other); + } + private: friend class DefinitionData; std::weak_ptr d; }; } #endif diff --git a/src/lib/state.cpp b/src/lib/state.cpp index 4b89548..f9abfa9 100644 --- a/src/lib/state.cpp +++ b/src/lib/state.cpp @@ -1,113 +1,114 @@ /* 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() { m_contextStack.pop_back(); } 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 { - return d->m_contextStack == other.d->m_contextStack && d->m_defData == other.d->m_defData; + // 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 28ba360..eed129b 100644 --- a/src/lib/state_p.h +++ b/src/lib/state_p.h @@ -1,74 +1,73 @@ /* 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 DefinitionData; 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(); Context* topContext() const; const QStringList &topCaptures() const; +private: /** - * definition pointer to check for invalid states - * FIXME: this is a hack, one could get the same pointer - * later for other object + * weak reference to the used definition to filter out invalid states */ - DefinitionData *m_defData = nullptr; - -private: + DefinitionRef m_defRef; /** * the context stack combines the active context + valid captures */ QVector> m_contextStack; }; } #endif