KTextEditor

katematch.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
3 SPDX-FileCopyrightText: 2007 Sebastian Pipping <webmaster@hartwork.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "katematch.h"
9
10#include "katedocument.h"
11#include "kateregexpsearch.h"
12
13KateMatch::KateMatch(KTextEditor::DocumentPrivate *document, KTextEditor::SearchOptions options)
14 : m_document(document)
15 , m_options(options)
16{
17 m_resultRanges.append(KTextEditor::Range::invalid());
18}
19
20KTextEditor::Range KateMatch::searchText(KTextEditor::Range range, const QString &pattern)
21{
22 m_resultRanges = m_document->searchText(range, pattern, m_options);
23
24 return m_resultRanges[0];
25}
26
27KTextEditor::Range KateMatch::replace(const QString &replacement, bool blockMode, int replacementCounter)
28{
29 // Placeholders depending on search mode
30 // skip place-holder stuff if we have no \ at all inside the replacement, the buildReplacement is expensive
31 const bool usePlaceholders =
32 (m_options.testFlag(KTextEditor::Regex) || m_options.testFlag(KTextEditor::EscapeSequences)) && replacement.contains(QLatin1Char('\\'));
33
34 const QString finalReplacement = usePlaceholders ? buildReplacement(replacement, blockMode, replacementCounter) : replacement;
35
36 // Track replacement operation, reuse range if already there
37 if (m_afterReplaceRange) {
38 m_afterReplaceRange->setRange(range());
39 } else {
40 m_afterReplaceRange.reset(m_document->newMovingRange(range(), KTextEditor::MovingRange::ExpandLeft | KTextEditor::MovingRange::ExpandRight));
41 }
42
43 // replace and return results range
44 m_document->replaceText(range(), finalReplacement, blockMode && !range().onSingleLine());
45 return m_afterReplaceRange->toRange();
46}
47
48KTextEditor::Range KateMatch::range() const
49{
50 if (!m_resultRanges.isEmpty()) {
51 return m_resultRanges[0];
52 }
53
55}
56
57bool KateMatch::isEmpty() const
58{
59 return range().isEmpty();
60}
61
62bool KateMatch::isValid() const
63{
64 return range().isValid();
65}
66
67QString KateMatch::buildReplacement(const QString &replacement, bool blockMode, int replacementCounter) const
68{
69 QStringList capturedTexts;
70 capturedTexts.reserve(m_resultRanges.size());
71 for (KTextEditor::Range captureRange : std::as_const(m_resultRanges)) {
72 // Copy capture content
73 capturedTexts << m_document->text(captureRange, blockMode);
74 }
75
76 return KateRegExpSearch::buildReplacement(replacement, capturedTexts, replacementCounter);
77}
Backend of KTextEditor::Document related public KTextEditor interfaces.
@ ExpandRight
Expand to encapsulate new characters to the right of the range.
@ ExpandLeft
Expand to encapsulate new characters to the left of the range.
An object representing a section of text, from one Cursor to another.
static constexpr Range invalid() noexcept
Returns an invalid range.
static QString buildReplacement(const QString &text, const QStringList &capturedTexts, int replacementCounter)
Returns a modified version of text where.
@ Regex
Treats the pattern as a regular expression.
Definition document.h:51
@ EscapeSequences
Plaintext mode: Processes escape sequences.
Definition document.h:58
QFlags< SearchOption > SearchOptions
Stores a combination of SearchOption values.
Definition document.h:65
void reserve(qsizetype size)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 31 2025 12:11:24 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.