File completion: when no files match entered text as prefix, try as regex
This commit is contained in:
parent
cfd7ee049a
commit
777a79d37c
|
@ -13,6 +13,20 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
|
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&) {}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
CandidateList complete_filename(const Context& context,
|
CandidateList complete_filename(const Context& context,
|
||||||
const String& prefix,
|
const String& prefix,
|
||||||
ByteCount cursor_pos)
|
ByteCount cursor_pos)
|
||||||
|
@ -22,10 +36,7 @@ CandidateList complete_filename(const Context& context,
|
||||||
String dirprefix;
|
String dirprefix;
|
||||||
String fileprefix = real_prefix;
|
String fileprefix = real_prefix;
|
||||||
|
|
||||||
String ignored_files = context.options()["ignored_files"].as_string();
|
boost::regex ignored_files = make_regex_ifp(context.options()["ignored_files"].as_string());
|
||||||
boost::regex ignored_files_regex;
|
|
||||||
if (not ignored_files.empty())
|
|
||||||
ignored_files_regex = boost::regex(ignored_files.c_str());
|
|
||||||
|
|
||||||
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)
|
||||||
|
@ -41,36 +52,47 @@ CandidateList complete_filename(const Context& context,
|
||||||
}
|
}
|
||||||
|
|
||||||
DIR* dir = opendir(dirname.c_str());
|
DIR* dir = opendir(dirname.c_str());
|
||||||
auto closeDir = on_scope_end([=](){ closedir(dir); });
|
auto closeDir = on_scope_end([=]{ closedir(dir); });
|
||||||
|
|
||||||
CandidateList result;
|
CandidateList result;
|
||||||
if (not dir)
|
if (not dir)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
const bool check_ignored_files = not ignored_files.empty() and
|
const bool check_ignored_files = not ignored_files.empty() and
|
||||||
not boost::regex_match(fileprefix.c_str(), ignored_files_regex);
|
not boost::regex_match(fileprefix.c_str(), ignored_files);
|
||||||
|
|
||||||
|
boost::regex file_regex = make_regex_ifp(fileprefix);
|
||||||
|
CandidateList regex_result;
|
||||||
while (dirent* entry = readdir(dir))
|
while (dirent* entry = readdir(dir))
|
||||||
{
|
{
|
||||||
String filename = entry->d_name;
|
String filename = entry->d_name;
|
||||||
if (filename.empty())
|
if (filename.empty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (check_ignored_files and
|
if (check_ignored_files and boost::regex_match(filename.c_str(), ignored_files))
|
||||||
boost::regex_match(filename.c_str(), ignored_files_regex))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (filename.substr(0, fileprefix.length()) == fileprefix)
|
const bool match_prefix = (filename.substr(0, fileprefix.length()) == fileprefix);
|
||||||
|
const bool match_regex = not file_regex.empty() and
|
||||||
|
boost::regex_match(filename.c_str(), file_regex);
|
||||||
|
|
||||||
|
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)
|
||||||
name += '/';
|
name += '/';
|
||||||
if (fileprefix.length() != 0 or filename[0] != '.')
|
if (fileprefix.length() != 0 or filename[0] != '.')
|
||||||
result.push_back(escape(name));
|
{
|
||||||
|
if (match_prefix)
|
||||||
|
result.push_back(escape(name));
|
||||||
|
if (match_regex)
|
||||||
|
regex_result.push_back(escape(name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::sort(result.begin(), result.end());
|
CandidateList& real_result = result.empty() ? regex_result : result;
|
||||||
return result;
|
std::sort(real_result.begin(), real_result.end());
|
||||||
|
return real_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user