From 858c7628118c06653b84feb85cca72ca67aa1239 Mon Sep 17 00:00:00 2001 From: Abhishek Sudhakaran Date: Wed, 26 Jun 2024 15:18:55 +0530 Subject: [PATCH] feat: introduce Qml to parser add Qml parser to all places except tests. --- src/core/codedocument_p.cpp | 3 +-- src/core/qmldocument.cpp | 3 ++- src/core/qmldocument.h | 5 +++-- src/gui/mainwindow.cpp | 2 +- src/gui/treesitterinspector.cpp | 11 +++++++---- src/treesitter/parser.cpp | 12 ++++++++++++ src/treesitter/parser.h | 3 +++ 7 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/core/codedocument_p.cpp b/src/core/codedocument_p.cpp index a5608ea5..8e8a2141 100644 --- a/src/core/codedocument_p.cpp +++ b/src/core/codedocument_p.cpp @@ -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: diff --git a/src/core/qmldocument.cpp b/src/core/qmldocument.cpp index 94df0941..a0ca4e7b 100644 --- a/src/core/qmldocument.cpp +++ b/src/core/qmldocument.cpp @@ -13,8 +13,9 @@ namespace Core { QmlDocument::QmlDocument(QObject *parent) - : TextDocument(Type::Qml, parent) + : CodeDocument(Type::Qml, parent) { } +QmlDocument::~QmlDocument() = default; } diff --git a/src/core/qmldocument.h b/src/core/qmldocument.h index ec4b647c..1049ae2c 100644 --- a/src/core/qmldocument.h +++ b/src/core/qmldocument.h @@ -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; }; } diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index bd3cf302..18d2f478 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -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(document); ui->actionSwitchHeaderSource->setEnabled(cppEnabled); diff --git a/src/gui/treesitterinspector.cpp b/src/gui/treesitterinspector.cpp index 89ffb3fc..944cc479 100644 --- a/src/gui/treesitterinspector.cpp +++ b/src/gui/treesitterinspector.cpp @@ -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) { @@ -149,7 +149,8 @@ void TreeSitterInspector::changeQuery() } try { - auto query = std::make_shared(tree_sitter_cpp(), ui->query->toPlainText()); + auto lang = treesitter::Parser::getLanguage(m_document->type()); + auto query = std::make_shared(lang, ui->query->toPlainText()); m_treemodel.setQuery(query, makePredicates()); m_errorHighlighter->setUtf8Position(-1); @@ -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); @@ -281,8 +283,9 @@ void TreeSitterInspector::prepareTransformation( } try { - auto query = std::make_shared(tree_sitter_cpp(), m_queryText); - treesitter::Parser parser(tree_sitter_cpp()); + auto lang = treesitter::Parser::getLanguage(m_document->type()); + auto query = std::make_shared(lang, m_queryText); + treesitter::Parser parser(lang); treesitter::Transformation transformation(m_document->text(), std::move(parser), query, ui->target->toPlainText()); diff --git a/src/treesitter/parser.cpp b/src/treesitter/parser.cpp index 4541a938..279ae912 100644 --- a/src/treesitter/parser.cpp +++ b/src/treesitter/parser.cpp @@ -10,6 +10,7 @@ #include "parser.h" #include "tree.h" +#include "treesitter/languages.h" #include #include @@ -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(); + } +} } diff --git a/src/treesitter/parser.h b/src/treesitter/parser.h index 85514e15..7ef69fe9 100644 --- a/src/treesitter/parser.h +++ b/src/treesitter/parser.h @@ -10,6 +10,7 @@ #pragma once +#include "core/document.h" #include struct TSParser; @@ -38,6 +39,8 @@ class Parser const TSLanguage *language() const; + static TSLanguage *getLanguage(Core::Document::Type type); + private: TSParser *m_parser; };