jak-project/lsp/handlers/text_document/document_synchronization.cpp

78 lines
2.8 KiB
C++
Raw Permalink Normal View History

LSP: A bunch of new OpenGOAL language features (#3437) - Integrate the AST into the LSP, this makes parsing and tokenizing the files much easier - Consolidate most of the symbol info tracking in `goalc` to a single map. Fixed some issues where the old map would never evict symbols when re-compiling files. There is still some more to cleanup, but this now can be used as an incrementally updated source-of-truth for the LSP - re-compile files when they are saved. Ideally this would be done everytime they are changed but that: - may be too aggressive - goalc doesn't compile incrementally yet so it likely would be a worse UX Features added, see https://github.com/open-goal/opengoal-vscode/issues/256 - Hover ![image](https://github.com/open-goal/jak-project/assets/13153231/58dadb5d-582c-4c1f-9ffe-eaa4c85a0255) ![image](https://github.com/open-goal/jak-project/assets/13153231/b383adde-57fc-462c-a256-b2de5c30ca9a) - LSP Status fixed - Type Hierarchy ![image](https://github.com/open-goal/jak-project/assets/13153231/8e681377-1d4e-4336-ad70-1695a4607340) - Document Color ![image](https://github.com/open-goal/jak-project/assets/13153231/4e48ccd8-0ed1-4459-a133-5277561e4201) - Document Symbols ![Screenshot 2024-03-27 004105](https://github.com/open-goal/jak-project/assets/13153231/8e655034-43c4-4261-b6e0-85de00cbfc7f) - Completions ![Screenshot 2024-03-30 004504](https://github.com/open-goal/jak-project/assets/13153231/d123a187-af90-466b-9eb7-561b2ee97cd1) --------- Co-authored-by: Hat Kid <6624576+Hat-Kid@users.noreply.github.com>
2024-03-30 19:49:07 -04:00
#include "document_synchronization.h"
namespace lsp_handlers {
void did_open(Workspace& workspace, json raw_params) {
auto params = raw_params.get<LSPSpec::DidOpenTextDocumentParams>();
workspace.start_tracking_file(params.m_textDocument.m_uri, params.m_textDocument.m_languageId,
params.m_textDocument.m_text);
}
void did_change(Workspace& workspace, json raw_params) {
auto params = raw_params.get<LSPSpec::DidChangeTextDocumentParams>();
for (const auto& change : params.m_contentChanges) {
workspace.update_tracked_file(params.m_textDocument.m_uri, change.m_text);
}
}
void did_close(Workspace& workspace, json raw_params) {
auto params = raw_params.get<LSPSpec::DidCloseTextDocumentParams>();
workspace.stop_tracking_file(params.m_textDocument.m_uri);
}
void will_save(Workspace& workspace, json raw_params) {
auto params = raw_params.get<LSPSpec::WillSaveTextDocumentParams>();
workspace.tracked_file_will_save(params.textDocument.m_uri);
}
std::optional<json> did_open_push_diagnostics(Workspace& workspace, json raw_params) {
auto params = raw_params.get<LSPSpec::DidOpenTextDocumentParams>();
const auto file_type =
workspace.determine_filetype_from_languageid(params.m_textDocument.m_languageId);
LSPSpec::PublishDiagnosticParams publish_params;
publish_params.m_uri = params.m_textDocument.m_uri;
publish_params.m_version = params.m_textDocument.m_version;
if (file_type == Workspace::FileType::OpenGOALIR) {
auto maybe_tracked_file = workspace.get_tracked_ir_file(params.m_textDocument.m_uri);
if (!maybe_tracked_file) {
return {};
}
const auto& tracked_file = maybe_tracked_file.value().get();
publish_params.m_diagnostics = tracked_file.m_diagnostics;
}
json response;
response["method"] = "textDocument/publishDiagnostics";
response["params"] = publish_params;
return response;
}
std::optional<json> did_change_push_diagnostics(Workspace& workspace, json raw_params) {
auto params = raw_params.get<LSPSpec::DidChangeTextDocumentParams>();
const auto file_type = workspace.determine_filetype_from_uri(params.m_textDocument.m_uri);
LSPSpec::PublishDiagnosticParams publish_params;
publish_params.m_uri = params.m_textDocument.m_uri;
publish_params.m_version = params.m_textDocument.m_version;
if (file_type == Workspace::FileType::OpenGOALIR) {
auto maybe_tracked_file = workspace.get_tracked_ir_file(params.m_textDocument.m_uri);
if (!maybe_tracked_file) {
return {};
}
const auto& tracked_file = maybe_tracked_file.value().get();
publish_params.m_diagnostics = tracked_file.m_diagnostics;
}
json response;
response["method"] = "textDocument/publishDiagnostics";
response["params"] = publish_params;
return response;
}
} // namespace lsp_handlers