blob: a95dafcd461991d41e12d23091e25f3bfda556cb [file] [log] [blame]
Václav Kubernátcb3af402020-02-12 16:49:17 +01001/*
2 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
7#include <set>
8#include <string>
9struct Completion {
10 enum class WhenToAdd {
11 Always,
12 IfFullMatch
13 };
14 bool operator<(const Completion& b) const;
15 bool operator==(const Completion& b) const;
16 std::string m_value;
17
18 /** A completion can have a suffix specified. This suffix is appended to the completion, if it's the only valid completion present in
19 * the ParserContext. For example, let's say there are two valid schema paths: "user/name", "user/nationality" and "user/city" and the
20 * user tries to complete this path: "user/n". The parser determines these completions: "name", "nationality", "city". The parser
21 * filters out "city", because it doesn't start with an "n", so valid completions are "name" and "nationality". Their common prefix is
22 * "na", so the input becomes "user/na". Next, the user changes the output to "user/natio", the completions will again be "name",
23 * "nationality", "city", but now, after filtering, only one single completion remains - "nationality". This is where m_suffix gets in
24 * play: since there is only one completion remaining, m_suffix can get added depending on the value of m_whenToAdd. It it's set to
25 * Always, the suffix gets appended always. That means, the actual completion would be "nationality" plus whatever m_suffix is set to.
26 * Otherwise (if m_whenToAdd is set to IfFullMatch), the suffix will only get added after the user input fully matches the completion.
27 * For example, if the user input is "user/natio", the completion becomes just "nationality", but if the user input is "user/nationality",
28 * the completion becomes "nationality" plus the suffix.
29 */
30 std::string m_suffix = "";
31 WhenToAdd m_whenToAdd = WhenToAdd::Always;
32};
33
34/** Returns a subset of the original set with only the strings starting with prefix */
35std::set<Completion> filterByPrefix(const std::set<Completion>& set, const std::string_view prefix);