Add support for hidden options, not showed by completion

the decl command can take a -hidden parameter to hide an option
This commit is contained in:
Maxime Coste 2013-11-12 19:21:07 +00:00
parent c4de866903
commit 5a4650aecc
5 changed files with 66 additions and 34 deletions

View File

@ -398,9 +398,12 @@ window scope (+BOM+ or +eolformat+ for example).
New options can be declared using the +:decl+ command: New options can be declared using the +:decl+ command:
----------------------------- ---------------------------------------
:decl <type> <name> [<value>] :decl [-hidden] <type> <name> [<value>]
----------------------------- ---------------------------------------
the +-hidden+ parameter makes the option invisible in completion, but
still modifiable.
Some options are built in kakoune, and can be used to control it's behaviour: Some options are built in kakoune, and can be used to control it's behaviour:

View File

@ -458,29 +458,36 @@ void set_option(CommandParameters params, Context& context)
void declare_option(CommandParameters params, Context& context) void declare_option(CommandParameters params, Context& context)
{ {
if (params.size() != 2 and params.size() != 3) ParametersParser parser(params, { { "hidden", false } },
throw wrong_argument_count(); ParametersParser::Flags::OptionsOnlyAtStart,
2, 3);
Option* opt = nullptr; Option* opt = nullptr;
if (params[0] == "int") Option::Flags flags = Option::Flags::None;
opt = &GlobalOptions::instance().declare_option<int>(params[1], 0); if (parser.has_option("hidden"))
if (params[0] == "bool") flags = Option::Flags::Hidden;
opt = &GlobalOptions::instance().declare_option<bool>(params[1], 0);
else if (params[0] == "str")
opt = &GlobalOptions::instance().declare_option<String>(params[1], "");
else if (params[0] == "regex")
opt = &GlobalOptions::instance().declare_option<Regex>(params[1], Regex{});
else if (params[0] == "int-list")
opt = &GlobalOptions::instance().declare_option<std::vector<int>>(params[1], {});
else if (params[0] == "str-list")
opt = &GlobalOptions::instance().declare_option<std::vector<String>>(params[1], {});
else if (params[0] == "line-flag-list")
opt = &GlobalOptions::instance().declare_option<std::vector<LineAndFlag>>(params[1], {});
else
throw runtime_error("unknown type " + params[0]);
if (params.size() == 3) GlobalOptions& opts = GlobalOptions::instance();
opt->set_from_string(params[2]);
if (parser[0] == "int")
opt = &opts.declare_option<int>(parser[1], 0, flags);
if (parser[0] == "bool")
opt = &opts.declare_option<bool>(parser[1], 0, flags);
else if (parser[0] == "str")
opt = &opts.declare_option<String>(parser[1], "", flags);
else if (parser[0] == "regex")
opt = &opts.declare_option<Regex>(parser[1], Regex{}, flags);
else if (parser[0] == "int-list")
opt = &opts.declare_option<std::vector<int>>(parser[1], {}, flags);
else if (parser[0] == "str-list")
opt = &opts.declare_option<std::vector<String>>(parser[1], {}, flags);
else if (parser[0] == "line-flag-list")
opt = &opts.declare_option<std::vector<LineAndFlag>>(parser[1], {}, flags);
else
throw runtime_error("unknown type " + parser[0]);
if (parser.positional_count() == 3)
opt->set_from_string(parser[2]);
} }

View File

@ -7,8 +7,8 @@
namespace Kakoune namespace Kakoune
{ {
Option::Option(OptionManager& manager, String name) Option::Option(OptionManager& manager, String name, Flags flags)
: m_manager(manager), m_name(std::move(name)) {} : m_manager(manager), m_name(std::move(name)), m_flags(flags) {}
OptionManager::OptionManager(OptionManager& parent) OptionManager::OptionManager(OptionManager& parent)
: m_parent(&parent) : m_parent(&parent)
@ -72,6 +72,9 @@ CandidateList OptionManager::complete_option_name(const String& prefix,
result = m_parent->complete_option_name(prefix, cursor_pos); result = m_parent->complete_option_name(prefix, cursor_pos);
for (auto& option : m_options) for (auto& option : m_options)
{ {
if (option->flags() & Option::Flags::Hidden)
continue;
const auto& name = option->name(); const auto& name = option->name();
if (prefix_match(name, real_prefix) and not contains(result, name)) if (prefix_match(name, real_prefix) and not contains(result, name))
result.push_back(name); result.push_back(name);
@ -123,6 +126,7 @@ GlobalOptions::GlobalOptions()
declare_option<std::vector<String>>("completions", {}); declare_option<std::vector<String>>("completions", {});
declare_option<std::vector<String>>("path", { "./", "/usr/include" }); declare_option<std::vector<String>>("path", { "./", "/usr/include" });
declare_option<std::vector<String>>("completers", {"option", "filename", "word=buffer"}, declare_option<std::vector<String>>("completers", {"option", "filename", "word=buffer"},
Option::Flags::None,
[](const std::vector<String>& s) { [](const std::vector<String>& s) {
static const auto values = {"option", "word=buffer", "word=all", "filename" }; static const auto values = {"option", "word=buffer", "word=all", "filename" };
for (auto& v : s) for (auto& v : s)

View File

@ -22,7 +22,13 @@ class OptionManager;
class Option : public SafeCountable class Option : public SafeCountable
{ {
public: public:
Option(OptionManager& manager, String name); enum class Flags
{
None = 0,
Hidden = 1,
};
Option(OptionManager& manager, String name, Flags flags);
virtual ~Option() {} virtual ~Option() {}
template<typename T> const T& get() const; template<typename T> const T& get() const;
@ -37,9 +43,19 @@ public:
OptionManager& manager() const { return m_manager; } OptionManager& manager() const { return m_manager; }
virtual Option* clone(OptionManager& manager) const = 0; virtual Option* clone(OptionManager& manager) const = 0;
Flags flags() const { return m_flags; }
friend constexpr Flags operator|(Flags lhs, Flags rhs)
{ return (Flags)((int)lhs | (int)rhs); }
friend constexpr bool operator&(Flags lhs, Flags rhs)
{ return (bool)((int)lhs & (int)rhs); }
protected: protected:
OptionManager& m_manager; OptionManager& m_manager;
String m_name; String m_name;
Flags m_flags;
}; };
class OptionManagerWatcher class OptionManagerWatcher
@ -87,9 +103,9 @@ template<typename T>
class TypedOption : public Option class TypedOption : public Option
{ {
public: public:
TypedOption(OptionManager& manager, String name, const T& value, TypedOption(OptionManager& manager, String name, Option::Flags flags,
OptionChecker<T> checker) const T& value, OptionChecker<T> checker)
: Option(manager, std::move(name)), m_value(value), : Option(manager, std::move(name), flags), m_value(value),
m_checker(std::move(checker)) {} m_checker(std::move(checker)) {}
void set(T value) void set(T value)
@ -126,7 +142,7 @@ public:
Option* clone(OptionManager& manager) const override Option* clone(OptionManager& manager) const override
{ {
return new TypedOption{manager, name(), m_value, m_checker}; return new TypedOption{manager, name(), flags(), m_value, m_checker};
} }
private: private:
T m_value; T m_value;
@ -169,16 +185,18 @@ public:
template<typename T> template<typename T>
Option& declare_option(const String& name, const T& value, Option& declare_option(const String& name, const T& value,
Option::Flags flags = Option::Flags::None,
OptionChecker<T> checker = OptionChecker<T>{}) OptionChecker<T> checker = OptionChecker<T>{})
{ {
auto it = find_option(m_options, name); auto it = find_option(m_options, name);
if (it != m_options.end()) if (it != m_options.end())
{ {
if ((*it)->is_of_type<T>()) if ((*it)->is_of_type<T>() and (*it)->flags() == flags)
return **it; return **it;
throw runtime_error("option " + name + " already declared with different type"); throw runtime_error("option " + name + " already declared with different type or flags");
} }
m_options.emplace_back(new TypedOption<T>{*this, name, value, std::move(checker)}); m_options.emplace_back(new TypedOption<T>{*this, name, flags, value,
std::move(checker)});
return *m_options.back(); return *m_options.back();
} }
}; };

View File

@ -1,4 +1,4 @@
decl str clang_filename decl -hidden str clang_filename
decl str clang_options decl str clang_options
def clang-complete %{ def clang-complete %{