2012-04-03 17:39:20 +04:00
|
|
|
#include "option_manager.hh"
|
2012-06-14 17:16:44 +04:00
|
|
|
#include "assert.hh"
|
2012-04-03 17:39:20 +04:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-03-05 22:15:38 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
String option_to_string(const String& opt) { return opt; }
|
|
|
|
void option_from_string(const String& str, String& opt) { opt = str; }
|
|
|
|
|
|
|
|
String option_to_string(int opt) { return int_to_str(opt); }
|
|
|
|
void option_from_string(const String& str, int& opt) { opt = str_to_int(str); }
|
|
|
|
|
|
|
|
String option_to_string(bool opt) { return opt ? "true" : "false"; }
|
|
|
|
void option_from_string(const String& str, bool& opt)
|
|
|
|
{
|
|
|
|
if (str == "true" or str == "yes")
|
|
|
|
opt = true;
|
|
|
|
else if (str == "false" or str == "no")
|
|
|
|
opt = false;
|
|
|
|
else
|
|
|
|
throw runtime_error("boolean values are either true, yes, false or no");
|
|
|
|
}
|
|
|
|
|
2013-03-06 17:12:56 +04:00
|
|
|
template<typename T>
|
|
|
|
String option_to_string(const std::vector<T>& opt)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < opt.size(); ++i)
|
|
|
|
{
|
|
|
|
res += option_to_string(opt[i]);
|
|
|
|
if (i != opt.size() - 1)
|
|
|
|
res += ",";
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void option_from_string(const String& str, std::vector<T>& opt)
|
|
|
|
{
|
|
|
|
opt.clear();
|
|
|
|
std::vector<String> elems = split(str, ',');
|
|
|
|
for (auto& elem: elems)
|
|
|
|
{
|
|
|
|
T opt_elem;
|
|
|
|
option_from_string(elem, opt_elem);
|
|
|
|
opt.push_back(opt_elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 16:42:07 +04:00
|
|
|
String option_to_string(const Regex& re)
|
|
|
|
{
|
|
|
|
return String{re.str()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void option_from_string(const String& str, Regex& re)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
re = Regex{str.begin(), str.end()};
|
|
|
|
}
|
|
|
|
catch (boost::regex_error& err)
|
|
|
|
{
|
|
|
|
throw runtime_error("unable to create regex: "_str + err.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 22:15:38 +04:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
template<typename T>
|
|
|
|
class TypedOption : public Option
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TypedOption(OptionManager& manager, String name, const T& value)
|
|
|
|
: Option(manager, std::move(name)), m_value(value) {}
|
|
|
|
|
|
|
|
void set(const T& value)
|
|
|
|
{
|
|
|
|
if (m_value != value)
|
|
|
|
{
|
|
|
|
m_value = value;
|
|
|
|
m_manager.on_option_changed(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const T& get() const { return m_value; }
|
|
|
|
|
2013-03-05 22:15:38 +04:00
|
|
|
String get_as_string() const override
|
|
|
|
{
|
|
|
|
return option_to_string(m_value);
|
|
|
|
}
|
|
|
|
void set_from_string(const String& str) override
|
|
|
|
{
|
|
|
|
T val;
|
|
|
|
option_from_string(str, val);
|
|
|
|
set(val);
|
|
|
|
}
|
2013-03-03 20:25:40 +04:00
|
|
|
|
|
|
|
Option* clone(OptionManager& manager) const override
|
|
|
|
{
|
|
|
|
return new TypedOption{manager, name(), m_value};
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
T m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
Option::Option(OptionManager& manager, String name)
|
|
|
|
: m_manager(manager), m_name(std::move(name)) {}
|
|
|
|
|
2013-03-06 17:13:21 +04:00
|
|
|
template<typename T> const T& Option::get() const
|
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<const TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
|
|
|
throw runtime_error("option " + name() + " is not of type " + typeid(T).name());
|
|
|
|
return typed_opt->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> void Option::set(const T& val)
|
|
|
|
{
|
|
|
|
auto* typed_opt = dynamic_cast<TypedOption<T>*>(this);
|
|
|
|
if (not typed_opt)
|
|
|
|
throw runtime_error("option " + name() + " is not of type " + typeid(T).name());
|
|
|
|
return typed_opt->set(val);
|
|
|
|
}
|
2013-03-03 20:25:40 +04:00
|
|
|
|
|
|
|
template const String& Option::get<String>() const;
|
|
|
|
template void Option::set<String>(const String&);
|
|
|
|
|
|
|
|
template const int& Option::get<int>() const;
|
|
|
|
template void Option::set<int>(const int&);
|
|
|
|
|
2013-03-05 22:03:42 +04:00
|
|
|
template const bool& Option::get<bool>() const;
|
|
|
|
template void Option::set<bool>(const bool&);
|
|
|
|
|
2013-03-06 17:12:56 +04:00
|
|
|
template const std::vector<int>& Option::get<std::vector<int>>() const;
|
|
|
|
template void Option::set<std::vector<int>>(const std::vector<int>&);
|
2013-03-05 22:15:38 +04:00
|
|
|
|
2013-03-09 17:23:19 +04:00
|
|
|
template const std::vector<String>& Option::get<std::vector<String>>() const;
|
|
|
|
template void Option::set<std::vector<String>>(const std::vector<String>&);
|
|
|
|
|
2013-03-14 16:42:07 +04:00
|
|
|
template const Regex& Option::get<Regex>() const;
|
|
|
|
template void Option::set<Regex>(const Regex&);
|
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
OptionManager::OptionManager(OptionManager& parent)
|
|
|
|
: m_parent(&parent)
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
2012-06-14 17:16:44 +04:00
|
|
|
parent.register_watcher(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionManager::~OptionManager()
|
|
|
|
{
|
|
|
|
if (m_parent)
|
|
|
|
m_parent->unregister_watcher(*this);
|
|
|
|
|
|
|
|
assert(m_watchers.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::register_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
assert(not contains(m_watchers, &watcher));
|
|
|
|
m_watchers.push_back(&watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionManager::unregister_watcher(OptionManagerWatcher& watcher)
|
|
|
|
{
|
|
|
|
auto it = find(m_watchers.begin(), m_watchers.end(), &watcher);
|
|
|
|
assert(it != m_watchers.end());
|
|
|
|
m_watchers.erase(it);
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
template<typename T>
|
|
|
|
auto find_option(T& container, const String& name) -> decltype(container.begin())
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
using ptr_type = decltype(*container.begin());
|
|
|
|
return find_if(container, [&name](const ptr_type& opt) { return opt->name() == name; });
|
|
|
|
}
|
2012-06-14 17:16:44 +04:00
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
Option& OptionManager::get_local_option(const String& name)
|
|
|
|
{
|
|
|
|
auto it = find_option(m_options, name);
|
|
|
|
if (it != m_options.end())
|
|
|
|
return **it;
|
|
|
|
else if (m_parent)
|
2012-06-12 22:28:25 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
m_options.emplace_back((*m_parent)[name].clone(*this));
|
|
|
|
return *m_options.back();
|
2012-06-12 22:28:25 +04:00
|
|
|
}
|
2013-03-03 20:25:40 +04:00
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
|
2012-04-03 22:25:27 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:16:44 +04:00
|
|
|
const Option& OptionManager::operator[](const String& name) const
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
auto it = find_option(m_options, name);
|
2012-04-03 22:25:27 +04:00
|
|
|
if (it != m_options.end())
|
2013-03-03 20:25:40 +04:00
|
|
|
return **it;
|
2012-04-03 22:25:27 +04:00
|
|
|
else if (m_parent)
|
|
|
|
return (*m_parent)[name];
|
|
|
|
else
|
|
|
|
throw option_not_found(name);
|
|
|
|
}
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
CandidateList OptionManager::complete_option_name(const String& prefix,
|
2012-10-11 02:41:48 +04:00
|
|
|
ByteCount cursor_pos)
|
2012-04-03 22:25:27 +04:00
|
|
|
{
|
2012-04-14 05:17:09 +04:00
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
2012-04-03 22:25:27 +04:00
|
|
|
CandidateList result;
|
|
|
|
if (m_parent)
|
|
|
|
result = m_parent->complete_option_name(prefix, cursor_pos);
|
|
|
|
for (auto& option : m_options)
|
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
const auto& name = option->name();
|
|
|
|
if (name.substr(0, real_prefix.length()) == real_prefix and
|
|
|
|
not contains(result, name))
|
|
|
|
result.push_back(name);
|
2012-04-03 22:25:27 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
OptionManager::OptionList OptionManager::flatten_options() const
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
OptionList res = m_parent ? m_parent->flatten_options() : OptionList{};
|
2012-06-14 17:16:44 +04:00
|
|
|
for (auto& option : m_options)
|
2013-03-03 20:25:40 +04:00
|
|
|
{
|
|
|
|
auto it = find_option(res, option->name());
|
|
|
|
if (it != res.end())
|
|
|
|
*it = option.get();
|
|
|
|
else
|
|
|
|
res.emplace_back(option.get());
|
|
|
|
}
|
2012-06-14 17:16:44 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:25:40 +04:00
|
|
|
void OptionManager::on_option_changed(const Option& option)
|
2012-06-14 17:16:44 +04:00
|
|
|
{
|
|
|
|
// if parent option changed, but we overrided it, it's like nothing happened
|
2013-03-03 20:25:40 +04:00
|
|
|
if (&option.manager() != this and
|
|
|
|
find_option(m_options, option.name()) != m_options.end())
|
2012-06-14 17:16:44 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto watcher : m_watchers)
|
2013-03-03 20:25:40 +04:00
|
|
|
watcher->on_option_changed(option);
|
2012-06-14 17:16:44 +04:00
|
|
|
}
|
|
|
|
|
2012-11-22 16:50:29 +04:00
|
|
|
GlobalOptions::GlobalOptions()
|
2012-04-03 17:39:20 +04:00
|
|
|
: OptionManager()
|
|
|
|
{
|
2013-03-03 20:25:40 +04:00
|
|
|
declare_option<int>("tabstop", 8);
|
|
|
|
declare_option<int>("indentwidth", 4);
|
|
|
|
declare_option<String>("eolformat", "lf");
|
|
|
|
declare_option<String>("BOM", "no");
|
|
|
|
declare_option<String>("shell", "sh");
|
2013-03-05 22:03:42 +04:00
|
|
|
declare_option<bool>("complete_prefix", true);
|
|
|
|
declare_option<bool>("incsearch", true);
|
2013-03-14 16:42:07 +04:00
|
|
|
declare_option<Regex>("ignored_files", Regex{R"(^(\..*|.*\.(o|so|a))$)"});
|
2013-03-03 20:25:40 +04:00
|
|
|
declare_option<String>("filetype", "");
|
2013-03-09 17:23:19 +04:00
|
|
|
declare_option<std::vector<String>>("completions", {});
|
2013-03-13 21:52:55 +04:00
|
|
|
declare_option<std::vector<String>>("path", { "./", "/usr/include" });
|
2013-03-03 20:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Option& GlobalOptions::declare_option(const String& name, const T& value)
|
|
|
|
{
|
|
|
|
if (find_option(m_options, name) != m_options.end())
|
|
|
|
throw runtime_error("option " + name + " already declared");
|
|
|
|
m_options.emplace_back(new TypedOption<T>{*this, name, value});
|
|
|
|
return *m_options.back();
|
2012-04-03 17:39:20 +04:00
|
|
|
}
|
|
|
|
|
2013-03-06 17:12:56 +04:00
|
|
|
template Option& GlobalOptions::declare_option<>(const String&, const std::vector<int>&);
|
|
|
|
|
2012-04-03 17:39:20 +04:00
|
|
|
}
|