Add a docstring to options

This commit is contained in:
Maxime Coste 2014-04-09 20:14:04 +01:00
parent 05d69f7f8e
commit e9276a0a85
3 changed files with 75 additions and 44 deletions

View File

@ -831,7 +831,8 @@ const CommandDesc declare_option_cmd = {
" str-list: list of character strings\n" " str-list: list of character strings\n"
" line-flag-list: list of line flags\n", " line-flag-list: list of line flags\n",
ParameterDesc{ ParameterDesc{
SwitchMap{ { "hidden", { false, "do not display option name when completing" } } }, SwitchMap{ { "hidden", { false, "do not display option name when completing" } },
{ "docstring", { true, "specify option description" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart, ParameterDesc::Flags::SwitchesOnlyAtStart,
2, 3 2, 3
}, },
@ -845,22 +846,26 @@ const CommandDesc declare_option_cmd = {
if (parser.has_option("hidden")) if (parser.has_option("hidden"))
flags = Option::Flags::Hidden; flags = Option::Flags::Hidden;
String docstring;
if (parser.has_option("docstring"))
docstring = parser.option_value("docstring");
GlobalOptions& opts = GlobalOptions::instance(); GlobalOptions& opts = GlobalOptions::instance();
if (parser[0] == "int") if (parser[0] == "int")
opt = &opts.declare_option<int>(parser[1], 0, flags); opt = &opts.declare_option<int>(parser[1], docstring, 0, flags);
if (parser[0] == "bool") if (parser[0] == "bool")
opt = &opts.declare_option<bool>(parser[1], 0, flags); opt = &opts.declare_option<bool>(parser[1], docstring, 0, flags);
else if (parser[0] == "str") else if (parser[0] == "str")
opt = &opts.declare_option<String>(parser[1], "", flags); opt = &opts.declare_option<String>(parser[1], docstring, "", flags);
else if (parser[0] == "regex") else if (parser[0] == "regex")
opt = &opts.declare_option<Regex>(parser[1], Regex{}, flags); opt = &opts.declare_option<Regex>(parser[1], docstring, {}, flags);
else if (parser[0] == "int-list") else if (parser[0] == "int-list")
opt = &opts.declare_option<std::vector<int>>(parser[1], {}, flags); opt = &opts.declare_option<std::vector<int>>(parser[1], docstring, {}, flags);
else if (parser[0] == "str-list") else if (parser[0] == "str-list")
opt = &opts.declare_option<std::vector<String>>(parser[1], {}, flags); opt = &opts.declare_option<std::vector<String>>(parser[1], docstring, {}, flags);
else if (parser[0] == "line-flag-list") else if (parser[0] == "line-flag-list")
opt = &opts.declare_option<std::vector<LineAndFlag>>(parser[1], {}, flags); opt = &opts.declare_option<std::vector<LineAndFlag>>(parser[1], docstring, {}, flags);
else else
throw runtime_error("unknown type " + parser[0]); throw runtime_error("unknown type " + parser[0]);

View File

@ -7,8 +7,10 @@
namespace Kakoune namespace Kakoune
{ {
Option::Option(OptionManager& manager, String name, Flags flags) Option::Option(OptionManager& manager, String name, String docstring,
: m_manager(manager), m_name(std::move(name)), m_flags(flags) {} Flags flags)
: m_manager(manager), m_name(std::move(name)),
m_docstring(std::move(docstring)), m_flags(flags) {}
OptionManager::OptionManager(OptionManager& parent) OptionManager::OptionManager(OptionManager& parent)
: m_parent(&parent) : m_parent(&parent)
@ -120,32 +122,51 @@ void OptionManager::on_option_changed(const Option& option)
GlobalOptions::GlobalOptions() GlobalOptions::GlobalOptions()
: OptionManager() : OptionManager()
{ {
declare_option<int>("tabstop", 8); declare_option("tabstop", "size of a tab character", 8);
declare_option<int>("indentwidth", 4); declare_option("indentwidth", "indentation width", 4);
declare_option<int>("scrolloff", 0); declare_option("scrolloff",
declare_option<String>("eolformat", "lf"); "number of lines to keep visible main cursor when scrolling",
declare_option<String>("BOM", "no"); 0);
declare_option<bool>("complete_prefix", true); declare_option("eolformat", "end of line format: 'crlf' or 'lf'", "lf"_str);
declare_option<bool>("incsearch", true); declare_option("BOM", "insert a byte order mark when writing buffer",
declare_option<bool>("autoinfo", true); "no"_str);
declare_option<bool>("autoshowcompl", true); declare_option("complete_prefix",
declare_option<bool>("aligntab", false); "complete up to common prefix in tab completion",
declare_option<Regex>("ignored_files", Regex{R"(^(\..*|.*\.(o|so|a))$)"}); true);
declare_option<String>("filetype", ""); declare_option("incsearch",
declare_option<std::vector<String>>("path", { "./", "/usr/include" }); "incrementaly apply search/select/split regex",
declare_option<std::vector<String>>("completers", {"filename", "word=buffer"}, true);
Option::Flags::None, declare_option("autoinfo",
[](const std::vector<String>& s) { "automatically display contextual help",
static const auto values = {"word=buffer", "word=all", "filename" }; true);
for (auto& v : s) declare_option("autoshowcompl",
{ "automatically display possible completions for prompts",
if (v.substr(0_byte, 7_byte) == "option=") true);
continue; declare_option("aligntab",
if (not contains(values, v)) "use tab characters when possible for alignement",
throw runtime_error(v + " is not a recognised value for completers"); false);
} declare_option("ignored_files",
}); "patterns to ignore when completing filenames",
declare_option<YesNoAsk>("autoreload", Ask); Regex{R"(^(\..*|.*\.(o|so|a))$)"});
declare_option("filetype", "buffer filetype", ""_str);
declare_option("path", "path to consider when trying to find a file",
std::vector<String>({ "./", "/usr/include" }));
declare_option("completers", "insert mode completers to execute.",
std::vector<String>({"filename", "word=buffer"}),
Option::Flags::None,
OptionChecker<std::vector<String>>([](const std::vector<String>& s) {
static const auto values = {"word=buffer", "word=all", "filename"};
for (auto& v : s)
{
if (v.substr(0_byte, 7_byte) == "option=")
continue;
if (not contains(values, v))
throw runtime_error(v + " is not a recognised value for completers");
}
}));
declare_option("autoreload",
"autoreload buffer when a filesystem modification is detected",
Ask);
} }
} }

View File

@ -28,7 +28,7 @@ public:
Hidden = 1, Hidden = 1,
}; };
Option(OptionManager& manager, String name, Flags flags); Option(OptionManager& manager, String name, String docstring, Flags flags);
virtual ~Option() {} virtual ~Option() {}
template<typename T> const T& get() const; template<typename T> const T& get() const;
@ -40,6 +40,7 @@ public:
virtual void add_from_string(const String& str) = 0; virtual void add_from_string(const String& str) = 0;
const String& name() const { return m_name; } const String& name() const { return m_name; }
const String& docstring() const { return m_docstring; }
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;
@ -55,6 +56,7 @@ public:
protected: protected:
OptionManager& m_manager; OptionManager& m_manager;
String m_name; String m_name;
String m_docstring;
Flags m_flags; Flags m_flags;
}; };
@ -106,10 +108,10 @@ template<typename T>
class TypedOption : public Option class TypedOption : public Option
{ {
public: public:
TypedOption(OptionManager& manager, String name, Option::Flags flags, TypedOption(OptionManager& manager, String name, String docstring,
const T& value, OptionChecker<T> checker) Option::Flags flags, const T& value, OptionChecker<T> checker)
: Option(manager, std::move(name), flags), m_value(value), : Option(manager, std::move(name), std::move(docstring), flags),
m_checker(std::move(checker)) {} m_value(value), m_checker(std::move(checker)) {}
void set(T value) void set(T value)
{ {
@ -145,7 +147,8 @@ public:
Option* clone(OptionManager& manager) const override Option* clone(OptionManager& manager) const override
{ {
return new TypedOption{manager, name(), flags(), m_value, m_checker}; return new TypedOption{manager, name(), docstring(), flags(),
m_value, m_checker};
} }
private: private:
T m_value; T m_value;
@ -187,7 +190,8 @@ public:
GlobalOptions(); GlobalOptions();
template<typename T> template<typename T>
Option& declare_option(const String& name, const T& value, Option& declare_option(const String& name, const String& docstring,
const T& value,
Option::Flags flags = Option::Flags::None, Option::Flags flags = Option::Flags::None,
OptionChecker<T> checker = OptionChecker<T>{}) OptionChecker<T> checker = OptionChecker<T>{})
{ {
@ -198,7 +202,8 @@ public:
return **it; return **it;
throw runtime_error("option " + name + " already declared with different type or flags"); throw runtime_error("option " + name + " already declared with different type or flags");
} }
m_options.emplace_back(new TypedOption<T>{*this, name, flags, value, m_options.emplace_back(new TypedOption<T>{*this, name, docstring,
flags, value,
std::move(checker)}); std::move(checker)});
return *m_options.back(); return *m_options.back();
} }