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