Commit a6a37ec5 authored by Leandro Melo's avatar Leandro Melo
Browse files

Generic highlighter: Consider definition's priorities properly.

parent 77ff1b8c
......@@ -32,15 +32,28 @@
using namespace TextEditor;
using namespace Internal;
const QLatin1String HighlightDefinitionMetaData::kPriority("priority");
const QLatin1String HighlightDefinitionMetaData::kName("name");
const QLatin1String HighlightDefinitionMetaData::kExtensions("extensions");
const QLatin1String HighlightDefinitionMetaData::kMimeType("mimetype");
const QLatin1String HighlightDefinitionMetaData::kVersion("version");
const QLatin1String HighlightDefinitionMetaData::kUrl("url");
HighlightDefinitionMetaData::HighlightDefinitionMetaData()
HighlightDefinitionMetaData::HighlightDefinitionMetaData() : m_priority(0)
{}
void HighlightDefinitionMetaData::setPriority(const int priority)
{ m_priority = priority; }
int HighlightDefinitionMetaData::priority() const
{ return m_priority; }
void HighlightDefinitionMetaData::setId(const QString &id)
{ m_id = id; }
const QString &HighlightDefinitionMetaData::id() const
{ return m_id; }
void HighlightDefinitionMetaData::setName(const QString &name)
{ m_name = name; }
......
......@@ -43,6 +43,12 @@ class HighlightDefinitionMetaData
public:
HighlightDefinitionMetaData();
void setPriority(const int priority);
int priority() const;
void setId(const QString &id);
const QString &id() const;
void setName(const QString &name);
const QString &name() const;
......@@ -58,6 +64,7 @@ public:
void setUrl(const QUrl &url);
const QUrl &url() const;
static const QLatin1String kPriority;
static const QLatin1String kName;
static const QLatin1String kExtensions;
static const QLatin1String kMimeType;
......@@ -65,6 +72,8 @@ public:
static const QLatin1String kUrl;
private:
int m_priority;
QString m_id;
QString m_name;
QString m_version;
QStringList m_patterns;
......
......@@ -65,6 +65,7 @@
#include <QtXml/QXmlStreamAttributes>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtAlgorithms>
using namespace TextEditor;
using namespace Internal;
......@@ -151,13 +152,20 @@ void Manager::gatherDefinitionsMimeTypes(QFutureInterface<Core::MimeType> &futur
QStringList filter(QLatin1String("*.xml"));
definitionsDir.setNameFilters(filter);
QList<QSharedPointer<HighlightDefinitionMetaData> > allMetaData;
const QFileInfoList &filesInfo = definitionsDir.entryInfoList();
foreach (const QFileInfo &fileInfo, filesInfo) {
const QSharedPointer<HighlightDefinitionMetaData> &metaData = parseMetadata(fileInfo);
if (metaData.isNull())
continue;
if (!metaData.isNull())
allMetaData.append(metaData);
}
// Definitions with high priority need to be added after (and then replace) definitions with
// low priority.
qSort(allMetaData.begin(), allMetaData.end(), PriorityComp());
const QString &id = fileInfo.absoluteFilePath();
foreach (const QSharedPointer<HighlightDefinitionMetaData> &metaData, allMetaData) {
const QString &id = metaData->id();
m_idByName.insert(metaData->name(), id);
m_definitionsMetaData.insert(id, metaData);
......@@ -211,14 +219,16 @@ QSharedPointer<HighlightDefinitionMetaData> Manager::parseMetadata(const QFileIn
QXmlStreamReader reader(&definitionFile);
while (!reader.atEnd() && !reader.hasError()) {
if (reader.readNext() == QXmlStreamReader::StartElement &&
reader.name() == kLanguage) {
if (reader.readNext() == QXmlStreamReader::StartElement && reader.name() == kLanguage) {
const QXmlStreamAttributes &atts = reader.attributes();
metaData->setId(fileInfo.absoluteFilePath());
metaData->setName(atts.value(HighlightDefinitionMetaData::kName).toString());
metaData->setVersion(atts.value(HighlightDefinitionMetaData::kVersion).toString());
metaData->setPatterns(atts.value(HighlightDefinitionMetaData::kExtensions).
toString().split(kSemiColon, QString::SkipEmptyParts));
metaData->setPriority(atts.value(HighlightDefinitionMetaData::kPriority).toString()
.toInt());
metaData->setPatterns(atts.value(HighlightDefinitionMetaData::kExtensions)
.toString().split(kSemiColon, QString::SkipEmptyParts));
QStringList mimeTypes = atts.value(HighlightDefinitionMetaData::kMimeType).
toString().split(kSemiColon, QString::SkipEmptyParts);
......
......@@ -107,6 +107,14 @@ private:
QFutureWatcher<void> m_downloadWatcher;
QFutureWatcher<Core::MimeType> m_mimeTypeWatcher;
struct PriorityComp
{
bool operator()(const QSharedPointer<HighlightDefinitionMetaData> &a,
const QSharedPointer<HighlightDefinitionMetaData> &b) {
return a->priority() < b->priority();
}
};
signals:
void definitionsMetaDataReady(const QList<Internal::HighlightDefinitionMetaData>&);
void errorDownloadingDefinitionsMetaData();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment