-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a find in files panel on the GUI
- Loading branch information
Showing
5 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#include "findinfilespanel.h" | ||
#include "core/project.h" | ||
#include "core/textdocument.h" | ||
#include "guisettings.h" | ||
#include <QFile> | ||
#include <QHBoxLayout> | ||
#include <QHeaderView> | ||
#include <QTreeWidgetItem> | ||
#include <QVBoxLayout> | ||
|
||
namespace Gui { | ||
|
||
enum FindInFilesRoles { LineRole = Qt::UserRole + 1, ColumnRole }; | ||
|
||
FindInFilesPanel::FindInFilesPanel(QWidget *parent) | ||
: QWidget(parent) | ||
, m_toolBar(new QWidget(this)) | ||
, m_resultsDisplay(new QTreeWidget(this)) | ||
{ | ||
setWindowTitle(tr("Find in Files")); | ||
setObjectName("FindInFilesPanel"); | ||
|
||
auto mainLayout = new QVBoxLayout(this); | ||
mainLayout->setContentsMargins(0, 0, 0, 0); | ||
mainLayout->setSpacing(0); | ||
setupToolBar(); | ||
mainLayout->addWidget(m_toolBar); | ||
|
||
m_resultsDisplay->setHeaderHidden(true); | ||
m_resultsDisplay->header()->setSectionResizeMode(0, QHeaderView::Stretch); | ||
mainLayout->addWidget(m_resultsDisplay); | ||
|
||
connect(m_resultsDisplay, &QTreeWidget::itemDoubleClicked, this, [this](QTreeWidgetItem *item, int) { | ||
openFileAtItem(item); | ||
}); | ||
connect(m_resultsDisplay, &QTreeWidget::itemActivated, this, [this](QTreeWidgetItem *item, int) { | ||
openFileAtItem(item); | ||
}); | ||
} | ||
|
||
QWidget *FindInFilesPanel::toolBar() const | ||
{ | ||
return m_toolBar; | ||
} | ||
|
||
void FindInFilesPanel::setupToolBar() | ||
{ | ||
auto layout = new QHBoxLayout(m_toolBar); | ||
layout->setContentsMargins(0, 0, 0, 0); | ||
layout->setSpacing(0); | ||
|
||
m_searchInput = new QLineEdit(m_toolBar); | ||
m_searchInput->setPlaceholderText(tr("Enter search pattern...")); | ||
layout->addWidget(m_searchInput); | ||
|
||
m_searchButton = new QToolButton(m_toolBar); | ||
m_searchButton->setText(tr("Find")); | ||
layout->addWidget(m_searchButton); | ||
|
||
connect(m_searchButton, &QToolButton::clicked, this, [this]() { | ||
findInFiles(m_searchInput->text()); | ||
}); | ||
|
||
connect(m_searchInput, &QLineEdit::returnPressed, this, [this]() { | ||
findInFiles(m_searchInput->text()); | ||
}); | ||
} | ||
|
||
void FindInFilesPanel::findInFiles(const QString &searchText) | ||
{ | ||
if (searchText.isEmpty()) { | ||
qWarning() << "The text to search for is empty"; | ||
return; | ||
} | ||
|
||
auto results = Core::Project::instance()->findInFiles(searchText); | ||
|
||
for (auto &resultVariant : results) { | ||
auto result = resultVariant.toMap(); | ||
|
||
QFile file(result["file"].toString()); | ||
if (file.open(QFile::ReadOnly)) { | ||
QTextStream stream(&file); | ||
int currentLine = 0; | ||
const int line = result["line"].toInt(); | ||
while (!stream.atEnd()) { | ||
const QString lineText = stream.readLine(); | ||
++currentLine; | ||
if (currentLine == line) { | ||
result["text"] = lineText; | ||
break; | ||
} | ||
} | ||
} | ||
resultVariant = result; | ||
} | ||
|
||
displayResults(results); | ||
} | ||
|
||
void FindInFilesPanel::displayResults(const QVariantList &results) const | ||
{ | ||
m_resultsDisplay->clear(); | ||
QMap<QString, QTreeWidgetItem *> fileItems; | ||
|
||
for (const auto &result : results) { | ||
const auto map = result.toMap(); | ||
const QString filePath = map["file"].toString(); | ||
const int line = map["line"].toInt(); | ||
const int column = map["column"].toInt(); | ||
const QString text = map["text"].toString(); | ||
|
||
auto it = fileItems.find(filePath); | ||
if (it == fileItems.end()) { | ||
auto fileItem = new QTreeWidgetItem(m_resultsDisplay); | ||
fileItem->setText(0, filePath); | ||
it = fileItems.insert(filePath, fileItem); | ||
} | ||
|
||
auto lineItem = new QTreeWidgetItem(it.value()); | ||
lineItem->setText(0, QString("%1 %2").arg(line).arg(text)); | ||
lineItem->setData(0, LineRole, line); | ||
lineItem->setData(1, ColumnRole, column); | ||
} | ||
} | ||
|
||
void FindInFilesPanel::openFileAtItem(QTreeWidgetItem *item) | ||
{ | ||
if (item->childCount() > 0) | ||
return; | ||
|
||
QTreeWidgetItem *parentItem = item->parent(); | ||
const QString filePath = parentItem->text(0); | ||
const int line = item->data(0, LineRole).toInt(); | ||
const int column = item->data(1, ColumnRole).toInt(); | ||
|
||
if (auto *doc = qobject_cast<Core::TextDocument *>(Core::Project::instance()->open(filePath))) { | ||
doc->gotoLine(line, column); | ||
doc->selectEndOfWord(); | ||
} else { | ||
qWarning() << "Unable to open the file:" << filePath; | ||
} | ||
} | ||
|
||
} // namespace Gui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QLineEdit> | ||
#include <QToolButton> | ||
#include <QTreeWidget> | ||
#include <QWidget> | ||
|
||
namespace Gui { | ||
|
||
class FindInFilesPanel : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit FindInFilesPanel(QWidget *parent = nullptr); | ||
|
||
QWidget *toolBar() const; | ||
void displayResults(const QVariantList &results) const; | ||
|
||
private: | ||
void setupToolBar(); | ||
void findInFiles(const QString &searchText); | ||
void openFileAtItem(QTreeWidgetItem *item); | ||
|
||
QWidget *const m_toolBar; | ||
QTreeWidget *m_resultsDisplay; | ||
QLineEdit *m_searchInput; | ||
QToolButton *m_searchButton; | ||
}; | ||
|
||
} // namespace Gui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters