Browse Source

language client: cleanup of workspace/applyEdit

master
eidheim 2 months ago
parent
commit
dd11a9c1d8
  1. 22
      src/source_language_protocol.cpp
  2. 2
      src/source_language_protocol.hpp

22
src/source_language_protocol.cpp

@ -64,13 +64,14 @@ LanguageProtocol::TextDocumentEdit::TextDocumentEdit(const JSON &text_document_e
LanguageProtocol::TextDocumentEdit::TextDocumentEdit(std::string file, std::vector<TextEdit> text_edits) : file(std::move(file)), text_edits(std::move(text_edits)) {} LanguageProtocol::TextDocumentEdit::TextDocumentEdit(std::string file, std::vector<TextEdit> text_edits) : file(std::move(file)), text_edits(std::move(text_edits)) {}
LanguageProtocol::WorkspaceEdit::WorkspaceEdit(const JSON &workspace_edit, boost::filesystem::path file_path) { LanguageProtocol::WorkspaceEdit::WorkspaceEdit(const JSON &workspace_edit, boost::filesystem::path path) {
boost::filesystem::path project_path; boost::filesystem::path project_path;
auto build = Project::Build::create(file_path); auto build = Project::Build::create(path);
boost::system::error_code ec;
if(!build->project_path.empty()) if(!build->project_path.empty())
project_path = build->project_path; project_path = build->project_path;
else else
project_path = file_path.parent_path(); project_path = boost::filesystem::is_directory(path, ec) ? path : path.parent_path();
try { try {
if(auto changes = workspace_edit.children_optional("changes")) { if(auto changes = workspace_edit.children_optional("changes")) {
for(auto &change : *changes) { for(auto &change : *changes) {
@ -587,20 +588,20 @@ void LanguageProtocol::Client::handle_server_request(const boost::variant<size_t
if(method == "workspace/applyEdit") { if(method == "workspace/applyEdit") {
std::promise<void> result_processed; std::promise<void> result_processed;
bool applied = true; bool applied = true;
dispatcher->post([&result_processed, &applied, params = std::make_shared<JSON>(std::move(params))] { dispatcher->post([this, &result_processed, &applied, params = std::make_shared<JSON>(std::move(params))] {
ScopeGuard guard({[&result_processed] { ScopeGuard guard({[&result_processed] {
result_processed.set_value(); result_processed.set_value();
}}); }});
if(auto current_view = dynamic_cast<Source::LanguageProtocolView *>(Notebook::get().get_current_view())) {
LanguageProtocol::WorkspaceEdit workspace_edit; LanguageProtocol::WorkspaceEdit workspace_edit;
try { try {
workspace_edit = LanguageProtocol::WorkspaceEdit(params->object("edit"), current_view->file_path); workspace_edit = LanguageProtocol::WorkspaceEdit(params->object("edit"), root_path);
} }
catch(...) { catch(...) {
applied = false; applied = false;
return; return;
} }
auto current_view = Notebook::get().get_current_view();
for(auto &document_change : workspace_edit.document_changes) { for(auto &document_change : workspace_edit.document_changes) {
if(auto edit = boost::get<TextDocumentEdit>(&document_change)) { if(auto edit = boost::get<TextDocumentEdit>(&document_change)) {
Source::View *view = nullptr; Source::View *view = nullptr;
@ -621,12 +622,17 @@ void LanguageProtocol::Client::handle_server_request(const boost::variant<size_t
auto buffer = view->get_buffer(); auto buffer = view->get_buffer();
buffer->begin_user_action(); buffer->begin_user_action();
auto get_line_pos = [this](Source::View *view, const Gtk::TextIter &iter) -> int {
if(capabilities.use_line_index)
return iter.get_line_index();
return utf16_code_unit_count(view->get_line(iter), 0, iter.get_line_index());
};
auto end_iter = buffer->end(); auto end_iter = buffer->end();
// If entire buffer is replaced // If entire buffer is replaced
if(edit->text_edits.size() == 1 && if(edit->text_edits.size() == 1 &&
edit->text_edits[0].range.start.line == 0 && edit->text_edits[0].range.start.character == 0 && edit->text_edits[0].range.start.line == 0 && edit->text_edits[0].range.start.character == 0 &&
(edit->text_edits[0].range.end.line > end_iter.get_line() || (edit->text_edits[0].range.end.line > end_iter.get_line() ||
(edit->text_edits[0].range.end.line == end_iter.get_line() && edit->text_edits[0].range.end.character >= current_view->get_line_pos(end_iter)))) { (edit->text_edits[0].range.end.line == end_iter.get_line() && edit->text_edits[0].range.end.character >= get_line_pos(view, end_iter)))) {
view->replace_text(edit->text_edits[0].new_text); view->replace_text(edit->text_edits[0].new_text);
} }
else { else {
@ -644,10 +650,8 @@ void LanguageProtocol::Client::handle_server_request(const boost::variant<size_t
buffer->end_user_action(); buffer->end_user_action();
} }
} }
if(current_view) if(current_view)
Notebook::get().open(current_view); Notebook::get().open(current_view);
}
}); });
result_processed.get_future().get(); result_processed.get_future().get();
write_response(id, std::string("{\"applied\":") + (applied ? "true" : "false") + '}'); write_response(id, std::string("{\"applied\":") + (applied ? "true" : "false") + '}');

2
src/source_language_protocol.hpp

@ -111,7 +111,7 @@ namespace LanguageProtocol {
class WorkspaceEdit { class WorkspaceEdit {
public: public:
WorkspaceEdit() = default; WorkspaceEdit() = default;
WorkspaceEdit(const JSON &workspace_edit, boost::filesystem::path file_path); WorkspaceEdit(const JSON &workspace_edit, boost::filesystem::path path);
std::vector<boost::variant<TextDocumentEdit, RenameFile>> document_changes; std::vector<boost::variant<TextDocumentEdit, RenameFile>> document_changes;
}; };

Loading…
Cancel
Save