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 | |
18 | KateHlManager *KateHlManager::self() |
19 | { |
20 | return KTextEditor::EditorPrivate::self()->hlManager(); |
21 | } |
22 | |
23 | QList<KSyntaxHighlighting::Theme> KateHlManager::sortedThemes() const |
24 | { |
25 | // get KSyntaxHighlighting themes |
26 | auto themes = repository().themes(); |
27 | |
28 | // sort by translated name |
29 | std::sort(first: themes.begin(), last: themes.end(), comp: [](const KSyntaxHighlighting::Theme &left, const KSyntaxHighlighting::Theme &right) { |
30 | return left.translatedName().compare(s: right.translatedName(), cs: Qt::CaseInsensitive) < 0; |
31 | }); |
32 | return themes; |
33 | } |
34 | |
35 | KateHighlighting *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(i: n); |
46 | |
47 | auto it = m_hlDict.find(x: mode.name()); |
48 | if (it == m_hlDict.end()) { |
49 | std::unique_ptr<KateHighlighting> hl(new KateHighlighting(mode)); |
50 | it = m_hlDict.emplace(args: mode.name(), args: std::move(hl)).first; |
51 | } |
52 | return it->second.get(); |
53 | } |
54 | |
55 | int 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(s: name, cs: Qt::CaseInsensitive) == 0) { |
61 | return idx; |
62 | } |
63 | idx++; |
64 | } |
65 | |
66 | return -1; |
67 | } |
68 | |
69 | void 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(x&: 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(name: hlMode) < 0) { |
86 | hlMode = QStringLiteral("None" ); |
87 | } |
88 | doc->setHighlightingMode(hlMode); |
89 | } |
90 | |
91 | // emit reloaded signal for our editor instance |
92 | Q_EMIT KTextEditor::EditorPrivate::self()->repositoryReloaded(editor: KTextEditor::EditorPrivate::self()); |
93 | } |
94 | |
95 | #include "moc_katesyntaxmanager.cpp" |
96 | |