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

Generic highlighter: Mime type aliases and parents are now considered when...

Generic highlighter: Mime type aliases and parents are now considered when searching for a definition.
parent 98bc350e
......@@ -78,8 +78,6 @@ QString Manager::definitionIdByName(const QString &name) const
QString Manager::definitionIdByMimeType(const QString &mimeType) const
{
Q_ASSERT(!mimeType.isEmpty());
if (m_idByMimeType.count(mimeType) <= 1) {
return m_idByMimeType.value(mimeType);
} else {
......@@ -94,6 +92,17 @@ QString Manager::definitionIdByMimeType(const QString &mimeType) const
}
}
QString Manager::definitionIdByAnyMimeType(const QStringList &mimeTypes) const
{
QString definitionId;
foreach (const QString &mimeType, mimeTypes) {
definitionId = definitionIdByMimeType(mimeType);
if (!definitionId.isEmpty())
break;
}
return definitionId;
}
const QSharedPointer<HighlightDefinition> &Manager::definition(const QString &id)
{
if (!m_definitions.contains(id)) {
......
......@@ -59,6 +59,7 @@ public:
QString definitionIdByName(const QString &name) const;
QString definitionIdByMimeType(const QString &mimeType) const;
QString definitionIdByAnyMimeType(const QStringList &mimeTypes) const;
bool isBuildingDefinition(const QString &id) const;
const QSharedPointer<HighlightDefinition> &definition(const QString &id);
......
......@@ -37,6 +37,7 @@
#include "highlighter.h"
#include "highlighterexception.h"
#include "manager.h"
#include "normalindenter.h"
#include <coreplugin/coreconstants.h>
#include <coreplugin/uniqueidmanager.h>
......@@ -65,6 +66,7 @@ PlainTextEditor::PlainTextEditor(QWidget *parent)
setRequestMarkEnabled(false);
setLineSeparatorsAllowed(true);
setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT));
setDisplayName(tr(Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME));
m_commentDefinition.clearCommentStyles();
......@@ -72,6 +74,9 @@ PlainTextEditor::PlainTextEditor(QWidget *parent)
connect(file(), SIGNAL(changed()), this, SLOT(configure()));
}
PlainTextEditor::~PlainTextEditor()
{}
QList<int> PlainTextEditorEditable::context() const
{
return m_context;
......@@ -95,32 +100,42 @@ void PlainTextEditor::unCommentSelection()
Utils::unCommentSelection(this, m_commentDefinition);
}
void PlainTextEditor::setFontSettings(const TextEditor::FontSettings & fs)
void PlainTextEditor::setFontSettings(const FontSettings & fs)
{
TextEditor::BaseTextEditor::setFontSettings(fs);
Highlighter *highlighter = static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
if (!highlighter)
return;
BaseTextEditor::setFontSettings(fs);
highlighter->configureFormats(fs);
highlighter->rehighlight();
if (baseTextDocument()->syntaxHighlighter()) {
Highlighter *highlighter =
static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
highlighter->configureFormats(fs);
highlighter->rehighlight();
}
}
void PlainTextEditor::configure()
{
const QString &mimeType = Core::ICore::instance()->mimeDatabase()->findByFile(
QFileInfo(file()->fileName())).type();
baseTextDocument()->setMimeType(mimeType);
configure(Core::ICore::instance()->mimeDatabase()->findByFile(file()->fileName()));
}
void PlainTextEditor::configure(const Core::MimeType &mimeType)
{
if (mimeType.isNull())
return;
const QString &type = mimeType.type();
setMimeType(type);
QString definitionId = Manager::instance()->definitionIdByMimeType(type);
if (definitionId.isEmpty())
definitionId = findDefinitionId(mimeType, true);
const QString &definitionId = Manager::instance()->definitionIdByMimeType(mimeType);
if (!definitionId.isEmpty()) {
try {
const QSharedPointer<HighlightDefinition> &definition =
Manager::instance()->definition(definitionId);
Highlighter *highlighter = new Highlighter(definition->initialContext());
highlighter->configureFormats(TextEditor::TextEditorSettings::instance()->fontSettings());
highlighter->configureFormats(TextEditorSettings::instance()->fontSettings());
baseTextDocument()->setSyntaxHighlighter(highlighter);
......@@ -132,11 +147,27 @@ void PlainTextEditor::configure()
}
}
// @todo: Indentation specification through the definition files is not really being
// used because Kate recommends to configure indentation through another feature.
// Maybe we should provide something similar in Creator? For now, only normal
// indentation is supported.
m_indenter.reset(new TextEditor::NormalIndenter);
// @todo: Indentation specification through the definition files is not really being used
// because Kate recommends to configure indentation through another feature. Maybe we should
// provide something similar in Creator? For now, only normal indentation is supported.
m_indenter.reset(new NormalIndenter);
}
QString PlainTextEditor::findDefinitionId(const Core::MimeType &mimeType,
bool considerParents) const
{
QString definitionId = Manager::instance()->definitionIdByAnyMimeType(mimeType.aliases());
if (definitionId.isEmpty() && considerParents) {
definitionId = Manager::instance()->definitionIdByAnyMimeType(mimeType.subClassesOf());
if (definitionId.isEmpty()) {
foreach (const QString &parent, mimeType.subClassesOf()) {
const Core::MimeType &parentMimeType =
Core::ICore::instance()->mimeDatabase()->findByType(parent);
definitionId = findDefinitionId(parentMimeType, considerParents);
}
}
}
return definitionId;
}
void PlainTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
......
......@@ -31,16 +31,20 @@
#define PLAINTEXTEDITOR_H
#include "basetexteditor.h"
#include "normalindenter.h"
#include <utils/uncommentselection.h>
#include <QtCore/QList>
#include <QtCore/QScopedPointer>
namespace Core {
class MimeType;
}
namespace TextEditor {
class PlainTextEditor;
class Indenter;
class TEXTEDITOR_EXPORT PlainTextEditorEditable : public BaseTextEditorEditable
{
......@@ -63,10 +67,13 @@ class TEXTEDITOR_EXPORT PlainTextEditor : public BaseTextEditor
public:
PlainTextEditor(QWidget *parent);
~PlainTextEditor();
void configure(const Core::MimeType &mimeType);
public slots:
virtual void unCommentSelection();
virtual void setFontSettings(const TextEditor::FontSettings &);
virtual void setFontSettings(const FontSettings &fs);
private slots:
void configure();
......@@ -76,8 +83,10 @@ protected:
virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
private:
QString findDefinitionId(const Core::MimeType &mimeType, bool considerParents) const;
Utils::CommentDefinition m_commentDefinition;
QScopedPointer<TextEditor::Indenter> m_indenter;
QScopedPointer<Indenter> m_indenter;
};
} // namespace TextEditor
......
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