Kate
katematch.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de> 00003 Copyright (C) 2007 Sebastian Pipping <webmaster@hartwork.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "katematch.h" 00021 00022 #include "kateregexpsearch.h" 00023 #include "katedocument.h" 00024 #include <ktexteditor/movingrange.h> 00025 00026 KateMatch::KateMatch(KateDocument *document, KTextEditor::Search::SearchOptions options) 00027 : m_document(document) 00028 , m_options(options) 00029 { 00030 m_resultRanges.append(KTextEditor::Range::invalid()); 00031 } 00032 00033 00034 KTextEditor::Range KateMatch::searchText(const KTextEditor::Range &range, const QString &pattern) 00035 { 00036 m_resultRanges = m_document->searchText(range, pattern, m_options); 00037 00038 return m_resultRanges[0]; 00039 } 00040 00041 00042 KTextEditor::Range KateMatch::replace(const QString &replacement, bool blockMode, int replacementCounter) 00043 { 00044 // Placeholders depending on search mode 00045 const bool usePlaceholders = m_options.testFlag(KTextEditor::Search::Regex) || 00046 m_options.testFlag(KTextEditor::Search::EscapeSequences); 00047 00048 const QString finalReplacement = usePlaceholders ? buildReplacement(replacement, blockMode, replacementCounter) 00049 : replacement; 00050 00051 // Track replacement operation 00052 KTextEditor::MovingRange *const afterReplace = m_document->newMovingRange(range(), KTextEditor::MovingRange::ExpandLeft | KTextEditor::MovingRange::ExpandRight); 00053 00054 blockMode = blockMode && !range().onSingleLine(); 00055 m_document->replaceText(range(), finalReplacement, blockMode); 00056 00057 const KTextEditor::Range result = *afterReplace; 00058 delete afterReplace; 00059 00060 return result; 00061 } 00062 00063 00064 KTextEditor::Range KateMatch::range() const 00065 { 00066 if (m_resultRanges.size() > 0) 00067 return m_resultRanges[0]; 00068 00069 return KTextEditor::Range::invalid(); 00070 } 00071 00072 00073 bool KateMatch::isEmpty() const 00074 { 00075 return range().isEmpty(); 00076 } 00077 00078 00079 bool KateMatch::isValid() const 00080 { 00081 return range().isValid(); 00082 } 00083 00084 00085 QString KateMatch::buildReplacement(const QString &replacement, bool blockMode, int replacementCounter) const { 00086 QStringList capturedTexts; 00087 foreach (const KTextEditor::Range &captureRange, m_resultRanges) { 00088 // Copy capture content 00089 capturedTexts << m_document->text(captureRange, blockMode); 00090 } 00091 00092 return KateRegExpSearch::buildReplacement(replacement, capturedTexts, replacementCounter); 00093 } 00094 00095 00096 // kate: space-indent on; indent-width 4; replace-tabs on;
KDE 4.6 API Reference