File indexing completed on 2024-04-21 03:57:44

0001 /*
0002     SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2003, 2004 Anders Lund <anders@alweb.dk>
0004     SPDX-FileCopyrightText: 2003 Hamish Rodda <rodda@kde.org>
0005     SPDX-FileCopyrightText: 2001, 2002 Joseph Wenninger <jowenn@kde.org>
0006     SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0007     SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
0008 
0009     SPDX-License-Identifier: LGPL-2.0-or-later
0010 */
0011 
0012 #include "katesyntaxmanager.h"
0013 
0014 #include "katedocument.h"
0015 #include "kateglobal.h"
0016 #include "katehighlight.h"
0017 
0018 KateHlManager *KateHlManager::self()
0019 {
0020     return KTextEditor::EditorPrivate::self()->hlManager();
0021 }
0022 
0023 QList<KSyntaxHighlighting::Theme> KateHlManager::sortedThemes() const
0024 {
0025     // get KSyntaxHighlighting themes
0026     auto themes = repository().themes();
0027 
0028     // sort by translated name
0029     std::sort(themes.begin(), themes.end(), [](const KSyntaxHighlighting::Theme &left, const KSyntaxHighlighting::Theme &right) {
0030         return left.translatedName().compare(right.translatedName(), Qt::CaseInsensitive) < 0;
0031     });
0032     return themes;
0033 }
0034 
0035 KateHighlighting *KateHlManager::getHl(int n)
0036 {
0037     // default to "None", must exist
0038     const auto modeList = this->modeList();
0039 
0040     if (n < 0 || n >= modeList.count()) {
0041         n = nameFind(QStringLiteral("None"));
0042         Q_ASSERT(n >= 0);
0043     }
0044 
0045     const auto &mode = modeList.at(n);
0046 
0047     auto it = m_hlDict.find(mode.name());
0048     if (it == m_hlDict.end()) {
0049         std::unique_ptr<KateHighlighting> hl(new KateHighlighting(mode));
0050         it = m_hlDict.emplace(mode.name(), std::move(hl)).first;
0051     }
0052     return it->second.get();
0053 }
0054 
0055 int KateHlManager::nameFind(const QString &name) const
0056 {
0057     const auto modeList = this->modeList();
0058     int idx = 0;
0059     for (const auto &mode : modeList) {
0060         if (mode.name().compare(name, Qt::CaseInsensitive) == 0) {
0061             return idx;
0062         }
0063         idx++;
0064     }
0065 
0066     return -1;
0067 }
0068 
0069 void KateHlManager::reload()
0070 {
0071     // we need to ensure the KateHighlight objects survive until the end of this function
0072     std::unordered_map<QString, std::unique_ptr<KateHighlighting>> keepHighlighingsAlive;
0073     keepHighlighingsAlive.swap(m_hlDict);
0074 
0075     // recreate repository
0076     // this might even remove highlighting modes known before
0077     m_repository.reload();
0078 
0079     // let all documents use the new highlighters
0080     // will be created on demand
0081     // if old hl not found, use none
0082     const auto docs = KTextEditor::EditorPrivate::self()->documents();
0083     for (auto doc : docs) {
0084         auto hlMode = doc->highlightingMode();
0085         if (nameFind(hlMode) < 0) {
0086             hlMode = QStringLiteral("None");
0087         }
0088         doc->setHighlightingMode(hlMode);
0089     }
0090 
0091     // emit reloaded signal for our editor instance
0092     Q_EMIT KTextEditor::EditorPrivate::self()->repositoryReloaded(KTextEditor::EditorPrivate::self());
0093 }
0094 
0095 #include "moc_katesyntaxmanager.cpp"