8#include "katemodemanager.h"
9#include "katemodemenulist.h"
10#include "katestatusbar.h"
13#include "kateconfig.h"
14#include "kateglobal.h"
15#include "katepartdebug.h"
16#include "katesyntaxmanager.h"
19#include <KConfigGroup>
20#include <KSyntaxHighlighting/WildcardMatcher>
23#include <QMimeDatabase>
33 std::copy(v.
begin(), v.
end(), std::back_inserter(l));
37KateModeManager::KateModeManager()
42KateModeManager::~KateModeManager()
47bool compareKateFileType(
const KateFileType *
const left,
const KateFileType *
const right)
50 if (comparison == 0) {
53 return comparison < 0;
59void KateModeManager::update()
65 KateFileType *normalType =
nullptr;
69 for (
int z = 0; z < g.count(); z++) {
72 KateFileType *
type =
new KateFileType();
74 type->wildcards = cg.readXdgListEntry(QStringLiteral(
"Wildcards"));
75 type->mimetypes = cg.readXdgListEntry(QStringLiteral(
"Mimetypes"));
76 type->priority = cg.readEntry(QStringLiteral(
"Priority"), 0);
77 type->varLine = cg.readEntry(QStringLiteral(
"Variables"));
78 type->indenter = cg.readEntry(QStringLiteral(
"Indenter"));
80 type->hl = cg.readEntry(QStringLiteral(
"Highlighting"));
83 type->hlGenerated = cg.readEntry(QStringLiteral(
"Highlighting Generated"),
false);
91 type->section = cg.readEntry(QStringLiteral(
"Section"));
92 type->version = cg.readEntry(QStringLiteral(
"Highlighting Version"));
95 if (!
type->hlGenerated) {
107 const auto modes = KateHlManager::self()->modeList();
108 for (
int i = 0; i < modes.size(); ++i) {
118 KateFileType *
type =
nullptr;
119 bool newType =
false;
120 if (m_name2Type.
contains(modes[i].name())) {
121 type = m_name2Type[modes[i].name()];
124 if (
type->hlGenerated) {
129 type =
new KateFileType();
130 type->name = modes[i].name();
132 type->hlGenerated =
true;
138 type->name = modes[i].name();
139 type->section = modes[i].section();
140 type->wildcards = vectorToList(modes[i].extensions());
141 type->mimetypes = vectorToList(modes[i].mimeTypes());
142 type->priority = modes[i].priority();
144 type->indenter = modes[i].indenter();
145 type->hl = modes[i].name();
148 type->translatedName = modes[i].translatedName();
149 type->translatedSection = modes[i].translatedSection();
153 std::sort(m_types.
begin(), m_types.
end(), compareKateFileType);
157 normalType =
new KateFileType();
161 normalType->name = QStringLiteral(
"Normal");
162 normalType->translatedName =
i18n(
"Normal");
163 normalType->hl = QStringLiteral(
"None");
164 normalType->hlGenerated =
true;
171 if (view->statusBar() && view->statusBar()->modeMenu()) {
172 view->statusBar()->modeMenu()->reloadItems();
186 for (
const KateFileType *type : v) {
189 config.writeEntry(
"Section",
type->section);
190 config.writeXdgListEntry(
"Wildcards",
type->wildcards);
191 config.writeXdgListEntry(
"Mimetypes",
type->mimetypes);
192 config.writeEntry(
"Priority",
type->priority);
193 config.writeEntry(
"Indenter",
type->indenter);
200 config.writeEntry(
"Variables", varLine);
202 config.writeEntry(
"Highlighting",
type->hl);
205 config.writeEntry(
"Highlighting Generated",
type->hlGenerated);
206 config.writeEntry(
"Highlighting Version",
type->version);
211 const auto groupNames = katerc.groupList();
212 for (
const QString &groupName : groupNames) {
213 if (newg.
indexOf(groupName) == -1) {
214 katerc.deleteGroup(groupName);
248 if (!(result = wildcardsFind(fileName)).isEmpty()) {
252 QString backupSuffix = KateDocumentConfig::global()->backupSuffix();
253 if (fileName.
endsWith(backupSuffix)) {
254 if (!(result = wildcardsFind(fileName.
left(length - backupSuffix.
length()))).isEmpty()) {
259 for (
auto &commonSuffix : commonSuffixes) {
260 if (commonSuffix != backupSuffix && fileName.
endsWith(commonSuffix)) {
261 if (!(result = wildcardsFind(fileName.
left(length - commonSuffix.size()))).isEmpty()) {
270 if (!fileToReadFrom.
isEmpty()) {
275 return mimeTypesFind(mtName);
278template<
typename UnaryStringPredicate>
281 const KateFileType *
match =
nullptr;
282 auto matchPriority = std::numeric_limits<int>::lowest();
283 for (
const KateFileType *type : types) {
284 if (
type->priority > matchPriority && std::any_of((
type->*list).cbegin(), (
type->*list).cend(), anyOfCondition)) {
286 matchPriority =
type->priority;
292QString KateModeManager::wildcardsFind(
const QString &fileName)
const
295 return findHighestPriorityTypeNameIf(m_types, &KateFileType::wildcards, [&fileNameNoPath](
const QString &wildcard) {
296 return KSyntaxHighlighting::WildcardMatcher::exactMatch(fileNameNoPath, wildcard);
300QString KateModeManager::mimeTypesFind(
const QString &mimeTypeName)
const
302 return findHighestPriorityTypeNameIf(m_types, &KateFileType::mimetypes, [&mimeTypeName](
const QString &name) {
303 return mimeTypeName ==
name;
307const KateFileType &KateModeManager::fileType(
const QString &name)
const
309 for (
int i = 0; i < m_types.
size(); ++i) {
310 if (m_types[i]->name == name) {
315 static KateFileType notype;
A KParts derived class representing a text document.
virtual QString mimeType()=0
Get this document's mimetype.
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
QString i18n(const char *text, const TYPE &arg...)
KDB_EXPORT KDbVersionInfo version()
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QString fileName() const const
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
void append(QList< T > &&value)
bool isEmpty() const const
void prepend(parameter_type value)
void reserve(qsizetype size)
qsizetype size() const const
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString & prepend(QChar ch)
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
QStringView left(qsizetype length) const const
bool endsWith(QChar ch) const const
bool isEmpty() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
QString toString(FormattingOptions options) const const