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:
parent
c4de866903
commit
5a4650aecc
|
@ -398,9 +398,12 @@ window scope (+BOM+ or +eolformat+ for example).
|
|||
|
||||
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:
|
||||
|
||||
|
|
|
@ -458,29 +458,36 @@ void set_option(CommandParameters params, Context& context)
|
|||
|
||||
void declare_option(CommandParameters params, Context& context)
|
||||
{
|
||||
if (params.size() != 2 and params.size() != 3)
|
||||
throw wrong_argument_count();
|
||||
ParametersParser parser(params, { { "hidden", false } },
|
||||
ParametersParser::Flags::OptionsOnlyAtStart,
|
||||
2, 3);
|
||||
Option* opt = nullptr;
|
||||
|
||||
if (params[0] == "int")
|
||||
opt = &GlobalOptions::instance().declare_option<int>(params[1], 0);
|
||||
if (params[0] == "bool")
|
||||
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]);
|
||||
Option::Flags flags = Option::Flags::None;
|
||||
if (parser.has_option("hidden"))
|
||||
flags = Option::Flags::Hidden;
|
||||
|
||||
if (params.size() == 3)
|
||||
opt->set_from_string(params[2]);
|
||||
GlobalOptions& opts = GlobalOptions::instance();
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
namespace Kakoune
|
||||
{
|
||||
|
||||
Option::Option(OptionManager& manager, String name)
|
||||
: m_manager(manager), m_name(std::move(name)) {}
|
||||
Option::Option(OptionManager& manager, String name, Flags flags)
|
||||
: m_manager(manager), m_name(std::move(name)), m_flags(flags) {}
|
||||
|
||||
OptionManager::OptionManager(OptionManager& 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);
|
||||
for (auto& option : m_options)
|
||||
{
|
||||
if (option->flags() & Option::Flags::Hidden)
|
||||
continue;
|
||||
|
||||
const auto& name = option->name();
|
||||
if (prefix_match(name, real_prefix) and not contains(result, name))
|
||||
result.push_back(name);
|
||||
|
@ -123,6 +126,7 @@ GlobalOptions::GlobalOptions()
|
|||
declare_option<std::vector<String>>("completions", {});
|
||||
declare_option<std::vector<String>>("path", { "./", "/usr/include" });
|
||||
declare_option<std::vector<String>>("completers", {"option", "filename", "word=buffer"},
|
||||
Option::Flags::None,
|
||||
[](const std::vector<String>& s) {
|
||||
static const auto values = {"option", "word=buffer", "word=all", "filename" };
|
||||
for (auto& v : s)
|
||||
|
|
|
@ -22,7 +22,13 @@ class OptionManager;
|
|||
class Option : public SafeCountable
|
||||
{
|
||||
public:
|
||||
Option(OptionManager& manager, String name);
|
||||
enum class Flags
|
||||
{
|
||||
None = 0,
|
||||
Hidden = 1,
|
||||
};
|
||||
|
||||
Option(OptionManager& manager, String name, Flags flags);
|
||||
virtual ~Option() {}
|
||||
|
||||
template<typename T> const T& get() const;
|
||||
|
@ -37,9 +43,19 @@ public:
|
|||
OptionManager& manager() const { return m_manager; }
|
||||
|
||||
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:
|
||||
OptionManager& m_manager;
|
||||
String m_name;
|
||||
Flags m_flags;
|
||||
};
|
||||
|
||||
class OptionManagerWatcher
|
||||
|
@ -87,9 +103,9 @@ template<typename T>
|
|||
class TypedOption : public Option
|
||||
{
|
||||
public:
|
||||
TypedOption(OptionManager& manager, String name, const T& value,
|
||||
OptionChecker<T> checker)
|
||||
: Option(manager, std::move(name)), m_value(value),
|
||||
TypedOption(OptionManager& manager, String name, Option::Flags flags,
|
||||
const T& value, OptionChecker<T> checker)
|
||||
: Option(manager, std::move(name), flags), m_value(value),
|
||||
m_checker(std::move(checker)) {}
|
||||
|
||||
void set(T value)
|
||||
|
@ -126,7 +142,7 @@ public:
|
|||
|
||||
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:
|
||||
T m_value;
|
||||
|
@ -169,16 +185,18 @@ public:
|
|||
|
||||
template<typename T>
|
||||
Option& declare_option(const String& name, const T& value,
|
||||
Option::Flags flags = Option::Flags::None,
|
||||
OptionChecker<T> checker = OptionChecker<T>{})
|
||||
{
|
||||
auto it = find_option(m_options, name);
|
||||
if (it != m_options.end())
|
||||
{
|
||||
if ((*it)->is_of_type<T>())
|
||||
if ((*it)->is_of_type<T>() and (*it)->flags() == flags)
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
decl str clang_filename
|
||||
decl -hidden str clang_filename
|
||||
decl str clang_options
|
||||
|
||||
def clang-complete %{
|
||||
|
|
Loading…
Reference in New Issue
Block a user