Skip to content

Commit

Permalink
feat: introduce Qml to parser
Browse files Browse the repository at this point in the history
add Qml parser to all places except tests.
  • Loading branch information
itzurabhi authored and narnaud committed Jun 27, 2024
1 parent 9c48991 commit 87ffede
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/core/codedocument_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ void TreeSitterHelper::clear()
treesitter::Parser &TreeSitterHelper::parser()
{
if (!m_parser) {
// TODO: Make language configurable
m_parser = treesitter::Parser(tree_sitter_cpp());
m_parser = treesitter::Parser(treesitter::Parser::getLanguage(m_document->type()));
}

// Regarding const-ness:
Expand Down
3 changes: 2 additions & 1 deletion src/core/qmldocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
namespace Core {

QmlDocument::QmlDocument(QObject *parent)
: TextDocument(Type::Qml, parent)
: CodeDocument(Type::Qml, parent)
{
}

QmlDocument::~QmlDocument() = default;
}
5 changes: 3 additions & 2 deletions src/core/qmldocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@

#pragma once

#include "textdocument.h"
#include "codedocument.h"

namespace Core {

class QmlDocument : public TextDocument
class QmlDocument : public CodeDocument
{
Q_OBJECT

public:
explicit QmlDocument(QObject *parent = nullptr);
~QmlDocument() override;
};

}
2 changes: 1 addition & 1 deletion src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ void MainWindow::updateActions()
const bool lspEnabled = codeDocument && codeDocument->hasLspClient();
ui->actionFollowSymbol->setEnabled(lspEnabled);
ui->actionSwitchDeclDef->setEnabled(lspEnabled);
ui->actionTreeSitterInspector->setEnabled(lspEnabled);
ui->actionTreeSitterInspector->setEnabled(codeDocument != nullptr);

const bool cppEnabled = codeDocument && qobject_cast<Core::CppDocument *>(document);
ui->actionSwitchHeaderSource->setEnabled(cppEnabled);
Expand Down
11 changes: 7 additions & 4 deletions src/gui/treesitterinspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void QueryErrorHighlighter::setUtf8Position(int position)
TreeSitterInspector::TreeSitterInspector(QWidget *parent)
: QDialog(parent)
, ui(new Ui::TreeSitterInspector)
, m_parser(tree_sitter_cpp())
, m_parser(nullptr)
, m_errorHighlighter(nullptr)
, m_document(nullptr)
{
Expand Down Expand Up @@ -149,7 +149,8 @@ void TreeSitterInspector::changeQuery()
}

try {
auto query = std::make_shared<treesitter::Query>(tree_sitter_cpp(), ui->query->toPlainText());
auto lang = treesitter::Parser::getLanguage(m_document->type());
auto query = std::make_shared<treesitter::Query>(lang, ui->query->toPlainText());
m_treemodel.setQuery(query, makePredicates());
m_errorHighlighter->setUtf8Position(-1);

Expand Down Expand Up @@ -211,6 +212,7 @@ void TreeSitterInspector::setDocument(Core::CodeDocument *document)
}

m_document = document;
m_parser = treesitter::Parser::getLanguage(document->type());
if (m_document) {
connect(m_document, &Core::CodeDocument::textChanged, this, &TreeSitterInspector::changeText);
connect(m_document, &Core::CodeDocument::positionChanged, this, &TreeSitterInspector::changeCursor);
Expand Down Expand Up @@ -281,8 +283,9 @@ void TreeSitterInspector::prepareTransformation(
}

try {
auto query = std::make_shared<treesitter::Query>(tree_sitter_cpp(), m_queryText);
treesitter::Parser parser(tree_sitter_cpp());
auto lang = treesitter::Parser::getLanguage(m_document->type());
auto query = std::make_shared<treesitter::Query>(lang, m_queryText);
treesitter::Parser parser(lang);

treesitter::Transformation transformation(m_document->text(), std::move(parser), query,
ui->target->toPlainText());
Expand Down
12 changes: 12 additions & 0 deletions src/treesitter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "parser.h"
#include "tree.h"
#include "treesitter/languages.h"

#include <tree_sitter/api.h>
#include <utility>
Expand Down Expand Up @@ -62,4 +63,15 @@ const TSLanguage *Parser::language() const
return ts_parser_language(m_parser);
}

TSLanguage *Parser::getLanguage(Core::Document::Type type)
{
switch (type) {
case Core::Document::Type::Qml:
return tree_sitter_qmljs();
case Core::Document::Type::Cpp:
return tree_sitter_cpp();
default:
Q_UNREACHABLE();
}
}
}
3 changes: 3 additions & 0 deletions src/treesitter/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#pragma once

#include "core/document.h"
#include <QString>

struct TSParser;
Expand Down Expand Up @@ -38,6 +39,8 @@ class Parser

const TSLanguage *language() const;

static TSLanguage *getLanguage(Core::Document::Type type);

private:
TSParser *m_parser;
};
Expand Down

0 comments on commit 87ffede

Please sign in to comment.