2011-09-14 17:41:56 +02:00
|
|
|
#include "completion.hh"
|
|
|
|
|
2011-09-22 20:55:45 +02:00
|
|
|
#include "buffer_manager.hh"
|
2011-09-14 17:41:56 +02:00
|
|
|
#include "utils.hh"
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2012-01-15 03:02:18 +01:00
|
|
|
#include <algorithm>
|
2011-09-14 17:41:56 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
CandidateList complete_filename(const String& prefix,
|
2011-09-16 11:17:55 +02:00
|
|
|
size_t cursor_pos)
|
2011-09-14 17:41:56 +02:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String real_prefix = prefix.substr(0, cursor_pos);
|
|
|
|
auto dir_end = std::find(real_prefix.begin(), real_prefix.end(), '/');
|
|
|
|
String dirname = "./";
|
|
|
|
String dirprefix;
|
|
|
|
String fileprefix = real_prefix;
|
2011-09-14 17:41:56 +02:00
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
if (dir_end != real_prefix.end())
|
2011-09-14 17:41:56 +02:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
dirname = String(real_prefix.begin(), dir_end + 1);
|
2011-12-22 14:44:04 +01:00
|
|
|
dirprefix = dirname;
|
2012-04-14 03:17:09 +02:00
|
|
|
fileprefix = String(dir_end + 1, real_prefix.end());
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 20:45:13 +02:00
|
|
|
DIR* dir = opendir(dirname.c_str());
|
|
|
|
auto closeDir = on_scope_end([=](){ closedir(dir); });
|
2011-09-14 17:41:56 +02:00
|
|
|
|
|
|
|
CandidateList result;
|
2012-03-04 20:43:47 +01:00
|
|
|
if (not dir)
|
|
|
|
return result;
|
|
|
|
|
2011-09-14 17:41:56 +02:00
|
|
|
while (dirent* entry = readdir(dir))
|
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String filename = entry->d_name;
|
2012-01-15 03:01:58 +01:00
|
|
|
if (filename.empty())
|
|
|
|
continue;
|
|
|
|
|
2011-09-14 17:41:56 +02:00
|
|
|
if (filename.substr(0, fileprefix.length()) == fileprefix)
|
2011-12-22 14:44:04 +01:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String name = dirprefix + filename;
|
2011-12-22 14:44:04 +01:00
|
|
|
if (entry->d_type == DT_DIR)
|
|
|
|
name += '/';
|
2012-01-15 03:01:58 +01:00
|
|
|
if (fileprefix.length() or filename[0] != '.')
|
|
|
|
result.push_back(name);
|
2011-12-22 14:44:04 +01:00
|
|
|
}
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
2012-01-15 03:02:18 +01:00
|
|
|
std::sort(result.begin(), result.end());
|
2011-09-14 17:41:56 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|