Commit f42fe8c8 authored by Kåre Särs's avatar Kåre Särs

Add search as you type to the search and replace plugin

parent a858cfef
......@@ -12,6 +12,7 @@ endif(NOT KDE4_FOUND)
set(katesearchplugin_PART_SRCS
plugin_search.cpp
search_while_typing.cpp
search_open_files.cpp
search_folder.cpp
search_project.cpp
......
/* Kate search plugin
*
* Copyright (C) 2011 by Kåre Särs <kare.sars@iki.fi>
* Copyright (C) 2011-2012 by Kåre Särs <kare.sars@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -42,6 +42,8 @@
#include <kaboutdata.h>
#include <kurlcompletion.h>
#include <klineedit.h>
#include <kcolorscheme.h>
#include <QKeyEvent>
#include <QClipboard>
#include <QMenu>
......@@ -219,6 +221,8 @@ m_projectPluginView(0)
connect(m_ui.displayOptions, SIGNAL(toggled(bool)), this, SLOT(toggleOptions(bool)));
connect(m_ui.searchPlaceCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(searchPlaceChanged()));
connect(m_ui.searchCombo, SIGNAL(editTextChanged(QString)), this, SLOT(searchPatternChanged()));
connect(m_ui.matchCase, SIGNAL(stateChanged(int)), this, SLOT(searchPatternChanged()));
connect(m_ui.useRegExp, SIGNAL(stateChanged(int)), this, SLOT(searchPatternChanged()));
connect(m_ui.stopButton, SIGNAL(clicked()), &m_searchOpenFiles, SLOT(cancelSearch()));
connect(m_ui.stopButton, SIGNAL(clicked()), &m_searchFolder, SLOT(cancelSearch()));
connect(m_ui.stopButton, SIGNAL(clicked()), &m_searchProject, SLOT(cancelSearch()));
......@@ -237,6 +241,10 @@ m_projectPluginView(0)
this, SLOT(matchFound(QString,int,int,QString,int)));
connect(&m_searchProject, SIGNAL(searchDone()), this, SLOT(searchDone()));
connect(&m_searchWhileTyping, SIGNAL(matchFound(QString,int,int,QString,int)),
this, SLOT(matchFound(QString,int,int,QString,int)));
connect(&m_searchWhileTyping, SIGNAL(searchDone()), this, SLOT(searchWhileTypingDone()));
connect(m_kateApp->documentManager(), SIGNAL(documentWillBeDeleted(KTextEditor::Document*)),
&m_searchOpenFiles, SLOT(cancelSearch()));
......@@ -362,12 +370,12 @@ void KatePluginSearchView::startSearch()
// return pressed in the folder combo or filter combo
return;
}
m_curResults =qobject_cast<Results *>(m_ui.resultTabWidget->currentWidget());
m_ui.searchCombo->addToHistory(m_ui.searchCombo->currentText());
m_curResults = qobject_cast<Results *>(m_ui.resultTabWidget->currentWidget());
if (!m_curResults) {
kWarning() << "This is a bug";
return;
}
m_ui.searchCombo->addToHistory(m_ui.searchCombo->currentText());
m_ui.newTabButton->setDisabled(true);
m_ui.searchCombo->setDisabled(true);
......@@ -440,6 +448,34 @@ void KatePluginSearchView::searchPlaceChanged()
void KatePluginSearchView::searchPatternChanged()
{
m_ui.searchButton->setDisabled(m_ui.searchCombo->currentText().isEmpty());
if (m_ui.searchCombo->currentText().length() < 3) return;
if (!mainWindow()->activeView()) return;
KTextEditor::Document *doc = mainWindow()->activeView()->document();
if (!doc) return;
m_curResults =qobject_cast<Results *>(m_ui.resultTabWidget->currentWidget());
if (!m_curResults) {
kWarning() << "This is a bug";
return;
}
QRegExp reg(m_ui.searchCombo->currentText(),
m_ui.matchCase->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive,
m_ui.useRegExp->isChecked() ? QRegExp::RegExp : QRegExp::FixedString);
m_curResults->regExp = reg;
clearMarks();
m_curResults->tree->clear();
m_curResults->buttonContainer->setEnabled(false);
m_curResults->matches = 0;
m_curResults->selectAllCB->setText(i18n("Select all"));
m_curResults->selectAllCB->setChecked(true);
disconnect(m_curResults->tree, SIGNAL(itemChanged(QTreeWidgetItem*,int)), m_curResults, SLOT(checkCheckedState()));
m_searchWhileTyping.startSearch(doc, reg);
}
QTreeWidgetItem * KatePluginSearchView::rootFileItem(const QString &url)
......@@ -618,10 +654,54 @@ void KatePluginSearchView::searchDone()
connect(m_curResults->tree, SIGNAL(itemChanged(QTreeWidgetItem*,int)), m_curResults, SLOT(checkCheckedState()));
indicateMatch(m_curResults->tree->topLevelItemCount() > 0);
m_curResults = 0;
m_toolView->unsetCursor();
}
void KatePluginSearchView::searchWhileTypingDone()
{
if (!m_curResults) {
return;
}
if (m_curResults->tree->topLevelItemCount() > 0) {
m_curResults->tree->setCurrentItem(m_curResults->tree->topLevelItem(0));
m_curResults->setFocus(Qt::OtherFocusReason);
}
m_curResults->tree->resizeColumnToContents(0);
m_curResults->buttonContainer->setEnabled(true);
connect(m_curResults->tree, SIGNAL(itemChanged(QTreeWidgetItem*,int)), m_curResults, SLOT(checkCheckedState()));
indicateMatch(m_curResults->tree->topLevelItemCount() > 0);
if (m_curResults->tree->topLevelItemCount() > 0) {
itemSelected(m_curResults->tree->topLevelItem(0));
}
m_curResults = 0;
m_ui.searchCombo->lineEdit()->setFocus();
}
void KatePluginSearchView::indicateMatch(bool hasMatch) {
QLineEdit * const lineEdit = m_ui.searchCombo->lineEdit();
QPalette background(lineEdit->palette());
if (hasMatch) {
// Green background for line edit
KColorScheme::adjustBackground(background, KColorScheme::PositiveBackground);
}
else {
// Reset background of line edit
background = QPalette();
}
// Red background for line edit
//KColorScheme::adjustBackground(background, KColorScheme::NegativeBackground);
// Neutral background
//KColorScheme::adjustBackground(background, KColorScheme::NeutralBackground);
lineEdit->setPalette(background);
}
void KatePluginSearchView::itemSelected(QTreeWidgetItem *item)
{
if (!item) return;
......
......@@ -31,6 +31,7 @@
#include "ui_search.h"
#include "ui_results.h"
#include "search_while_typing.h"
#include "search_open_files.h"
#include "search_folder.h"
#include "search_project.h"
......@@ -108,6 +109,8 @@ private Q_SLOTS:
void addMatchMark(KTextEditor::Document* doc, int line, int column, int len);
void searchDone();
void searchWhileTypingDone();
void indicateMatch(bool hasMatch);
void itemSelected(QTreeWidgetItem *item);
......@@ -140,6 +143,7 @@ private:
SearchOpenFiles m_searchOpenFiles;
SearchFolder m_searchFolder;
SearchProject m_searchProject;
SearchWhileTyping m_searchWhileTyping;
ReplaceMatches m_replacer;
KAction *m_matchCase;
KAction *m_useRegExp;
......
/* Kate search plugin
*
* Copyright (C) 2012 by Kåre Särs <kare.sars@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file called COPYING; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "search_while_typing.h"
#include "search_while_typing.moc"
#include <QTime>
void SearchWhileTyping::startSearch(const KTextEditor::Document *doc, const QRegExp &regExp)
{
int column;
QTime maxTime;
maxTime.start();
for (int line =0; line < doc->lines(); line++) {
if (maxTime.elapsed() > 20) {
kDebug() << "Search time exceeded -> stop" << maxTime.elapsed() << line;
break;
}
column = regExp.indexIn(doc->line(line));
while (column != -1) {
emit matchFound(doc->url().pathOrUrl(), line, column,
doc->line(line), regExp.matchedLength());
column = regExp.indexIn(doc->line(line), column + 1);
}
}
emit searchDone();
}
/* Kate search plugin
*
* Copyright (C) 2012 by Kåre Särs <kare.sars@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file called COPYING; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef _SEARCH_WHILE_TYPING_H_
#define _SEARCH_WHILE_TYPING_H_
#include <QObject>
#include <QRegExp>
#include <ktexteditor/document.h>
class SearchWhileTyping: public QObject
{
Q_OBJECT
public:
void startSearch(const KTextEditor::Document *doc, const QRegExp &regExp);
Q_SIGNALS:
void matchFound(const QString &url, int line, int column, const QString &lineContent, int matchLen);
void searchDone();
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment