Skip to content

Commit dc47479

Browse files
feat: Add basic support for C# documents
Currently, this only includes basic support for the TreeSitter grammar, without support for symbols or LSP.
1 parent f53e552 commit dc47479

17 files changed

+159
-1
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232
[submodule "3rdparty/photonwidgets"]
3333
path = 3rdparty-kdab/photonwidgets
3434
url = ssh://codereview.kdab.com:29418/photonwidgets
35+
[submodule "3rdparty/tree-sitter-c-sharp"]
36+
path = 3rdparty/tree-sitter-c-sharp
37+
url = https://github.com/tree-sitter/tree-sitter-c-sharp.git

3rdparty/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,15 @@ target_link_libraries(${PROJECT_NAME} PRIVATE TreeSitter)
9999
# Always build tree-sitter with optimizations enabled. We shouldn't have to
100100
# debug it and it's performance critical.
101101
enable_optimizations(${PROJECT_NAME})
102+
103+
# TreeSitterCSharp
104+
# ##############################################################################
105+
check_submodule(tree-sitter tree-sitter-c-sharp)
106+
project(TreeSitterCSharp LANGUAGES C)
107+
108+
add_library(${PROJECT_NAME} STATIC tree-sitter-c-sharp/src/parser.c
109+
tree-sitter-c-sharp/src/scanner.c)
110+
target_link_libraries(${PROJECT_NAME} PRIVATE TreeSitter)
111+
# Always build tree-sitter with optimizations enabled. We shouldn't have to
112+
# debug it and it's performance critical.
113+
enable_optimizations(${PROJECT_NAME})

3rdparty/tree-sitter-c-sharp

Submodule tree-sitter-c-sharp added at 362a8a4

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ The script API is available in this documentation, and ranges from normal text t
1111

1212
Knut can be used via a command line interface or a user interface.
1313

14+
## Supported programming languages
15+
16+
Knut supports multiple programming languages and file types to different degrees.
17+
18+
This table provides a basic overview of the current support.
19+
Please refer to the subclasses of [Document](https://kdab.github.io/knut/API/knut/document.html) for details on the supported functions of each class.
20+
21+
<!-- NOTE: SYNC WITH docs/index.md! -->
22+
| | 🌳 TreeSitter | 🔣 Code Items | 🧑‍💻 LSP | 🖥️ File Viewer |
23+
|----------------------------|---------------|---------------|---------|---------------|
24+
| C/C++ |||| |
25+
| C# |||| |
26+
| JSON || ✔️ || |
27+
| [Qt Translate (.ts)][QtTs] || ✔️ || |
28+
| [Qt Qml][Qml] |||||
29+
| [Qt Ui files][QtUi] |||||
30+
| [Slint][Slint] |||||
31+
| [MFC rc files][MfcRc] || ✔️ || ✔️ |
32+
33+
> ✅ Fully Supported
34+
> ✔️ Partial Support
35+
> ❌ Unsupported
36+
37+
[QtTs]: https://doc.qt.io/qt-6/linguist-ts-file-format.html
38+
[QtUi]: https://doc.qt.io/qt-6/designer-ui-file-format.html
39+
[Qml]: https://doc.qt.io/qt-6/qmlreference.html
40+
[Slint]: https://slint.dev/
41+
[MfcRc]: https://learn.microsoft.com/en-us/windows/win32/menurc/about-resource-files
42+
43+
1444
## Requirements
1545

1646
Knut is using [Language Server Protocol](https://microsoft.github.io/language-server-protocol/) servers for the GUI. It is not used for the scripts, so if you are using Knut with the command line interface, you don't need it.

docs/index.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,39 @@
33
Knut is an automation tool for code transformation using scripts. The main use case is for migration, but it could be used elsewhere.
44

55
The scripts are written either in javascript or QML languages, the latter being able to display a user interface to customize some parts of the application.
6-
The script API is available in this documentation, and ranges from normal text transformations (find and replace) to getting contextual information based on the file language (using [TreeSitter](https://tree-sitter.github.io/tree-sitter/).
6+
The script API is available in this documentation, and ranges from normal text transformations (find and replace) to getting contextual information based on the file language (using [TreeSitter](https://tree-sitter.github.io/tree-sitter/)).
77

88
Knut can be used via a command line interface or a user interface.
99

10+
## Supported programming languages
11+
12+
Knut supports multiple programming languages and file types to different degrees.
13+
14+
This table provides a basic overview of the current support.
15+
Please refer to the subclasses of [Document](https://kdab.github.io/knut/API/knut/document.html) for details on the supported functions of each class.
16+
17+
<!-- NOTE: SYNC WITH README.md! -->
18+
| | 🌳 TreeSitter | 🔣 Code Items | 🧑‍💻 LSP | 🖥️ File Viewer |
19+
|----------------------------|---------------|---------------|---------|---------------|
20+
| C/C++ |||| |
21+
| C# |||| |
22+
| JSON || ✔️ || |
23+
| [Qt Translate (.ts)][QtTs] || ✔️ || |
24+
| [Qt Qml][Qml] |||||
25+
| [Qt Ui files][QtUi] |||||
26+
| [Slint][Slint] |||||
27+
| [MFC rc files][MfcRc] || ✔️ || ✔️ |
28+
29+
> ✅ Fully Supported
30+
> ✔️ Partial Support
31+
> ❌ Unsupported
32+
33+
[QtTs]: https://doc.qt.io/qt-6/linguist-ts-file-format.html
34+
[QtUi]: https://doc.qt.io/qt-6/designer-ui-file-format.html
35+
[Qml]: https://doc.qt.io/qt-6/qmlreference.html
36+
[Slint]: https://slint.dev/
37+
[MfcRc]: https://learn.microsoft.com/en-us/windows/win32/menurc/about-resource-files
38+
1039
# Licensing
1140

1241
Knut is © Klarälvdalens Datakonsult AB (KDAB) and is licensed according to the terms of [GPL 3.0](https://www.gnu.org/licenses/gpl-3.0.en.html).

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ set(PROJECT_SOURCES
2525
cppdocument.cpp
2626
cppdocument_p.h
2727
cppdocument_p.cpp
28+
csharpdocument.h
29+
csharpdocument.cpp
2830
functionsymbol.h
2931
functionsymbol.cpp
3032
dataexchange.h

src/core/codedocument.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ namespace Core {
4747
* functionality.
4848
*
4949
* This class provides the language-independent basis of integration with Tree-sitter and the LSP.
50+
*
51+
* Currently supported languages are: C/C++, Qml and C#
5052
*/
5153

5254
CodeDocument::~CodeDocument() = default;

src/core/core/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"ico": "image_type",
5555
"slint": "slint_type",
5656
"qml": "qml_type",
57+
"cs": "csharp_type",
5758
"ts": "qtts_type"
5859
},
5960
"text_editor": {

src/core/csharpdocument.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 "csharpdocument.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("CSharpDocument::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+
CSharpDocument::CSharpDocument(QObject *parent)
32+
: CodeDocument(Type::CSharp, parent)
33+
{
34+
helper()->querySymbols = queryAllSymbols;
35+
}
36+
37+
CSharpDocument::~CSharpDocument() = default;
38+
39+
} // namespace Core

src/core/csharpdocument.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 CSharpDocument : public CodeDocument
18+
{
19+
Q_OBJECT
20+
21+
public:
22+
explicit CSharpDocument(QObject *parent = nullptr);
23+
~CSharpDocument() override;
24+
};
25+
26+
}

src/core/document.h

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Document : public QObject
3636
Image,
3737
Slint,
3838
Qml,
39+
CSharp,
3940
QtTs,
4041
Json,
4142
};
@@ -105,6 +106,7 @@ NLOHMANN_JSON_SERIALIZE_ENUM(Document::Type,
105106
{Document::Type::Slint, "slint_type"},
106107
{Document::Type::QtTs, "qtts_type"},
107108
{Document::Type::Qml, "qml_type"},
109+
{Document::Type::CSharp, "csharp_type"},
108110
{Document::Type::Json, "json_type"}})
109111

110112
} // namespace Core

src/core/project.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "project.h"
1212
#include "cppdocument.h"
13+
#include "csharpdocument.h"
1314
#include "imagedocument.h"
1415
#include "jsondocument.h"
1516
#include "logger.h"
@@ -221,6 +222,8 @@ static Document *createDocument(const QString &suffix)
221222
return new QtTsDocument();
222223
case Document::Type::Qml:
223224
return new QmlDocument();
225+
case Document::Type::CSharp:
226+
return new CSharpDocument();
224227
case Document::Type::Json:
225228
return new JsonDocument();
226229
default:

src/gui/apiexecutorwidget.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "apiexecutorwidget.h"
1212
#include "core/cppdocument.h"
13+
#include "core/csharpdocument.h"
1314
#include "core/imagedocument.h"
1415
#include "core/jsondocument.h"
1516
#include "core/logger.h"
@@ -76,6 +77,8 @@ static const QMetaObject *metaObjectFromType(Core::Document::Type type)
7677
return &Core::QtTsDocument::staticMetaObject;
7778
case Core::Document::Type::Json:
7879
return &Core::JsonDocument::staticMetaObject;
80+
case Core::Document::Type::CSharp:
81+
return &Core::CSharpDocument::staticMetaObject;
7982
}
8083
Q_UNREACHABLE();
8184
}

src/gui/mainwindow.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ QWidget *MainWindow::widgetForDocument(Core::Document *document)
762762
tsView->setTsDocument(qobject_cast<Core::QtTsDocument *>(document));
763763
return tsView;
764764
}
765+
case Core::Document::Type::CSharp:
765766
case Core::Document::Type::Cpp: {
766767
auto codeView = new CodeView();
767768
codeView->setDocument(qobject_cast<Core::CodeDocument *>(document));

src/treesitter/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target_link_libraries(
3030
TreeSitter
3131
TreeSitterCpp
3232
TreeSitterQmlJs
33+
TreeSitterCSharp
3334
kdalgorithms
3435
knut-utils
3536
Qt::Core)

src/treesitter/languages.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ struct TSLanguage;
1515
extern "C" {
1616
TSLanguage *tree_sitter_cpp();
1717
TSLanguage *tree_sitter_qmljs();
18+
TSLanguage *tree_sitter_c_sharp();
1819
}

src/treesitter/parser.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ TSLanguage *Parser::getLanguage(Core::Document::Type type)
7575
return tree_sitter_qmljs();
7676
case Core::Document::Type::Cpp:
7777
return tree_sitter_cpp();
78+
case Core::Document::Type::CSharp:
79+
return tree_sitter_c_sharp();
7880
default:
7981
Q_UNREACHABLE();
8082
}

0 commit comments

Comments
 (0)