Skip to content

Commit

Permalink
feat: Saving & loading TS queries in inspector
Browse files Browse the repository at this point in the history
This should make it more convenient to design queries and store them for
later reuse.

Fix #62
  • Loading branch information
LeonMatthesKDAB authored and narnaud committed Aug 14, 2024
1 parent eb9e3cf commit 987f638
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
49 changes: 49 additions & 0 deletions src/gui/treesitterinspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/Theme>
#include <QFileDialog>
#include <QMessageBox>
#include <QPalette>
#include <QTextEdit>
Expand Down Expand Up @@ -115,6 +116,11 @@ TreeSitterInspector::TreeSitterInspector(QWidget *parent)

connect(ui->query, &QPlainTextEdit::textChanged, this, &TreeSitterInspector::changeQuery);

connect(ui->saveButton, &QPushButton::clicked, this, &TreeSitterInspector::saveToFile);
connect(ui->loadButton, &QPushButton::clicked, this, &TreeSitterInspector::loadFromFile);

connect(ui->enableUnnamed, &QCheckBox::toggled, this, &TreeSitterInspector::showUnnamedChanged);

changeCurrentDocument(Core::Project::instance()->currentDocument());
}

Expand All @@ -123,6 +129,49 @@ TreeSitterInspector::~TreeSitterInspector()
delete ui;
}

void TreeSitterInspector::saveToFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Query"), "", tr("Query Files (*.scm)"));

QFile file(fileName);
// Note: at least on Linux, the file dialog will already warn if the file exists, so no need
// to do that manually.

if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::warning(this, tr("Error"), tr("Could not open file to write!"));
return;
}

QTextStream out(&file);
out << ui->query->toPlainText() << Qt::flush;
}

void TreeSitterInspector::loadFromFile()
{
if (!ui->query->toPlainText().isEmpty()) {
auto result =
QMessageBox::warning(this, tr("Warning"), tr("Loading a query will overwrite the existing one, continue?"),
QMessageBox::Ok | QMessageBox::Cancel);
if (result != QMessageBox::Ok) {
return;
}
}

QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), tr("Query Files (*.scm)"));
if (fileName.isNull()) {
return;
}

QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("Error"), tr("Could not open file!"));
return;
}

QTextStream in(&file);
ui->query->setPlainText(in.readAll());
}

void TreeSitterInspector::changeQueryState()
{
if (m_treemodel.hasQuery()) {
Expand Down
3 changes: 3 additions & 0 deletions src/gui/treesitterinspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class TreeSitterInspector : public QDialog
~TreeSitterInspector() override;

private:
void saveToFile();
void loadFromFile();

void showUnnamedChanged();
void changeCurrentDocument(Core::Document *document);
void setDocument(Core::CodeDocument *document);
Expand Down
56 changes: 52 additions & 4 deletions src/gui/treesitterinspector.ui
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="stateLabel">
<property name="font">
<font>
<pointsize>14</pointsize>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>TreeSitter State</string>
</property>
Expand All @@ -32,10 +38,52 @@
</widget>
</item>
<item>
<widget class="QLabel" name="transformationsLabel">
<property name="text">
<string>Query</string>
</property>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="queryLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<bold>true</bold>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Query</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>Save As...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="loadButton">
<property name="text">
<string>Load...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
Expand Down

0 comments on commit 987f638

Please sign in to comment.