File indexing completed on 2024-05-05 03:58:35

0001 /*
0002     SPDX-FileCopyrightText: 2013-2016 Simon St James <kdedevel@etotheipiplusone.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "matchhighlighter.h"
0008 
0009 #include "kateconfig.h"
0010 #include "katedocument.h"
0011 #include "katerenderer.h"
0012 #include "kateview.h"
0013 
0014 using namespace KateVi;
0015 
0016 MatchHighlighter::MatchHighlighter(KTextEditor::ViewPrivate *view)
0017     : m_view(view)
0018 {
0019     updateMatchHighlightAttrib();
0020     m_highlightedMatch = m_view->doc()->newMovingRange(KTextEditor::Range::invalid(), Kate::TextRange::DoNotExpand);
0021     m_highlightedMatch->setView(m_view); // Show only in this view.
0022     m_highlightedMatch->setAttributeOnlyForViews(true);
0023     // Use z depth defined in moving ranges interface.
0024     m_highlightedMatch->setZDepth(-10000.0);
0025     m_highlightedMatch->setAttribute(m_highlightMatchAttribute);
0026     connect(m_view, &KTextEditor::View::configChanged, this, &MatchHighlighter::updateMatchHighlightAttrib);
0027 }
0028 
0029 MatchHighlighter::~MatchHighlighter()
0030 {
0031     delete m_highlightedMatch;
0032 }
0033 
0034 void MatchHighlighter::updateMatchHighlight(KTextEditor::Range matchRange)
0035 {
0036     // Note that if matchRange is invalid, the highlight will not be shown, so we
0037     // don't need to check for that explicitly.
0038     m_highlightedMatch->setRange(matchRange);
0039 }
0040 
0041 void MatchHighlighter::updateMatchHighlightAttrib()
0042 {
0043     const QColor &matchColour = m_view->rendererConfig()->searchHighlightColor();
0044     if (!m_highlightMatchAttribute) {
0045         m_highlightMatchAttribute = new KTextEditor::Attribute;
0046     }
0047     m_highlightMatchAttribute->setBackground(matchColour);
0048     KTextEditor::Attribute::Ptr mouseInAttribute(new KTextEditor::Attribute());
0049     m_highlightMatchAttribute->setDynamicAttribute(KTextEditor::Attribute::ActivateMouseIn, mouseInAttribute);
0050     m_highlightMatchAttribute->dynamicAttribute(KTextEditor::Attribute::ActivateMouseIn)->setBackground(matchColour);
0051 }