diff --git a/src/plugins/texteditor/generichighlighter/highlighter.cpp b/src/plugins/texteditor/generichighlighter/highlighter.cpp index 8b4df9a0a5cd8410459e7ed4a4cb951786d7324d..fc2fa35bd6ca6ea35b89e29d2598a61273c6402a 100644 --- a/src/plugins/texteditor/generichighlighter/highlighter.cpp +++ b/src/plugins/texteditor/generichighlighter/highlighter.cpp @@ -51,15 +51,12 @@ namespace { const Highlighter::KateFormatMap Highlighter::m_kateFormats; -Highlighter::Highlighter(const QSharedPointer<Context> &defaultContext,QTextDocument *parent) : +Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent), m_persistentStatesCounter(PersistentsStart), m_dynamicContextsCounter(0), - m_isBroken(false), - m_defaultContext(defaultContext) -{ - m_persistentStates.insert(m_defaultContext->name(), Default); -} + m_isBroken(false) +{} Highlighter::~Highlighter() {} @@ -88,37 +85,47 @@ Highlighter::KateFormatMap::KateFormatMap() m_ids.insert(QLatin1String("dsError"), Highlighter::Error); } -void Highlighter::highlightBlock(const QString &text) +void Highlighter::configureFormat(TextFormatId id, const QTextCharFormat &format) { - if (m_isBroken) - return; + m_creatorFormats[id] = format; +} - try { - setupDataForBlock(text); +void Highlighter::setDefaultContext(const QSharedPointer<Context> &defaultContext) +{ + m_defaultContext = defaultContext; + m_persistentStates.insert(m_defaultContext->name(), Default); +} - handleContextChange(m_currentContext->lineBeginContext(), m_currentContext->definition()); +void Highlighter::highlightBlock(const QString &text) +{ + if (!m_defaultContext.isNull() && !m_isBroken) { + try { + setupDataForBlock(text); - ProgressData progress; - const int length = text.length(); - while (progress.offset() < length) { + handleContextChange(m_currentContext->lineBeginContext(), + m_currentContext->definition()); - if (progress.offset() > 0 && - progress.onlySpacesSoFar() && - !text.at(progress.offset()).isSpace()) { - progress.setOnlySpacesSoFar(false); + ProgressData progress; + const int length = text.length(); + while (progress.offset() < length) { + if (progress.offset() > 0 && + progress.onlySpacesSoFar() && + !text.at(progress.offset()).isSpace()) { + progress.setOnlySpacesSoFar(false); + } + + iterateThroughRules(text, length, &progress, false, m_currentContext->rules()); } - iterateThroughRules(text, length, &progress, false, m_currentContext->rules()); + handleContextChange(m_currentContext->lineEndContext(), + m_currentContext->definition(), + false); + m_contexts.clear(); + } catch (const HighlighterException &) { + m_isBroken = true; } - - handleContextChange(m_currentContext->lineEndContext(), m_currentContext->definition(), - false); - } catch (const HighlighterException &) { - m_isBroken = true; - return; } - m_contexts.clear(); applyVisualWhitespaceFormat(text); } @@ -462,8 +469,3 @@ void Highlighter::setCurrentContext() } m_currentContext = m_contexts.back(); } - -void Highlighter::configureFormat(TextFormatId id, const QTextCharFormat &format) -{ - m_creatorFormats[id] = format; -} diff --git a/src/plugins/texteditor/generichighlighter/highlighter.h b/src/plugins/texteditor/generichighlighter/highlighter.h index 5400ff77a3d5aaa32c1be85276ccf7cdab2a08e9..2aaabbd88c779fb539e47c7817c88ab09b348588 100644 --- a/src/plugins/texteditor/generichighlighter/highlighter.h +++ b/src/plugins/texteditor/generichighlighter/highlighter.h @@ -51,7 +51,7 @@ class ProgressData; class Highlighter : public QSyntaxHighlighter { public: - Highlighter(const QSharedPointer<Context> &defaultContext, QTextDocument *parent = 0); + Highlighter(QTextDocument *parent = 0); virtual ~Highlighter(); enum TextFormatId { @@ -73,6 +73,8 @@ public: }; void configureFormat(TextFormatId id, const QTextCharFormat &format); + void setDefaultContext(const QSharedPointer<Context> &defaultContext); + protected: virtual void highlightBlock(const QString &text); diff --git a/src/plugins/texteditor/plaintexteditor.cpp b/src/plugins/texteditor/plaintexteditor.cpp index 17a77fb80aed24b22a64d71f4f542f8b207260e1..e62e2df035e24d571b21d42f0816206039b78fee 100644 --- a/src/plugins/texteditor/plaintexteditor.cpp +++ b/src/plugins/texteditor/plaintexteditor.cpp @@ -160,32 +160,31 @@ void PlainTextEditor::fileChanged() void PlainTextEditor::configure(const Core::MimeType &mimeType) { + Highlighter *highlighter = new Highlighter(); + baseTextDocument()->setSyntaxHighlighter(highlighter); m_isMissingSyntaxDefinition = true; - if (mimeType.isNull()) - return; + QString definitionId; + if (!mimeType.isNull()) { + const QString &type = mimeType.type(); + setMimeType(type); - const QString &type = mimeType.type(); - setMimeType(type); - - QString definitionId = Manager::instance()->definitionIdByMimeType(type); - if (definitionId.isEmpty()) - definitionId = findDefinitionId(mimeType, true); + definitionId = Manager::instance()->definitionIdByMimeType(type); + if (definitionId.isEmpty()) + definitionId = findDefinitionId(mimeType, true); + } if (!definitionId.isEmpty()) { const QSharedPointer<HighlightDefinition> &definition = Manager::instance()->definition(definitionId); if (!definition.isNull()) { - Highlighter *highlighter = new Highlighter(definition->initialContext()); - baseTextDocument()->setSyntaxHighlighter(highlighter); + highlighter->setDefaultContext(definition->initialContext()); m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces()); m_commentDefinition.setSingleLine(definition->singleLineComment()); m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart()); m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd()); - setFontSettings(TextEditorSettings::instance()->fontSettings()); - m_isMissingSyntaxDefinition = false; } } else if (file()) { @@ -194,6 +193,8 @@ void PlainTextEditor::configure(const Core::MimeType &mimeType) m_isMissingSyntaxDefinition = false; } + setFontSettings(TextEditorSettings::instance()->fontSettings()); + // @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.