Completion: dedicated completion header and basic filename completion

This commit is contained in:
Maxime Coste 2011-09-14 15:41:56 +00:00
parent 4b38cd3cd0
commit b59ad6a174
4 changed files with 67 additions and 11 deletions

View File

@ -96,6 +96,13 @@ Completions CommandManager::complete(const std::string& command_line, size_t cur
return result;
}
if (token_to_complete == 1) // filename completion
{
Completions result(tokens[1].first, cursor_pos);
std::string prefix = command_line.substr(tokens[1].first, cursor_pos);
result.candidates = complete_filename(prefix);
return result;
}
return Completions(cursor_pos, cursor_pos);
}

View File

@ -7,7 +7,7 @@
#include <functional>
#include "exception.hh"
#include "completion.hh"
namespace Kakoune
{
@ -20,16 +20,6 @@ struct wrong_argument_count : runtime_error
typedef std::vector<std::string> CommandParameters;
typedef std::function<void (const CommandParameters&)> Command;
struct Completions
{
CommandParameters candidates;
size_t start;
size_t end;
Completions(size_t start, size_t end)
: start(start), end(end) {}
};
class CommandManager
{
public:

34
src/completion.cc Normal file
View File

@ -0,0 +1,34 @@
#include "completion.hh"
#include "utils.hh"
#include <dirent.h>
namespace Kakoune
{
CandidateList complete_filename(const std::string& prefix)
{
size_t dir_end = prefix.find_last_of('/');
std::string dirname = "./";
std::string fileprefix = prefix;
if (dir_end != std::string::npos)
{
dirname = prefix.substr(0, dir_end + 1);
fileprefix = prefix.substr(dir_end + 1, std::string::npos);
}
auto dir = auto_raii(opendir(dirname.c_str()), closedir);
CandidateList result;
while (dirent* entry = readdir(dir))
{
std::string filename = entry->d_name;
if (filename.substr(0, fileprefix.length()) == fileprefix)
result.push_back(filename);
}
return result;
}
}

25
src/completion.hh Normal file
View File

@ -0,0 +1,25 @@
#ifndef completion_hh_INCLUDED
#define completion_hh_INCLUDED
#include <string>
#include <vector>
namespace Kakoune
{
typedef std::vector<std::string> CandidateList;
struct Completions
{
CandidateList candidates;
size_t start;
size_t end;
Completions(size_t start, size_t end)
: start(start), end(end) {}
};
CandidateList complete_filename(const std::string& prefix);
}
#endif // completion_hh_INCLUDED