From cafba7db580f9f5a26cad8c62a99ad8bea072f04 Mon Sep 17 00:00:00 2001 From: Leandro Melo <leandro.melo@nokia.com> Date: Mon, 3 May 2010 16:39:47 +0200 Subject: [PATCH] Integrating highlighter formats with Creator's font settings. --- src/plugins/genericeditor/XML/java.xml | 4 +- src/plugins/genericeditor/editor.cpp | 19 ++- src/plugins/genericeditor/editor.h | 3 + .../genericeditor/genericeditorplugin.cpp | 4 + .../highlightdefinitionhandler.cpp | 5 +- src/plugins/genericeditor/highlighter.cpp | 97 +++++++++++- src/plugins/genericeditor/highlighter.h | 13 +- src/plugins/genericeditor/itemdata.cpp | 141 +++++++++--------- src/plugins/genericeditor/itemdata.h | 45 +++++- src/plugins/genericeditor/reuse.h | 9 ++ 10 files changed, 251 insertions(+), 89 deletions(-) diff --git a/src/plugins/genericeditor/XML/java.xml b/src/plugins/genericeditor/XML/java.xml index c7f0f396732..a5e30dcbb6e 100644 --- a/src/plugins/genericeditor/XML/java.xml +++ b/src/plugins/genericeditor/XML/java.xml @@ -3838,8 +3838,8 @@ <itemData name="Normal Text" defStyleNum="dsNormal"/> <itemData name="Keyword" defStyleNum="dsKeyword"/> <itemData name="Function" defStyleNum="dsFunction"/> - <itemData name="StaticImports" defStyleNum="dsKeyword" color="#800080" selColor="#FFFFFF" bold="0" italic="0"/> - <itemData name="Imports" defStyleNum="dsKeyword" color="#808000" selColor="#FFFFFF" bold="0" italic="0"/> + <itemData name="StaticImports" defStyleNum="dsKeyword" color="#B00080" selColor="#FFFFFF" bold="0" italic="0"/> + <itemData name="Imports" defStyleNum="dsKeyword" color="#B00080" selColor="#FFFFFF" bold="0" italic="0"/> <itemData name="Data Type" defStyleNum="dsDataType"/> <itemData name="Decimal" defStyleNum="dsDecVal"/> <itemData name="Octal" defStyleNum="dsBaseN"/> diff --git a/src/plugins/genericeditor/editor.cpp b/src/plugins/genericeditor/editor.cpp index 1d50eb1ec3d..3a19588a5f0 100644 --- a/src/plugins/genericeditor/editor.cpp +++ b/src/plugins/genericeditor/editor.cpp @@ -39,6 +39,8 @@ #include <coreplugin/mimedatabase.h> #include <texteditor/texteditorconstants.h> #include <texteditor/basetextdocument.h> +#include <texteditor/texteditorsettings.h> +#include <texteditor/fontsettings.h> #include <QtCore/QSharedPointer> #include <QtCore/QFileInfo> @@ -64,6 +66,18 @@ TextEditor::BaseTextEditorEditable *Editor::createEditableInterface() return editable; } +void Editor::setFontSettings(const TextEditor::FontSettings & fs) +{ + TextEditor::BaseTextEditor::setFontSettings(fs); + + Highlighter *highlighter = static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter()); + if (!highlighter) + return; + + highlighter->configureFormats(fs); + highlighter->rehighlight(); +} + void Editor::configure() { const QString &mimeType = Core::ICore::instance()->mimeDatabase()->findByFile( @@ -76,7 +90,10 @@ void Editor::configure() QSharedPointer<HighlightDefinition> definition = GenericEditorPlugin::instance()->definition(definitionId); - baseTextDocument()->setSyntaxHighlighter(new Highlighter(definition->initialContext())); + Highlighter *highlighter = new Highlighter(definition->initialContext()); + highlighter->configureFormats(TextEditor::TextEditorSettings::instance()->fontSettings()); + + baseTextDocument()->setSyntaxHighlighter(highlighter); m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces()); m_commentDefinition.setSingleLine(definition->singleLineComment()); diff --git a/src/plugins/genericeditor/editor.h b/src/plugins/genericeditor/editor.h index b8bd9aa6d24..a1317aca956 100644 --- a/src/plugins/genericeditor/editor.h +++ b/src/plugins/genericeditor/editor.h @@ -53,6 +53,9 @@ public: protected: virtual TextEditor::BaseTextEditorEditable *createEditableInterface(); +public slots: + virtual void setFontSettings(const TextEditor::FontSettings &); + private slots: void configure(); diff --git a/src/plugins/genericeditor/genericeditorplugin.cpp b/src/plugins/genericeditor/genericeditorplugin.cpp index 2b5e0c3eb0b..eea12541a45 100644 --- a/src/plugins/genericeditor/genericeditorplugin.cpp +++ b/src/plugins/genericeditor/genericeditorplugin.cpp @@ -38,6 +38,7 @@ #include <coreplugin/icore.h> #include <coreplugin/mimedatabase.h> +#include <texteditor/texteditorsettings.h> #include <utils/qtcassert.h> #include <QtCore/QtPlugin> @@ -142,6 +143,7 @@ bool GenericEditorPlugin::initialize(const QStringList &arguments, QString *erro m_actionHandler = new TextEditor::TextEditorActionHandler( GenericEditor::Constants::GENERIC_EDITOR, + TextEditor::TextEditorActionHandler::Format | TextEditor::TextEditorActionHandler::UnCommentSelection); m_actionHandler->initializeActions(); @@ -154,6 +156,8 @@ void GenericEditorPlugin::extensionsInitialized() void GenericEditorPlugin::initializeEditor(Editor *editor) { m_actionHandler->setupActions(editor); + + TextEditor::TextEditorSettings::instance()->initializeEditor(editor); } QString GenericEditorPlugin::definitionIdByName(const QString &name) const diff --git a/src/plugins/genericeditor/highlightdefinitionhandler.cpp b/src/plugins/genericeditor/highlightdefinitionhandler.cpp index ff4717474ec..85313b528c6 100644 --- a/src/plugins/genericeditor/highlightdefinitionhandler.cpp +++ b/src/plugins/genericeditor/highlightdefinitionhandler.cpp @@ -275,7 +275,6 @@ void HighlightDefinitionHandler::itemDataElementStarted(const QXmlAttributes &at itemData->setBold(atts.value(kBold)); itemData->setUnderlined(atts.value(kUnderline)); itemData->setStrikedOut(atts.value(kStrikeout)); - itemData->configureFormat(); } void HighlightDefinitionHandler::commentElementStarted(const QXmlAttributes &atts) const @@ -434,8 +433,8 @@ void HighlightDefinitionHandler::processIncludeRules(const QSharedPointer<Contex const QString &sourceName = instruction.sourceContext(); if (sourceName.startsWith(kDoubleHash)) { // This refers to an external definition. The rules included are the ones from its - // initial context. Others contexts and rules from the external definition will - // transparently to the highlighter. This because contexts and rules know the + // initial context. Others contexts and rules from the external definition will work + // transparently to the highlighter. This is because contexts and rules know the // definition they are from. QString externalName = QString::fromRawData(sourceName.unicode() + 2, sourceName.length() - 2); diff --git a/src/plugins/genericeditor/highlighter.cpp b/src/plugins/genericeditor/highlighter.cpp index fc3784103db..a9f32829bdc 100644 --- a/src/plugins/genericeditor/highlighter.cpp +++ b/src/plugins/genericeditor/highlighter.cpp @@ -34,8 +34,13 @@ #include "itemdata.h" #include "highlighterexception.h" #include "progressdata.h" +#include "reuse.h" -#include <QtCore/QStringList> +#include <texteditor/texteditorconstants.h> +#include <texteditor/fontsettings.h> + +#include <QtCore/QLatin1String> +#include <QtCore/QLatin1Char> using namespace GenericEditor; using namespace Internal; @@ -91,11 +96,13 @@ void Highlighter::highlightBlock(const QString &text) handleContextChange(m_currentContext->lineEndContext(), m_currentContext->definition(), false); - - m_contexts.clear(); } catch (const HighlighterException &) { m_isBroken = true; + return; } + + m_contexts.clear(); + applyVisualWhitespaceFormat(text); } void Highlighter::setupDataForBlock(const QString &text) @@ -283,18 +290,53 @@ void Highlighter::handleContextChange(const QString &contextName, void Highlighter::applyFormat(int offset, int count, - const QString &itemData, + const QString &itemDataName, const QSharedPointer<HighlightDefinition> &definition) { if (count == 0) return; + QSharedPointer<ItemData> itemData; try { - setFormat(offset, count, definition->itemData(itemData)->format()); + itemData = definition->itemData(itemDataName); } catch (const HighlighterException &) { - // This case does not break the highlighter. In fact, currently there are broken xml - // definition files which Kate can cope with. For instance, the Printf context in java.xml - // points to an inexistent Printf item data. + // Although the formatting is skipped this case does not break the highlighter. In fact, + // currently there are broken xml definition files which Kate can cope with. For instance, + // the Printf context in java.xml points to an inexistent Printf item data. + return; + } + + QTextCharFormat format = m_genericFormats.value(itemData->style()); + + // Apply personalizations (if specified) for this particular item data from the current + // definition only. + if (itemData->color().isValid()) + format.setForeground(itemData->color()); + if (itemData->isItalicSpecified()) + format.setFontItalic(itemData->isItalic()); + if (itemData->isBoldSpecified()) + format.setFontWeight(toFontWeight(itemData->isBold())); + if (itemData->isUnderlinedSpecified()) + format.setFontUnderline(itemData->isUnderlined()); + if (itemData->isStrikedOutSpecified()) + format.setFontStrikeOut(itemData->isStrikedOut()); + + setFormat(offset, count, format); +} + +void Highlighter::applyVisualWhitespaceFormat(const QString &text) +{ + int offset = 0; + const int length = text.length(); + while (offset < length) { + if (text.at(offset).isSpace()) { + int start = offset++; + while (offset < length && text.at(offset).isSpace()) + ++offset; + setFormat(start, offset - start, m_visualWhitespaceFormat); + } else { + ++offset; + } } } @@ -384,3 +426,42 @@ void Highlighter::setCurrentContext() throw HighlighterException(); m_currentContext = m_contexts.back(); } + +void Highlighter::configureFormats(const TextEditor::FontSettings & fs) +{ + m_visualWhitespaceFormat = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE)); + + m_genericFormats[ItemData::kDsNormal] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_TEXT)); + m_genericFormats[ItemData::kDsKeyword] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_KEYWORD)); + m_genericFormats[ItemData::kDsDataType] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_TYPE)); + m_genericFormats[ItemData::kDsDecVal] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_NUMBER)); + m_genericFormats[ItemData::kDsBaseN] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_NUMBER)); + m_genericFormats[ItemData::kDsFloat] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_NUMBER)); + m_genericFormats[ItemData::kDsChar] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_STRING)); + m_genericFormats[ItemData::kDsString] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_STRING)); + m_genericFormats[ItemData::kDsComment] = fs.toTextCharFormat( + QLatin1String(TextEditor::Constants::C_COMMENT)); + + // Currently Creator does not have corresponding formats for the following items. We can + // implement them... Just for now I will leave hardcoded colors. + QTextCharFormat format; + format.setForeground(Qt::blue); + m_genericFormats[ItemData::kDsOthers] = format; + format.setForeground(Qt::red); + m_genericFormats[ItemData::kDsAlert] = format; + format.setForeground(Qt::darkBlue); + m_genericFormats[ItemData::kDsFunction] = format; + format.setForeground(Qt::darkGray); + m_genericFormats[ItemData::kDsRegionMarker] = format; + format.setForeground(Qt::darkRed); + m_genericFormats[ItemData::kDsError] = format; +} diff --git a/src/plugins/genericeditor/highlighter.h b/src/plugins/genericeditor/highlighter.h index 0bc43526c90..e52bf8e0554 100644 --- a/src/plugins/genericeditor/highlighter.h +++ b/src/plugins/genericeditor/highlighter.h @@ -30,6 +30,7 @@ #ifndef HIGHLIGHTER_H #define HIGHLIGHTER_H +#include <QtCore/QString> #include <QtCore/QVector> #include <QtCore/QSharedPointer> #include <QtCore/QStringList> @@ -39,6 +40,10 @@ #include <texteditor/basetextdocumentlayout.h> +namespace TextEditor { +class FontSettings; +} + namespace GenericEditor { namespace Internal { @@ -53,6 +58,8 @@ public: Highlighter(const QSharedPointer<Context> &defaultContext, QTextDocument *parent = 0); virtual ~Highlighter(); + void configureFormats(const TextEditor::FontSettings & fs); + protected: virtual void highlightBlock(const QString &text); @@ -80,8 +87,9 @@ private: void applyFormat(int offset, int count, - const QString &itemData, + const QString &itemDataName, const QSharedPointer<HighlightDefinition> &definition); + void applyVisualWhitespaceFormat(const QString &text); QString currentContextSequence() const; void mapContextSequence(const QString &contextSequence); @@ -136,6 +144,9 @@ private: // Captures used in dynamic rules. QStringList m_currentCaptures; + + QTextCharFormat m_visualWhitespaceFormat; + QHash<QString, QTextCharFormat> m_genericFormats; }; } // namespace Internal diff --git a/src/plugins/genericeditor/itemdata.cpp b/src/plugins/genericeditor/itemdata.cpp index 62ed36f01c1..b7e42980454 100644 --- a/src/plugins/genericeditor/itemdata.cpp +++ b/src/plugins/genericeditor/itemdata.cpp @@ -33,95 +33,98 @@ using namespace GenericEditor; using namespace Internal; -namespace { - static const QLatin1String kDsNormal("dsNormal"); - static const QLatin1String kDsKeyword("dsKeyword"); - static const QLatin1String kDsDataType("dsDataType"); - static const QLatin1String kDsDecVal("dsDecVal"); - static const QLatin1String kDsBaseN("dsBaseN"); - static const QLatin1String kDsFloat("dsFloat"); - static const QLatin1String kDsChar("dsChar"); - static const QLatin1String kDsString("dsString"); - static const QLatin1String kDsComment("dsComment"); - static const QLatin1String kDsOthers("dsOthers"); - static const QLatin1String kDsAlert("dsAlert"); - static const QLatin1String kDsFunction("dsFunction"); - static const QLatin1String kDsRegionMarker("dsRegionMarker"); - static const QLatin1String kDsError("dsError"); -} +const QLatin1String ItemData::kDsNormal("dsNormal"); +const QLatin1String ItemData::kDsKeyword("dsKeyword"); +const QLatin1String ItemData::kDsDataType("dsDataType"); +const QLatin1String ItemData::kDsDecVal("dsDecVal"); +const QLatin1String ItemData::kDsBaseN("dsBaseN"); +const QLatin1String ItemData::kDsFloat("dsFloat"); +const QLatin1String ItemData::kDsChar("dsChar"); +const QLatin1String ItemData::kDsString("dsString"); +const QLatin1String ItemData::kDsComment("dsComment"); +const QLatin1String ItemData::kDsOthers("dsOthers"); +const QLatin1String ItemData::kDsAlert("dsAlert"); +const QLatin1String ItemData::kDsFunction("dsFunction"); +const QLatin1String ItemData::kDsRegionMarker("dsRegionMarker"); +const QLatin1String ItemData::kDsError("dsError"); -ItemData::ItemData() +ItemData::ItemData() : + m_italicSpecified(false), + m_boldSpecified(false), + m_underlinedSpecified(false), + m_strikedOutSpecified(false) {} void ItemData::setStyle(const QString &style) { m_style = style; } +const QString &ItemData::style() const +{ return m_style; } + void ItemData::setColor(const QString &color) { m_color.setNamedColor(color); } +const QColor &ItemData::color() const +{ return m_color; } + void ItemData::setSelectionColor(const QString &color) { m_selectionColor.setNamedColor(color); } +const QColor &ItemData::selectionColor() const +{ return m_selectionColor; } + void ItemData::setItalic(const QString &italic) -{ m_font.setItalic(toBool(italic)); } +{ + if (!italic.isEmpty()) { + m_italic = toBool(italic); + m_italicSpecified = true; + } +} + +bool ItemData::isItalic() const +{ return m_italic; } + +bool ItemData::isItalicSpecified() const +{ return m_italicSpecified; } void ItemData::setBold(const QString &bold) -{ m_font.setBold(toBool(bold)); } +{ + if (!bold.isEmpty()) { + m_bold = toBool(bold); + m_boldSpecified = true; + } +} -void ItemData::setUnderlined(const QString &underlined) -{ m_font.setUnderline(toBool(underlined)); } +bool ItemData::isBold() const +{ return m_bold; } -void ItemData::setStrikedOut(const QString &striked) -{ m_font.setStrikeOut(toBool(striked)); } +bool ItemData::isBoldSpecified() const +{ return m_boldSpecified; } -void ItemData::configureFormat() +void ItemData::setUnderlined(const QString &underlined) { - //Todo: Overwrite defaults when true? - m_format.setFont(m_font); - - if (m_style == kDsNormal) { - m_format.setForeground(Qt::black); - } else if (m_style == kDsKeyword) { - m_format.setForeground(Qt::black); - m_format.setFontWeight(QFont::Bold); - } else if (m_style == kDsDataType) { - m_format.setForeground(Qt::blue); - } else if (m_style == kDsDecVal) { - m_format.setForeground(Qt::darkYellow); - } else if (m_style == kDsBaseN) { - m_format.setForeground(Qt::darkYellow); - m_format.setFontWeight(QFont::Bold); - } else if (m_style == kDsFloat) { - m_format.setForeground(Qt::darkYellow); - m_format.setFontUnderline(true); - } else if (m_style == kDsChar) { - m_format.setForeground(Qt::magenta); - } else if (m_style == kDsString) { - m_format.setForeground(Qt::red); - } else if (m_style == kDsComment) { - m_format.setForeground(Qt::darkGray); - m_format.setFontItalic(true); - m_format.setFontWeight(QFont::Bold); - } else if (m_style == kDsOthers) { - m_format.setForeground(Qt::darkGreen); - } else if (m_style == kDsAlert) { - m_format.setForeground(Qt::darkRed); - m_format.setFontWeight(QFont::Bold); - } else if (m_style == kDsFunction) { - m_format.setForeground(Qt::darkBlue); - m_format.setFontWeight(QFont::Bold); - } else if (m_style == kDsRegionMarker) { - m_format.setForeground(Qt::yellow); - } else if (m_style == kDsError) { - m_format.setForeground(Qt::darkRed); - m_format.setFontUnderline(true); + if (!underlined.isEmpty()) { + m_underlined = toBool(underlined); + m_underlinedSpecified = true; } +} + +bool ItemData::isUnderlined() const +{ return m_underlined; } - if (m_color.isValid()) - m_format.setForeground(QColor(m_color)); - //if (m_selectionColor.isValid()) - //m_format.setBackground(QColor(m_selectionColor)); +bool ItemData::isUnderlinedSpecified() const +{ return m_underlinedSpecified; } + +void ItemData::setStrikedOut(const QString &striked) +{ + if (!striked.isEmpty()) { + m_strikedOut = toBool(striked); + m_strikedOutSpecified = true; + } } -const QTextCharFormat &ItemData::format() const -{ return m_format; } +bool ItemData::isStrikedOut() const +{ return m_strikedOut; } + +bool ItemData::isStrikedOutSpecified() const +{ return m_strikedOutSpecified; } diff --git a/src/plugins/genericeditor/itemdata.h b/src/plugins/genericeditor/itemdata.h index 67a968a4bcf..62819acb21b 100644 --- a/src/plugins/genericeditor/itemdata.h +++ b/src/plugins/genericeditor/itemdata.h @@ -45,22 +45,57 @@ public: ItemData(); void setStyle(const QString &style); + const QString &style() const; + void setColor(const QString &color); + const QColor &color() const; + void setSelectionColor(const QString &color); + const QColor &selectionColor() const; + void setItalic(const QString &italic); + bool isItalic() const; + bool isItalicSpecified() const; + void setBold(const QString &bold); + bool isBold() const; + bool isBoldSpecified() const; + void setUnderlined(const QString &underlined); - void setStrikedOut(const QString &striked); - void configureFormat(); + bool isUnderlined() const; + bool isUnderlinedSpecified() const; + + void setStrikedOut(const QString &striked); + bool isStrikedOut() const; + bool isStrikedOutSpecified() const; - const QTextCharFormat &format() const; + static const QLatin1String kDsNormal; + static const QLatin1String kDsKeyword; + static const QLatin1String kDsDataType; + static const QLatin1String kDsDecVal; + static const QLatin1String kDsBaseN; + static const QLatin1String kDsFloat; + static const QLatin1String kDsChar; + static const QLatin1String kDsString; + static const QLatin1String kDsComment; + static const QLatin1String kDsOthers; + static const QLatin1String kDsAlert; + static const QLatin1String kDsFunction; + static const QLatin1String kDsRegionMarker; + static const QLatin1String kDsError; private: QString m_style; QColor m_color; QColor m_selectionColor; - QFont m_font; - QTextCharFormat m_format; + bool m_italic; + bool m_italicSpecified; + bool m_bold; + bool m_boldSpecified; + bool m_underlined; + bool m_underlinedSpecified; + bool m_strikedOut; + bool m_strikedOutSpecified; }; } // namespace Internal diff --git a/src/plugins/genericeditor/reuse.h b/src/plugins/genericeditor/reuse.h index f6c66f4a10a..019f0e0e443 100644 --- a/src/plugins/genericeditor/reuse.h +++ b/src/plugins/genericeditor/reuse.h @@ -34,6 +34,7 @@ #include <QtCore/QString> #include <QtCore/QLatin1String> #include <QtCore/QChar> +#include <QtGui/QFont> namespace GenericEditor { namespace Internal { @@ -56,6 +57,14 @@ inline Qt::CaseSensitivity toCaseSensitivity(const bool sensitive) return Qt::CaseInsensitive; } +inline QFont::Weight toFontWeight(const bool bold) +{ + if (bold) + return QFont::Bold; + else + return QFont::Normal; +} + inline bool isOctalDigit(const QChar &c) { static const QLatin1Char k0('0'); -- GitLab