♻️ 重构

This commit is contained in:
csh
2025-12-05 21:03:18 +08:00
parent 4c2e242920
commit 549f1d1b0a
161 changed files with 26415 additions and 28009 deletions
@@ -1,6 +1,5 @@
#include <spdlog/spdlog.h>
#include "./initialize.hpp"
#include "../../service/symbol.hpp"
#include "../../scheduler/async_executor.hpp"
#include "../../protocol/transform/facade.hpp"
@@ -18,9 +17,13 @@ namespace lsp::provider
std::string Initialize::ProvideResponse(const protocol::RequestMessage& request, ExecutionContext& context)
{
spdlog::debug("InitializeProvider: Providing response for method {}", request.method);
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);
@@ -108,62 +111,33 @@ namespace lsp::provider
void Initialize::ProcessWorkspaceFolder(const std::vector<protocol::WorkspaceFolder>& workspace_folders, ExecutionContext& context)
{
auto symbol = context.GetService<service::Symbol>();
auto& manager_hub = context.GetManagerHub();
auto& scheduler = context.GetScheduler();
for (const auto& workspace_folder : workspace_folders)
{
std::string workspace_path = UriToPath(workspace_folder.uri);
scheduler.Submit("Load workspace symbols", [symbol, workspace_path, folder_name = workspace_folder.name]() -> std::optional<std::string> {
auto task_id = fmt::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
{
symbol->LoadWorkspaceSymbols(workspace_path);
return fmt::format("Loaded workspace {} symbols", workspace_path);
manager_hub.symbols().LoadWorkspace(uri);
return fmt::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::string& result) { spdlog::info("Worksapce loading result:", result); });
} }, [](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::info("Initiated loading for {} workspace folder(s)", workspace_folders.size());
spdlog::debug("Initiated loading for {} workspace folder(s)", workspace_folders.size());
}
std::string UriDecode(const std::string& encoded)
{
std::string decoded;
for (size_t i = 0; i < encoded.length(); ++i)
{
if (encoded[i] == '%' && i + 2 < encoded.length())
{
std::string hex = encoded.substr(i + 1, 2);
char ch = static_cast<char>(std::stoi(hex, nullptr, 16));
decoded += ch;
i += 2;
}
else if (encoded[i] == '+')
{
decoded += ' ';
}
else
{
decoded += encoded[i];
}
}
return decoded;
}
std::string Initialize::UriToPath(const protocol::DocumentUri& uri)
{
std::string path = uri;
if (path.find("file://") == 0)
path = path.substr(7);
#ifdef _WIN32
if (path.length() > 0 && path[0] == '/')
path = path.substr(1);
#endif
path = UriDecode(path);
return path;
}
}
@@ -19,6 +19,5 @@ namespace lsp::provider
protocol::SemanticTokensOptions BuildSemanticTokenOptions();
void ProcessWorkspaceFolder(const std::vector<protocol::WorkspaceFolder>& workspace_folders, ExecutionContext& context);
std::string UriToPath(const protocol::DocumentUri& uri);
};
}