10 return std::ranges::count(line, open) - std::ranges::count(line, close);
15 const std::size_t string_begin = line.find_first_not_of(
" \t");
16 if (std::string::npos != string_begin)
18 const std::size_t string_end = line.find_last_not_of(
" \t");
19 line = line.substr(string_begin, string_end - string_begin + 1);
26 for (
const auto c : str)
27 len += (c & 0xc0) != 0x80;
33 const std::string word_break =
" \t\n\r\v\f=+*&^%$#@!,./?<>;`~'\"[]{}()\\|";
34 long i =
static_cast<long>(prefix.size()) - 1;
35 std::size_t count = 0;
39 if (word_break.find(prefix[
static_cast<std::size_t
>(i)]) != std::string::npos)
49 replxx::Replxx::completions_t
hookCompletion(
const std::vector<std::string>& words,
const std::string& context,
int& length)
51 replxx::Replxx::completions_t completions;
52 std::size_t utf8_context_len =
contextLen(context);
53 std::size_t prefix_len = context.size() - utf8_context_len;
55 if (prefix_len > 0 && context[prefix_len - 1] ==
'\\')
61 length =
static_cast<int>(
codepointLength(context.substr(prefix_len, utf8_context_len)));
63 const std::string prefix = context.substr(prefix_len);
64 for (
const auto& e : words)
66 if (e.starts_with(prefix) == 0)
67 completions.emplace_back(e.c_str());
73 void hookColor(
const std::vector<std::pair<std::string, replxx::Replxx::Color>>& words_colors,
const std::string& context, replxx::Replxx::colors_t&
colors)
76 for (
const auto& [regex, color] : words_colors)
79 std::string str = context;
82 while (std::regex_search(str, match, std::regex(regex)))
84 std::string c = match[0];
85 std::string prefix = match.prefix().str();
89 for (std::size_t i = 0; i < len; ++i)
90 colors.at(pos + i) = color;
98 replxx::Replxx::hints_t
hookHint(
const std::vector<std::string>& words,
const std::string& context,
int& length, replxx::Replxx::Color& color)
100 replxx::Replxx::hints_t hints;
103 const std::size_t utf8_context_len =
contextLen(context);
104 const std::size_t prefix_len = context.size() - utf8_context_len;
105 length =
static_cast<int>(
codepointLength(context.substr(prefix_len, utf8_context_len)));
106 const std::string prefix = context.substr(prefix_len);
108 if (prefix.size() >= 2 || (!prefix.empty() && prefix.at(0) ==
'.'))
110 for (
const auto& e : words)
112 if (e.compare(0, prefix.size(), prefix) == 0)
113 hints.emplace_back(e.c_str());
117 if (hints.size() == 1)
118 color = replxx::Replxx::Color::GREEN;
std::size_t codepointLength(const std::string &str)
std::size_t contextLen(const std::string &prefix)
long countOpenEnclosures(const std::string &line, char open, char close)
Count the open enclosure and its counterpart: (), {}, [].
void hookColor(const std::vector< std::pair< std::string, replxx::Replxx::Color > > &words_colors, const std::string &context, replxx::Replxx::colors_t &colors)
constexpr std::array colors
replxx::Replxx::completions_t hookCompletion(const std::vector< std::string > &words, const std::string &context, int &length)
replxx::Replxx::hints_t hookHint(const std::vector< std::string > &words, const std::string &context, int &length, replxx::Replxx::Color &color)
void trimWhitespace(std::string &line)
Remove whitespaces at the start and end of a string.