Serve document links for quoted includes
A bounded core resolves quoted include directives from the document directory and reports the spelling range; the server advertises documentLinkProvider and serves textDocument/documentLink from snapshots, keeping only targets that exist on disk. Angle includes are skipped because their search roots are unknown. Registered feature tests cover resolution, indentation, line counting, skipping and bounds, and the exact-capability-set assertion was updated. A real stdio session linked extra.hpp and skipped <vector> and a missing file.
This commit is contained in:
1 parent
a5b9f34c9e
commit
62d6ad01e8
13 files changed
+224
-5
No files matched your search
@@ -2,11 +2,14 @@
|
||||
|
||||
#include <vls/DocumentSync.hpp>
|
||||
#include <vls/Json.hpp>
|
||||
#include <vls/features/DocumentLinks.hpp>
|
||||
#include <vls/semantic/EditorDiagnostics.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
@@ -305,6 +308,8 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
Json{std::string{Void::LanguageServer::Documents::encoding_name(encoding)}}},
|
||||
{"textDocumentSync", Json{std::move(sync)}},
|
||||
{"workspace", Json{std::move(workspace)}}};
|
||||
capabilities.emplace("documentLinkProvider",
|
||||
Json{Json::Object{{"resolveProvider", Json{false}}}});
|
||||
if (diagnostics_) {
|
||||
capabilities.emplace("documentSymbolProvider", Json{true});
|
||||
capabilities.emplace("selectionRangeProvider", Json{true});
|
||||
@@ -441,6 +446,33 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
}
|
||||
return reply(id, "result", diagnostics_->selection_ranges(*uri, selected));
|
||||
}
|
||||
if (*method == "textDocument/documentLink") {
|
||||
const auto *uri = as_string(member(member(params, "textDocument"), "uri"));
|
||||
if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri))
|
||||
return error(id, -32602, "Expected textDocument.uri");
|
||||
const auto snapshot = documents_.snapshot(*uri);
|
||||
if (!snapshot)
|
||||
return reply(id, "result", Json{Json::Array{}});
|
||||
const auto links = features::document_links::includes(
|
||||
snapshot->text, snapshot->identity.canonical_path.native());
|
||||
Json::Array result;
|
||||
for (const auto &link : links) {
|
||||
std::error_code error;
|
||||
if (!std::filesystem::exists(link.path, error))
|
||||
continue;
|
||||
const auto point = [](Void::LanguageServer::Documents::Position value) {
|
||||
return Json{
|
||||
Json::Object{{"line", Json{static_cast<std::int64_t>(value.line)}},
|
||||
{"character", Json{static_cast<std::int64_t>(value.character)}}}};
|
||||
};
|
||||
result.push_back(Json{Json::Object{
|
||||
{"range", Json{Json::Object{
|
||||
{"start", point({link.range.start.line, link.range.start.character})},
|
||||
{"end", point({link.range.end.line, link.range.end.character})}}}},
|
||||
{"target", Json{Void::LanguageServer::Documents::path_to_file_uri(link.path)}}}});
|
||||
}
|
||||
return reply(id, "result", Json{std::move(result)});
|
||||
}
|
||||
if (*method == "textDocument/codeLens") {
|
||||
if (!diagnostics_ || !diagnostics_->supports_occurrences())
|
||||
return error(id, -32601, "Method not found");
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <vls/features/DocumentLinks.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
|
||||
namespace vls::features::document_links {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
std::vector<Link> includes(std::string_view text, std::string_view document_path,
|
||||
const Limits &limits) {
|
||||
std::vector<Link> links;
|
||||
if (document_path.size() > limits.pathBytes)
|
||||
return links;
|
||||
const fs::path directory = fs::path{document_path}.parent_path();
|
||||
const auto stop = std::min(text.size(), limits.scanBytes);
|
||||
std::size_t offset{};
|
||||
std::uint32_t line_number{};
|
||||
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")) {
|
||||
const 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);
|
||||
if (!spelling.empty() && spelling.size() <= limits.pathBytes) {
|
||||
const fs::path include{spelling};
|
||||
const auto resolved = include.is_absolute()
|
||||
? include.lexically_normal()
|
||||
: (directory / include).lexically_normal();
|
||||
const auto start = offset + first + 8 + space + 1;
|
||||
links.push_back(
|
||||
{{{line_number, static_cast<std::uint32_t>(start - offset)},
|
||||
{line_number, static_cast<std::uint32_t>(close - offset)}},
|
||||
resolved.native()});
|
||||
if (links.size() == limits.links)
|
||||
return links;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
offset = end + 1;
|
||||
++line_number;
|
||||
}
|
||||
return links;
|
||||
}
|
||||
|
||||
} // namespace vls::features::document_links
|
||||
Reference in new issue
Block a user