2012-04-03 17:39:20 +04:00
|
|
|
#ifndef option_manager_hh_INCLUDED
|
|
|
|
#define option_manager_hh_INCLUDED
|
|
|
|
|
2012-04-03 22:25:27 +04:00
|
|
|
#include "completion.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "exception.hh"
|
2017-06-23 11:54:21 +03:00
|
|
|
#include "hash_map.hh"
|
2017-08-29 11:23:03 +03:00
|
|
|
#include "option.hh"
|
|
|
|
#include "ranges.hh"
|
2017-06-23 11:54:21 +03:00
|
|
|
#include "utils.hh"
|
2017-08-29 11:23:03 +03:00
|
|
|
#include "vector.hh"
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2016-03-14 12:31:13 +03:00
|
|
|
#include <memory>
|
2016-08-06 11:05:50 +03:00
|
|
|
#include <type_traits>
|
2016-03-14 12:31:13 +03:00
|
|
|
|
2012-04-03 17:39:20 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
class OptionManager;
|
2017-05-25 10:38:11 +03:00
|
|
|
class Context;
|
2013-03-03 20:25:40 +04:00
|
|
|
|
2014-04-12 23:03:26 +04:00
|
|
|
enum class OptionFlags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Hidden = 1,
|
|
|
|
};
|
|
|
|
|
2017-03-15 20:55:34 +03:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<OptionFlags>) { return true; }
|
2014-04-12 23:03:26 +04:00
|
|
|
|
|
|
|
class OptionDesc
|
2012-04-03 17:39:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-04-12 23:03:26 +04:00
|
|
|
OptionDesc(String name, String docstring, OptionFlags flags);
|
|
|
|
|
|
|
|
const String& name() const { return m_name; }
|
|
|
|
const String& docstring() const { return m_docstring; }
|
2013-11-12 23:21:07 +04:00
|
|
|
|
2014-04-12 23:03:26 +04:00
|
|
|
OptionFlags flags() const { return m_flags; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
String m_name;
|
|
|
|
String m_docstring;
|
|
|
|
OptionFlags m_flags;
|
|
|
|
};
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2017-03-07 19:11:40 +03:00
|
|
|
class Option : public UseMemoryDomain<MemoryDomain::Options>
|
2014-04-12 23:03:26 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-04-30 22:40:23 +04:00
|
|
|
virtual ~Option() = default;
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
template<typename T> const T& get() const;
|
2015-12-12 14:45:45 +03:00
|
|
|
template<typename T> T& get_mutable();
|
2015-07-24 01:42:48 +03:00
|
|
|
template<typename T> void set(const T& val, bool notify=true);
|
2013-04-17 21:10:51 +04:00
|
|
|
template<typename T> bool is_of_type() const;
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
virtual String get_as_string() const = 0;
|
2014-11-16 23:55:36 +03:00
|
|
|
virtual void set_from_string(StringView str) = 0;
|
|
|
|
virtual void add_from_string(StringView str) = 0;
|
2017-05-25 10:38:11 +03:00
|
|
|
virtual void update(const Context& context) = 0;
|
2012-06-14 17:16:44 +04:00
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
virtual Option* clone(OptionManager& manager) const = 0;
|
2014-04-12 23:03:26 +04:00
|
|
|
OptionManager& manager() const { return m_manager; }
|
2013-11-12 23:21:07 +04:00
|
|
|
|
2014-04-12 23:03:26 +04:00
|
|
|
const String& name() const { return m_desc.name(); }
|
|
|
|
const String& docstring() const { return m_desc.docstring(); }
|
|
|
|
OptionFlags flags() const { return m_desc.flags(); }
|
2013-11-12 23:21:07 +04:00
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
protected:
|
2014-04-12 23:03:26 +04:00
|
|
|
Option(const OptionDesc& desc, OptionManager& manager);
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
OptionManager& m_manager;
|
2014-04-12 23:03:26 +04:00
|
|
|
const OptionDesc& m_desc;
|
2012-04-03 17:39:20 +04:00
|
|
|
};
|
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
class OptionManagerWatcher
|
|
|
|
{
|
|
|
|
public:
|
2013-03-03 20:25:40 +04:00
|
|
|
virtual void on_option_changed(const Option& option) = 0;
|
2012-06-14 17:16:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class OptionManager : private OptionManagerWatcher
|
2012-04-03 17:39:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-06-14 17:16:44 +04:00
|
|
|
OptionManager(OptionManager& parent);
|
2017-05-22 12:31:56 +03:00
|
|
|
~OptionManager();
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2014-11-16 23:55:36 +03:00
|
|
|
Option& operator[] (StringView name);
|
|
|
|
const Option& operator[] (StringView name) const;
|
|
|
|
Option& get_local_option(StringView name);
|
2012-06-14 17:16:44 +04:00
|
|
|
|
2015-08-10 15:54:52 +03:00
|
|
|
void unset_option(StringView name);
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2015-01-12 16:24:30 +03:00
|
|
|
using OptionList = Vector<const Option*>;
|
2013-03-03 20:25:40 +04:00
|
|
|
OptionList flatten_options() const;
|
2012-06-14 17:16:44 +04:00
|
|
|
|
2016-08-21 22:24:18 +03:00
|
|
|
void register_watcher(OptionManagerWatcher& watcher) const;
|
|
|
|
void unregister_watcher(OptionManagerWatcher& watcher) const;
|
2012-06-14 17:16:44 +04:00
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
void on_option_changed(const Option& option) override;
|
2012-04-03 17:39:20 +04:00
|
|
|
private:
|
|
|
|
OptionManager()
|
|
|
|
: m_parent(nullptr) {}
|
|
|
|
// the only one allowed to construct a root option manager
|
2014-10-30 17:00:42 +03:00
|
|
|
friend class Scope;
|
|
|
|
friend class OptionsRegistry;
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2017-06-23 11:54:21 +03:00
|
|
|
HashMap<StringView, std::unique_ptr<Option>, MemoryDomain::Options> m_options;
|
2012-04-03 17:39:20 +04:00
|
|
|
OptionManager* m_parent;
|
2012-06-14 17:16:44 +04:00
|
|
|
|
2016-08-21 22:24:18 +03:00
|
|
|
mutable Vector<OptionManagerWatcher*, MemoryDomain::Options> m_watchers;
|
2012-04-03 17:39:20 +04:00
|
|
|
};
|
|
|
|
|
2013-03-26 16:47:14 +04:00
|
|
|
template<typename T>
|
|
|
|
class TypedOption : public Option
|
|
|
|
{
|
|
|
|
public:
|
2014-08-19 21:56:11 +04:00
|
|
|
TypedOption(OptionManager& manager, const OptionDesc& desc, const T& value)
|
|
|
|
: Option(desc, manager), m_value(value) {}
|
2013-03-26 16:47:14 +04:00
|
|
|
|
2015-07-24 01:42:48 +03:00
|
|
|
void set(T value, bool notify = true)
|
2013-03-26 16:47:14 +04:00
|
|
|
{
|
2016-02-12 01:07:18 +03:00
|
|
|
validate(value);
|
2013-03-26 16:47:14 +04:00
|
|
|
if (m_value != value)
|
|
|
|
{
|
2013-05-06 15:52:41 +04:00
|
|
|
m_value = std::move(value);
|
2015-07-24 01:42:48 +03:00
|
|
|
if (notify)
|
|
|
|
manager().on_option_changed(*this);
|
2013-03-26 16:47:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const T& get() const { return m_value; }
|
2015-12-12 14:45:45 +03:00
|
|
|
T& get_mutable() { return m_value; }
|
2013-03-26 16:47:14 +04:00
|
|
|
|
|
|
|
String get_as_string() const override
|
|
|
|
{
|
|
|
|
return option_to_string(m_value);
|
|
|
|
}
|
2014-11-16 23:55:36 +03:00
|
|
|
void set_from_string(StringView str) override
|
2013-03-26 16:47:14 +04:00
|
|
|
{
|
|
|
|
T val;
|
|
|
|
option_from_string(str, val);
|
2013-05-06 15:52:41 +04:00
|
|
|
set(std::move(val));
|
2013-03-26 16:47:14 +04:00
|
|
|
}
|
2014-11-16 23:55:36 +03:00
|
|
|
void add_from_string(StringView str) override
|
2013-03-29 22:34:57 +04:00
|
|
|
{
|
2015-12-12 14:26:34 +03:00
|
|
|
if (option_add(m_value, str))
|
2013-03-29 22:34:57 +04:00
|
|
|
m_manager.on_option_changed(*this);
|
|
|
|
}
|
2017-05-25 10:38:11 +03:00
|
|
|
void update(const Context& context) override
|
|
|
|
{
|
|
|
|
option_update(m_value, context);
|
|
|
|
}
|
2013-03-26 16:47:14 +04:00
|
|
|
private:
|
2016-02-12 01:07:18 +03:00
|
|
|
virtual void validate(const T& value) const {}
|
2013-03-26 16:47:14 +04:00
|
|
|
T m_value;
|
|
|
|
};
|
|
|
|
|
2016-02-12 01:07:18 +03:00
|
|
|
template<typename T, void (*validator)(const T&)>
|
|
|
|
class TypedCheckedOption : public TypedOption<T>
|
|
|
|
{
|
|
|
|
using TypedOption<T>::TypedOption;
|
|
|
|
|
|
|
|
Option* clone(OptionManager& manager) const override
|
|
|
|
{
|
|
|
|
return new TypedCheckedOption{manager, this->m_desc, this->get()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void validate(const T& value) const override { if (validator != nullptr) validator(value); }
|
|
|
|
};
|
|
|
|
|
2013-03-26 16:47:14 +04:00
|
|
|
template<typename T> const T& Option::get() const
|
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<const TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
2015-06-01 23:15:59 +03:00
|
|
|
throw runtime_error(format("option '{}' is not of type '{}'", name(), typeid(T).name()));
|
2013-03-26 16:47:14 +04:00
|
|
|
return typed_opt->get();
|
|
|
|
}
|
|
|
|
|
2015-12-12 14:45:45 +03:00
|
|
|
template<typename T> T& Option::get_mutable()
|
|
|
|
{
|
|
|
|
return const_cast<T&>(get<T>());
|
|
|
|
}
|
|
|
|
|
2015-07-24 01:42:48 +03:00
|
|
|
template<typename T> void Option::set(const T& val, bool notify)
|
2013-03-26 16:47:14 +04:00
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
2015-06-01 23:15:59 +03:00
|
|
|
throw runtime_error(format("option '{}' is not of type '{}'", name(), typeid(T).name()));
|
2015-07-24 01:42:48 +03:00
|
|
|
return typed_opt->set(val, notify);
|
2013-03-26 16:47:14 +04:00
|
|
|
}
|
|
|
|
|
2013-04-17 21:10:51 +04:00
|
|
|
template<typename T> bool Option::is_of_type() const
|
|
|
|
{
|
|
|
|
return dynamic_cast<const TypedOption<T>*>(this) != nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-30 17:00:42 +03:00
|
|
|
class OptionsRegistry
|
2012-04-03 17:39:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-10-30 17:00:42 +03:00
|
|
|
OptionsRegistry(OptionManager& global_manager) : m_global_manager(global_manager) {}
|
2012-04-03 17:39:20 +04:00
|
|
|
|
2016-02-12 01:07:18 +03:00
|
|
|
template<typename T, void (*validator)(const T&) = nullptr>
|
2016-08-06 08:52:11 +03:00
|
|
|
Option& declare_option(StringView name, StringView docstring,
|
2014-04-09 23:14:04 +04:00
|
|
|
const T& value,
|
2014-08-19 21:56:11 +04:00
|
|
|
OptionFlags flags = OptionFlags::None)
|
2013-03-26 16:47:14 +04:00
|
|
|
{
|
2016-09-27 11:54:55 +03:00
|
|
|
auto is_not_identifier = [](char c) {
|
|
|
|
return (c < 'a' or c > 'z') and
|
|
|
|
(c < 'A' or c > 'Z') and
|
|
|
|
(c < '0' or c > '9') and c != '_';
|
|
|
|
};
|
|
|
|
|
|
|
|
if (contains_that(name, is_not_identifier))
|
|
|
|
throw runtime_error{format("name '{}' contains char out of [a-zA-Z0-9_]", name)};
|
|
|
|
|
2014-10-30 17:00:42 +03:00
|
|
|
auto& opts = m_global_manager.m_options;
|
2017-06-23 11:54:21 +03:00
|
|
|
auto it = opts.find(name);
|
2014-10-30 17:00:42 +03:00
|
|
|
if (it != opts.end())
|
2013-04-17 21:10:51 +04:00
|
|
|
{
|
2017-06-23 11:54:21 +03:00
|
|
|
if (it->value->is_of_type<T>() and it->value->flags() == flags)
|
|
|
|
return *it->value;
|
2016-09-27 11:54:55 +03:00
|
|
|
throw runtime_error{format("option '{}' already declared with different type or flags", name)};
|
2013-04-17 21:10:51 +04:00
|
|
|
}
|
2017-03-15 20:42:02 +03:00
|
|
|
String doc = docstring.empty() ? format("[{}]", option_type_name(Meta::Type<T>{}))
|
|
|
|
: format("[{}] - {}", option_type_name(Meta::Type<T>{}), docstring);
|
2016-08-07 12:47:26 +03:00
|
|
|
m_descs.emplace_back(new OptionDesc{name.str(), std::move(doc), flags});
|
2017-06-23 11:54:21 +03:00
|
|
|
return *opts.insert({m_descs.back()->name(),
|
2016-03-12 18:27:54 +03:00
|
|
|
std::make_unique<TypedCheckedOption<T, validator>>(m_global_manager, *m_descs.back(), value)});
|
2013-03-26 16:47:14 +04:00
|
|
|
}
|
2014-05-05 15:55:04 +04:00
|
|
|
|
2015-08-10 15:53:30 +03:00
|
|
|
const OptionDesc* option_desc(StringView name) const
|
2014-05-05 15:55:04 +04:00
|
|
|
{
|
2015-08-10 15:53:30 +03:00
|
|
|
auto it = find_if(m_descs,
|
2016-09-27 00:59:02 +03:00
|
|
|
[&name](const std::unique_ptr<const OptionDesc>& opt)
|
2015-08-10 15:53:30 +03:00
|
|
|
{ return opt->name() == name; });
|
|
|
|
return it != m_descs.end() ? it->get() : nullptr;
|
2014-05-05 15:55:04 +04:00
|
|
|
}
|
2015-08-10 15:38:06 +03:00
|
|
|
|
2015-08-10 15:53:30 +03:00
|
|
|
bool option_exists(StringView name) const { return option_desc(name) != nullptr; }
|
|
|
|
|
2015-08-10 15:38:06 +03:00
|
|
|
CandidateList complete_option_name(StringView prefix, ByteCount cursor_pos) const;
|
2014-04-12 23:03:26 +04:00
|
|
|
private:
|
2014-10-30 17:00:42 +03:00
|
|
|
OptionManager& m_global_manager;
|
2016-09-27 00:59:02 +03:00
|
|
|
Vector<std::unique_ptr<const OptionDesc>, MemoryDomain::Options> m_descs;
|
2013-03-03 20:25:40 +04:00
|
|
|
};
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_manager_hh_INCLUDED
|