Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

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

S&R: Add match-endLine & endColumn to enable easier handling of ranges

parent 240f663f
......@@ -105,8 +105,10 @@ void SearchDiskFiles::searchSingleLineRegExp(const QString &fileName)
// limit line length
if (line.length() > 1024) line = line.left(1024);
QUrl fileUrl = QUrl::fromUserInput(fileName);
emit matchFound(fileUrl.toString(),fileUrl.fileName(),
i, column, line, match.capturedLength());
emit matchFound(fileUrl.toString(), fileUrl.fileName(),
line, match.capturedLength(),
i, column, i, column+match.capturedLength());
match = m_regExp.match(line, column + match.capturedLength());
column = match.capturedStart();
m_matchCount++;
......@@ -167,11 +169,14 @@ void SearchDiskFiles::searchMultiLineRegExp(const QString &fileName)
break;
}
QUrl fileUrl = QUrl::fromUserInput(fileName);
int startColumn = (column - lineStart[line]);
int endLine = line + match.captured().count(QLatin1Char('\n'));
int lastNL = match.captured().lastIndexOf(QLatin1Char('\n'));
int endColumn = lastNL == -1 ? startColumn + match.captured().length() : match.captured().length() - lastNL-1;
emit matchFound(fileUrl.toString(),fileUrl.fileName(),
line,
(column - lineStart[line]),
fullDoc.mid(lineStart[line], column - lineStart[line])+match.captured(),
match.capturedLength());
match.capturedLength(),
line, startColumn, endLine, endColumn);
match = tmpRegExp.match(fullDoc, column + match.capturedLength());
column = match.capturedStart();
m_matchCount++;
......
......@@ -51,8 +51,9 @@ public Q_SLOTS:
void cancelSearch();
Q_SIGNALS:
void matchFound(const QString &url, const QString &docName, int line, int column,
const QString &lineContent, int matchLen);
void matchFound(const QString &url, const QString &docName,
const QString &lineContent, int matchLen,
int line, int column, int endLine, int endColumn);
void searchDone();
void searching(const QString &file);
......
......@@ -703,7 +703,7 @@ QTreeWidgetItem * KatePluginSearchView::rootFileItem(const QString &url, const Q
return item;
}
void KatePluginSearchView::addMatchMark(KTextEditor::Document* doc, int line, int column, int matchLen)
void KatePluginSearchView::addMatchMark(KTextEditor::Document* doc, int line, int column, int endLine, int endColumn)
{
if (!doc) return;
......@@ -731,14 +731,6 @@ void KatePluginSearchView::addMatchMark(KTextEditor::Document* doc, int line, in
attr->setForeground(activeView->defaultStyleAttribute(KTextEditor::dsNormal)->foreground().color());
}
}
// calculate end line in case of multi-line match
int endLine = line;
int endColumn = column + matchLen;
while ((endLine < doc->lines()) && (endColumn > doc->line(endLine).size())) {
endColumn -= doc->line(endLine).size();
endColumn--; // remove one for '\n'
endLine++;
}
KTextEditor::Range range(line, column, endLine, endColumn);
......@@ -776,30 +768,33 @@ void KatePluginSearchView::addMatchMark(KTextEditor::Document* doc, int line, in
this, SLOT(clearMarks()), Qt::UniqueConnection);
}
void KatePluginSearchView::matchFound(const QString &url, const QString &fName, int line, int column,
const QString &lineContent, int matchLen)
void KatePluginSearchView::matchFound(const QString &url, const QString &fName,
const QString &lineContent, int matchLen,
int startLine, int startColumn, int endLine, int endColumn)
{
if (!m_curResults) {
return;
}
QString pre = lineContent.left(column).toHtmlEscaped();
QString match = lineContent.mid(column, matchLen).toHtmlEscaped();
QString pre = lineContent.left(startColumn).toHtmlEscaped();
QString match = lineContent.mid(startColumn, matchLen).toHtmlEscaped();
match.replace(QLatin1Char('\n'), QStringLiteral("\\n"));
QString post = lineContent.mid(column + matchLen).toHtmlEscaped();
QString post = lineContent.mid(startColumn + matchLen).toHtmlEscaped();
QStringList row;
row << i18n("Line: <b>%1</b>: %2", line+1, pre+QStringLiteral("<b>")+match+QStringLiteral("</b>")+post);
row << i18n("Line: <b>%1</b>: %2", startLine+1, pre+QStringLiteral("<b>")+match+QStringLiteral("</b>")+post);
TreeWidgetItem *item = new TreeWidgetItem(rootFileItem(url, fName), row);
item->setData(0, ReplaceMatches::FileUrlRole, url);
item->setData(0, Qt::ToolTipRole, url);
item->setData(0, ReplaceMatches::FileNameRole, fName);
item->setData(0, ReplaceMatches::LineRole, line);
item->setData(0, ReplaceMatches::ColumnRole, column);
item->setData(0, ReplaceMatches::LineRole, startLine);
item->setData(0, ReplaceMatches::ColumnRole, startColumn);
item->setData(0, ReplaceMatches::MatchLenRole, matchLen);
item->setData(0, ReplaceMatches::PreMatchRole, pre);
item->setData(0, ReplaceMatches::MatchRole, match);
item->setData(0, ReplaceMatches::PostMatchRole, post);
item->setData(0, ReplaceMatches::EndLineRole, endLine);
item->setData(0, ReplaceMatches::EndColumnRole, endColumn);
item->setCheckState (0, Qt::Checked);
m_curResults->matches++;
......@@ -812,7 +807,7 @@ void KatePluginSearchView::matchFound(const QString &url, const QString &fName,
else {
doc = m_kateApp->findUrl(QUrl::fromUserInput(url));
}
addMatchMark(doc, line, column, matchLen);
addMatchMark(doc, startLine, startColumn, endLine, endColumn);
}
void KatePluginSearchView::clearMarks()
......@@ -1299,13 +1294,13 @@ void KatePluginSearchView::replaceSingleMatch()
return;
}
int dLine = m_mainWindow->activeView()->cursorPosition().line();
int dColumn = m_mainWindow->activeView()->cursorPosition().column();
int cursorLine = m_mainWindow->activeView()->cursorPosition().line();
int cursorColumn = m_mainWindow->activeView()->cursorPosition().column();
int iLine = item->data(0, ReplaceMatches::LineRole).toInt();
int iColumn = item->data(0, ReplaceMatches::ColumnRole).toInt();
int startLine = item->data(0, ReplaceMatches::LineRole).toInt();
int startColumn = item->data(0, ReplaceMatches::ColumnRole).toInt();
if ((dLine != iLine) || (dColumn != iColumn)) {
if ((cursorLine != startLine) || (cursorColumn != startColumn)) {
itemSelected(item);
return;
}
......@@ -1315,8 +1310,8 @@ void KatePluginSearchView::replaceSingleMatch()
int i;
for (i=0; i<m_matchRanges.size(); i++) {
if (m_matchRanges[i]->document() != doc) continue;
if (m_matchRanges[i]->start().line() != iLine) continue;
if (m_matchRanges[i]->start().column() != iColumn) continue;
if (m_matchRanges[i]->start().line() != startLine) continue;
if (m_matchRanges[i]->start().column() != startColumn) continue;
break;
}
......@@ -1342,7 +1337,13 @@ void KatePluginSearchView::replaceSingleMatch()
replaceText.replace(QStringLiteral("¤Search&Replace¤"), QStringLiteral("\\\\"));
doc->replaceText(m_matchRanges[i]->toRange(), replaceText);
addMatchMark(doc, dLine, dColumn, replaceText.size());
// FIXME temporary find end- line & column
int endLine = startLine + replaceText.count(QLatin1Char('\n'));
int lastNL = replaceText.lastIndexOf(QLatin1Char('\n'));
int endColumn = lastNL == -1 ? startColumn + replaceText.length() : replaceText.length() - lastNL-1;
addMatchMark(doc, startLine, startColumn, endLine, endColumn);
replaceText.replace(QLatin1Char('\n'), QStringLiteral("\\n"));
replaceText.replace(QLatin1Char('\t'), QStringLiteral("\\t"));
......@@ -1359,9 +1360,10 @@ void KatePluginSearchView::replaceSingleMatch()
item = res->tree->itemBelow(item);
if (!item) break;
if (item->data(0, ReplaceMatches::FileUrlRole).toString() != doc->url().toString()) break;
iLine = item->data(0, ReplaceMatches::LineRole).toInt();
iColumn = item->data(0, ReplaceMatches::ColumnRole).toInt();
if ((m_matchRanges[i]->start().line() == iLine) && (m_matchRanges[i]->start().column() == iColumn)) {
int itemLine = item->data(0, ReplaceMatches::LineRole).toInt();
int itemColumn = item->data(0, ReplaceMatches::ColumnRole).toInt();
if ((m_matchRanges[i]->start().line() == itemLine) && (m_matchRanges[i]->start().column() == itemColumn)) {
// The single change does not influence the rest of the document
break;
}
item->setData(0, ReplaceMatches::LineRole, m_matchRanges[i]->start().line());
......@@ -1491,16 +1493,13 @@ void KatePluginSearchView::docViewChanged()
}
}
if (fileItem) {
int line;
int column;
int len;
QTreeWidgetItem *item;
for (int i=0; i<fileItem->childCount(); i++) {
item = fileItem->child(i);
line = item->data(0, ReplaceMatches::LineRole).toInt();
column = item->data(0, ReplaceMatches::ColumnRole).toInt();
len = item->data(0, ReplaceMatches::MatchLenRole).toInt();
addMatchMark(doc, line, column, len);
QTreeWidgetItem *item = fileItem->child(i);
int line = item->data(0, ReplaceMatches::LineRole).toInt();
int column = item->data(0, ReplaceMatches::ColumnRole).toInt();
int endLine = item->data(0, ReplaceMatches::EndLineRole).toInt();
int endColumn = item->data(0, ReplaceMatches::EndColumnRole).toInt();
addMatchMark(doc, line, column, endLine, endColumn);
}
}
}
......
......@@ -137,10 +137,10 @@ private Q_SLOTS:
void folderFileListChanged();
void matchFound(const QString &url, const QString &fileName, int line, int column,
const QString &lineContent, int matchLen);
void matchFound(const QString &url, const QString &fileName,
const QString &lineContent, int matchLen, int startLine, int startColumn, int endLine, int endColumn);
void addMatchMark(KTextEditor::Document* doc, int line, int column, int len);
void addMatchMark(KTextEditor::Document* doc, int line, int column, int endLine, int endColumn);
void searchDone();
void searchWhileTypingDone();
......
......@@ -141,7 +141,7 @@ void ReplaceMatches::doReplaceNextMatch()
item = rootItem->child(i);
if (item->checkState(0) == Qt::Unchecked) continue;
line = endLine= item->data(0, LineRole).toInt();
line = endLine = item->data(0, LineRole).toInt();
column = item->data(0, ColumnRole).toInt();
matchLen = item->data(0, MatchLenRole).toInt();
matchLines = doc->line(line).mid(column);
......@@ -196,10 +196,13 @@ void ReplaceMatches::doReplaceNextMatch()
}
for (int i=0; i<rVector.size(); i++) {
line = rVector[i]->start().line();
column = rVector[i]->start().column();
int startLine = rVector[i]->start().line();
int startColumn = rVector[i]->start().column();
int endLine = startLine + rTexts[i].count(QLatin1Char('\n'));
int lastNL = rTexts[i].lastIndexOf(QLatin1Char('\n'));
int endColumn = lastNL == -1 ? startColumn + rTexts[i].length() : rTexts[i].length() - lastNL-1;
doc->replaceText(*rVector[i], rTexts[i]);
emit matchReplaced(doc, line, column, rTexts[i].length());
emit matchReplaced(doc, startLine, startColumn, endLine, endColumn);
}
qDeleteAll(rVector);
......
......@@ -41,7 +41,10 @@ public:
MatchLenRole,
PreMatchRole,
MatchRole,
PostMatchRole
PostMatchRole,
EndLineRole,
EndColumnRole,
ReplacedRole,
};
ReplaceMatches(QObject *parent = nullptr);
......@@ -59,7 +62,7 @@ private Q_SLOTS:
Q_SIGNALS:
void replaceNextMatch();
void matchReplaced(KTextEditor::Document* doc, int line, int column, int matchLen);
void matchReplaced(KTextEditor::Document* doc, int line, int column, int endLine, int endColumn);
void replaceStatus(const QUrl &url);
void replaceDone();
......
......@@ -104,8 +104,9 @@ int SearchOpenFiles::searchSingleLineRegExp(KTextEditor::Document *doc, const QR
match = regExp.match(doc->line(line));
column = match.capturedStart();
while (column != -1 && !match.captured().isEmpty()) {
emit matchFound(doc->url().toString(), doc->documentName(), line, column,
doc->line(line), match.capturedLength());
emit matchFound(doc->url().toString(), doc->documentName(),
doc->line(line), match.capturedLength(),
line, column, line, column+match.capturedLength());
match = regExp.match(doc->line(line), column + match.capturedLength());
column = match.capturedStart();
}
......@@ -113,15 +114,15 @@ int SearchOpenFiles::searchSingleLineRegExp(KTextEditor::Document *doc, const QR
return 0;
}
int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRegularExpression &regExp, int startLine)
int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRegularExpression &regExp, int inStartLine)
{
int column = 0;
int line = 0;
int startLine = 0;
QTime time;
time.start();
QRegularExpression tmpRegExp = regExp;
if (startLine == 0) {
if (inStartLine == 0) {
// Copy the whole file to a temporary buffer to be able to search newlines
m_fullDoc.clear();
m_lineStart.clear();
......@@ -137,9 +138,9 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
}
}
else {
if (startLine>0 && startLine<m_lineStart.size()) {
column = m_lineStart[startLine];
line = startLine;
if (inStartLine>0 && inStartLine<m_lineStart.size()) {
column = m_lineStart[inStartLine];
startLine = inStartLine;
}
else {
return 0;
......@@ -158,28 +159,33 @@ int SearchOpenFiles::searchMultiLineRegExp(KTextEditor::Document *doc, const QRe
while (column != -1 && !match.captured().isEmpty()) {
// search for the line number of the match
int i;
line = -1;
startLine = -1;
for (i=1; i<m_lineStart.size(); i++) {
if (m_lineStart[i] > column) {
line = i-1;
startLine = i-1;
break;
}
}
if (line == -1) {
if (startLine == -1) {
break;
}
int startColumn = (column - m_lineStart[startLine]);
int endLine = startLine + match.captured().count(QLatin1Char('\n'));
int lastNL = match.captured().lastIndexOf(QLatin1Char('\n'));
int endColumn = lastNL == -1 ? startColumn + match.captured().length() : match.captured().length() - lastNL-1;
emit matchFound(doc->url().toString(), doc->documentName(),
line,
(column - m_lineStart[line]),
doc->line(line).left(column - m_lineStart[line])+match.captured(),
match.capturedLength());
doc->line(startLine).left(column - m_lineStart[startLine])+match.captured(),
match.capturedLength(),
startLine, startColumn, endLine, endColumn);
match = tmpRegExp.match(m_fullDoc, column + match.capturedLength());
column = match.capturedStart();
if (time.elapsed() > 100) {
//qDebug() << "Search time exceeded" << time.elapsed() << line;
return line;
return startLine;
}
}
return 0;
......
......@@ -51,7 +51,8 @@ private:
Q_SIGNALS:
void searchNextFile(int startLine);
void matchFound(const QString &url, const QString &fileName, int line, int column, const QString &lineContent, int matchLen);
void matchFound(const QString &url, const QString &fileName, const QString &lineContent, int matchLen,
int line, int column, int endLine, int endColumn);
void searchDone();
void searching(const QString &file);
......
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