From fa10f7dc23f8b93c0f9ef3fb5477871a20aaa974 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 1 Jun 2022 17:36:44 -0700 Subject: Add language server daemon. (#2251) * Add language server daemon. * Fix. Co-authored-by: Yong He Co-authored-by: jsmall-nvidia Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> --- build/visual-studio/slangd/slangd.vcxproj | 286 ++++++++++++++++++++++ build/visual-studio/slangd/slangd.vcxproj.filters | 27 ++ premake5.lua | 5 + slang.sln | 15 ++ tools/slangd/language-server-protocol.cpp | 191 +++++++++++++++ tools/slangd/language-server-protocol.h | 148 +++++++++++ tools/slangd/language-server.cpp | 184 ++++++++++++++ tools/slangd/language-server.h | 35 +++ 8 files changed, 891 insertions(+) create mode 100644 build/visual-studio/slangd/slangd.vcxproj create mode 100644 build/visual-studio/slangd/slangd.vcxproj.filters create mode 100644 tools/slangd/language-server-protocol.cpp create mode 100644 tools/slangd/language-server-protocol.h create mode 100644 tools/slangd/language-server.cpp create mode 100644 tools/slangd/language-server.h diff --git a/build/visual-studio/slangd/slangd.vcxproj b/build/visual-studio/slangd/slangd.vcxproj new file mode 100644 index 000000000..d52a5b466 --- /dev/null +++ b/build/visual-studio/slangd/slangd.vcxproj @@ -0,0 +1,286 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Debug + ARM + + + Debug aarch64 + Win32 + + + Debug aarch64 + x64 + + + Debug aarch64 + ARM + + + Release + Win32 + + + Release + x64 + + + Release + ARM + + + Release aarch64 + Win32 + + + Release aarch64 + x64 + + + Release aarch64 + ARM + + + + {B2D63B45-92B0-40F7-B242-CCA4DFD64341} + true + Win32Proj + slangd + + + + Application + true + Unicode + v142 + + + Application + true + Unicode + v142 + + + Application + true + Unicode + v142 + true + + + Application + false + Unicode + v142 + + + Application + false + Unicode + v142 + + + Application + false + Unicode + v142 + true + + + + + + + + + + + + + + + + + + + + + + + + + true + ..\..\..\bin\windows-x86\debug\ + ..\..\..\intermediate\windows-x86\debug\slangd\ + slangd + .exe + + + true + ..\..\..\bin\windows-x64\debug\ + ..\..\..\intermediate\windows-x64\debug\slangd\ + slangd + .exe + + + true + ..\..\..\bin\windows-aarch64\debug\ + ..\..\..\intermediate\windows-aarch64\debug\slangd\ + slangd + .exe + + + false + ..\..\..\bin\windows-x86\release\ + ..\..\..\intermediate\windows-x86\release\slangd\ + slangd + .exe + + + false + ..\..\..\bin\windows-x64\release\ + ..\..\..\intermediate\windows-x64\release\slangd\ + slangd + .exe + + + false + ..\..\..\bin\windows-aarch64\release\ + ..\..\..\intermediate\windows-aarch64\release\slangd\ + slangd + .exe + + + + NotUsing + Level3 + _DEBUG;%(PreprocessorDefinitions) + ..\..\..;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + MultiThreadedDebug + + + Console + true + + + + + NotUsing + Level3 + _DEBUG;%(PreprocessorDefinitions) + ..\..\..;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + MultiThreadedDebug + + + Console + true + + + + + NotUsing + Level3 + _DEBUG;%(PreprocessorDefinitions) + ..\..\..;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + MultiThreadedDebug + + + Console + true + + + + + NotUsing + Level3 + NDEBUG;%(PreprocessorDefinitions) + ..\..\..;%(AdditionalIncludeDirectories) + Full + true + true + false + true + MultiThreaded + + + Console + true + true + + + + + NotUsing + Level3 + NDEBUG;%(PreprocessorDefinitions) + ..\..\..;%(AdditionalIncludeDirectories) + Full + true + true + false + true + MultiThreaded + + + Console + true + true + + + + + NotUsing + Level3 + NDEBUG;%(PreprocessorDefinitions) + ..\..\..;%(AdditionalIncludeDirectories) + Full + true + true + false + true + MultiThreaded + + + Console + true + true + + + + + + + + + + + + + {12C1E89D-F5D0-41D3-8E8D-FB3F358F8126} + + + {F9BE7957-8399-899E-0C49-E714FDDD4B65} + + + {DB00DA62-0533-4AFD-B59F-A67D5B3A0808} + + + + + + \ No newline at end of file diff --git a/build/visual-studio/slangd/slangd.vcxproj.filters b/build/visual-studio/slangd/slangd.vcxproj.filters new file mode 100644 index 000000000..b21b4cc7f --- /dev/null +++ b/build/visual-studio/slangd/slangd.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {21EB8090-0D4E-1035-B6D3-48EBA215DCB7} + + + {E9C7FDCE-D52A-8D73-7EB0-C5296AF258F6} + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/premake5.lua b/premake5.lua index 9b985e29d..a1661e611 100644 --- a/premake5.lua +++ b/premake5.lua @@ -784,6 +784,11 @@ standardProject("slang-rt", "source/slang-rt") includedirs { "." } links { "compiler-core", "core", "slang" } + +tool "slangd" + uuid "B2D63B45-92B0-40F7-B242-CCA4DFD64341" + includedirs { "." } + links { "compiler-core", "core", "slang" } -- -- `slang-generate` is a tool we use for source code generation on diff --git a/slang.sln b/slang.sln index f9584f25d..c00df8e81 100644 --- a/slang.sln +++ b/slang.sln @@ -17,6 +17,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "slang-embed", "build\visual EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "slang-generate", "build\visual-studio\slang-generate\slang-generate.vcxproj", "{66174227-8541-41FC-A6DF-4764FC66F78E}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "slangd", "build\visual-studio\slangd\slangd.vcxproj", "{B2D63B45-92B0-40F7-B242-CCA4DFD64341}" +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-process", "build\visual-studio\test-process\test-process.vcxproj", "{BE412850-4BB9-429A-877C-BFBC4B34186C}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-server", "build\visual-studio\test-server\test-server.vcxproj", "{23149706-C12F-4329-B6AA-8266407C32D3}" @@ -173,6 +175,18 @@ Global {66174227-8541-41FC-A6DF-4764FC66F78E}.Release|Win32.Build.0 = Release|Win32 {66174227-8541-41FC-A6DF-4764FC66F78E}.Release|x64.ActiveCfg = Release|x64 {66174227-8541-41FC-A6DF-4764FC66F78E}.Release|x64.Build.0 = Release|x64 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Debug|aarch64.ActiveCfg = Debug aarch64|ARM + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Debug|aarch64.Build.0 = Debug aarch64|ARM + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Debug|Win32.ActiveCfg = Debug|Win32 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Debug|Win32.Build.0 = Debug|Win32 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Debug|x64.ActiveCfg = Debug|x64 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Debug|x64.Build.0 = Debug|x64 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Release|aarch64.ActiveCfg = Release aarch64|ARM + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Release|aarch64.Build.0 = Release aarch64|ARM + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Release|Win32.ActiveCfg = Release|Win32 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Release|Win32.Build.0 = Release|Win32 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Release|x64.ActiveCfg = Release|x64 + {B2D63B45-92B0-40F7-B242-CCA4DFD64341}.Release|x64.Build.0 = Release|x64 {BE412850-4BB9-429A-877C-BFBC4B34186C}.Debug|aarch64.ActiveCfg = Debug aarch64|ARM {BE412850-4BB9-429A-877C-BFBC4B34186C}.Debug|aarch64.Build.0 = Debug aarch64|ARM {BE412850-4BB9-429A-877C-BFBC4B34186C}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -473,6 +487,7 @@ Global {CA8A30D1-8FA9-4330-B7F7-84709246D8DC} = {FD47AE19-69FD-260F-F2F1-20E65EA61D13} {7F773DD9-EB8F-2403-B43C-B49C2014B99C} = {FD47AE19-69FD-260F-F2F1-20E65EA61D13} {66174227-8541-41FC-A6DF-4764FC66F78E} = {FD47AE19-69FD-260F-F2F1-20E65EA61D13} + {B2D63B45-92B0-40F7-B242-CCA4DFD64341} = {FD47AE19-69FD-260F-F2F1-20E65EA61D13} {BE412850-4BB9-429A-877C-BFBC4B34186C} = {FD47AE19-69FD-260F-F2F1-20E65EA61D13} {23149706-C12F-4329-B6AA-8266407C32D3} = {FD47AE19-69FD-260F-F2F1-20E65EA61D13} {4B47A364-37C4-96A7-6041-97BB4C1D333B} = {EB5FC2C6-D72D-B6CC-C0C1-26F3AC2E9231} diff --git a/tools/slangd/language-server-protocol.cpp b/tools/slangd/language-server-protocol.cpp new file mode 100644 index 000000000..eca89fd86 --- /dev/null +++ b/tools/slangd/language-server-protocol.cpp @@ -0,0 +1,191 @@ +#include "language-server-protocol.h" + +namespace Slang +{ +namespace LanguageServerProtocol +{ +static const StructRttiInfo _makeTextDocumentSyncOptionsRtti() +{ + TextDocumentSyncOptions obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::TextDocumentSyncOptions", nullptr); + builder.addField("change", &obj.change); + builder.addField("openClose", &obj.openClose); + return builder.make(); +} +const StructRttiInfo TextDocumentSyncOptions::g_rttiInfo = _makeTextDocumentSyncOptionsRtti(); + +static const StructRttiInfo _makeTextDocumentItemRtti() +{ + TextDocumentItem obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::TextDocumentItem", nullptr); + builder.addField("uri", &obj.uri); + builder.addField("version", &obj.version); + builder.addField("languageId", &obj.languageId); + builder.addField("text", &obj.text); + return builder.make(); +} +const StructRttiInfo TextDocumentItem::g_rttiInfo = _makeTextDocumentItemRtti(); + +static const StructRttiInfo _makeTextDocumentIdentifierRtti() +{ + TextDocumentIdentifier obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::TextDocumentIdentifier", nullptr); + builder.addField("uri", &obj.uri); + return builder.make(); +} +const StructRttiInfo TextDocumentIdentifier::g_rttiInfo = _makeTextDocumentIdentifierRtti(); + +static const StructRttiInfo _makeVersionedTextDocumentIdentifierRtti() +{ + VersionedTextDocumentIdentifier obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::VersionedTextDocumentIdentifier", nullptr); + builder.addField("uri", &obj.uri); + builder.addField("version", &obj.version); + return builder.make(); +} +const StructRttiInfo VersionedTextDocumentIdentifier::g_rttiInfo = + _makeVersionedTextDocumentIdentifierRtti(); + +static const StructRttiInfo _makePositionRtti() +{ + Position obj; + StructRttiBuilder builder( + &obj, "LanguageServerProtocol::Position", nullptr); + builder.addField("line", &obj.line); + builder.addField("character", &obj.character); + return builder.make(); +} +const StructRttiInfo Position::g_rttiInfo = _makePositionRtti(); + +static const StructRttiInfo _makeRangeRtti() +{ + Range obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::Range", nullptr); + builder.addField("start", &obj.start); + builder.addField("end", &obj.end); + return builder.make(); +} +const StructRttiInfo Range::g_rttiInfo = _makeRangeRtti(); + +static const StructRttiInfo _makeDidOpenTextDocumentRtti() +{ + DidOpenTextDocumentParams obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::DidOpenTextDocumentParams", nullptr); + builder.addField("textDocument", &obj.textDocument); + return builder.make(); +} +const StructRttiInfo DidOpenTextDocumentParams::g_rttiInfo = _makeDidOpenTextDocumentRtti(); +const UnownedStringSlice DidOpenTextDocumentParams::methodName = + UnownedStringSlice::fromLiteral("textDocument/didOpen"); + +static const StructRttiInfo _makeTextDocumentContentChangeEventRtti() +{ + TextDocumentContentChangeEvent obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::TextDocumentContentChangeEvent", nullptr); + builder.addField("range", &obj.range, StructRttiInfo::Flag::Optional); + builder.addField("text", &obj.text); + return builder.make(); +} +const StructRttiInfo TextDocumentContentChangeEvent::g_rttiInfo = + _makeTextDocumentContentChangeEventRtti(); + +static const StructRttiInfo _makeDidChangeTextDocumentParamsRtti() +{ + DidChangeTextDocumentParams obj; + StructRttiBuilder builder( + &obj, "LanguageServerProtocol::DidChangeTextDocumentParams", nullptr); + builder.addField("textDocument", &obj.textDocument); + builder.addField("contentChanges", &obj.contentChanges); + return builder.make(); +} +const StructRttiInfo DidChangeTextDocumentParams::g_rttiInfo = + _makeDidChangeTextDocumentParamsRtti(); +const UnownedStringSlice DidChangeTextDocumentParams::methodName = + UnownedStringSlice::fromLiteral("textDocument/didChange"); + + +static const StructRttiInfo _makeDidCloseTextDocumentParamsRtti() +{ + DidCloseTextDocumentParams obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::DidCloseTextDocumentParams", nullptr); + builder.addField("textDocument", &obj.textDocument); + return builder.make(); +} +const StructRttiInfo DidCloseTextDocumentParams::g_rttiInfo = _makeDidCloseTextDocumentParamsRtti(); +const UnownedStringSlice DidCloseTextDocumentParams::methodName = + UnownedStringSlice::fromLiteral("textDocument/didClose"); + +static const StructRttiInfo _makeServerCapabilitiesRtti() +{ + ServerCapabilities obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::ServerCapabilities", nullptr); + builder.addField("positionEncoding", &obj.positionEncoding); + builder.addField("textDocumentSync", &obj.textDocumentSync); + return builder.make(); +} +const StructRttiInfo ServerCapabilities::g_rttiInfo = _makeServerCapabilitiesRtti(); + +static const StructRttiInfo _makeServerInfoRtti() +{ + ServerInfo obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::ServerInfo", nullptr); + builder.addField("name", &obj.name); + builder.addField("version", &obj.version); + return builder.make(); +} +const StructRttiInfo ServerInfo::g_rttiInfo = _makeServerInfoRtti(); + + +static const StructRttiInfo _makeInitializeResultRtti() +{ + InitializeResult obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::InitializeResult", nullptr); + builder.addField("capabilities", &obj.capabilities); + builder.addField("serverInfo", &obj.serverInfo); + return builder.make(); +} +const StructRttiInfo InitializeResult::g_rttiInfo = _makeInitializeResultRtti(); + +const UnownedStringSlice InitializeParams::methodName = + UnownedStringSlice::fromLiteral("initialize"); + +const UnownedStringSlice ShutdownParams::methodName = UnownedStringSlice::fromLiteral("shutdown"); + +const UnownedStringSlice ExitParams::methodName = UnownedStringSlice::fromLiteral("exit"); + +static const StructRttiInfo _makeWorkspaceFolderRtti() +{ + WorkspaceFolder obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::WorkspaceFolder", nullptr); + builder.addField("uri", &obj.uri); + builder.addField("name", &obj.name); + return builder.make(); +} +const StructRttiInfo WorkspaceFolder::g_rttiInfo = _makeWorkspaceFolderRtti(); + +static const StructRttiInfo _makeInitializeParamsRtti() +{ + InitializeParams obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::InitializeParams", nullptr); + builder.addField("workspaceFolders", &obj.workspaceFolders, StructRttiInfo::Flag::Optional); + return builder.make(); +} +const StructRttiInfo InitializeParams::g_rttiInfo = _makeInitializeParamsRtti(); + +static const StructRttiInfo _makeNullResponseRtti() +{ + NullResponse obj; + StructRttiBuilder builder(&obj, "LanguageServerProtocol::NullResponse", nullptr); + return builder.make(); +} +const StructRttiInfo NullResponse::g_rttiInfo = _makeNullResponseRtti(); + +NullResponse* NullResponse::get() +{ + static NullResponse result = {}; + return &result; +} + +} // namespace LanguageServerProtocol + +} diff --git a/tools/slangd/language-server-protocol.h b/tools/slangd/language-server-protocol.h new file mode 100644 index 000000000..c76f91d5c --- /dev/null +++ b/tools/slangd/language-server-protocol.h @@ -0,0 +1,148 @@ +#pragma once + +#include "../../slang-com-helper.h" +#include "../../slang-com-ptr.h" +#include "../../slang.h" + +#include "../../source/core/slang-rtti-info.h" +#include "../../source/compiler-core/slang-json-value.h" + +namespace Slang +{ +namespace LanguageServerProtocol +{ + struct ServerInfo + { + String name; + String version; + + static const StructRttiInfo g_rttiInfo; + }; + + enum class TextDocumentSyncKind + { + None = 0, + Full = 1, + Incremental = 2 + }; + + struct TextDocumentSyncOptions + { + bool openClose; + int32_t change; // TextDocumentSyncKind + static const StructRttiInfo g_rttiInfo; + }; + + struct TextDocumentItem + { + String uri; + String languageId; + int version; + String text; + static const StructRttiInfo g_rttiInfo; + }; + + struct TextDocumentIdentifier + { + String uri; + static const StructRttiInfo g_rttiInfo; + }; + + struct VersionedTextDocumentIdentifier + { + String uri; + int version; + static const StructRttiInfo g_rttiInfo; + }; + + struct Position + { + int line = -1; + int character = -1; + static const StructRttiInfo g_rttiInfo; + }; + + struct Range + { + Position start; + Position end; + static const StructRttiInfo g_rttiInfo; + }; + + struct DidOpenTextDocumentParams + { + TextDocumentItem textDocument; + static const StructRttiInfo g_rttiInfo; + static const UnownedStringSlice methodName; + }; + + struct TextDocumentContentChangeEvent + { + Range range; // optional + String text; + static const StructRttiInfo g_rttiInfo; + }; + + struct DidChangeTextDocumentParams + { + VersionedTextDocumentIdentifier textDocument; + List contentChanges; + static const StructRttiInfo g_rttiInfo; + static const UnownedStringSlice methodName; + + }; + + struct DidCloseTextDocumentParams + { + TextDocumentIdentifier textDocument; + static const StructRttiInfo g_rttiInfo; + static const UnownedStringSlice methodName; + }; + + struct ServerCapabilities + { + String positionEncoding; + TextDocumentSyncOptions textDocumentSync; + static const StructRttiInfo g_rttiInfo; + }; + + struct WorkspaceFolder + { + String uri; + String name; + static const StructRttiInfo g_rttiInfo; + }; + + struct InitializeParams + { + List workspaceFolders; + static const UnownedStringSlice methodName; + static const StructRttiInfo g_rttiInfo; + }; + + struct NullResponse + { + static const StructRttiInfo g_rttiInfo; + static NullResponse* get(); + }; + + struct InitializeResult + { + ServerCapabilities capabilities; + ServerInfo serverInfo; + + static const StructRttiInfo g_rttiInfo; + }; + + struct ShutdownParams + { + static const UnownedStringSlice methodName; + }; + + struct ExitParams + { + static const UnownedStringSlice methodName; + }; + +} +} // namespace LanguageServerProtocol diff --git a/tools/slangd/language-server.cpp b/tools/slangd/language-server.cpp new file mode 100644 index 000000000..7cb744ecd --- /dev/null +++ b/tools/slangd/language-server.cpp @@ -0,0 +1,184 @@ +// language-server.cpp + +// This file implements the language server for Slang, conforming to the Language Server Protocol. +// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/ + + +#include +#include +#include +#include + +#include "../../source/core/slang-secure-crt.h" +#include "../../slang-com-helper.h" + +#include "language-server-protocol.h" +#include "language-server.h" + +namespace Slang +{ +using namespace LanguageServerProtocol; + +SlangResult LanguageServer::init(const InitializeParams& args) +{ + SLANG_RETURN_ON_FAIL(m_connection->initWithStdStreams()); + m_workspaceFolders = args.workspaceFolders; + return SLANG_OK; +} + +slang::IGlobalSession* LanguageServer::getOrCreateGlobalSession() +{ + if (!m_session) + { + // Just create the global session in the regular way if there isn't one set + if (SLANG_FAILED(slang_createGlobalSession(SLANG_API_VERSION, m_session.writeRef()))) + { + return nullptr; + } + } + + return m_session; +} + +SlangResult LanguageServer::_executeSingle() +{ + // If we don't have a message, we can quit for now + if (!m_connection->hasMessage()) + { + return SLANG_OK; + } + + const JSONRPCMessageType msgType = m_connection->getMessageType(); + + switch (msgType) + { + case JSONRPCMessageType::Call: + { + JSONRPCCall call; + SLANG_RETURN_ON_FAIL(m_connection->getRPCOrSendError(&call)); + + // Do different things + if (call.method == ExitParams::methodName) + { + m_quit = true; + return SLANG_OK; + } + else if (call.method == ShutdownParams::methodName) + { + m_connection->sendResult(NullResponse::get(), call.id); + return SLANG_OK; + } + else if (call.method == InitializeParams::methodName) + { + InitializeParams args = {}; + SLANG_RETURN_ON_FAIL( + m_connection->toNativeArgsOrSendError(call.params, &args, call.id)); + + init(args); + + InitializeResult result = {}; + result.serverInfo.name = "SlangLanguageServer"; + result.serverInfo.version = "1.0"; + result.capabilities.positionEncoding = "utf-8"; + result.capabilities.textDocumentSync.openClose = true; + result.capabilities.textDocumentSync.change = (int)TextDocumentSyncKind::Full; + m_connection->sendResult(&result, call.id); + return SLANG_OK; + } + else if (call.method == DidOpenTextDocumentParams::methodName) + { + DidOpenTextDocumentParams args; + SLANG_RETURN_ON_FAIL( + m_connection->toNativeArgsOrSendError(call.params, &args, call.id)); + return didOpenTextDocument(args); + } + else if (call.method == DidCloseTextDocumentParams::methodName) + { + DidCloseTextDocumentParams args; + SLANG_RETURN_ON_FAIL( + m_connection->toNativeArgsOrSendError(call.params, &args, call.id)); + return didCloseTextDocument(args); + } + else if (call.method == DidChangeTextDocumentParams::methodName) + { + DidChangeTextDocumentParams args; + SLANG_RETURN_ON_FAIL( + m_connection->toNativeArgsOrSendError(call.params, &args, call.id)); + return didChangeTextDocument(args); + } + else if (call.method == "initialized") + { + return SLANG_OK; + } + else + { + return m_connection->sendError(JSONRPC::ErrorCode::MethodNotFound, call.id); + } + } + default: + { + return m_connection->sendError( + JSONRPC::ErrorCode::InvalidRequest, m_connection->getCurrentMessageId()); + } + } + + return SLANG_OK; +} + +SlangResult LanguageServer::didOpenTextDocument(const DidOpenTextDocumentParams& args) +{ + return SLANG_OK; +} +SlangResult LanguageServer::didCloseTextDocument(const DidCloseTextDocumentParams& args) +{ + return SLANG_OK; +} +SlangResult LanguageServer::didChangeTextDocument(const DidChangeTextDocumentParams& args) +{ + return SLANG_OK; +} + +void LanguageServer::update() +{ + +} + +SlangResult LanguageServer::execute() +{ + m_connection = new JSONRPCConnection(); + m_connection->initWithStdStreams(); + while (m_connection->isActive() && !m_quit) + { + // Consume all messages first. + while (m_connection->tryReadMessage() == SLANG_OK) + { + const SlangResult res = _executeSingle(); + } + + // Now we can use this time to reparse user's code, report diagnostics, etc. + update(); + } + + return SLANG_OK; +} + +} // namespace LanguageServer + +int main(int argc, const char* const* argv) +{ + bool isDebug = false; + for (auto i = 1; i < argc; i++) + { + if (Slang::UnownedStringSlice(argv[i]) == "--debug") + { + isDebug = true; + } + } + if (isDebug) + { + std::this_thread::sleep_for(std::chrono::seconds(10)); + } + Slang::LanguageServer server; + SLANG_RETURN_ON_FAIL(server.execute()); + return 0; +} diff --git a/tools/slangd/language-server.h b/tools/slangd/language-server.h new file mode 100644 index 000000000..829a629b1 --- /dev/null +++ b/tools/slangd/language-server.h @@ -0,0 +1,35 @@ +#pragma once + +#include "../../source/core/slang-io.h" +#include "../../source/core/slang-process-util.h" +#include "../../source/core/slang-string-util.h" +#include "../../source/core/slang-string.h" +#include "../../source/core/slang-writer.h" +#include "../../source/compiler-core/slang-json-rpc-connection.h" +#include "language-server-protocol.h" + +namespace Slang +{ + class LanguageServer + { + public: + RefPtr m_connection; + ComPtr m_session; + bool m_quit = false; + List m_workspaceFolders; + + SlangResult init(const LanguageServerProtocol::InitializeParams& args); + SlangResult execute(); + void update(); + SlangResult didOpenTextDocument( + const LanguageServerProtocol::DidOpenTextDocumentParams& args); + SlangResult didCloseTextDocument( + const LanguageServerProtocol::DidCloseTextDocumentParams& args); + SlangResult didChangeTextDocument( + const LanguageServerProtocol::DidChangeTextDocumentParams& args); + + private: + SlangResult _executeSingle(); + slang::IGlobalSession* getOrCreateGlobalSession(); + }; +} -- cgit v1.2.3