• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 12967345409

25 Jan 2025 06:18PM UTC coverage: 78.707% (-0.7%) from 79.433%
12967345409

Pull #508

github

web-flow
Merge 9dc6a4590 into 40f0b6abe
Pull Request #508: Tests for repl

0 of 7 new or added lines in 1 file covered. (0.0%)

5781 of 7345 relevant lines covered (78.71%)

14900.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.58
/src/arkscript/REPL/Utils.cpp
1
#include <CLI/REPL/Utils.hpp>
2

3
#include <regex>
4
#include <algorithm>
5
#include <numeric>
6
#include <ranges>
7

8
namespace Ark::internal
9
{
10
    long countOpenEnclosures(const std::string& line, const char open, const char close)
6✔
11
    {
6✔
12
        return std::ranges::count(line, open) - std::ranges::count(line, close);
6✔
13
    }
14

15
    void trimWhitespace(std::string& line)
4✔
16
    {
4✔
17
        const std::size_t string_begin = line.find_first_not_of(" \t");
4✔
18
        if (std::string::npos != string_begin)
4✔
19
        {
20
            const std::size_t string_end = line.find_last_not_of(" \t");
4✔
21
            line = line.substr(string_begin, string_end - string_begin + 1);
4✔
22
        }
4✔
23
    }
4✔
24

25
    std::size_t codepointLength(const std::string& str)
×
26
    {
×
NEW
27
        return std::accumulate(
×
NEW
28
            str.begin(),
×
NEW
29
            str.end(),
×
30
            std::size_t { 0 },
NEW
31
            [](const std::size_t acc, const char c) {
×
NEW
32
                return acc + ((c & 0xc0) != 0x80);
×
33
            });
34
    }
35

36
    std::size_t contextLen(const std::string& prefix)
×
37
    {
×
38
        const std::string word_break = " \t\n\r\v\f=+*&^%$#@!,./?<>;`~'\"[]{}()\\|";
×
39
        std::size_t count = 0;
×
40

NEW
41
        for (const auto c : std::ranges::views::reverse(prefix))
×
42
        {
NEW
43
            if (word_break.find(c) != std::string::npos)
×
44
                break;
×
45
            ++count;
×
46
        }
×
47

48
        return count;
×
49
    }
×
50

51
    replxx::Replxx::completions_t hookCompletion(const std::vector<std::string>& words, const std::string& context, int& length)
×
52
    {
×
53
        replxx::Replxx::completions_t completions;
×
54
        std::size_t utf8_context_len = contextLen(context);
×
55
        std::size_t prefix_len = context.size() - utf8_context_len;
×
56

57
        if (prefix_len > 0 && context[prefix_len - 1] == '\\')
×
58
        {
59
            --prefix_len;
×
60
            ++utf8_context_len;
×
61
        }
×
62

63
        length = static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
×
64

65
        const std::string prefix = context.substr(prefix_len);
×
66
        for (const auto& e : words)
×
67
        {
68
            if (e.starts_with(prefix) == 0)
×
69
                completions.emplace_back(e.c_str());
×
70
        }
×
71

72
        return completions;
×
73
    }
×
74

75
    void hookColor(const std::vector<std::pair<std::string, replxx::Replxx::Color>>& words_colors, const std::string& context, replxx::Replxx::colors_t& colors)
×
76
    {
×
77
        // highlight matching regex sequences
78
        for (const auto& [regex, color] : words_colors)
×
79
        {
80
            std::size_t pos = 0;
×
81
            std::string str = context;
×
82
            std::smatch match;
×
83

84
            while (std::regex_search(str, match, std::regex(regex)))
×
85
            {
86
                std::string c = match[0];
×
87
                std::string prefix = match.prefix().str();
×
88
                const std::size_t len = codepointLength(c);
×
89

90
                pos += codepointLength(prefix);
×
91
                for (std::size_t i = 0; i < len; ++i)
×
92
                    colors.at(pos + i) = color;
×
93

94
                pos += len;
×
95
                str = match.suffix();
×
96
            }
×
97
        }
×
98
    }
×
99

100
    replxx::Replxx::hints_t hookHint(const std::vector<std::string>& words, const std::string& context, int& length, replxx::Replxx::Color& color)
×
101
    {
×
102
        replxx::Replxx::hints_t hints;
×
103
        // only show hint if prefix is at least 'n' chars long
104
        // or if prefix begins with a specific character
105
        const std::size_t utf8_context_len = contextLen(context);
×
106
        const std::size_t prefix_len = context.size() - utf8_context_len;
×
107
        length = static_cast<int>(codepointLength(context.substr(prefix_len, utf8_context_len)));
×
108
        const std::string prefix = context.substr(prefix_len);
×
109

110
        if (prefix.size() >= 2 || (!prefix.empty() && prefix.at(0) == '.'))
×
111
        {
112
            for (const auto& e : words)
×
113
            {
114
                if (e.compare(0, prefix.size(), prefix) == 0)
×
115
                    hints.emplace_back(e.c_str());
×
116
            }
×
117
        }
×
118

119
        if (hints.size() == 1)
×
120
            color = replxx::Replxx::Color::GREEN;
×
121

122
        return hints;
×
123
    }
×
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc