Commit 44d04251 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Kåre Särs
Browse files

Add a widget to view projects TODOs/FIXMEs in project info toolview

BUG: 423968
parent 121e142c
......@@ -58,6 +58,7 @@ target_sources(
kateprojectinfoviewnotes.cpp
kateprojectconfigpage.cpp
kateprojectcodeanalysistool.cpp
kateprojecttodoview.cpp
branchesdialog.cpp
branchcheckoutdialog.cpp
branchesdialogmodel.cpp
......
......@@ -11,11 +11,14 @@
#include "kateprojectinfoviewnotes.h"
#include "kateprojectinfoviewterminal.h"
#include "kateprojectpluginview.h"
#include "kateprojecttodoview.h"
#include <KLocalizedString>
#include <QFileInfo>
#include "git/gitutils.h"
KateProjectInfoView::KateProjectInfoView(KateProjectPluginView *pluginView, KateProject *project)
: m_pluginView(pluginView)
, m_project(project)
......@@ -51,6 +54,15 @@ KateProjectInfoView::KateProjectInfoView(KateProjectPluginView *pluginView, Kate
*/
addTab(new KateProjectInfoViewCodeAnalysis(pluginView, project), i18n("Code Analysis"));
/**
* to do and fix me view
*/
if (GitUtils::isGitRepo(project->baseDir())) {
auto todoWidget = new KateProjectTODOView(m_pluginView->mainWindow(), project->baseDir());
addTab(todoWidget, i18n("TODOs"));
connect(project, &KateProject::modelChanged, todoWidget, &KateProjectTODOView::refresh);
}
/**
* notes
*/
......
#include "kateprojecttodoview.h"
#include <QProcess>
#include <QStandardItemModel>
#include <QTreeView>
#include <QUrl>
#include <QVBoxLayout>
#include <KTextEditor/MainWindow>
#include <KTextEditor/View>
#include <KLocalizedString>
KateProjectTODOView::KateProjectTODOView(KTextEditor::MainWindow *mainWin, const QString &projectPath)
: m_projectPath(projectPath)
, m_mainWindow(mainWin)
{
refresh();
m_model = new QStandardItemModel(this);
m_treeView = new QTreeView(this);
m_treeView->setModel(m_model);
m_treeView->setUniformRowHeights(true);
m_treeView->setRootIsDecorated(false);
connect(m_treeView, &QTreeView::clicked, this, [this](const QModelIndex &idx) {
QModelIndex fileIdx = m_model->index(idx.row(), 0);
QModelIndex lineIdx = m_model->index(idx.row(), 1);
if (!fileIdx.isValid() || !lineIdx.isValid() || !m_mainWindow) {
return;
}
QUrl file = QUrl::fromLocalFile(fileIdx.data(Qt::UserRole + 2).toString());
KTextEditor::View *v = m_mainWindow->openUrl(file);
if (v) {
v->setCursorPosition({lineIdx.data(Qt::UserRole + 1).toInt() - 1, 0});
}
});
QVBoxLayout *layout = new QVBoxLayout;
layout->setSpacing(0);
layout->addWidget(m_treeView);
setLayout(layout);
}
void KateProjectTODOView::reload(const QByteArray &data)
{
m_model->clear();
m_model->setHorizontalHeaderLabels(QStringList() << i18n("File") << i18n("Line") << i18n("Text"));
const QList<QByteArray> lines = data.split('\n');
for (const QByteArray &l : lines) {
int colon = l.indexOf(':');
if (colon < 0) {
continue;
}
QString file = QString::fromUtf8(l.mid(0, colon));
++colon;
int preCol = colon;
colon = l.indexOf(':', colon);
if (colon < 0) {
continue;
}
QByteArray lineBa = l.mid(preCol, colon - preCol);
QString line = QString::fromUtf8(lineBa);
bool ok = false;
int lineNo = lineBa.toInt(&ok);
if (!ok) {
continue;
}
QString text = QString::fromUtf8(l.mid(++colon));
auto *lineCol = new QStandardItem();
lineCol->setText(line);
lineCol->setData(lineNo, Qt::UserRole + 1);
auto *fileCol = new QStandardItem();
fileCol->setText(file);
fileCol->setData(QString(m_projectPath + QLatin1Char('/') + file), Qt::UserRole + 2);
auto *textCol = new QStandardItem();
textCol->setText(text.trimmed());
m_model->appendRow({fileCol, lineCol, textCol});
}
m_treeView->resizeColumnToContents(0);
m_treeView->resizeColumnToContents(1);
}
void KateProjectTODOView::refresh()
{
QProcess *git = new QProcess(this);
git->setWorkingDirectory(m_projectPath);
git->setArguments({QStringLiteral("grep"),
QStringLiteral("-P"), // perl regex
QStringLiteral("-i"), // case insensitive
QStringLiteral("-n"), // get line numbers
QStringLiteral("(TODO|FIXME)\\b")});
git->setProgram(QStringLiteral("git"));
connect(git, &QProcess::finished, this, [this, git](int exitCode, QProcess::ExitStatus exitStatus) {
if (exitCode != 0 || exitStatus != QProcess::NormalExit) {
git->deleteLater();
return;
}
reload(git->readAllStandardOutput());
git->deleteLater();
});
git->start(QProcess::ReadOnly);
}
#ifndef KATEPROJECTTODOVIEW_H
#define KATEPROJECTTODOVIEW_H
#include <QWidget>
#include <QPointer>
namespace KTextEditor
{
class MainWindow;
}
class KateProjectTODOView : public QWidget
{
Q_OBJECT
public:
explicit KateProjectTODOView(KTextEditor::MainWindow *mainWin, const QString &projectPath);
void refresh();
private:
void reload(const QByteArray &data);
QString m_projectPath;
class QTreeView *m_treeView;
class QStandardItemModel *m_model;
QPointer<KTextEditor::MainWindow> m_mainWindow;
};
#endif // KATEPROJECTTODOVIEW_H
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