2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "baseannotationhighlighter.h"
|
2019-07-03 18:34:30 +02:00
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
#include <texteditor/fontsettings.h>
|
2018-09-12 10:03:35 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2019-07-03 18:34:30 +02:00
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QColor>
|
2023-05-19 13:03:01 +02:00
|
|
|
#include <QDebug>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextCharFormat>
|
2023-05-19 13:03:01 +02:00
|
|
|
#include <QTextDocument>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
typedef QMap<QString, QTextCharFormat> ChangeNumberFormatMap;
|
|
|
|
|
|
2011-03-28 14:19:17 +02:00
|
|
|
/*!
|
2012-01-07 12:31:48 +01:00
|
|
|
\class VcsBase::BaseAnnotationHighlighter
|
2011-03-28 14:19:17 +02:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The BaseAnnotationHighlighter class is the base class for a
|
|
|
|
|
highlighter for annotation lines of the form 'changenumber:XXXX'.
|
2011-03-28 14:19:17 +02:00
|
|
|
|
|
|
|
|
The change numbers are assigned a color gradient. Example:
|
|
|
|
|
\code
|
|
|
|
|
112: text1 <color 1>
|
|
|
|
|
113: text2 <color 2>
|
|
|
|
|
112: text3 <color 1>
|
|
|
|
|
\endcode
|
|
|
|
|
*/
|
|
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
namespace VcsBase {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-01-07 03:35:54 +01:00
|
|
|
class BaseAnnotationHighlighterPrivate
|
|
|
|
|
{
|
2011-12-08 13:07:00 +01:00
|
|
|
public:
|
2016-02-03 12:15:21 +01:00
|
|
|
BaseAnnotationHighlighterPrivate(BaseAnnotationHighlighter *q_) : q(q_) { }
|
2013-08-13 12:57:31 +02:00
|
|
|
|
|
|
|
|
void updateOtherFormats();
|
2023-05-19 13:03:01 +02:00
|
|
|
QSet<QString> annotationChanges() const;
|
2013-08-13 12:57:31 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
ChangeNumberFormatMap m_changeNumberMap;
|
2023-05-19 13:03:01 +02:00
|
|
|
Annotation m_annotation;
|
2012-03-06 14:32:32 +01:00
|
|
|
QColor m_background;
|
2016-02-03 12:15:21 +01:00
|
|
|
BaseAnnotationHighlighter *const q;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
void BaseAnnotationHighlighterPrivate::updateOtherFormats()
|
|
|
|
|
{
|
2018-09-18 12:46:48 +02:00
|
|
|
m_background = q->fontSettings()
|
2018-09-12 10:03:35 +02:00
|
|
|
.toTextCharFormat(TextEditor::C_TEXT)
|
|
|
|
|
.brushProperty(QTextFormat::BackgroundBrush)
|
|
|
|
|
.color();
|
2019-07-03 18:34:30 +02:00
|
|
|
q->setChangeNumbers(Utils::toSet(m_changeNumberMap.keys()));
|
2013-08-13 12:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:03:01 +02:00
|
|
|
|
|
|
|
|
QSet<QString> BaseAnnotationHighlighterPrivate::annotationChanges() const
|
|
|
|
|
{
|
|
|
|
|
if (!q->document())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
QSet<QString> changes;
|
|
|
|
|
const QString text = q->document()->toPlainText();
|
|
|
|
|
QStringView txt = QStringView(text);
|
|
|
|
|
if (txt.isEmpty())
|
|
|
|
|
return changes;
|
|
|
|
|
if (!m_annotation.separatorPattern.pattern().isEmpty()) {
|
|
|
|
|
const QRegularExpressionMatch match = m_annotation.separatorPattern.match(txt);
|
|
|
|
|
if (match.hasMatch())
|
|
|
|
|
txt.truncate(match.capturedStart());
|
|
|
|
|
}
|
|
|
|
|
QRegularExpressionMatchIterator i = m_annotation.entryPattern.globalMatch(txt);
|
|
|
|
|
while (i.hasNext()) {
|
|
|
|
|
const QRegularExpressionMatch match = i.next();
|
|
|
|
|
changes.insert(match.captured(1));
|
|
|
|
|
}
|
|
|
|
|
return changes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BaseAnnotationHighlighter::BaseAnnotationHighlighter(const Annotation &annotation, QTextDocument *document)
|
|
|
|
|
: TextEditor::SyntaxHighlighter(document)
|
|
|
|
|
, d(new BaseAnnotationHighlighterPrivate(this))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2017-05-05 20:34:29 +02:00
|
|
|
setDefaultTextFormatCategories();
|
2013-08-13 12:57:31 +02:00
|
|
|
|
2023-05-19 13:03:01 +02:00
|
|
|
d->m_annotation = annotation;
|
|
|
|
|
d->updateOtherFormats();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseAnnotationHighlighter::~BaseAnnotationHighlighter()
|
|
|
|
|
{
|
2016-02-03 12:15:21 +01:00
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:03:01 +02:00
|
|
|
void BaseAnnotationHighlighter::documentChanged(QTextDocument *oldDoc, QTextDocument *newDoc)
|
|
|
|
|
{
|
|
|
|
|
if (oldDoc)
|
|
|
|
|
disconnect(oldDoc,
|
|
|
|
|
&QTextDocument::contentsChange,
|
|
|
|
|
this,
|
|
|
|
|
&BaseAnnotationHighlighter::setChangeNumbersForAnnotation);
|
|
|
|
|
|
|
|
|
|
if (newDoc)
|
|
|
|
|
connect(newDoc,
|
|
|
|
|
&QTextDocument::contentsChange,
|
|
|
|
|
this,
|
|
|
|
|
&BaseAnnotationHighlighter::setChangeNumbersForAnnotation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseAnnotationHighlighter::setChangeNumbersForAnnotation()
|
|
|
|
|
{
|
|
|
|
|
setChangeNumbers(d->annotationChanges());
|
|
|
|
|
d->updateOtherFormats();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void BaseAnnotationHighlighter::setChangeNumbers(const ChangeNumbers &changeNumbers)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
d->m_changeNumberMap.clear();
|
2022-05-15 09:27:53 +03:00
|
|
|
const int changeNumberCount = changeNumbers.size();
|
|
|
|
|
if (changeNumberCount == 0)
|
|
|
|
|
return;
|
|
|
|
|
// Assign a color gradient to annotation change numbers. Give
|
|
|
|
|
// each change number a unique color.
|
|
|
|
|
const QList<QColor> colors =
|
|
|
|
|
TextEditor::SyntaxHighlighter::generateColors(changeNumberCount, d->m_background);
|
|
|
|
|
int m = 0;
|
|
|
|
|
const int cstep = colors.count() / changeNumberCount;
|
|
|
|
|
const ChangeNumbers::const_iterator cend = changeNumbers.constEnd();
|
|
|
|
|
for (ChangeNumbers::const_iterator it = changeNumbers.constBegin(); it != cend; ++it) {
|
|
|
|
|
QTextCharFormat format;
|
|
|
|
|
format.setForeground(colors.at(m));
|
|
|
|
|
d->m_changeNumberMap.insert(*it, format);
|
|
|
|
|
m += cstep;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseAnnotationHighlighter::highlightBlock(const QString &text)
|
|
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
if (text.isEmpty() || d->m_changeNumberMap.empty())
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
|
|
|
|
const QString change = changeNumber(text);
|
2011-09-07 14:26:11 +02:00
|
|
|
const ChangeNumberFormatMap::const_iterator it = d->m_changeNumberMap.constFind(change);
|
|
|
|
|
if (it != d->m_changeNumberMap.constEnd())
|
2017-05-08 10:52:28 +02:00
|
|
|
setFormatWithSpaces(text, 0, text.length(), it.value());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
void BaseAnnotationHighlighter::setFontSettings(const TextEditor::FontSettings &fontSettings)
|
2012-03-06 14:32:32 +01:00
|
|
|
{
|
2013-08-13 12:57:31 +02:00
|
|
|
SyntaxHighlighter::setFontSettings(fontSettings);
|
|
|
|
|
d->updateOtherFormats();
|
2012-03-06 14:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-19 13:03:01 +02:00
|
|
|
void BaseAnnotationHighlighter::rehighlight()
|
|
|
|
|
{
|
|
|
|
|
const QSet<QString> changes = d->annotationChanges();
|
|
|
|
|
if (changes.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
setChangeNumbers(changes);
|
|
|
|
|
TextEditor::SyntaxHighlighter::rehighlight();
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
} // namespace VcsBase
|