🐛 fix: support LSP request id variants

This commit is contained in:
csh 2025-12-21 15:38:46 +08:00
parent 8e564fc8bb
commit 1de97aace5
6 changed files with 16 additions and 9 deletions

View File

@ -47,7 +47,7 @@ export namespace lsp::codec
namespace debug
{
std::string GetTypeName(const protocol::LSPAny& any);
std::string GetIdString(const std::variant<int, std::string>& id);
std::string GetIdString(const protocol::RequestId& id);
}
}
@ -134,10 +134,10 @@ namespace lsp::codec
return "unknown";
}
inline std::string GetIdString(const std::variant<int, std::string>& id)
inline std::string GetIdString(const protocol::RequestId& id)
{
return std::visit([](const auto& value) -> std::string {
if constexpr (std::is_same_v<std::decay_t<decltype(value)>, int>)
if constexpr (std::is_same_v<std::decay_t<decltype(value)>, protocol::integer>)
return std::to_string(value);
else
return value;

View File

@ -189,6 +189,10 @@ namespace lsp::codec
{
if (any.Is<protocol::decimal>())
return static_cast<T>(any.Get<protocol::decimal>());
if (any.Is<protocol::integer>())
return static_cast<T>(any.Get<protocol::integer>());
if (any.Is<protocol::uinteger>())
return static_cast<T>(any.Get<protocol::uinteger>());
}
throw ConversionError("LSPAny does not contain a compatible numeric type");

View File

@ -315,7 +315,9 @@ namespace lsp::core
void LspServer::HandleResponse(const protocol::ResponseMessage& response)
{
const std::string id = response.id.value_or("<no id>");
std::string id = "<no id>";
if (response.id.has_value())
id = transform::debug::GetIdString(response.id.value());
spdlog::debug("Received response: {}", id);
// 当前服务器作为 client 的场景较少,这里暂时不处理
}

View File

@ -16,6 +16,7 @@ export namespace lsp::protocol
using URI = string;
using DocumentUri = string;
using ProgressToken = std::variant<string, integer>;
using RequestId = std::variant<integer, string>;
struct LSPAny;
using LSPObject = std::map<string, LSPAny>;

View File

@ -32,7 +32,7 @@ export namespace lsp::protocol
struct RequestMessage
{
string jsonrpc = "2.0";
string id;
RequestId id;
string method;
std::optional<LSPAny> params;
};
@ -48,7 +48,7 @@ export namespace lsp::protocol
struct ResponseMessage
{
string jsonrpc = "2.0";
std::optional<string> id;
std::optional<RequestId> id;
std::optional<LSPAny> result;
std::optional<ResponseError> error;
};
@ -64,6 +64,6 @@ export namespace lsp::protocol
struct CancelParams
{
// The request id to cancel.
std::variant<int, std::string> id; // integer | string
RequestId id; // integer | string
};
}

View File

@ -403,7 +403,7 @@ namespace lsp::test
TestResult result;
result.passed = true;
std::variant<int, std::string> id = 123;
protocol::RequestId id = static_cast<protocol::integer>(123);
std::string id_str = transform::debug::GetIdString(id);
assertEqual(std::string("123"), id_str, "int id应该转换为'123'");
@ -416,7 +416,7 @@ namespace lsp::test
TestResult result;
result.passed = true;
std::variant<int, std::string> id = std::string("abc-123-def");
protocol::RequestId id = std::string("abc-123-def");
std::string id_str = transform::debug::GetIdString(id);
assertEqual(std::string("abc-123-def"), id_str, "string id应该保持为'abc-123-def'");