59
53
bool KateSyntaxDocument::setIdentifier(const QString &identifier)
61
// if the current file is the same as the new one don't do anything.
62
if (currentFile != identifier) {
63
// let's open the new file
66
if (f.open(QIODevice::ReadOnly)) {
67
// Let's parse the contets of the xml file
68
/* The result of this function should be check for robustness,
69
a false returned means a parse error */
72
bool success = setContent(&f, &errorMsg, &line, &col);
74
// Ok, now the current file is the pretended one (identifier)
75
currentFile = identifier;
77
// Close the file, is not longer needed
81
KMessageBox::error(QApplication::activeWindow(), i18n("<qt>The error <b>%4</b><br /> has been detected in the file %1 at %2/%3</qt>", identifier,
55
// already existing in cache? be done
56
if (m_domDocuments.contains(identifier)) {
57
currentFile = identifier;
63
if (!f.open(QIODevice::ReadOnly)) {
64
// Oh o, we couldn't open the file.
65
KMessageBox::error(QApplication::activeWindow(), i18n("Unable to open %1", identifier));
70
QDomDocument *document = new QDomDocument();
73
if (!document->setContent(&f, &errorMsg, &line, &col)) {
74
KMessageBox::error(QApplication::activeWindow(), i18n("<qt>The error <b>%4</b><br /> has been detected in the file %1 at %2/%3</qt>", identifier,
82
75
line, col, i18nc("QXml", errorMsg.toUtf8().data())));
86
// Oh o, we couldn't open the file.
87
KMessageBox::error(QApplication::activeWindow(), i18n("Unable to open %1", identifier));
81
currentFile = identifier;
82
m_domDocuments[currentFile] = document;
86
void KateSyntaxDocument::clearCache()
88
qDeleteAll(m_domDocuments);
89
m_domDocuments.clear();
95
95
* Jump to the next group, KateSyntaxContextData::currentGroup will point to the next group
334
/** Generate the list of hl modes, store them in myModeList
335
force: if true forces to rebuild the Mode List from the xml files (instead of katesyntax...rc)
337
void KateSyntaxDocument::setupModeList()
339
// If there's something in myModeList the Mode List was already built so, don't do it again
340
if (!myModeList.isEmpty()) {
344
// Let's get a list of all the index & xml files for hl
345
QStringList dirsWithIndexFiles;
346
QStringList xmlFiles;
348
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("katepart5/syntax"), QStandardPaths::LocateDirectory);
349
foreach (const QString &dir, dirs) {
352
// if dir has index json, only take that into account!
353
if (d.exists(QStringLiteral("index.json"))) {
354
dirsWithIndexFiles.append(dir);
358
// else get all xml files
359
const QStringList fileNames = d.entryList(QStringList() << QStringLiteral("*.xml"));
360
foreach (const QString &file, fileNames) {
361
xmlFiles.append(dir + QLatin1Char('/') + file);
365
// only allow each name once!
366
QSet<QString> hlNames;
368
// preference: xml files, to allow users to overwrite system files with index!
369
Q_FOREACH (const QString xmlFile, xmlFiles) {
370
// We're forced to read the xml files or the mode doesn't exist in the katesyntax...rc
372
if (!f.open(QIODevice::ReadOnly))
375
// Ok we opened the file, let's read the contents and close the file
376
/* the return of setContent should be checked because a false return shows a parsing error */
379
if (!setContent(&f, &errMsg, &line, &col))
382
QDomElement root = documentElement();
386
// If the 'first' tag is language, go on
387
if (root.tagName() != QLatin1String("language"))
390
// get name, only allow hls once!
391
const QString name = root.attribute(QLatin1String("name"));
392
if (hlNames.contains(name))
395
// let's make the mode list item.
396
KateSyntaxModeListItem *mli = new KateSyntaxModeListItem;
399
mli->section = root.attribute(QLatin1String("section"));
400
mli->mimetype = root.attribute(QLatin1String("mimetype"));
401
mli->extension = root.attribute(QLatin1String("extensions"));
402
mli->version = root.attribute(QLatin1String("version"));
403
mli->priority = root.attribute(QLatin1String("priority"));
404
mli->style = root.attribute(QLatin1String("style"));
405
mli->author = root.attribute(QLatin1String("author"));
406
mli->license = root.attribute(QLatin1String("license"));
407
mli->indenter = root.attribute(QLatin1String("indenter"));
409
QString hidden = root.attribute(QLatin1String("hidden"));
410
mli->hidden = (hidden == QLatin1String("true") || hidden == QLatin1String("TRUE"));
412
mli->identifier = xmlFile;
414
// translate section + name
415
mli->section = i18nc("Language Section", mli->section.toUtf8().data());
416
mli->nameTranslated = i18nc("Language", mli->name.toUtf8().data());
418
// Append the new item to the list.
419
myModeList.append(mli);
420
hlNames.insert(name);
424
Q_FOREACH (const QString dir, dirsWithIndexFiles) {
426
* open the file for reading, bail out on error!
428
QFile file (dir + QStringLiteral("/index.json"));
429
if (!file.open (QFile::ReadOnly))
433
* parse the whole file, bail out again on error!
435
const QJsonDocument index (QJsonDocument::fromBinaryData(file.readAll()));
440
* iterate over all hls in the index
442
QMapIterator<QString, QVariant> i(index.toVariant().toMap());
443
while (i.hasNext()) {
447
QVariantMap map = i.value().toMap();
449
// get name, only allow hls once!
450
const QString name = map[QLatin1String("name")].toString();
451
if (hlNames.contains(name))
454
// let's make the mode list item.
455
KateSyntaxModeListItem *mli = new KateSyntaxModeListItem;
458
mli->section = map[QLatin1String("section")].toString();
459
mli->mimetype = map[QLatin1String("mimetype")].toString();
460
mli->extension = map[QLatin1String("extensions")].toString();
461
mli->version = map[QLatin1String("version")].toString();
462
mli->priority = map[QLatin1String("priority")].toString();
463
mli->style = map[QLatin1String("style")].toString();
464
mli->author = map[QLatin1String("author")].toString();
465
mli->license = map[QLatin1String("license")].toString();
466
mli->indenter = map[QLatin1String("indenter")].toString();
467
mli->hidden = map[QLatin1String("hidden")].toBool();
469
mli->identifier = dir + QLatin1Char('/') + i.key();
471
// translate section + name
472
mli->section = i18nc("Language Section", mli->section.toUtf8().data());
473
mli->nameTranslated = i18nc("Language", mli->name.toUtf8().data());
475
// Append the new item to the list.
476
myModeList.append(mli);
477
hlNames.insert(name);