Update includes when files are renamed
The server advertises willRename and answers workspace/willRenameFiles with a WorkspaceEdit. A bounded core resolves quoted includes against the document directory and replaces only the spelling (relative stays relative, absolute stays absolute, angle includes untouched), with registered tests. EditorDiagnostics converts the byte edits to negotiated positions for every open overlay; a real stdio session returned the extra.hpp -> renamed.hpp edit.
This commit is contained in:
1 parent
158125ace5
commit
b0b48d961c
12 files changed
+277
-5
No files matched your search
+25
-1
@@ -298,7 +298,8 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
{"workspaceFolders", Json{std::move(folders)}},
|
||||
{"fileOperations", Json{Json::Object{{"didCreate", file_operation_registration()},
|
||||
{"didRename", file_operation_registration()},
|
||||
{"didDelete", file_operation_registration()}}}}};
|
||||
{"didDelete", file_operation_registration()},
|
||||
{"willRename", file_operation_registration()}}}}};
|
||||
Json::Object capabilities{
|
||||
{"positionEncoding",
|
||||
Json{std::string{Void::LanguageServer::Documents::encoding_name(encoding)}}},
|
||||
@@ -431,6 +432,29 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
}
|
||||
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
|
||||
}
|
||||
if (*method == "workspace/willRenameFiles") {
|
||||
if (!diagnostics_)
|
||||
return error(id, -32601, "Method not found");
|
||||
const auto *files = as_array(member(params, "files"));
|
||||
if (!files || files->size() > 4096)
|
||||
return error(id, -32602, "Expected a bounded files array");
|
||||
std::vector<semantic::EditorDiagnostics::RenamedFile> renames;
|
||||
for (const auto &item : *files) {
|
||||
const auto *fields = as_object(&item);
|
||||
const auto *old_uri = fields ? as_string(find_member(*fields, "oldUri")) : nullptr;
|
||||
const auto *new_uri = fields ? as_string(find_member(*fields, "newUri")) : nullptr;
|
||||
if (!old_uri || !new_uri)
|
||||
return error(id, -32602, "Expected oldUri and newUri");
|
||||
const auto old_identity = Void::LanguageServer::Documents::normalize_uri(*old_uri);
|
||||
const auto new_identity = Void::LanguageServer::Documents::normalize_uri(*new_uri);
|
||||
if (!old_identity || !new_identity || !old_identity->is_file_uri ||
|
||||
!new_identity->is_file_uri)
|
||||
return error(id, -32602, "Expected file URIs in the rename list");
|
||||
renames.push_back(
|
||||
{old_identity->canonical_path.native(), new_identity->canonical_path.native()});
|
||||
}
|
||||
return reply(id, "result", diagnostics_->renamed_files(renames));
|
||||
}
|
||||
if (*method == "textDocument/inlayHint") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#include <vls/features/IncludeUpdate.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
|
||||
namespace vls::features::include_update {
|
||||
namespace fs = std::filesystem;
|
||||
namespace {
|
||||
bool blank(std::string_view text) {
|
||||
return text.find_first_not_of(" \t") == std::string_view::npos;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<Edit> include_edits(std::string_view text, std::string_view document_path,
|
||||
std::string_view old_path, std::string_view new_path,
|
||||
const Limits &limits) {
|
||||
std::vector<Edit> edits;
|
||||
if (document_path.size() > limits.pathBytes || old_path.size() > limits.pathBytes ||
|
||||
new_path.size() > limits.pathBytes || old_path.empty() || new_path.empty())
|
||||
return edits;
|
||||
const fs::path directory = fs::path{document_path}.parent_path();
|
||||
const fs::path old_target = fs::path{old_path}.lexically_normal();
|
||||
const fs::path new_target = fs::path{new_path}.lexically_normal();
|
||||
const auto stop = std::min(text.size(), limits.scanBytes);
|
||||
std::size_t offset{};
|
||||
while (offset < stop) {
|
||||
auto end = text.find('\n', offset);
|
||||
if (end == std::string_view::npos || end > stop)
|
||||
end = stop;
|
||||
auto line = text.substr(offset, end - offset);
|
||||
const auto first = line.find_first_not_of(" \t");
|
||||
if (first != std::string_view::npos) {
|
||||
line.remove_prefix(first);
|
||||
if (line.starts_with("#include")) {
|
||||
auto rest = line.substr(8);
|
||||
const auto space = rest.find_first_not_of(" \t");
|
||||
if (space != std::string_view::npos && rest[space] == '"') {
|
||||
const auto open = offset + first + 8 + space + 1;
|
||||
const auto close = text.find('"', open);
|
||||
if (close != std::string_view::npos && close < end) {
|
||||
const auto spelling = text.substr(open, close - open);
|
||||
const fs::path include{spelling};
|
||||
const auto resolved = include.is_absolute()
|
||||
? include.lexically_normal()
|
||||
: (directory / include).lexically_normal();
|
||||
if (resolved == old_target) {
|
||||
std::string replacement;
|
||||
if (include.is_absolute())
|
||||
replacement = new_target.native();
|
||||
else {
|
||||
auto relative = new_target.lexically_relative(directory);
|
||||
if (relative.empty() || relative.native().starts_with(".."))
|
||||
relative = new_target;
|
||||
replacement = relative.native();
|
||||
if (spelling.starts_with("./") && !replacement.starts_with("./"))
|
||||
replacement = "./" + replacement;
|
||||
}
|
||||
if (replacement.size() > limits.pathBytes)
|
||||
return edits;
|
||||
edits.push_back({open, close, std::move(replacement)});
|
||||
if (edits.size() == limits.edits)
|
||||
return edits;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
static_cast<void>(blank(rest));
|
||||
}
|
||||
}
|
||||
}
|
||||
offset = end + 1;
|
||||
}
|
||||
return edits;
|
||||
}
|
||||
|
||||
} // namespace vls::features::include_update
|
||||
@@ -708,6 +708,38 @@ void EditorDiagnostics::files_changed(std::span<const features::file_operations:
|
||||
}
|
||||
}
|
||||
|
||||
Json EditorDiagnostics::renamed_files(std::span<const RenamedFile> renames) const {
|
||||
auto &s = *state_;
|
||||
Json::Object changes;
|
||||
for (const auto &overlay : s.documents.list_overlays()) {
|
||||
if (!overlay || !overlay->identity.is_file_uri)
|
||||
continue;
|
||||
for (const auto &rename : renames) {
|
||||
const auto edits = features::include_update::include_edits(
|
||||
overlay->text, overlay->identity.canonical_path.native(), rename.old_path,
|
||||
rename.new_path);
|
||||
if (edits.empty())
|
||||
continue;
|
||||
auto &list = changes[overlay->identity.display_uri];
|
||||
if (!std::holds_alternative<Json::Array>(list.value))
|
||||
list = Json{Json::Array{}};
|
||||
auto &array = std::get<Json::Array>(list.value);
|
||||
for (const auto &edit : edits) {
|
||||
const auto start = overlay->offset_to_position(edit.start);
|
||||
const auto end = overlay->offset_to_position(edit.end);
|
||||
if (!start || !end)
|
||||
continue;
|
||||
array.push_back(
|
||||
Json{Json::Object{{"range", range_json(Documents::Range{*start, *end})},
|
||||
{"newText", Json{edit.replacement}}}});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changes.empty())
|
||||
return Json{nullptr};
|
||||
return Json{Json::Object{{"changes", Json{std::move(changes)}}}};
|
||||
}
|
||||
|
||||
void EditorDiagnostics::finish_update(bool workspace_changed) {
|
||||
auto &s = *state_;
|
||||
auto overlays = s.documents.list_overlays();
|
||||
|
||||
Reference in new issue
Block a user