Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add basic support for Rust documents #237

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@
[submodule "3rdparty/tree-sitter-c-sharp"]
path = 3rdparty/tree-sitter-c-sharp
url = https://github.com/tree-sitter/tree-sitter-c-sharp.git
[submodule "3rdparty/tree-sitter-rust"]
path = 3rdparty/tree-sitter-rust
url = https://github.com/tree-sitter/tree-sitter-rust.git
12 changes: 12 additions & 0 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ target_link_libraries(${PROJECT_NAME} PRIVATE TreeSitter)
# Always build tree-sitter with optimizations enabled. We shouldn't have to
# debug it and it's performance critical.
enable_optimizations(${PROJECT_NAME})

# TreeSitterRust
# ##############################################################################
check_submodule(tree-sitter tree-sitter-rust)
project(TreeSitterRust LANGUAGES C)

add_library(${PROJECT_NAME} STATIC tree-sitter-rust/src/parser.c
tree-sitter-rust/src/scanner.c)
target_link_libraries(${PROJECT_NAME} PRIVATE TreeSitter)
# Always build tree-sitter with optimizations enabled. We shouldn't have to
# debug it and it's performance critical.
enable_optimizations(${PROJECT_NAME})
1 change: 1 addition & 0 deletions 3rdparty/tree-sitter-rust
Submodule tree-sitter-rust added at 1f63b3
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Please refer to the subclasses of [Document](https://kdab.github.io/knut/API/knu
|----------------------------|---------------|---------------|---------|---------------|
| C/C++ | ✅ | ✅ | ✅ | |
| C# | ✅ | ❌ | ❌ | |
| Rust | ✅ | ❌ | ❌ | |
| JSON | ❌ | ✔️ | ❌ | |
| [Qt Translate (.ts)][QtTs] | ❌ | ✔️ | ❌ | |
| [Qt Qml][Qml] | ✅ | ✅ | ❌ | ✅ |
Expand Down
2 changes: 2 additions & 0 deletions docs/API/knut/codedocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ functionality.

This class provides the language-independent basis of integration with Tree-sitter and the LSP.

Currently supported languages are: C/C++, Rust, Qml and C#

## Method Documentation

#### <a name="findSymbol"></a>[Symbol](../knut/symbol.md) **findSymbol**(string name, int options = TextDocument.NoFindFlags)
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Please refer to the subclasses of [Document](https://kdab.github.io/knut/API/knu
|----------------------------|---------------|---------------|---------|---------------|
| C/C++ | ✅ | ✅ | ✅ | |
| C# | ✅ | ❌ | ❌ | |
| Rust | ✅ | ❌ | ❌ | |
| JSON | ❌ | ✔️ | ❌ | |
| [Qt Translate (.ts)][QtTs] | ❌ | ✔️ | ❌ | |
| [Qt Qml][Qml] | ✅ | ✅ | ❌ | ✅ |
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ set(PROJECT_SOURCES
cppdocument_p.cpp
csharpdocument.h
csharpdocument.cpp
rustdocument.h
rustdocument.cpp
functionsymbol.h
functionsymbol.cpp
dataexchange.h
Expand Down
2 changes: 1 addition & 1 deletion src/core/codedocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace Core {
*
* This class provides the language-independent basis of integration with Tree-sitter and the LSP.
*
* Currently supported languages are: C/C++, Qml and C#
* Currently supported languages are: C/C++, Rust, Qml and C#
*/

CodeDocument::~CodeDocument() = default;
Expand Down
3 changes: 2 additions & 1 deletion src/core/core/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"slint": "slint_type",
"qml": "qml_type",
"cs": "csharp_type",
"ts": "qtts_type"
"ts": "qtts_type",
"rs": "rust_type"
},
"text_editor": {
"tab": {
Expand Down
4 changes: 3 additions & 1 deletion src/core/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Document : public QObject
CSharp,
QtTs,
Json,
Rust,
};
Q_ENUM(Type)

Expand Down Expand Up @@ -107,7 +108,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(Document::Type,
{Document::Type::QtTs, "qtts_type"},
{Document::Type::Qml, "qml_type"},
{Document::Type::CSharp, "csharp_type"},
{Document::Type::Json, "json_type"}})
{Document::Type::Json, "json_type"},
{Document::Type::Rust, "rust_type"}})

} // namespace Core

Expand Down
3 changes: 3 additions & 0 deletions src/core/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qttsdocument.h"
#include "qtuidocument.h"
#include "rcdocument.h"
#include "rustdocument.h"
#include "settings.h"
#include "slintdocument.h"
#include "textdocument.h"
Expand Down Expand Up @@ -226,6 +227,8 @@ static Document *createDocument(const QString &suffix)
return new CSharpDocument();
case Document::Type::Json:
return new JsonDocument();
case Document::Type::Rust:
return new RustDocument();
default:
return new TextDocument();
}
Expand Down
39 changes: 39 additions & 0 deletions src/core/rustdocument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This file is part of Knut.

SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>

SPDX-License-Identifier: GPL-3.0-only

Contact KDAB at <[email protected]> for commercial licensing options.
*/

#include "rustdocument.h"

#include "codedocument_p.h"
#include "symbol.h"

namespace {

QList<Core::Symbol *> queryAllSymbols(Core::CodeDocument *const document)
{
Q_UNUSED(document);
// TODO
spdlog::warn("RustDocument::symbols: Symbols are not (yet) supported for C# code. "
"Some functionality may not work as expected!");
return {};
}

} // anonymous namespace

namespace Core {

RustDocument::RustDocument(QObject *parent)
: CodeDocument(Type::CSharp, parent)
{
helper()->querySymbols = queryAllSymbols;
}

RustDocument::~RustDocument() = default;

} // namespace Core
26 changes: 26 additions & 0 deletions src/core/rustdocument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of Knut.

SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>

SPDX-License-Identifier: GPL-3.0-only

Contact KDAB at <[email protected]> for commercial licensing options.
*/

#pragma once

#include "codedocument.h"

namespace Core {

class RustDocument : public CodeDocument
{
Q_OBJECT

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

}
3 changes: 3 additions & 0 deletions src/gui/apiexecutorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "core/qttsdocument.h"
#include "core/qtuidocument.h"
#include "core/rcdocument.h"
#include "core/rustdocument.h"
#include "core/slintdocument.h"
#include "guisettings.h"
#include "ui_apiexecutorwidget.h"
Expand Down Expand Up @@ -79,6 +80,8 @@ static const QMetaObject *metaObjectFromType(Core::Document::Type type)
return &Core::JsonDocument::staticMetaObject;
case Core::Document::Type::CSharp:
return &Core::CSharpDocument::staticMetaObject;
case Core::Document::Type::Rust:
return &Core::RustDocument::staticMetaObject;
}
Q_UNREACHABLE();
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ QWidget *MainWindow::widgetForDocument(Core::Document *document)
return tsView;
}
case Core::Document::Type::CSharp:
case Core::Document::Type::Rust:
case Core::Document::Type::Cpp: {
auto codeView = new CodeView(this);
codeView->setDocument(qobject_cast<Core::CodeDocument *>(document));
Expand Down
1 change: 1 addition & 0 deletions src/treesitter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_link_libraries(
TreeSitterCpp
TreeSitterQmlJs
TreeSitterCSharp
TreeSitterRust
kdalgorithms
knut-utils
Qt::Core)
Expand Down
1 change: 1 addition & 0 deletions src/treesitter/languages.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ extern "C" {
TSLanguage *tree_sitter_cpp();
TSLanguage *tree_sitter_qmljs();
TSLanguage *tree_sitter_c_sharp();
TSLanguage *tree_sitter_rust();
}
2 changes: 2 additions & 0 deletions src/treesitter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ TSLanguage *Parser::getLanguage(Core::Document::Type type)
return tree_sitter_cpp();
case Core::Document::Type::CSharp:
return tree_sitter_c_sharp();
case Core::Document::Type::Rust:
return tree_sitter_rust();
default:
Q_UNREACHABLE();
}
Expand Down