diff --git a/src/command_manager.cc b/src/command_manager.cc index d62fbdc2..26751db4 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -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); } diff --git a/src/command_manager.hh b/src/command_manager.hh index fc3d81f3..e46c5a77 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -7,7 +7,7 @@ #include #include "exception.hh" - +#include "completion.hh" namespace Kakoune { @@ -20,16 +20,6 @@ struct wrong_argument_count : runtime_error typedef std::vector CommandParameters; typedef std::function 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: diff --git a/src/completion.cc b/src/completion.cc new file mode 100644 index 00000000..a1eed12a --- /dev/null +++ b/src/completion.cc @@ -0,0 +1,34 @@ +#include "completion.hh" + +#include "utils.hh" + +#include + +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; +} + +} diff --git a/src/completion.hh b/src/completion.hh new file mode 100644 index 00000000..497fa54f --- /dev/null +++ b/src/completion.hh @@ -0,0 +1,25 @@ +#ifndef completion_hh_INCLUDED +#define completion_hh_INCLUDED + +#include +#include + +namespace Kakoune +{ + +typedef std::vector 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