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

S&R: Improve '$' (end of line) handling in multi-line-search

parent 840023d9
......@@ -108,7 +108,7 @@ void SearchDiskFiles::searchMultiLineRegExp(const QString &fileName)
int line = 0;
static QString fullDoc;
static QVector<int> lineStart;
QRegExp tmpRegExp = m_regExp;
if (!file.open(QFile::ReadOnly)) {
return;
......@@ -125,11 +125,17 @@ void SearchDiskFiles::searchMultiLineRegExp(const QString &fileName)
lineStart << i+1;
}
}
if (tmpRegExp.pattern().endsWith("$")) {
fullDoc += '\n';
QString newPatern = tmpRegExp.pattern();
newPatern.replace("$", "(?=\\n)");
tmpRegExp.setPattern(newPatern);
}
column = m_regExp.indexIn(fullDoc, column);
column = tmpRegExp.indexIn(fullDoc, column);
while (column != -1) {
if (m_cancelSearch) break;
if (m_regExp.cap().isEmpty()) break;
if (tmpRegExp.cap().isEmpty()) break;
// search for the line number of the match
int i;
line = -1;
......@@ -145,9 +151,9 @@ void SearchDiskFiles::searchMultiLineRegExp(const QString &fileName)
emit matchFound(fileName,
line,
(column - lineStart[line]),
fullDoc.mid(lineStart[line], column - lineStart[line])+m_regExp.cap(),
m_regExp.matchedLength());
column = m_regExp.indexIn(fullDoc, column + m_regExp.matchedLength());
fullDoc.mid(lineStart[line], column - lineStart[line])+tmpRegExp.cap(),
tmpRegExp.matchedLength());
column = tmpRegExp.indexIn(fullDoc, column + tmpRegExp.matchedLength());
}
}
......@@ -633,8 +633,17 @@ void KatePluginSearchView::addMatchMark(KTextEditor::Document* doc, int line, in
}
KTextEditor::Range range(line, column, endLine, endColumn);
if (m_curResults && !replace) {
if (!m_curResults->regExp.exactMatch(doc->text(range))) {
// special handling for "(?=\\n)" in multi-line search
QRegExp tmpReg = m_curResults->regExp;
if (m_curResults->regExp.pattern().endsWith("(?=\\n)")) {
QString newPatern = tmpReg.pattern();
newPatern.replace("(?=\\n)", "$");
tmpReg.setPattern(newPatern);
}
if (tmpReg.indexIn(doc->text(range)) != 0) {
kDebug() << doc->text(range) << "Does not match" << m_curResults->regExp.pattern();
return;
}
......@@ -1199,6 +1208,11 @@ void KatePluginSearchView::itemSelected(QTreeWidgetItem *item)
{
if (!item) return;
m_curResults = qobject_cast<Results *>(m_ui.resultTabWidget->currentWidget());
if (!m_curResults) {
return;
}
while (item->data(2, Qt::UserRole).toString().isEmpty()) {
item->treeWidget()->expandItem(item);
item = item->child(0);
......
......@@ -112,6 +112,7 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
int line = 0;
QTime time;
time.start();
QRegExp tmpRegExp = regExp;
if (startLine == 0) {
// Copy the whole file to a temporary buffer to be able to search newlines
......@@ -122,7 +123,11 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
m_fullDoc += doc->line(i) + '\n';
m_lineStart << m_fullDoc.size();
}
m_fullDoc.remove(m_fullDoc.size()-1, 1);
if (!regExp.pattern().endsWith("$")) {
// if regExp ends with '$' leave the extra newline at the end as
// '$' will be replaced with (?=\\n), which needs the extra newline
m_fullDoc.remove(m_fullDoc.size()-1, 1);
}
}
else {
if (startLine>0 && startLine<m_lineStart.size()) {
......@@ -134,9 +139,15 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
}
}
column = regExp.indexIn(m_fullDoc, column);
if (regExp.pattern().endsWith("$")) {
QString newPatern = tmpRegExp.pattern();
newPatern.replace("$", "(?=\\n)");
tmpRegExp.setPattern(newPatern);
}
column = tmpRegExp.indexIn(m_fullDoc, column);
while (column != -1) {
if (regExp.cap().isEmpty()) break;
if (tmpRegExp.cap().isEmpty()) break;
// search for the line number of the match
int i;
line = -1;
......@@ -152,9 +163,9 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
emit matchFound(doc->url().pathOrUrl(),
line,
(column - m_lineStart[line]),
doc->line(line).left(column - m_lineStart[line])+regExp.cap(),
regExp.matchedLength());
column = regExp.indexIn(m_fullDoc, column + regExp.matchedLength());
doc->line(line).left(column - m_lineStart[line])+tmpRegExp.cap(),
tmpRegExp.matchedLength());
column = tmpRegExp.indexIn(m_fullDoc, column + tmpRegExp.matchedLength());
if (time.elapsed() > 100) {
//kDebug() << "Search time exceeded" << time.elapsed() << line;
......
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