Files
Language-Server/src/features/SignatureHelp.cpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

259 lines
8.1 KiB
C++

#include <vls/features/SignatureHelp.hpp>
#include <algorithm>
#include <utility>
namespace vls::features::signature {
namespace {
bool word(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') ||
ch == '_' || ch == ':';
}
// Opens an angle bracket only where a template argument list plausibly starts:
// after a name, '>' or ':' and not before '=' or '<' (operators).
bool opens_angle(std::string_view text, std::size_t index) {
if (index == 0 || index + 1 >= text.size())
return false;
const char previous = text[index - 1];
const char next = text[index + 1];
return (word(previous) || previous == '>' || previous == '*') && next != '=' && next != '<';
}
std::string bounded(std::string value, std::size_t limit) {
if (value.size() > limit)
value.resize(limit);
return value;
}
std::string trim(std::string_view text) {
const auto first = text.find_first_not_of(" \t\r\n");
if (first == std::string_view::npos)
return {};
const auto last = text.find_last_not_of(" \t\r\n");
return std::string{text.substr(first, last - first + 1)};
}
} // namespace
Signature parse(std::string_view signature, const Limits &limits) {
Signature result;
result.label = bounded(std::string{signature}, limits.labelBytes);
const auto open = signature.find('(');
if (open == std::string_view::npos)
return result;
const auto close = signature.find_last_of(')');
if (close == std::string_view::npos || close < open)
return result;
const auto list = signature.substr(open + 1, close - open - 1);
if (trim(list).empty())
return result;
int paren{}, square{}, brace{}, angle{};
std::size_t start{};
const auto emit = [&](std::size_t end) {
const auto piece = list.substr(start, end - start);
const auto cleaned = trim(piece);
if (cleaned.empty())
return;
const auto first = piece.find(cleaned.front());
Parameter parameter;
parameter.label = bounded(cleaned, limits.parameterBytes);
if (first != std::string_view::npos && first < limits.labelBytes) {
const auto begin = static_cast<std::uint32_t>(open + 1 + start + first);
parameter.range =
std::pair{begin, static_cast<std::uint32_t>(std::min<std::size_t>(
begin + parameter.label.size(), limits.labelBytes))};
}
if (result.parameters.size() < limits.parameters)
result.parameters.push_back(std::move(parameter));
};
for (std::size_t index = 0; index != list.size(); ++index) {
const char ch = list[index];
switch (ch) {
case '(':
++paren;
break;
case ')':
paren = std::max(0, paren - 1);
break;
case '[':
++square;
break;
case ']':
square = std::max(0, square - 1);
break;
case '{':
++brace;
break;
case '}':
brace = std::max(0, brace - 1);
break;
case '<':
if (opens_angle(list, index))
++angle;
break;
case '>':
angle = std::max(0, angle - 1);
break;
case ',':
if (paren == 0 && square == 0 && brace == 0 && angle == 0) {
emit(index);
start = index + 1;
}
break;
default:
break;
}
}
emit(list.size());
return result;
}
std::uint32_t active_parameter(std::string_view text, std::size_t offset, const Limits &limits) {
if (offset > text.size())
offset = text.size();
const std::size_t lower = offset > limits.scanBytes ? offset - limits.scanBytes : 0;
std::size_t open{};
bool found{};
int depth{};
for (std::size_t index = offset; index > lower; --index) {
const char ch = text[index - 1];
if (ch == ')')
++depth;
else if (ch == '(') {
if (depth == 0) {
open = index - 1;
found = true;
break;
}
--depth;
}
}
if (!found)
return 0;
std::uint32_t commas{};
int paren{}, square{}, brace{};
char quote{'\0'};
bool line_comment{};
bool block_comment{};
for (std::size_t index = open + 1; index < offset && index < open + 1 + limits.scanBytes;
++index) {
const char ch = text[index];
const char next = index + 1 < offset ? text[index + 1] : '\0';
if (line_comment) {
if (ch == '\n')
line_comment = false;
continue;
}
if (block_comment) {
if (ch == '*' && next == '/') {
block_comment = false;
++index;
}
continue;
}
if (quote != '\0') {
if (ch == '\\')
++index;
else if (ch == quote)
quote = '\0';
continue;
}
if (ch == '/' && next == '/') {
line_comment = true;
++index;
} else if (ch == '/' && next == '*') {
block_comment = true;
++index;
} else if (ch == '"' || ch == '\'') {
quote = ch;
} else if (ch == '(') {
++paren;
} else if (ch == ')') {
paren = std::max(0, paren - 1);
} else if (ch == '[') {
++square;
} else if (ch == ']') {
square = std::max(0, square - 1);
} else if (ch == '{') {
++brace;
} else if (ch == '}') {
brace = std::max(0, brace - 1);
} else if (ch == ',' && paren == 0 && square == 0 && brace == 0) {
++commas;
}
}
return commas;
}
bool in_call(std::string_view text, std::size_t use_start, std::size_t use_end, std::size_t offset,
const Limits &limits) {
if (offset > text.size() || use_end > offset || use_start > use_end)
return false;
const auto open = text.find('(', use_end);
if (open == std::string_view::npos || open >= offset)
return false;
const auto stop = std::min({text.size(), offset, open + 1 + limits.scanBytes});
int depth{};
char quote{'\0'};
bool line_comment{};
bool block_comment{};
for (std::size_t index = open; index < stop; ++index) {
const char ch = text[index];
const char next = index + 1 < stop ? text[index + 1] : '\0';
if (line_comment) {
if (ch == '\n')
line_comment = false;
continue;
}
if (block_comment) {
if (ch == '*' && next == '/') {
block_comment = false;
++index;
}
continue;
}
if (quote != '\0') {
if (ch == '\\')
++index;
else if (ch == quote)
quote = '\0';
continue;
}
if (ch == '/' && next == '/') {
line_comment = true;
++index;
} else if (ch == '/' && next == '*') {
block_comment = true;
++index;
} else if (ch == '"' || ch == '\'') {
quote = ch;
} else if (ch == '(') {
++depth;
} else if (ch == ')') {
if (--depth == 0)
return false;
}
}
return depth > 0;
}
Result build(std::span<const std::string> signatures, std::string_view text, std::size_t offset,
const Limits &limits) {
Result result;
for (const auto &candidate : signatures) {
if (result.signatures.size() >= limits.signatures)
break;
if (candidate.empty())
continue;
auto parsed = parse(candidate, limits);
if (parsed.label.empty())
continue;
result.signatures.push_back(std::move(parsed));
}
if (!result.signatures.empty())
result.active_parameter = active_parameter(text, offset, limits);
return result;
}
} // namespace vls::features::signature