#include #include #include 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(open + 1 + start + first); parameter.range = std::pair{begin, static_cast(std::min( 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 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