add support for regex options, make ignored_files one

This commit is contained in:
Maxime Coste 2013-03-14 13:42:07 +01:00
parent 0b45a725e4
commit 50d9c4dfda
4 changed files with 28 additions and 10 deletions

View File

@ -424,7 +424,7 @@ void define_command(const CommandParameters& params, Context& context)
{ {
const String& prefix = token_to_complete < params.size() ? const String& prefix = token_to_complete < params.size() ?
params[token_to_complete] : String(); params[token_to_complete] : String();
return complete_filename(context.options()["ignored_files"].get<String>(), prefix, pos_in_token); return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), pos_in_token);
}; };
} }
else if (parser.has_option("shell-completion")) else if (parser.has_option("shell-completion"))
@ -792,7 +792,7 @@ void register_commands()
PerArgumentCommandCompleter filename_completer({ PerArgumentCommandCompleter filename_completer({
[](const Context& context, const String& prefix, ByteCount cursor_pos) [](const Context& context, const String& prefix, ByteCount cursor_pos)
{ return complete_filename(prefix, context.options()["ignored_files"].get<String>(), cursor_pos); } { return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), cursor_pos); }
}); });
cm.register_commands({ "e", "edit" }, edit<false>, filename_completer); cm.register_commands({ "e", "edit" }, edit<false>, filename_completer);
cm.register_commands({ "e!", "edit!" }, edit<true>, filename_completer); cm.register_commands({ "e!", "edit!" }, edit<true>, filename_completer);

View File

@ -217,7 +217,7 @@ static boost::regex make_regex_ifp(const String& ex)
} }
std::vector<String> complete_filename(const String& prefix, std::vector<String> complete_filename(const String& prefix,
const String& ignored_regex, const Regex& ignored_regex,
ByteCount cursor_pos) ByteCount cursor_pos)
{ {
String real_prefix = parse_filename(prefix.substr(0, cursor_pos)); String real_prefix = parse_filename(prefix.substr(0, cursor_pos));
@ -225,8 +225,6 @@ std::vector<String> complete_filename(const String& prefix,
String dirprefix; String dirprefix;
String fileprefix = real_prefix; String fileprefix = real_prefix;
boost::regex ignored_files = make_regex_ifp(ignored_regex);
ByteCount dir_end = -1; ByteCount dir_end = -1;
for (ByteCount i = 0; i < real_prefix.length(); ++i) for (ByteCount i = 0; i < real_prefix.length(); ++i)
{ {
@ -247,8 +245,8 @@ std::vector<String> complete_filename(const String& prefix,
if (not dir) if (not dir)
return result; return result;
const bool check_ignored_files = not ignored_files.empty() and const bool check_ignored_regex = not ignored_regex.empty() and
not boost::regex_match(fileprefix.c_str(), ignored_files); not boost::regex_match(fileprefix.c_str(), ignored_regex);
boost::regex file_regex = make_regex_ifp(fileprefix); boost::regex file_regex = make_regex_ifp(fileprefix);
std::vector<String> regex_result; std::vector<String> regex_result;
@ -258,7 +256,7 @@ std::vector<String> complete_filename(const String& prefix,
if (filename.empty()) if (filename.empty())
continue; continue;
if (check_ignored_files and boost::regex_match(filename.c_str(), ignored_files)) if (check_ignored_regex and boost::regex_match(filename.c_str(), ignored_regex))
continue; continue;
const bool match_prefix = (filename.substr(0, fileprefix.length()) == fileprefix); const bool match_prefix = (filename.substr(0, fileprefix.length()) == fileprefix);

View File

@ -33,7 +33,7 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename);
String find_file(const String& filename, const memoryview<String>& paths); String find_file(const String& filename, const memoryview<String>& paths);
std::vector<String> complete_filename(const String& prefix, std::vector<String> complete_filename(const String& prefix,
const String& ignore_regex, const Regex& ignore_regex,
ByteCount cursor_pos = -1); ByteCount cursor_pos = -1);
} }

View File

@ -52,6 +52,23 @@ void option_from_string(const String& str, std::vector<T>& opt)
} }
} }
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());
}
}
} }
template<typename T> template<typename T>
@ -124,6 +141,9 @@ template void Option::set<std::vector<int>>(const std::vector<int>&);
template const std::vector<String>& Option::get<std::vector<String>>() const; template const std::vector<String>& Option::get<std::vector<String>>() const;
template void Option::set<std::vector<String>>(const std::vector<String>&); template void Option::set<std::vector<String>>(const std::vector<String>&);
template const Regex& Option::get<Regex>() const;
template void Option::set<Regex>(const Regex&);
OptionManager::OptionManager(OptionManager& parent) OptionManager::OptionManager(OptionManager& parent)
: m_parent(&parent) : m_parent(&parent)
{ {
@ -236,7 +256,7 @@ GlobalOptions::GlobalOptions()
declare_option<String>("shell", "sh"); declare_option<String>("shell", "sh");
declare_option<bool>("complete_prefix", true); declare_option<bool>("complete_prefix", true);
declare_option<bool>("incsearch", true); declare_option<bool>("incsearch", true);
declare_option<String>("ignored_files", R"(^(\..*|.*\.(o|so|a))$)"); declare_option<Regex>("ignored_files", Regex{R"(^(\..*|.*\.(o|so|a))$)"});
declare_option<String>("filetype", ""); declare_option<String>("filetype", "");
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" });