File indexing completed on 2024-12-01 10:39:48

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "syntaxhighlightingmanager.h"
0008 #include "ruqola_debug.h"
0009 
0010 namespace
0011 {
0012 bool sortCaseInsensitively(const QString &lhs, const QString &rhs)
0013 {
0014     return lhs.compare(rhs, Qt::CaseInsensitive) < 0;
0015 }
0016 }
0017 
0018 SyntaxHighlightingManager::SyntaxHighlightingManager(QObject *parent)
0019     : QObject(parent)
0020 {
0021     initialize();
0022 }
0023 
0024 void SyntaxHighlightingManager::initialize()
0025 {
0026     mDefaultDef = mRepo.definitionForName(QStringLiteral("C++"));
0027     if (mDefaultDef.isValid()) {
0028         mSyntaxHighlightingInitialized = true;
0029         const auto definitions = mRepo.definitions();
0030         mDefinitions.reserve(definitions.size());
0031         for (const auto &definition : definitions) {
0032             mDefinitions.append(definition.name());
0033         }
0034         std::sort(mDefinitions.begin(), mDefinitions.end(), sortCaseInsensitively);
0035     } else {
0036         qCWarning(RUQOLA_LOG) << "Unable to find definition";
0037     }
0038 }
0039 
0040 KSyntaxHighlighting::Repository &SyntaxHighlightingManager::repo() const
0041 {
0042     return mRepo;
0043 }
0044 
0045 KSyntaxHighlighting::Definition SyntaxHighlightingManager::def(const QString &name) const
0046 {
0047     auto it = std::lower_bound(mDefinitions.begin(), mDefinitions.end(), name, sortCaseInsensitively);
0048     if (it != mDefinitions.end() && name.compare(*it, Qt::CaseInsensitive) == 0) {
0049         return mRepo.definitionForName(*it);
0050     }
0051     return {};
0052 }
0053 
0054 KSyntaxHighlighting::Definition SyntaxHighlightingManager::defaultDef() const
0055 {
0056     return mDefaultDef;
0057 }
0058 
0059 bool SyntaxHighlightingManager::syntaxHighlightingInitialized() const
0060 {
0061     return mSyntaxHighlightingInitialized;
0062 }
0063 
0064 SyntaxHighlightingManager *SyntaxHighlightingManager::self()
0065 {
0066     static SyntaxHighlightingManager s_self;
0067     return &s_self;
0068 }
0069 
0070 #include "moc_syntaxhighlightingmanager.cpp"