| 1 | /* |
|---|---|
| 2 | SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> |
| 3 | SPDX-FileCopyrightText: 2024 Jonathan Poelen <jonathan.poelen@gmail.com> |
| 4 | |
| 5 | SPDX-License-Identifier: MIT |
| 6 | */ |
| 7 | |
| 8 | #include "contextswitch_p.h" |
| 9 | #include "definition_p.h" |
| 10 | #include "ksyntaxhighlighting_logging.h" |
| 11 | #include <QStringTokenizer> |
| 12 | |
| 13 | using namespace KSyntaxHighlighting; |
| 14 | |
| 15 | void ContextSwitch::resolve(DefinitionData &def, QStringView context) |
| 16 | { |
| 17 | if (context.isEmpty() || context == QStringLiteral("#stay")) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | while (context.startsWith(QStringLiteral("#pop"))) { |
| 22 | ++m_popCount; |
| 23 | if (context.size() > 4 && context.at(n: 4) == QLatin1Char('!')) { |
| 24 | context = context.sliced(pos: 5); |
| 25 | break; |
| 26 | } |
| 27 | context = context.sliced(pos: 4); |
| 28 | } |
| 29 | |
| 30 | m_isStay = !m_popCount; |
| 31 | |
| 32 | if (context.isEmpty()) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | for (auto contextPart : QStringTokenizer{context, u'!'}) { |
| 37 | const qsizetype defNameIndex = contextPart.indexOf(QStringLiteral("##")); |
| 38 | auto defName = (defNameIndex <= -1) ? QStringView() : contextPart.sliced(pos: defNameIndex + 2); |
| 39 | auto contextName = (defNameIndex <= -1) ? contextPart : contextPart.sliced(pos: 0, n: defNameIndex); |
| 40 | |
| 41 | auto resolvedCtx = def.resolveIncludedContext(defName, contextName); |
| 42 | if (resolvedCtx.context) { |
| 43 | m_contexts.append(t: resolvedCtx.context); |
| 44 | } else { |
| 45 | auto part = (defName.isEmpty() || resolvedCtx.def) ? "context": "definition in"; |
| 46 | qCWarning(Log) << "cannot find"<< part << contextPart << "in"<< def.name; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | m_isStay = m_contexts.isEmpty(); |
| 51 | } |
| 52 |
