Changeset View
Changeset View
Standalone View
Standalone View
src/lib/definition.cpp
Show All 25 Lines | |||||
26 | 26 | | |||
27 | #include "definition.h" | 27 | #include "definition.h" | ||
28 | #include "definition_p.h" | 28 | #include "definition_p.h" | ||
29 | #include "definitionref_p.h" | 29 | #include "definitionref_p.h" | ||
30 | 30 | | |||
31 | #include "context_p.h" | 31 | #include "context_p.h" | ||
32 | #include "format.h" | 32 | #include "format.h" | ||
33 | #include "format_p.h" | 33 | #include "format_p.h" | ||
34 | #include "repository.h" | ||||
34 | #include "repository_p.h" | 35 | #include "repository_p.h" | ||
35 | #include "rule_p.h" | 36 | #include "rule_p.h" | ||
36 | #include "ksyntaxhighlighting_logging.h" | 37 | #include "ksyntaxhighlighting_logging.h" | ||
37 | #include "ksyntaxhighlighting_version.h" | 38 | #include "ksyntaxhighlighting_version.h" | ||
38 | #include "xml_p.h" | 39 | #include "xml_p.h" | ||
39 | 40 | | |||
40 | #include <QCoreApplication> | 41 | #include <QCoreApplication> | ||
41 | #include <QDebug> | 42 | #include <QDebug> | ||
▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Line(s) | |||||
216 | QStringList Definition::foldingIgnoreList() const | 217 | QStringList Definition::foldingIgnoreList() const | ||
217 | { | 218 | { | ||
218 | d->load(); | 219 | d->load(); | ||
219 | return d->foldingIgnoreList; | 220 | return d->foldingIgnoreList; | ||
220 | } | 221 | } | ||
221 | 222 | | |||
222 | QStringList Definition::keywordLists() const | 223 | QStringList Definition::keywordLists() const | ||
223 | { | 224 | { | ||
224 | d->load(); | 225 | d->load(DefinitionData::OnlyKeywords(true)); | ||
225 | return d->keywordLists.keys(); | 226 | return d->keywordLists.keys(); | ||
226 | } | 227 | } | ||
227 | 228 | | |||
228 | QStringList Definition::keywordList(const QString& name) const | 229 | QStringList Definition::keywordList(const QString& name) const | ||
229 | { | 230 | { | ||
230 | d->load(); | 231 | d->load(DefinitionData::OnlyKeywords(true)); | ||
231 | const auto list = d->keywordList(name); | 232 | const auto list = d->keywordList(name); | ||
232 | return list ? list->keywords() : QStringList(); | 233 | return list ? list->keywords() : QStringList(); | ||
233 | } | 234 | } | ||
234 | 235 | | |||
235 | QVector<Format> Definition::formats() const | 236 | QVector<Format> Definition::formats() const | ||
236 | { | 237 | { | ||
237 | d->load(); | 238 | d->load(); | ||
238 | 239 | | |||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Line(s) | 347 | { | |||
351 | return Format(); | 352 | return Format(); | ||
352 | } | 353 | } | ||
353 | 354 | | |||
354 | bool DefinitionData::isLoaded() const | 355 | bool DefinitionData::isLoaded() const | ||
355 | { | 356 | { | ||
356 | return !contexts.isEmpty(); | 357 | return !contexts.isEmpty(); | ||
357 | } | 358 | } | ||
358 | 359 | | |||
359 | bool DefinitionData::load() | 360 | bool DefinitionData::load(OnlyKeywords onlyKeywords) | ||
360 | { | 361 | { | ||
361 | if (fileName.isEmpty()) | 362 | if (fileName.isEmpty()) | ||
362 | return false; | 363 | return false; | ||
363 | 364 | | |||
364 | if (isLoaded()) | 365 | if (isLoaded()) | ||
365 | return true; | 366 | return true; | ||
366 | 367 | | |||
368 | if (bool(onlyKeywords) && keywordIsLoaded) | ||||
369 | return true; | ||||
370 | | ||||
367 | QFile file(fileName); | 371 | QFile file(fileName); | ||
368 | if (!file.open(QFile::ReadOnly)) | 372 | if (!file.open(QFile::ReadOnly)) | ||
369 | return false; | 373 | return false; | ||
370 | 374 | | |||
371 | QXmlStreamReader reader(&file); | 375 | QXmlStreamReader reader(&file); | ||
372 | while (!reader.atEnd()) { | 376 | while (!reader.atEnd()) { | ||
373 | const auto token = reader.readNext(); | 377 | const auto token = reader.readNext(); | ||
374 | if (token != QXmlStreamReader::StartElement) | 378 | if (token != QXmlStreamReader::StartElement) | ||
375 | continue; | 379 | continue; | ||
376 | 380 | | |||
377 | if (reader.name() == QLatin1String("highlighting")) | 381 | if (reader.name() == QLatin1String("highlighting")) { | ||
378 | loadHighlighting(reader); | 382 | loadHighlighting(reader, onlyKeywords); | ||
383 | if (bool(onlyKeywords)) { | ||||
384 | return true; | ||||
385 | } | ||||
386 | } | ||||
379 | 387 | | |||
380 | else if (reader.name() == QLatin1String("general")) | 388 | else if (reader.name() == QLatin1String("general")) | ||
381 | loadGeneral(reader); | 389 | loadGeneral(reader); | ||
382 | } | 390 | } | ||
383 | 391 | | |||
384 | for (auto it = keywordLists.begin(); it != keywordLists.end(); ++it) | 392 | for (auto it = keywordLists.begin(); it != keywordLists.end(); ++it) { | ||
385 | (*it).setCaseSensitivity(caseSensitive); | 393 | it->setCaseSensitivity(caseSensitive); | ||
394 | } | ||||
386 | 395 | | |||
387 | foreach (auto context, contexts) { | 396 | foreach (auto context, contexts) { | ||
388 | context->resolveContexts(); | 397 | context->resolveContexts(); | ||
389 | context->resolveIncludes(); | 398 | context->resolveIncludes(); | ||
390 | context->resolveAttributeFormat(); | 399 | context->resolveAttributeFormat(); | ||
391 | } | 400 | } | ||
392 | 401 | | |||
393 | Q_ASSERT(std::is_sorted(wordDelimiters.constBegin(), wordDelimiters.constEnd())); | 402 | Q_ASSERT(std::is_sorted(wordDelimiters.constBegin(), wordDelimiters.constEnd())); | ||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Line(s) | 475 | { | |||
486 | const auto mts = reader.attributes().value(QStringLiteral("mimetype")).toString(); | 495 | const auto mts = reader.attributes().value(QStringLiteral("mimetype")).toString(); | ||
487 | foreach (const auto &mt, mts.split(QLatin1Char(';'), QString::SkipEmptyParts)) | 496 | foreach (const auto &mt, mts.split(QLatin1Char(';'), QString::SkipEmptyParts)) | ||
488 | mimetypes.push_back(mt); | 497 | mimetypes.push_back(mt); | ||
489 | if (reader.attributes().hasAttribute(QStringLiteral("casesensitive"))) | 498 | if (reader.attributes().hasAttribute(QStringLiteral("casesensitive"))) | ||
490 | caseSensitive = Xml::attrToBool(reader.attributes().value(QStringLiteral("casesensitive"))) ? Qt::CaseSensitive : Qt::CaseInsensitive; | 499 | caseSensitive = Xml::attrToBool(reader.attributes().value(QStringLiteral("casesensitive"))) ? Qt::CaseSensitive : Qt::CaseInsensitive; | ||
491 | return true; | 500 | return true; | ||
492 | } | 501 | } | ||
493 | 502 | | |||
494 | void DefinitionData::loadHighlighting(QXmlStreamReader& reader) | 503 | void DefinitionData::loadHighlighting(QXmlStreamReader& reader, OnlyKeywords onlyKeywords) | ||
495 | { | 504 | { | ||
496 | Q_ASSERT(reader.name() == QLatin1String("highlighting")); | 505 | Q_ASSERT(reader.name() == QLatin1String("highlighting")); | ||
497 | Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); | 506 | Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); | ||
498 | 507 | | |||
508 | // skip highlighting | ||||
509 | reader.readNext(); | ||||
510 | | ||||
499 | while (!reader.atEnd()) { | 511 | while (!reader.atEnd()) { | ||
500 | switch (reader.tokenType()) { | 512 | switch (reader.tokenType()) { | ||
501 | case QXmlStreamReader::StartElement: | 513 | case QXmlStreamReader::StartElement: | ||
502 | if (reader.name() == QLatin1String("list")) { | 514 | if (reader.name() == QLatin1String("list")) { | ||
515 | if (!keywordIsLoaded) { | ||||
503 | KeywordList keywords; | 516 | KeywordList keywords; | ||
504 | keywords.load(reader); | 517 | keywords.load(reader); | ||
505 | keywordLists.insert(keywords.name(), keywords); | 518 | keywordLists.insert(keywords.name(), keywords); | ||
519 | } | ||||
520 | else { | ||||
521 | reader.skipCurrentElement(); | ||||
522 | } | ||||
523 | } else if (bool(onlyKeywords)) { | ||||
524 | resolveIncludeKeywords(); | ||||
525 | return; | ||||
506 | } else if (reader.name() == QLatin1String("contexts")) { | 526 | } else if (reader.name() == QLatin1String("contexts")) { | ||
527 | resolveIncludeKeywords(); | ||||
507 | loadContexts(reader); | 528 | loadContexts(reader); | ||
508 | reader.readNext(); | 529 | reader.readNext(); | ||
509 | } else if (reader.name() == QLatin1String("itemDatas")) { | 530 | } else if (reader.name() == QLatin1String("itemDatas")) { | ||
510 | loadItemData(reader); | 531 | loadItemData(reader); | ||
511 | } else { | 532 | } else { | ||
512 | reader.readNext(); | 533 | reader.readNext(); | ||
513 | } | 534 | } | ||
514 | break; | 535 | break; | ||
515 | case QXmlStreamReader::EndElement: | 536 | case QXmlStreamReader::EndElement: | ||
516 | return; | 537 | return; | ||
517 | default: | 538 | default: | ||
518 | reader.readNext(); | 539 | reader.readNext(); | ||
519 | break; | 540 | break; | ||
520 | } | 541 | } | ||
521 | } | 542 | } | ||
522 | } | 543 | } | ||
523 | 544 | | |||
545 | void DefinitionData::resolveIncludeKeywords() | ||||
546 | { | ||||
547 | if (keywordIsLoaded) { | ||||
548 | return; | ||||
549 | } | ||||
550 | | ||||
551 | keywordIsLoaded = true; | ||||
552 | | ||||
553 | for (auto it = keywordLists.begin(); it != keywordLists.end(); ++it) { | ||||
554 | it->resolveIncludeKeywords(*this); | ||||
555 | } | ||||
556 | } | ||||
557 | | ||||
524 | void DefinitionData::loadContexts(QXmlStreamReader& reader) | 558 | void DefinitionData::loadContexts(QXmlStreamReader& reader) | ||
525 | { | 559 | { | ||
526 | Q_ASSERT(reader.name() == QLatin1String("contexts")); | 560 | Q_ASSERT(reader.name() == QLatin1String("contexts")); | ||
527 | Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); | 561 | Q_ASSERT(reader.tokenType() == QXmlStreamReader::StartElement); | ||
528 | 562 | | |||
529 | while (!reader.atEnd()) { | 563 | while (!reader.atEnd()) { | ||
530 | switch (reader.tokenType()) { | 564 | switch (reader.tokenType()) { | ||
531 | case QXmlStreamReader::StartElement: | 565 | case QXmlStreamReader::StartElement: | ||
▲ Show 20 Lines • Show All 266 Lines • Show Last 20 Lines |