Skip to content

Commit

Permalink
feat: Add a code view with button for TreeSitter
Browse files Browse the repository at this point in the history
The button will display the treeSitter inspector dialog.

Related-to #62
  • Loading branch information
narnaud committed Jul 18, 2024
1 parent aa41a17 commit 39efa03
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ set(PROJECT_SOURCES
apiexecutorwidget.h
apiexecutorwidget.cpp
apiexecutorwidget.ui
codeview.cpp
codeview.h
documentpalette.h
documentpalette.cpp
fileselector.h
Expand Down
25 changes: 25 additions & 0 deletions src/gui/codeview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 "codeview.h"
#include "guisettings.h"

namespace Gui {

CodeView::CodeView(QWidget *parent)
: TextView(parent)
{
auto *action = new QAction(tr("Show TreeSitter Explorer"));
GuiSettings::setIcon(action, ":/gui/file-tree.png");
connect(action, &QAction::triggered, this, &CodeView::treeSitterExplorerRequested);
addAction(action);
}

} // namespace Gui
29 changes: 29 additions & 0 deletions src/gui/codeview.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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 "textview.h"
#include "treesitter/parser.h"

namespace Gui {

class CodeView : public TextView
{
Q_OBJECT

public:
explicit CodeView(QWidget *parent = nullptr);

signals:
void treeSitterExplorerRequested();
};

} // namespace Gui
1 change: 1 addition & 0 deletions src/gui/gui.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<file>gui/magnify-minus.png</file>
<file>gui/magnify-plus.png</file>
<file>gui/lightbulb.png</file>
<file>gui/file-tree.png</file>
</qresource>
<qresource prefix="/org.kde.syntax-highlighting/syntax-addons">
<file alias="rc.xml">gui/rc.xml</file>
Expand Down
Binary file added src/gui/gui/file-tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/gui/knutstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ int KnutStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const
return 1;
case PM_TabBarTabVSpace:
return 10;
case PM_SplitterWidth:
return 1;
default:
break;
}
Expand Down
12 changes: 10 additions & 2 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "mainwindow.h"
#include "codeview.h"
#include "core/codedocument.h"
#include "core/cppdocument.h"
#include "core/document.h"
Expand Down Expand Up @@ -325,6 +326,8 @@ void MainWindow::inspectTreeSitter()
m_treeSitterInspector = new TreeSitterInspector(this);
}
m_treeSitterInspector->show();
m_treeSitterInspector->raise();
m_treeSitterInspector->activateWindow();
}

MainWindow::~MainWindow() = default;
Expand Down Expand Up @@ -674,7 +677,7 @@ void MainWindow::changeTab()
ui->actionSwitchHeaderSource->setEnabled(document->type() == Core::Document::Type::Cpp);
}

static QWidget *widgetForDocument(Core::Document *document)
QWidget *MainWindow::widgetForDocument(Core::Document *document)
{
switch (document->type()) {
case Core::Document::Type::Rc: {
Expand Down Expand Up @@ -717,8 +720,13 @@ static QWidget *widgetForDocument(Core::Document *document)
tsView->setTsDocument(qobject_cast<Core::QtTsDocument *>(document));
return tsView;
}
case Core::Document::Type::Cpp: {
auto codeView = new CodeView();
codeView->setDocument(qobject_cast<Core::CodeDocument *>(document));
connect(codeView, &CodeView::treeSitterExplorerRequested, this, &MainWindow::inspectTreeSitter);
return codeView;
}
case Core::Document::Type::Json:
case Core::Document::Type::Cpp:
case Core::Document::Type::Text:
default: {
auto textView = new TextView();
Expand Down
5 changes: 5 additions & 0 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class QMenu;
class QFileSystemModel;
class QTreeView;

namespace Core {
class Document;
}

namespace Gui {

class Palette;
Expand Down Expand Up @@ -100,6 +104,7 @@ class MainWindow : public QMainWindow
void openDocument(const QModelIndex &index);
void updateRecentProjects();
void changeTab();
QWidget *widgetForDocument(Core::Document *document);
void changeCurrentDocument();
QDockWidget *createDock(QWidget *widget, Qt::DockWidgetArea area, QWidget *toolbar = nullptr);
void reloadDocuments();
Expand Down

0 comments on commit 39efa03

Please sign in to comment.