Skip to content

Commit

Permalink
feat: introduce Qml to parser
Browse files Browse the repository at this point in the history
add the Qml tree sitter language support to all places except test files.
  • Loading branch information
itzurabhi committed Jun 26, 2024
1 parent 9c48991 commit ccf4de9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ add_custom_command(
${CMAKE_BINARY_DIR}/compile_commands.json compile_commands.json
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/test_data/projects/cpp-project)

add_custom_command(
OUTPUT
${CMAKE_CURRENT_LIST_DIR}/test_data/projects/qml-project/compile_commands.json
COMMENT "Creating compile_commands.json file for qml-project"
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_BINARY_DIR}/compile_commands.json compile_commands.json
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/test_data/projects/qml-project)

# Post-initialization
# ##############################################################################
# Need to be done *after* compiling 3rd parties
Expand Down
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::getTSLanguageForDocumentType(m_document->type()));
}

// Regarding const-ness:
Expand Down
4 changes: 3 additions & 1 deletion src/core/qmldocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
*/

#include "qmldocument.h"
#include "astnode.h"

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;
};

}
3 changes: 2 additions & 1 deletion src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,11 @@ void MainWindow::updateActions()
ui->actionExecuteAPI->setEnabled(textDocument != nullptr);

auto *codeDocument = qobject_cast<Core::CodeDocument *>(document);
const bool qmlEnabled = codeDocument && qobject_cast<Core::QmlDocument *>(document);
const bool lspEnabled = codeDocument && codeDocument->hasLspClient();
ui->actionFollowSymbol->setEnabled(lspEnabled);
ui->actionSwitchDeclDef->setEnabled(lspEnabled);
ui->actionTreeSitterInspector->setEnabled(lspEnabled);
ui->actionTreeSitterInspector->setEnabled(lspEnabled || qmlEnabled);

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::getTSLanguageForDocumentType(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::getTSLanguageForDocumentType(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::getTSLanguageForDocumentType(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
15 changes: 15 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,18 @@ const TSLanguage *Parser::language() const
return ts_parser_language(m_parser);
}

TSLanguage *Parser::getTSLanguageForDocumentType(Core::Document::Type type)
{
switch (type) {
case Core::Document::Type::Qml:
return tree_sitter_qmljs();
// FIXME: make sane default for other documents.
case Core::Document::Type::Cpp:
default:
return tree_sitter_cpp();
break;
}

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 *getTSLanguageForDocumentType(Core::Document::Type type);

private:
TSParser *m_parser;
};
Expand Down
1 change: 1 addition & 0 deletions test_data/projects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#

add_subdirectory(cpp-project)
add_subdirectory(qml-project)

0 comments on commit ccf4de9

Please sign in to comment.