Skip to content

Commit

Permalink
feat(wip): Add support for C# documents
Browse files Browse the repository at this point in the history
Currently, this only includes basic support for the TreeSitter grammar,
without support for symbols or LSP.
  • Loading branch information
LeonMatthesKDAB committed Jan 7, 2025
1 parent f53e552 commit 18b8cd2
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@
[submodule "3rdparty/photonwidgets"]
path = 3rdparty-kdab/photonwidgets
url = ssh://codereview.kdab.com:29418/photonwidgets
[submodule "3rdparty/tree-sitter-c-sharp"]
path = 3rdparty/tree-sitter-c-sharp
url = https://github.com/tree-sitter/tree-sitter-c-sharp.git
12 changes: 12 additions & 0 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,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})

# TreeSitterCSharp
# ##############################################################################
check_submodule(tree-sitter tree-sitter-c-sharp)
project(TreeSitterCSharp LANGUAGES C)

add_library(${PROJECT_NAME} STATIC tree-sitter-c-sharp/src/parser.c
tree-sitter-c-sharp/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-c-sharp
Submodule tree-sitter-c-sharp added at 362a8a
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ set(PROJECT_SOURCES
cppdocument.cpp
cppdocument_p.h
cppdocument_p.cpp
csharpdocument.h
csharpdocument.cpp
functionsymbol.h
functionsymbol.cpp
dataexchange.h
Expand Down
1 change: 1 addition & 0 deletions src/core/core/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"ico": "image_type",
"slint": "slint_type",
"qml": "qml_type",
"cs": "csharp_type",
"ts": "qtts_type"
},
"text_editor": {
Expand Down
35 changes: 35 additions & 0 deletions src/core/csharpdocument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
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 "csharpdocument.h"

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

namespace {
QList<Core::Symbol *> queryAllSymbols(Core::CodeDocument *const document)
{
Q_UNUSED(document);
// TODO
return {};
}
}

namespace Core {

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

CSharpDocument::~CSharpDocument() = default;

} // namespace Core
26 changes: 26 additions & 0 deletions src/core/csharpdocument.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 CSharpDocument : public CodeDocument
{
Q_OBJECT

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

}
2 changes: 2 additions & 0 deletions src/core/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Document : public QObject
Image,
Slint,
Qml,
CSharp,
QtTs,
Json,
};
Expand Down Expand Up @@ -105,6 +106,7 @@ NLOHMANN_JSON_SERIALIZE_ENUM(Document::Type,
{Document::Type::Slint, "slint_type"},
{Document::Type::QtTs, "qtts_type"},
{Document::Type::Qml, "qml_type"},
{Document::Type::CSharp, "csharp_type"},
{Document::Type::Json, "json_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 @@ -10,6 +10,7 @@

#include "project.h"
#include "cppdocument.h"
#include "csharpdocument.h"
#include "imagedocument.h"
#include "jsondocument.h"
#include "logger.h"
Expand Down Expand Up @@ -221,6 +222,8 @@ static Document *createDocument(const QString &suffix)
return new QtTsDocument();
case Document::Type::Qml:
return new QmlDocument();
case Document::Type::CSharp:
return new CSharpDocument();
case Document::Type::Json:
return new JsonDocument();
default:
Expand Down
3 changes: 3 additions & 0 deletions src/gui/apiexecutorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "apiexecutorwidget.h"
#include "core/cppdocument.h"
#include "core/csharpdocument.h"
#include "core/imagedocument.h"
#include "core/jsondocument.h"
#include "core/logger.h"
Expand Down Expand Up @@ -76,6 +77,8 @@ static const QMetaObject *metaObjectFromType(Core::Document::Type type)
return &Core::QtTsDocument::staticMetaObject;
case Core::Document::Type::Json:
return &Core::JsonDocument::staticMetaObject;
case Core::Document::Type::CSharp:
return &Core::CSharpDocument::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 @@ -762,6 +762,7 @@ QWidget *MainWindow::widgetForDocument(Core::Document *document)
tsView->setTsDocument(qobject_cast<Core::QtTsDocument *>(document));
return tsView;
}
case Core::Document::Type::CSharp:
case Core::Document::Type::Cpp: {
auto codeView = new CodeView();
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 @@ -30,6 +30,7 @@ target_link_libraries(
TreeSitter
TreeSitterCpp
TreeSitterQmlJs
TreeSitterCSharp
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 @@ -15,4 +15,5 @@ struct TSLanguage;
extern "C" {
TSLanguage *tree_sitter_cpp();
TSLanguage *tree_sitter_qmljs();
TSLanguage *tree_sitter_c_sharp();
}
2 changes: 2 additions & 0 deletions src/treesitter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ TSLanguage *Parser::getLanguage(Core::Document::Type type)
return tree_sitter_qmljs();
case Core::Document::Type::Cpp:
return tree_sitter_cpp();
case Core::Document::Type::CSharp:
return tree_sitter_c_sharp();
default:
Q_UNREACHABLE();
}
Expand Down

0 comments on commit 18b8cd2

Please sign in to comment.