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
12using namespace KSyntaxHighlighting;
13
14void ContextSwitch::resolve(DefinitionData &def, QStringView context)
15{
16 if (context.isEmpty() || context == QStringLiteral("#stay")) {
17 return;
18 }
19
20 while (context.startsWith(QStringLiteral("#pop"))) {
21 ++m_popCount;
22 if (context.size() > 4 && context.at(n: 4) == QLatin1Char('!')) {
23 context = context.sliced(pos: 5);
24 break;
25 }
26 context = context.sliced(pos: 4);
27 }
28
29 m_isStay = !m_popCount;
30
31 if (context.isEmpty()) {
32 return;
33 }
34
35 const qsizetype defNameIndex = context.indexOf(QStringLiteral("##"));
36 auto defName = (defNameIndex <= -1) ? QStringView() : context.sliced(pos: defNameIndex + 2);
37 auto contextName = (defNameIndex <= -1) ? context : context.sliced(pos: 0, n: defNameIndex);
38
39 m_context = def.resolveIncludedContext(defName, contextName).context;
40
41 if (m_context) {
42 m_isStay = false;
43 } else {
44 qCWarning(Log) << "cannot find context" << contextName << "in" << def.name;
45 }
46}
47

source code of syntax-highlighting/src/lib/contextswitch.cpp