tsl-devkit/lsp-server/src/core/server.hpp

65 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <atomic>
#include <memory>
#include <optional>
#include <string>
#include "./dispacther.hpp"
#include "../scheduler/request_scheduler.hpp"
#include "../provider/base/provider_registry.hpp"
namespace lsp::core
{
class LspServer
{
public:
LspServer(size_t concurrency = std::thread::hardware_concurrency());
~LspServer();
void Run();
private:
// 读取LSP消息
std::optional<std::string> ReadMessage();
// 处理LSP请求 - 返回序列化的响应或空字符串(对于通知)
void HandleMessage(const std::string& raw_message);
// 发送LSP响应
void SendResponse(const std::string& response);
// 处理不同类型的消息
void HandleRequest(const protocol::RequestMessage& request);
void HandleNotification(const protocol::NotificationMessage& notification);
void HandleResponse(const protocol::ResponseMessage& response);
// 生命周期事件处理
void OnLifecycleEvent(providers::ServerLifecycleEvent event);
// 判断是否需要同步处理
bool RequiresSyncProcessing(const std::string& method) const;
// 检查是否可以处理请求
bool CanProcessRequest(const std::string& method) const;
// 处理取消请求
void HandleCancelRequest(const protocol::NotificationMessage& notification);
private:
void InitializeCoreServices();
void InitializeExtensionServices();
void SendError(const protocol::RequestMessage& request, protocol::ErrorCode code, const std::string& message);
void SendStateError(const protocol::RequestMessage& request);
private:
// 核心组件-必需的生命周期和LspServer一致
RequestDispatcher dispatcher_;
scheduler::RequestScheduler scheduler_;
// 可选/扩展组件 -- 所以用智能指针
std::unique_ptr<services::DocumentService> document_service_;
std::atomic<bool> is_initialized_ = false;
std::atomic<bool> is_shutting_down_ = false;
std::mutex output_mutex_;
};
}