Skip to content

Commit ef221e0

Browse files
Montelnarnaud
authored andcommitted
feat: Add basic support for Rust documents
Currently, this only includes basic support for the TreeSitter grammar, without support for symbols or LSP.
1 parent e8318da commit ef221e0

18 files changed

+104
-3
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@
3535
[submodule "3rdparty/tree-sitter-c-sharp"]
3636
path = 3rdparty/tree-sitter-c-sharp
3737
url = https://github.com/tree-sitter/tree-sitter-c-sharp.git
38+
[submodule "3rdparty/tree-sitter-rust"]
39+
path = 3rdparty/tree-sitter-rust
40+
url = https://github.com/tree-sitter/tree-sitter-rust.git

3rdparty/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,15 @@ target_link_libraries(${PROJECT_NAME} PRIVATE TreeSitter)
111111
# Always build tree-sitter with optimizations enabled. We shouldn't have to
112112
# debug it and it's performance critical.
113113
enable_optimizations(${PROJECT_NAME})
114+
115+
# TreeSitterRust
116+
# ##############################################################################
117+
check_submodule(tree-sitter tree-sitter-rust)
118+
project(TreeSitterRust LANGUAGES C)
119+
120+
add_library(${PROJECT_NAME} STATIC tree-sitter-rust/src/parser.c
121+
tree-sitter-rust/src/scanner.c)
122+
target_link_libraries(${PROJECT_NAME} PRIVATE TreeSitter)
123+
# Always build tree-sitter with optimizations enabled. We shouldn't have to
124+
# debug it and it's performance critical.
125+
enable_optimizations(${PROJECT_NAME})

3rdparty/tree-sitter-rust

Submodule tree-sitter-rust added at 1f63b33

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Please refer to the subclasses of [Document](https://kdab.github.io/knut/API/knu
2323
|----------------------------|---------------|---------------|---------|---------------|
2424
| C/C++ |||| |
2525
| C# |||| |
26+
| Rust |||| |
2627
| JSON || ✔️ || |
2728
| [Qt Translate (.ts)][QtTs] || ✔️ || |
2829
| [Qt Qml][Qml] |||||

docs/API/knut/codedocument.md

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ functionality.
3939

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

42+
Currently supported languages are: C/C++, Rust, Qml and C#
43+
4244
## Method Documentation
4345

4446
#### <a name="findSymbol"></a>[Symbol](../knut/symbol.md) **findSymbol**(string name, int options = TextDocument.NoFindFlags)

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Please refer to the subclasses of [Document](https://kdab.github.io/knut/API/knu
1919
|----------------------------|---------------|---------------|---------|---------------|
2020
| C/C++ |||| |
2121
| C# |||| |
22+
| Rust |||| |
2223
| JSON || ✔️ || |
2324
| [Qt Translate (.ts)][QtTs] || ✔️ || |
2425
| [Qt Qml][Qml] |||||

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ set(PROJECT_SOURCES
2727
cppdocument_p.cpp
2828
csharpdocument.h
2929
csharpdocument.cpp
30+
rustdocument.h
31+
rustdocument.cpp
3032
functionsymbol.h
3133
functionsymbol.cpp
3234
dataexchange.h

src/core/codedocument.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace Core {
4747
*
4848
* This class provides the language-independent basis of integration with Tree-sitter and the LSP.
4949
*
50-
* Currently supported languages are: C/C++, Qml and C#
50+
* Currently supported languages are: C/C++, Rust, Qml and C#
5151
*/
5252

5353
CodeDocument::~CodeDocument() = default;

src/core/core/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"slint": "slint_type",
5656
"qml": "qml_type",
5757
"cs": "csharp_type",
58-
"ts": "qtts_type"
58+
"ts": "qtts_type",
59+
"rs": "rust_type"
5960
},
6061
"text_editor": {
6162
"tab": {

src/core/document.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Document : public QObject
3939
CSharp,
4040
QtTs,
4141
Json,
42+
Rust,
4243
};
4344
Q_ENUM(Type)
4445

@@ -107,7 +108,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(Document::Type,
107108
{Document::Type::QtTs, "qtts_type"},
108109
{Document::Type::Qml, "qml_type"},
109110
{Document::Type::CSharp, "csharp_type"},
110-
{Document::Type::Json, "json_type"}})
111+
{Document::Type::Json, "json_type"},
112+
{Document::Type::Rust, "rust_type"}})
111113

112114
} // namespace Core
113115

src/core/project.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qttsdocument.h"
2121
#include "qtuidocument.h"
2222
#include "rcdocument.h"
23+
#include "rustdocument.h"
2324
#include "settings.h"
2425
#include "slintdocument.h"
2526
#include "textdocument.h"
@@ -226,6 +227,8 @@ static Document *createDocument(const QString &suffix)
226227
return new CSharpDocument();
227228
case Document::Type::Json:
228229
return new JsonDocument();
230+
case Document::Type::Rust:
231+
return new RustDocument();
229232
default:
230233
return new TextDocument();
231234
}

src/core/rustdocument.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of Knut.
3+
4+
SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
5+
6+
SPDX-License-Identifier: GPL-3.0-only
7+
8+
Contact KDAB at <[email protected]> for commercial licensing options.
9+
*/
10+
11+
#include "rustdocument.h"
12+
13+
#include "codedocument_p.h"
14+
#include "symbol.h"
15+
16+
namespace {
17+
18+
QList<Core::Symbol *> queryAllSymbols(Core::CodeDocument *const document)
19+
{
20+
Q_UNUSED(document);
21+
// TODO
22+
spdlog::warn("RustDocument::symbols: Symbols are not (yet) supported for C# code. "
23+
"Some functionality may not work as expected!");
24+
return {};
25+
}
26+
27+
} // anonymous namespace
28+
29+
namespace Core {
30+
31+
RustDocument::RustDocument(QObject *parent)
32+
: CodeDocument(Type::CSharp, parent)
33+
{
34+
helper()->querySymbols = queryAllSymbols;
35+
}
36+
37+
RustDocument::~RustDocument() = default;
38+
39+
} // namespace Core

src/core/rustdocument.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
This file is part of Knut.
3+
4+
SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
5+
6+
SPDX-License-Identifier: GPL-3.0-only
7+
8+
Contact KDAB at <[email protected]> for commercial licensing options.
9+
*/
10+
11+
#pragma once
12+
13+
#include "codedocument.h"
14+
15+
namespace Core {
16+
17+
class RustDocument : public CodeDocument
18+
{
19+
Q_OBJECT
20+
21+
public:
22+
explicit RustDocument(QObject *parent = nullptr);
23+
~RustDocument() override;
24+
};
25+
26+
}

src/gui/apiexecutorwidget.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "core/qttsdocument.h"
2020
#include "core/qtuidocument.h"
2121
#include "core/rcdocument.h"
22+
#include "core/rustdocument.h"
2223
#include "core/slintdocument.h"
2324
#include "guisettings.h"
2425
#include "ui_apiexecutorwidget.h"
@@ -79,6 +80,8 @@ static const QMetaObject *metaObjectFromType(Core::Document::Type type)
7980
return &Core::JsonDocument::staticMetaObject;
8081
case Core::Document::Type::CSharp:
8182
return &Core::CSharpDocument::staticMetaObject;
83+
case Core::Document::Type::Rust:
84+
return &Core::RustDocument::staticMetaObject;
8285
}
8386
Q_UNREACHABLE();
8487
}

src/gui/mainwindow.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ QWidget *MainWindow::widgetForDocument(Core::Document *document)
763763
return tsView;
764764
}
765765
case Core::Document::Type::CSharp:
766+
case Core::Document::Type::Rust:
766767
case Core::Document::Type::Cpp: {
767768
auto codeView = new CodeView(this);
768769
codeView->setDocument(qobject_cast<Core::CodeDocument *>(document));

src/treesitter/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ target_link_libraries(
3131
TreeSitterCpp
3232
TreeSitterQmlJs
3333
TreeSitterCSharp
34+
TreeSitterRust
3435
kdalgorithms
3536
knut-utils
3637
Qt::Core)

src/treesitter/languages.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ extern "C" {
1616
TSLanguage *tree_sitter_cpp();
1717
TSLanguage *tree_sitter_qmljs();
1818
TSLanguage *tree_sitter_c_sharp();
19+
TSLanguage *tree_sitter_rust();
1920
}

src/treesitter/parser.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ TSLanguage *Parser::getLanguage(Core::Document::Type type)
7777
return tree_sitter_cpp();
7878
case Core::Document::Type::CSharp:
7979
return tree_sitter_c_sharp();
80+
case Core::Document::Type::Rust:
81+
return tree_sitter_rust();
8082
default:
8183
Q_UNREACHABLE();
8284
}

0 commit comments

Comments
 (0)