KTextEditor

katesyntaxmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
3 SPDX-FileCopyrightText: 2003, 2004 Anders Lund <anders@alweb.dk>
4 SPDX-FileCopyrightText: 2003 Hamish Rodda <rodda@kde.org>
5 SPDX-FileCopyrightText: 2001, 2002 Joseph Wenninger <jowenn@kde.org>
6 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
7 SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
8
9 SPDX-License-Identifier: LGPL-2.0-or-later
10*/
11
12#include "katesyntaxmanager.h"
13
14#include "katedocument.h"
15#include "kateglobal.h"
16#include "katehighlight.h"
17
18KateHlManager *KateHlManager::self()
19{
21}
22
23QList<KSyntaxHighlighting::Theme> KateHlManager::sortedThemes() const
24{
25 // get KSyntaxHighlighting themes
26 auto themes = repository().themes();
27
28 // sort by translated name
29 std::sort(themes.begin(), themes.end(), [](const KSyntaxHighlighting::Theme &left, const KSyntaxHighlighting::Theme &right) {
30 return left.translatedName().compare(right.translatedName(), Qt::CaseInsensitive) < 0;
31 });
32 return themes;
33}
34
35KateHighlighting *KateHlManager::getHl(int n)
36{
37 // default to "None", must exist
38 const auto modeList = this->modeList();
39
40 if (n < 0 || n >= modeList.count()) {
41 n = nameFind(QStringLiteral("None"));
42 Q_ASSERT(n >= 0);
43 }
44
45 const auto &mode = modeList.at(n);
46
47 auto it = m_hlDict.find(mode.name());
48 if (it == m_hlDict.end()) {
49 std::unique_ptr<KateHighlighting> hl(new KateHighlighting(mode));
50 it = m_hlDict.emplace(mode.name(), std::move(hl)).first;
51 }
52 return it->second.get();
53}
54
55int KateHlManager::nameFind(const QString &name) const
56{
57 const auto modeList = this->modeList();
58 int idx = 0;
59 for (const auto &mode : modeList) {
60 if (mode.name().compare(name, Qt::CaseInsensitive) == 0) {
61 return idx;
62 }
63 idx++;
64 }
65
66 return -1;
67}
68
69void KateHlManager::reload()
70{
71 // we need to ensure the KateHighlight objects survive until the end of this function
72 std::unordered_map<QString, std::unique_ptr<KateHighlighting>> keepHighlighingsAlive;
73 keepHighlighingsAlive.swap(m_hlDict);
74
75 // recreate repository
76 // this might even remove highlighting modes known before
77 m_repository.reload();
78
79 // let all documents use the new highlighters
80 // will be created on demand
81 // if old hl not found, use none
82 const auto docs = KTextEditor::EditorPrivate::self()->documents();
83 for (auto doc : docs) {
84 auto hlMode = doc->highlightingMode();
85 if (nameFind(hlMode) < 0) {
86 hlMode = QStringLiteral("None");
87 }
88 doc->setHighlightingMode(hlMode);
89 }
90
91 // emit reloaded signal for our editor instance
93}
94
95#include "moc_katesyntaxmanager.cpp"
Q_INVOKABLE QList< KSyntaxHighlighting::Theme > themes() const
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
KateHlManager * hlManager()
hl manager
Definition kateglobal.h:273
QList< KTextEditor::Document * > documents() override
Returns a list of all documents of this editor.
Definition kateglobal.h:99
void repositoryReloaded(KTextEditor::Editor *editor)
This signal is emitted whenever the editor syntax repository is reloaded.
Q_EMITQ_EMIT
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:17:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.