complete_filename fallback on subsequence completion if prefix does not work

This commit is contained in:
Maxime Coste 2013-09-24 10:16:47 +02:00
parent 9018bc47df
commit 60f4e1104f

View File

@ -242,23 +242,6 @@ String find_file(const String& filename, memoryview<String> paths)
return ""; return "";
} }
static boost::regex make_regex_ifp(const String& ex)
{
boost::regex result;
if (not ex.empty())
{
try
{
result = boost::regex(ex.c_str());
}
catch(boost::regex_error& err)
{
write_debug(err.what());
}
}
return result;
}
std::vector<String> complete_filename(const String& prefix, std::vector<String> complete_filename(const String& prefix,
const Regex& ignored_regex, const Regex& ignored_regex,
ByteCount cursor_pos) ByteCount cursor_pos)
@ -291,8 +274,7 @@ std::vector<String> complete_filename(const String& prefix,
const bool check_ignored_regex = not ignored_regex.empty() and const bool check_ignored_regex = not ignored_regex.empty() and
not boost::regex_match(fileprefix.c_str(), ignored_regex); not boost::regex_match(fileprefix.c_str(), ignored_regex);
boost::regex file_regex = make_regex_ifp(fileprefix); std::vector<String> subseq_result;
std::vector<String> regex_result;
while (dirent* entry = readdir(dir)) while (dirent* entry = readdir(dir))
{ {
String filename = entry->d_name; String filename = entry->d_name;
@ -303,10 +285,8 @@ std::vector<String> complete_filename(const String& prefix,
continue; continue;
const bool match_prefix = prefix_match(filename, fileprefix); const bool match_prefix = prefix_match(filename, fileprefix);
const bool match_regex = not file_regex.empty() and const bool match_subseq = subsequence_match(filename, fileprefix);
boost::regex_match(filename.c_str(), file_regex); if (match_prefix or match_subseq)
if (match_prefix or match_regex)
{ {
String name = dirprefix + filename; String name = dirprefix + filename;
if (entry->d_type == DT_DIR) if (entry->d_type == DT_DIR)
@ -315,12 +295,12 @@ std::vector<String> complete_filename(const String& prefix,
{ {
if (match_prefix) if (match_prefix)
result.push_back(escape(name)); result.push_back(escape(name));
if (match_regex) if (match_subseq)
regex_result.push_back(escape(name)); subseq_result.push_back(escape(name));
} }
} }
} }
auto& real_result = result.empty() ? regex_result : result; auto& real_result = result.empty() ? subseq_result : result;
std::sort(real_result.begin(), real_result.end()); std::sort(real_result.begin(), real_result.end());
return real_result; return real_result;
} }