This commit is contained in:
csh
2025-07-05 13:18:34 +08:00
parent 0c4619261d
commit e2d88085f0
23 changed files with 112 additions and 84 deletions
@@ -3,6 +3,7 @@
#include <spdlog/spdlog.h>
#include "../../protocol/protocol.hpp"
#include "../../scheduler/request_scheduler.hpp"
#include "../../services/document.hpp"
namespace lsp::providers
{
@@ -17,7 +18,27 @@ namespace lsp::providers
using LifecycleCallback = std::function<void(ServerLifecycleEvent)>;
class ProviderContext;
class ExecutionContext
{
public:
ExecutionContext(scheduler::RequestScheduler* scheduler, LifecycleCallback lifecycle_callback, services::DocumentManager* document_manager = nullptr) :
scheduler_(scheduler), lifecycle_callback_(lifecycle_callback), document_manager_(document_manager) {}
scheduler::RequestScheduler* GetScheduler() const { return scheduler_; }
services::DocumentManager* GetDocumentManager() const { return document_manager_; }
void TriggerLifecycleEvent(ServerLifecycleEvent event) const
{
if (lifecycle_callback_)
lifecycle_callback_(event);
}
private:
scheduler::RequestScheduler* scheduler_;
LifecycleCallback lifecycle_callback_;
services::DocumentManager* document_manager_;
};
// LSP请求提供者接口基类
class IProvider
@@ -38,7 +59,7 @@ namespace lsp::providers
virtual ~IRequestProvider() = default;
// 处理LSP请求
virtual std::string ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context) = 0;
virtual std::string ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context) = 0;
static std::string BuildErrorResponseMessage(const protocol::RequestMessage& request, protocol::ErrorCode code, const std::string& message);
};
@@ -49,25 +70,7 @@ namespace lsp::providers
virtual ~INotificationProvider() = default;
// 处理LSP通知
virtual void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) = 0;
virtual void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) = 0;
};
class ProviderContext
{
public:
ProviderContext(scheduler::RequestScheduler* scheduler, LifecycleCallback lifecycle_callback) :
scheduler_(scheduler), lifecycle_callback_(lifecycle_callback) {}
scheduler::RequestScheduler* GetScheduler() const { return scheduler_; }
void TriggerLifecycleEvent(ServerLifecycleEvent event) const
{
if (lifecycle_callback_)
lifecycle_callback_(event);
}
private:
scheduler::RequestScheduler* scheduler_;
LifecycleCallback lifecycle_callback_;
};
}
@@ -12,7 +12,7 @@ namespace lsp::providers::cancel_request
return "CancelRequestProvider";
}
void CancelRequestProvider::HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context)
void CancelRequestProvider::HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context)
{
try
{
@@ -11,6 +11,6 @@ namespace lsp::providers::cancel_request
public:
std::string GetMethod() const override;
std::string GetProviderName() const override;
void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) override;
void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) override;
};
}
@@ -13,7 +13,7 @@ namespace lsp::providers::exit
return "ExitProvider";
}
void ExitProvider::HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context)
void ExitProvider::HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context)
{
// static_cast<void>(context);
spdlog::debug("ExitProvider: Providing response for method {}", notification.method);
@@ -8,6 +8,6 @@ namespace lsp::providers::exit
public:
std::string GetMethod() const override;
std::string GetProviderName() const override;
void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) override;
void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) override;
};
}
@@ -14,7 +14,7 @@ namespace lsp::providers::initialize
return "InitializeProvider";
}
std::string InitializeProvider::ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context)
std::string InitializeProvider::ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context)
{
spdlog::debug("InitializeProvider: Providing response for method {}", request.method);
protocol::ResponseMessage response;
@@ -8,7 +8,7 @@ namespace lsp::providers::initialize
{
public:
InitializeProvider() = default;
std::string ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context) override;
std::string ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context) override;
std::string GetMethod() const override;
std::string GetProviderName() const override;
@@ -14,7 +14,7 @@ namespace lsp::providers::initialized
return "InitializedProvider";
}
void InitializedProvider::HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context)
void InitializedProvider::HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context)
{
static_cast<void>(context); // 如果不需要上下文,可以忽略
spdlog::debug("InitializeProvider: Providing response for method {}", notification.method);
@@ -9,6 +9,6 @@ namespace lsp::providers::initialized
InitializedProvider() = default;
std::string GetMethod() const override;
std::string GetProviderName() const override;
void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) override;
void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) override;
};
}
@@ -14,7 +14,7 @@ namespace lsp::providers::shutdown
return "ShutdownProvider";
}
std::string ShutdownProvider::ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context)
std::string ShutdownProvider::ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context)
{
spdlog::debug("ShutdownProvider: Providing response for method {}", request.method);
@@ -10,6 +10,6 @@ namespace lsp::providers::shutdown
std::string GetMethod() const override;
std::string GetProviderName() const override;
std::string ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context) override;
std::string ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context) override;
};
}
@@ -14,7 +14,7 @@ namespace lsp::providers::text_document
return "CompletionProvider";
}
std::string CompletionProvider::ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context)
std::string CompletionProvider::ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context)
{
static_cast<void>(context);
spdlog::debug("CompletionProvider: Providing response for method {}", request.method);
@@ -14,7 +14,7 @@ namespace lsp::providers::text_document
std::string GetMethod() const override;
std::string GetProviderName() const override;
std::string ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context) override;
std::string ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context) override;
private:
// 构建完整的补全响应
@@ -14,7 +14,7 @@ namespace lsp::providers::text_document
return "DidChangeProvider";
}
void DidChangeProvider::HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context)
void DidChangeProvider::HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context)
{
static_cast<void>(context);
spdlog::debug("DidChangeProvider: Providing response for method {}", notification.method);
@@ -9,6 +9,6 @@ namespace lsp::providers::text_document
DidChangeProvider() = default;
std::string GetMethod() const override;
std::string GetProviderName() const override;
void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) override;
void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) override;
};
}
@@ -13,7 +13,7 @@ namespace lsp::providers::text_document
return "DidOpenProvider";
}
void DidOpenProvider::HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context)
void DidOpenProvider::HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context)
{
static_cast<void>(context);
spdlog::debug("DidOpenProvider: Providing response for method {}", notification.method);
@@ -10,6 +10,6 @@ namespace lsp::providers::text_document
DidOpenProvider() = default;
std::string GetMethod() const override;
std::string GetProviderName() const override;
void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) override;
void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) override;
};
}
@@ -13,7 +13,7 @@ namespace lsp::providers::set_trace
return "SetTraceProvider";
}
void SetTraceProvider::HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context)
void SetTraceProvider::HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context)
{
static_cast<void>(context);
spdlog::debug("SetTraceProvider: Providing response for method {}", notification.method);
@@ -9,6 +9,6 @@ namespace lsp::providers::set_trace
SetTraceProvider() = default;
std::string GetMethod() const override;
std::string GetProviderName() const override;
void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) override;
void HandleNotification(const protocol::NotificationMessage& notification, ExecutionContext& context) override;
};
}