Coverage Report

Created: 2026-05-31 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/syntax-highlighting/src/lib/contextswitch.cpp
Line
Count
Source
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
1.87k
{
17
1.87k
    if (context.isEmpty() || context == QStringLiteral("#stay")) {
18
1.18k
        return;
19
1.18k
    }
20
21
1.07k
    while (context.startsWith(QStringLiteral("#pop"))) {
22
440
        ++m_popCount;
23
440
        if (context.size() > 4 && context.at(4) == QLatin1Char('!')) {
24
44
            context = context.sliced(5);
25
44
            break;
26
44
        }
27
396
        context = context.sliced(4);
28
396
    }
29
30
682
    m_isStay = !m_popCount;
31
32
682
    if (context.isEmpty()) {
33
352
        return;
34
352
    }
35
36
330
    for (auto contextPart : QStringTokenizer{context, u'!'}) {
37
330
        const qsizetype defNameIndex = contextPart.indexOf(QStringLiteral("##"));
38
330
        auto defName = (defNameIndex <= -1) ? QStringView() : contextPart.sliced(defNameIndex + 2);
39
330
        auto contextName = (defNameIndex <= -1) ? contextPart : contextPart.sliced(0, defNameIndex);
40
41
330
        auto resolvedCtx = def.resolveIncludedContext(defName, contextName);
42
330
        if (resolvedCtx.context) {
43
330
            m_contexts.append(resolvedCtx.context);
44
330
        } else {
45
0
            auto part = (defName.isEmpty() || resolvedCtx.def) ? "context" : "definition in";
46
0
            qCWarning(Log) << "cannot find" << part << contextPart << "in" << def.name;
47
0
        }
48
330
    }
49
50
330
    m_isStay = m_contexts.isEmpty();
51
330
}