• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • search
katematch.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
3  Copyright (C) 2007 Sebastian Pipping <webmaster@hartwork.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "katematch.h"
21 
22 #include "kateregexpsearch.h"
23 #include "katedocument.h"
24 #include <ktexteditor/movingrange.h>
25 
26 KateMatch::KateMatch(KateDocument *document, KTextEditor::Search::SearchOptions options)
27  : m_document(document)
28  , m_options(options)
29 {
30  m_resultRanges.append(KTextEditor::Range::invalid());
31 }
32 
33 
34 KTextEditor::Range KateMatch::searchText(const KTextEditor::Range &range, const QString &pattern)
35 {
36  m_resultRanges = m_document->searchText(range, pattern, m_options);
37 
38  return m_resultRanges[0];
39 }
40 
41 
42 KTextEditor::Range KateMatch::replace(const QString &replacement, bool blockMode, int replacementCounter)
43 {
44  // Placeholders depending on search mode
45  const bool usePlaceholders = m_options.testFlag(KTextEditor::Search::Regex) ||
46  m_options.testFlag(KTextEditor::Search::EscapeSequences);
47 
48  const QString finalReplacement = usePlaceholders ? buildReplacement(replacement, blockMode, replacementCounter)
49  : replacement;
50 
51  // Track replacement operation
52  KTextEditor::MovingRange *const afterReplace = m_document->newMovingRange(range(), KTextEditor::MovingRange::ExpandLeft | KTextEditor::MovingRange::ExpandRight);
53 
54  blockMode = blockMode && !range().onSingleLine();
55  m_document->replaceText(range(), finalReplacement, blockMode);
56 
57  const KTextEditor::Range result = *afterReplace;
58  delete afterReplace;
59 
60  return result;
61 }
62 
63 
64 KTextEditor::Range KateMatch::range() const
65 {
66  if (m_resultRanges.size() > 0)
67  return m_resultRanges[0];
68 
69  return KTextEditor::Range::invalid();
70 }
71 
72 
73 bool KateMatch::isEmpty() const
74 {
75  return range().isEmpty();
76 }
77 
78 
79 bool KateMatch::isValid() const
80 {
81  return range().isValid();
82 }
83 
84 
85 QString KateMatch::buildReplacement(const QString &replacement, bool blockMode, int replacementCounter) const {
86  QStringList capturedTexts;
87  foreach (const KTextEditor::Range &captureRange, m_resultRanges) {
88  // Copy capture content
89  capturedTexts << m_document->text(captureRange, blockMode);
90  }
91 
92  return KateRegExpSearch::buildReplacement(replacement, capturedTexts, replacementCounter);
93 }
94 
95 
96 // kate: space-indent on; indent-width 4; replace-tabs on;
KateMatch::isValid
bool isValid() const
Definition: katematch.cpp:79
KateRegExpSearch::buildReplacement
static QString buildReplacement(const QString &text, const QStringList &capturedTexts, int replacementCounter)
Returns a modified version of text where.
Definition: kateregexpsearch.cpp:537
KateDocument::newMovingRange
virtual KTextEditor::MovingRange * newMovingRange(const KTextEditor::Range &range, KTextEditor::MovingRange::InsertBehaviors insertBehaviors=KTextEditor::MovingRange::DoNotExpand, KTextEditor::MovingRange::EmptyBehavior emptyBehavior=KTextEditor::MovingRange::AllowEmpty)
Create a new moving range for this document.
Definition: katedocument.cpp:4741
KateMatch::isEmpty
bool isEmpty() const
Definition: katematch.cpp:73
KateMatch::replace
KTextEditor::Range replace(const QString &replacement, bool blockMode, int replacementCounter=1)
Definition: katematch.cpp:42
QVector::append
void append(const T &value)
katedocument.h
kateregexpsearch.h
KateDocument::replaceText
virtual bool replaceText(const KTextEditor::Range &range, const QString &s, bool block=false)
Definition: katedocument.cpp:4686
QString
QStringList
KateDocument
Definition: katedocument.h:74
KateDocument::searchText
virtual QVector< KTextEditor::Range > searchText(const KTextEditor::Range &range, const QString &pattern, const KTextEditor::Search::SearchOptions options)
Definition: katedocument.cpp:1387
KateDocument::text
virtual QString text(const KTextEditor::Range &range, bool blockwise=false) const
Definition: katedocument.cpp:337
KateMatch::KateMatch
KateMatch(KateDocument *document, KTextEditor::Search::SearchOptions options)
Definition: katematch.cpp:26
QVector::size
int size() const
KateMatch::range
KTextEditor::Range range() const
Definition: katematch.cpp:64
KateMatch::searchText
KTextEditor::Range searchText(const KTextEditor::Range &range, const QString &pattern)
Definition: katematch.cpp:34
katematch.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal