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

Enable replacing -> Kate has now a replace in files plugin :)

(needs testing and refinement.)
parent cd133b3b
......@@ -187,11 +187,16 @@ m_curResults(0)
connect(m_kateApp->documentManager(), SIGNAL(documentWillBeDeleted(KTextEditor::Document*)),
&m_searchOpenFiles, SLOT(cancelSearch()));
connect(m_kateApp->documentManager(), SIGNAL(documentWillBeDeleted(KTextEditor::Document*)),
&m_replacer, SLOT(cancelReplace()));
// Hook into line edit context menus
m_ui.searchCombo->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_ui.searchCombo, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT(searchContextMenu(QPoint)));
m_replacer.setDocumentManager(m_kateApp->documentManager());
searchPlaceChanged();
m_toolView->installEventFilter(this);
......@@ -286,6 +291,7 @@ void KatePluginSearchView::startSearch()
QRegExp reg(m_ui.searchCombo->currentText(),
m_matchCase->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive,
m_useRegExp->isChecked() ? QRegExp::RegExp : QRegExp::FixedString);
m_curResults->regExp = reg;
clearMarks();
m_curResults->tree->clear();
......@@ -368,17 +374,21 @@ void KatePluginSearchView::matchFound(const QString &url, int line, int column,
if (!m_curResults) {
return;
}
QString bold = Qt::escape(lineContent.left(column));
bold += "<b>" + Qt::escape(lineContent.mid(column, matchLen)) + "</b>";
bold += Qt::escape(lineContent.mid(column + matchLen));
QString pre = Qt::escape(lineContent.left(column));
QString match = Qt::escape(lineContent.mid(column, matchLen));
QString post = Qt::escape(lineContent.mid(column + matchLen));
QStringList row;
row << i18n("Line: <b>%1</b>: %2", line+1, bold);
row << i18n("Line: <b>%1</b>: %2", line+1, pre+"<b>"+match+"</b>"+post);
QTreeWidgetItem *item = new QTreeWidgetItem(rootFileItem(url), row);
item->setData(0, Qt::UserRole, url);
item->setData(0, Qt::ToolTipRole, url);
item->setData(1, Qt::UserRole, line);
item->setData(2, Qt::UserRole, column);
item->setData(3, Qt::UserRole, matchLen);
item->setData(1, Qt::ToolTipRole, pre);
item->setData(2, Qt::ToolTipRole, match);
item->setData(3, Qt::ToolTipRole, post);
item->setCheckState (0, Qt::Checked);
// Add mark if the document is open
......@@ -529,9 +539,6 @@ void KatePluginSearchView::addTab()
res->tree->setItemDelegate(new SPHtmlDelegate(res->tree));
// temporarily disable until replace works.
res->replaceButton->setDisabled(true);
connect(res->tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
this, SLOT (itemSelected(QTreeWidgetItem*)));
......@@ -662,7 +669,7 @@ void KatePluginSearchView::replaceChecked()
}
m_replacer.replaceChecked(m_curResults->tree,
QRegExp()/*FIXME*/,
m_curResults->regExp,
m_curResults->replaceCombo->currentText());
}
......
......@@ -45,7 +45,8 @@ class Results: public QWidget, public Ui::Results
Q_OBJECT
public:
Results(QWidget *parent = 0): QWidget(parent), matches(0) { setupUi(this); }
int matches;
int matches;
QRegExp regExp;
};
class KatePluginSearch : public Kate::Plugin
......
......@@ -20,23 +20,34 @@
#include "replace_matches.h"
#include "replace_matches.moc"
#include <QTreeWidgetItem>
#include <ktexteditor/movinginterface.h>
#include <ktexteditor/movingrange.h>
ReplaceMatches::ReplaceMatches(QObject *parent) : QObject(parent), m_nextIndex(-1)
ReplaceMatches::ReplaceMatches(QObject *parent) : QObject(parent),
m_manager(0),
m_tree(0),
m_rootIndex(-1)
{
connect(this, SIGNAL(replaceNextMatch()), this, SLOT(doReplaceNextMatch()), Qt::QueuedConnection);
}
void ReplaceMatches::replaceChecked(QTreeWidget *tree, const QRegExp &regexp, const QString &replace)
{
if (m_nextIndex != -1) return;
if (m_manager == 0) return;
if (m_rootIndex != -1) return;
m_tree = tree;
m_nextIndex = 0;
m_rootIndex = 0;
m_regExp = regexp;
m_replace = replace;
m_replaceText = replace;
m_cancelReplace = false;
emit replaceNextMatch();
}
void ReplaceMatches::setDocumentManager(Kate::DocumentManager *manager)
{
m_manager = manager;
}
void ReplaceMatches::cancelReplace()
{
......@@ -45,39 +56,76 @@ void ReplaceMatches::cancelReplace()
void ReplaceMatches::doReplaceNextMatch()
{
if (m_cancelReplace) {
m_nextIndex = -1;
if ((!m_manager) || (m_cancelReplace)) {
m_rootIndex = -1;
emit replaceDone();
return;
}
kDebug();
// int column;
// NOTE The document managers signal documentWillBeDeleted() must be connected to
// cancelReplace(). A closed file could lead to a crash if it is not handled.
// for (int line =0; line < m_docList[m_nextIndex]->lines(); line++) {
// column = m_regExp.indexIn(m_docList[m_nextIndex]->line(line));
// while (column != -1) {
// if (m_docList[m_nextIndex]->url().isLocalFile() ) {
// emit matchFound(m_docList[m_nextIndex]->url().path(), line, column,
// m_docList[m_nextIndex]->line(line), m_regExp.matchedLength());
// }
// else {
// emit matchFound(m_docList[m_nextIndex]->url().prettyUrl(), line, column,
// m_docList[m_nextIndex]->line(line), m_regExp.matchedLength());
// }
// column = m_regExp.indexIn(m_docList[m_nextIndex]->line(line), column + 1);
// }
// }
// m_nextIndex++;
// if (m_nextIndex == m_docList.size()) {
// m_nextIndex = -1;
// Open the file
QTreeWidgetItem *rootItem = m_tree->topLevelItem(m_rootIndex);
if (!rootItem) {
m_rootIndex = -1;
emit replaceDone();
// }
// else {
// emit replaceNextMatch();
// }
return;
}
if (rootItem->checkState(0) == Qt::Unchecked) {
m_rootIndex++;
emit replaceNextMatch();
return;
}
KTextEditor::Document *doc = m_manager->findUrl(rootItem->data(0, Qt::UserRole).toString());
if (!doc) {
doc = m_manager->openUrl(rootItem->data(0, Qt::UserRole).toString());
}
if (!doc) {
m_rootIndex++;
emit replaceNextMatch();
return;
}
QVector<KTextEditor::MovingRange*> rVector;
KTextEditor::MovingInterface* miface = qobject_cast<KTextEditor::MovingInterface*>(doc);
int line;
int column;
int len;
QTreeWidgetItem *item;
// lines might be modified so search the document again
for (int i=0; i<rootItem->childCount(); i++) {
item = rootItem->child(i);
if (item->checkState(0) == Qt::Unchecked) continue;
line = item->data(1, Qt::UserRole).toInt();
column = item->data(2, Qt::UserRole).toInt();
len = item->data(3, Qt::UserRole).toInt();
if (m_regExp.indexIn(doc->line(line), column) != column) {
kDebug() << "expression does not match";
continue;
}
QString html = item->data(1, Qt::ToolTipRole).toString();
html += "<b><s>" + item->data(2, Qt::ToolTipRole).toString() + "</s> ";
html += m_replaceText + "</b>";
html += item->data(3, Qt::ToolTipRole).toString();
item->setData(0, Qt::DisplayRole, QString("Line: <b>%1</b>: %2").arg(line+1).arg(html));
KTextEditor::Range range(line, column, line, column+len);
KTextEditor::MovingRange* mr = miface->newMovingRange(range);
rVector.append(mr);
}
for (int i=0; i<rVector.size(); i++) {
doc->replaceText(*rVector[i], m_replaceText);
}
qDeleteAll(rVector);
m_rootIndex++;
emit replaceNextMatch();
}
......@@ -25,6 +25,7 @@
#include <QRegExp>
#include <QTreeWidget>
#include <ktexteditor/document.h>
#include <kate/documentmanager.h>
class ReplaceMatches: public QObject
{
......@@ -32,6 +33,7 @@ class ReplaceMatches: public QObject
public:
ReplaceMatches(QObject *parent = 0);
void setDocumentManager(Kate::DocumentManager *manager);
void replaceChecked(QTreeWidget *tree, const QRegExp &regexp, const QString &replace);
......@@ -46,10 +48,11 @@ Q_SIGNALS:
void replaceDone();
private:
Kate::DocumentManager *m_manager;
QTreeWidget *m_tree;
int m_nextIndex;
int m_rootIndex;
QRegExp m_regExp;
QString m_replace;
QString m_replaceText;
bool m_cancelReplace;
};
......
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