Coloring white spaces even if there's no highlight syntax definition set.

Relative to QTCREATORBUG-1225.
This commit is contained in:
Leandro Melo
2010-06-15 10:35:39 +02:00
parent 54ea41af47
commit 0894033f93
3 changed files with 50 additions and 45 deletions

View File

@@ -51,15 +51,12 @@ namespace {
const Highlighter::KateFormatMap Highlighter::m_kateFormats; const Highlighter::KateFormatMap Highlighter::m_kateFormats;
Highlighter::Highlighter(const QSharedPointer<Context> &defaultContext,QTextDocument *parent) : Highlighter::Highlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent), QSyntaxHighlighter(parent),
m_persistentStatesCounter(PersistentsStart), m_persistentStatesCounter(PersistentsStart),
m_dynamicContextsCounter(0), m_dynamicContextsCounter(0),
m_isBroken(false), m_isBroken(false)
m_defaultContext(defaultContext) {}
{
m_persistentStates.insert(m_defaultContext->name(), Default);
}
Highlighter::~Highlighter() Highlighter::~Highlighter()
{} {}
@@ -88,20 +85,29 @@ Highlighter::KateFormatMap::KateFormatMap()
m_ids.insert(QLatin1String("dsError"), Highlighter::Error); m_ids.insert(QLatin1String("dsError"), Highlighter::Error);
} }
void Highlighter::configureFormat(TextFormatId id, const QTextCharFormat &format)
{
m_creatorFormats[id] = format;
}
void Highlighter::setDefaultContext(const QSharedPointer<Context> &defaultContext)
{
m_defaultContext = defaultContext;
m_persistentStates.insert(m_defaultContext->name(), Default);
}
void Highlighter::highlightBlock(const QString &text) void Highlighter::highlightBlock(const QString &text)
{ {
if (m_isBroken) if (!m_defaultContext.isNull() && !m_isBroken) {
return;
try { try {
setupDataForBlock(text); setupDataForBlock(text);
handleContextChange(m_currentContext->lineBeginContext(), m_currentContext->definition()); handleContextChange(m_currentContext->lineBeginContext(),
m_currentContext->definition());
ProgressData progress; ProgressData progress;
const int length = text.length(); const int length = text.length();
while (progress.offset() < length) { while (progress.offset() < length) {
if (progress.offset() > 0 && if (progress.offset() > 0 &&
progress.onlySpacesSoFar() && progress.onlySpacesSoFar() &&
!text.at(progress.offset()).isSpace()) { !text.at(progress.offset()).isSpace()) {
@@ -111,14 +117,15 @@ void Highlighter::highlightBlock(const QString &text)
iterateThroughRules(text, length, &progress, false, m_currentContext->rules()); iterateThroughRules(text, length, &progress, false, m_currentContext->rules());
} }
handleContextChange(m_currentContext->lineEndContext(), m_currentContext->definition(), handleContextChange(m_currentContext->lineEndContext(),
m_currentContext->definition(),
false); false);
m_contexts.clear();
} catch (const HighlighterException &) { } catch (const HighlighterException &) {
m_isBroken = true; m_isBroken = true;
return; }
} }
m_contexts.clear();
applyVisualWhitespaceFormat(text); applyVisualWhitespaceFormat(text);
} }
@@ -462,8 +469,3 @@ void Highlighter::setCurrentContext()
} }
m_currentContext = m_contexts.back(); m_currentContext = m_contexts.back();
} }
void Highlighter::configureFormat(TextFormatId id, const QTextCharFormat &format)
{
m_creatorFormats[id] = format;
}

View File

@@ -51,7 +51,7 @@ class ProgressData;
class Highlighter : public QSyntaxHighlighter class Highlighter : public QSyntaxHighlighter
{ {
public: public:
Highlighter(const QSharedPointer<Context> &defaultContext, QTextDocument *parent = 0); Highlighter(QTextDocument *parent = 0);
virtual ~Highlighter(); virtual ~Highlighter();
enum TextFormatId { enum TextFormatId {
@@ -73,6 +73,8 @@ public:
}; };
void configureFormat(TextFormatId id, const QTextCharFormat &format); void configureFormat(TextFormatId id, const QTextCharFormat &format);
void setDefaultContext(const QSharedPointer<Context> &defaultContext);
protected: protected:
virtual void highlightBlock(const QString &text); virtual void highlightBlock(const QString &text);

View File

@@ -160,32 +160,31 @@ void PlainTextEditor::fileChanged()
void PlainTextEditor::configure(const Core::MimeType &mimeType) void PlainTextEditor::configure(const Core::MimeType &mimeType)
{ {
Highlighter *highlighter = new Highlighter();
baseTextDocument()->setSyntaxHighlighter(highlighter);
m_isMissingSyntaxDefinition = true; m_isMissingSyntaxDefinition = true;
if (mimeType.isNull()) QString definitionId;
return; if (!mimeType.isNull()) {
const QString &type = mimeType.type(); const QString &type = mimeType.type();
setMimeType(type); setMimeType(type);
QString definitionId = Manager::instance()->definitionIdByMimeType(type); definitionId = Manager::instance()->definitionIdByMimeType(type);
if (definitionId.isEmpty()) if (definitionId.isEmpty())
definitionId = findDefinitionId(mimeType, true); definitionId = findDefinitionId(mimeType, true);
}
if (!definitionId.isEmpty()) { if (!definitionId.isEmpty()) {
const QSharedPointer<HighlightDefinition> &definition = const QSharedPointer<HighlightDefinition> &definition =
Manager::instance()->definition(definitionId); Manager::instance()->definition(definitionId);
if (!definition.isNull()) { if (!definition.isNull()) {
Highlighter *highlighter = new Highlighter(definition->initialContext()); highlighter->setDefaultContext(definition->initialContext());
baseTextDocument()->setSyntaxHighlighter(highlighter);
m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces()); m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces());
m_commentDefinition.setSingleLine(definition->singleLineComment()); m_commentDefinition.setSingleLine(definition->singleLineComment());
m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart()); m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart());
m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd()); m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd());
setFontSettings(TextEditorSettings::instance()->fontSettings());
m_isMissingSyntaxDefinition = false; m_isMissingSyntaxDefinition = false;
} }
} else if (file()) { } else if (file()) {
@@ -194,6 +193,8 @@ void PlainTextEditor::configure(const Core::MimeType &mimeType)
m_isMissingSyntaxDefinition = false; m_isMissingSyntaxDefinition = false;
} }
setFontSettings(TextEditorSettings::instance()->fontSettings());
// @todo: Indentation specification through the definition files is not really being used // @todo: Indentation specification through the definition files is not really being used
// because Kate recommends to configure indentation through another feature. Maybe we should // because Kate recommends to configure indentation through another feature. Maybe we should
// provide something similar in Creator? For now, only normal indentation is supported. // provide something similar in Creator? For now, only normal indentation is supported.