♻️ 使用module重构所有代码

This commit is contained in:
csh
2025-12-07 23:07:03 +08:00
parent 549f1d1b0a
commit f7d5a74615
369 changed files with 2272844 additions and 2202476 deletions
@@ -0,0 +1,175 @@
module;
#include <format>
export module lsp.provider.initialize.initialize;
import spdlog;
import std;
import lsp.protocol;
import lsp.codec.facade;
import lsp.provider.base.interface;
import lsp.scheduler.async_executor;
namespace transform = lsp::codec;
export namespace lsp::provider
{
class Initialize : public IRequestProvider
{
public:
Initialize() = default;
std::string ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context) override;
std::string GetMethod() const override;
std::string GetProviderName() const override;
private:
protocol::InitializeResult BuildInitializeResult();
protocol::TextDocumentSyncOptions BuildTextDocumentSyncOptions();
protocol::CompletionOptions BuildCompletionOptions();
protocol::SemanticTokensOptions BuildSemanticTokenOptions();
void ProcessWorkspaceFolder(const std::vector<protocol::WorkspaceFolder>& workspace_folders, ExecutionContext& context);
};
}
namespace lsp::provider
{
std::string Initialize::GetMethod() const
{
return "initialize";
}
std::string Initialize::GetProviderName() const
{
return "Initialize";
}
std::string Initialize::ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context)
{
spdlog::debug("Initialize request {}", request.method);
protocol::InitializeParams params = transform::FromLSPAny.template operator()<protocol::InitializeParams>(request.params.value());
auto& manager_hub = context.GetManagerHub();
manager_hub.Initialize();
if (params.workspaceFolders)
ProcessWorkspaceFolder(params.workspaceFolders.value(), context);
protocol::ResponseMessage response;
response.id = request.id;
response.result = transform::ToLSPAny(BuildInitializeResult());
std::optional<std::string> json = transform::Serialize(response);
if (!json.has_value())
{
context.TriggerLifecycleEvent(ServerLifecycleEvent::kInitializeFailed);
return BuildErrorResponseMessage(request, protocol::ErrorCodes::InternalError, "Internal error");
}
context.TriggerLifecycleEvent(ServerLifecycleEvent::kInitialized);
return json.value();
}
protocol::InitializeResult Initialize::BuildInitializeResult()
{
protocol::InitializeResult result;
result.serverInfo.name = "TSL Language Server";
result.serverInfo.version = __DATE__;
result.capabilities.textDocumentSync = BuildTextDocumentSyncOptions();
result.capabilities.completionProvider = BuildCompletionOptions();
// result.capabilities.semanticTokensProvider = BuildSemanticTokenOptions();
return result;
}
protocol::TextDocumentSyncOptions Initialize::BuildTextDocumentSyncOptions()
{
protocol::TextDocumentSyncOptions options;
options.openClose = true;
options.change = protocol::TextDocumentSyncKind::Incremental;
return options;
}
protocol::CompletionOptions Initialize::BuildCompletionOptions()
{
protocol::CompletionOptions options;
options.triggerCharacters = { std::vector<std::string>{ ".", "(" } };
options.resolveProvider = true;
options.completionItem = { .labelDetailsSupport = true };
return options;
}
protocol::SemanticTokensOptions Initialize::BuildSemanticTokenOptions()
{
protocol::SemanticTokensOptions options;
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Namespace);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Type);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Class);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Enum);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Interface);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Struct);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::TypeParameter);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Parameter);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Variable);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Property);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::EnumMember);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Event);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Function);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Method);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Macro);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Keyword);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Modifier);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Comment);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::String);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Number);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Regexp);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Operator);
options.legend.tokenTypes.emplace_back(protocol::SemanticTokenTypesLiterals::Decorator);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Declaration);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Definition);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Readonly);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Static);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Deprecated);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Abstract);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Async);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Modification);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::Documentation);
options.legend.tokenModifiers.emplace_back(protocol::SemanticTokenModifiersLiterals::DefaultLibrary);
options.range = true;
options.full = protocol::SemanticTokensOptions::Full{ .delta = true };
return options;
}
void Initialize::ProcessWorkspaceFolder(const std::vector<protocol::WorkspaceFolder>& workspace_folders, ExecutionContext& context)
{
auto& manager_hub = context.GetManagerHub();
auto& scheduler = context.GetScheduler();
for (const auto& workspace_folder : workspace_folders)
{
auto task_id = std::format("Load workspace symbols: {}", workspace_folder.uri);
scheduler.Submit(task_id, [&manager_hub, uri = workspace_folder.uri, folder_name = workspace_folder.name]() -> std::optional<std::string> {
try
{
manager_hub.symbols().LoadWorkspace(uri);
return std::format("Loaded workspace {} symbols", uri);
}
catch (const std::exception& e)
{
spdlog::error("Failed to load workspace {} symbols: {}", folder_name, e.what());
throw; // Request会处理异常
} }, [](const std::optional<std::string>& result, bool cancelled) {
if (cancelled)
{
spdlog::debug("Workspace loading task cancelled");
}
else if (result)
{
spdlog::info("Workspace loading result: {}", *result);
} });
}
spdlog::debug("Initiated loading for {} workspace folder(s)", workspace_folders.size());
}
}