2011-09-14 17:41:56 +02:00
|
|
|
#ifndef completion_hh_INCLUDED
|
|
|
|
#define completion_hh_INCLUDED
|
|
|
|
|
|
|
|
#include <vector>
|
2011-10-11 00:38:58 +02:00
|
|
|
#include <functional>
|
2011-09-14 17:41:56 +02:00
|
|
|
|
2014-01-09 20:50:01 +01:00
|
|
|
#include "units.hh"
|
2014-04-18 15:02:14 +02:00
|
|
|
#include "string.hh"
|
2014-01-09 20:50:01 +01:00
|
|
|
|
2011-09-14 17:41:56 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-11-11 20:10:49 +01:00
|
|
|
class Context;
|
2012-08-06 21:37:43 +02:00
|
|
|
|
2014-01-09 20:50:01 +01:00
|
|
|
using CandidateList = std::vector<String>;
|
2011-09-14 17:41:56 +02:00
|
|
|
|
|
|
|
struct Completions
|
|
|
|
{
|
|
|
|
CandidateList candidates;
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount start;
|
|
|
|
ByteCount end;
|
2011-09-14 17:41:56 +02:00
|
|
|
|
2011-09-14 21:15:09 +02:00
|
|
|
Completions()
|
|
|
|
: start(0), end(0) {}
|
|
|
|
|
2012-10-11 00:41:48 +02:00
|
|
|
Completions(ByteCount start, ByteCount end)
|
2011-09-14 17:41:56 +02:00
|
|
|
: start(start), end(end) {}
|
2014-01-26 17:14:02 +01:00
|
|
|
|
|
|
|
Completions(ByteCount start, ByteCount end, CandidateList candidates)
|
|
|
|
: start(start), end(end), candidates(std::move(candidates)) {}
|
2011-09-14 17:41:56 +02:00
|
|
|
};
|
|
|
|
|
2013-11-04 22:53:10 +01:00
|
|
|
enum class CompletionFlags
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Fast
|
|
|
|
};
|
|
|
|
using Completer = std::function<Completions (const Context&, CompletionFlags,
|
2014-04-18 15:02:14 +02:00
|
|
|
StringView, ByteCount)>;
|
2011-10-11 00:38:58 +02:00
|
|
|
|
2013-11-04 22:53:10 +01:00
|
|
|
inline Completions complete_nothing(const Context& context, CompletionFlags,
|
2014-04-18 15:02:14 +02:00
|
|
|
StringView, ByteCount cursor_pos)
|
2011-10-11 00:38:58 +02:00
|
|
|
{
|
2011-11-10 21:57:25 +01:00
|
|
|
return Completions(cursor_pos, cursor_pos);
|
|
|
|
}
|
2011-10-11 00:38:58 +02:00
|
|
|
|
2013-12-30 15:20:05 +01:00
|
|
|
Completions shell_complete(const Context& context, CompletionFlags,
|
2014-04-18 15:02:14 +02:00
|
|
|
StringView, ByteCount cursor_pos);
|
2013-12-30 15:20:05 +01:00
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
inline Completions offset_pos(Completions completion, ByteCount offset)
|
|
|
|
{
|
|
|
|
return { completion.start + offset, completion.end + offset,
|
|
|
|
std::move(completion.candidates) };
|
|
|
|
}
|
|
|
|
|
2014-12-23 14:54:09 +01:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
template<typename Str, typename Func>
|
|
|
|
void do_matches(Str&& str, StringView prefix,
|
|
|
|
CandidateList* res, Func match_func)
|
|
|
|
{
|
|
|
|
if (match_func(str, prefix))
|
|
|
|
res->push_back(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Str, typename Func, typename... Rest>
|
|
|
|
void do_matches(Str&& str, StringView prefix,
|
|
|
|
CandidateList* res, Func match_func, Rest... rest)
|
|
|
|
{
|
|
|
|
do_matches(str, prefix, res, match_func);
|
|
|
|
do_matches(str, prefix, res+1, rest...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container, typename... MatchFunc>
|
|
|
|
CandidateList complete(StringView prefix, ByteCount cursor_pos,
|
|
|
|
const Container& container, MatchFunc... match_func)
|
|
|
|
{
|
|
|
|
CandidateList res[sizeof...(match_func)];
|
|
|
|
auto real_prefix = prefix.substr(0, cursor_pos);
|
|
|
|
for (const auto& elem : container)
|
|
|
|
detail::do_matches(elem, real_prefix, res, match_func...);
|
|
|
|
auto it = find_if(res, [](CandidateList& c) { return not c.empty(); });
|
|
|
|
return it == end(res) ? CandidateList{} : std::move(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
CandidateList complete(StringView prefix, ByteCount cursor_pos,
|
|
|
|
const Container& container)
|
|
|
|
{
|
|
|
|
return complete(prefix, cursor_pos, container, prefix_match);
|
|
|
|
}
|
|
|
|
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
|
|
|
#endif // completion_hh_INCLUDED
|