2011-09-14 17:41:56 +02:00
|
|
|
#ifndef completion_hh_INCLUDED
|
|
|
|
#define completion_hh_INCLUDED
|
|
|
|
|
2011-10-11 00:38:58 +02:00
|
|
|
#include <functional>
|
2016-02-09 23:50:10 +01:00
|
|
|
#include <algorithm>
|
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"
|
2015-01-09 14:57:21 +01:00
|
|
|
#include "vector.hh"
|
2016-02-09 23:50:10 +01:00
|
|
|
#include "ranked_match.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
|
|
|
|
2016-11-28 14:59:55 +01:00
|
|
|
using CandidateList = Vector<String, MemoryDomain::Completion>;
|
2011-09-14 17:41:56 +02:00
|
|
|
|
|
|
|
struct Completions
|
|
|
|
{
|
|
|
|
CandidateList candidates;
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount start;
|
|
|
|
ByteCount end;
|
2018-05-30 15:23:38 +02:00
|
|
|
bool quoted = false;
|
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
|
|
|
|
2018-05-30 15:23:38 +02:00
|
|
|
Completions(ByteCount start, ByteCount end, CandidateList candidates, bool quoted = false)
|
|
|
|
: candidates(std::move(candidates)), start(start), end(end), quoted{quoted} {}
|
2011-09-14 17:41:56 +02:00
|
|
|
};
|
|
|
|
|
2013-11-04 22:53:10 +01:00
|
|
|
enum class CompletionFlags
|
|
|
|
{
|
2016-08-05 14:53:19 +02:00
|
|
|
None = 0,
|
|
|
|
Fast = 1 << 0,
|
|
|
|
Start = 1 << 2,
|
2013-11-04 22:53:10 +01:00
|
|
|
};
|
2016-08-05 14:53:19 +02:00
|
|
|
|
2017-03-15 18:55:34 +01:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<CompletionFlags>) { return true; }
|
2016-08-05 14:53:19 +02:00
|
|
|
|
2013-11-04 22:53:10 +01:00
|
|
|
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
|
|
|
|
2017-03-17 00:08:10 +01:00
|
|
|
inline Completions complete_nothing(const Context&, CompletionFlags,
|
2014-04-18 15:02:14 +02:00
|
|
|
StringView, ByteCount cursor_pos)
|
2011-10-11 00:38:58 +02:00
|
|
|
{
|
2016-03-24 23:15:11 +01:00
|
|
|
return {cursor_pos, cursor_pos};
|
2011-11-10 21:57:25 +01:00
|
|
|
}
|
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,
|
2018-05-30 15:23:38 +02:00
|
|
|
std::move(completion.candidates), completion.quoted };
|
2014-06-15 17:04:38 +02:00
|
|
|
}
|
|
|
|
|
2016-02-09 23:50:10 +01:00
|
|
|
template<typename Container>
|
2016-03-02 14:32:35 +01:00
|
|
|
CandidateList complete(StringView query, ByteCount cursor_pos,
|
2016-02-09 23:50:10 +01:00
|
|
|
const Container& container)
|
2014-12-23 14:54:09 +01:00
|
|
|
{
|
2016-02-10 14:33:49 +01:00
|
|
|
using std::begin;
|
|
|
|
static_assert(not std::is_same<decltype(*begin(container)), String>::value,
|
2016-03-02 14:32:35 +01:00
|
|
|
"complete require long lived strings, not temporaries");
|
2016-02-10 14:33:49 +01:00
|
|
|
|
2016-03-02 14:32:35 +01:00
|
|
|
query = query.substr(0, cursor_pos);
|
2016-02-09 23:50:10 +01:00
|
|
|
Vector<RankedMatch> matches;
|
|
|
|
for (const auto& str : container)
|
2014-12-23 14:54:09 +01:00
|
|
|
{
|
2016-03-02 14:32:35 +01:00
|
|
|
if (RankedMatch match{str, query})
|
2016-02-09 23:50:10 +01:00
|
|
|
matches.push_back(match);
|
2014-12-23 14:54:09 +01:00
|
|
|
}
|
2016-02-09 23:50:10 +01:00
|
|
|
std::sort(matches.begin(), matches.end());
|
2015-05-26 19:38:48 +02:00
|
|
|
CandidateList res;
|
2016-02-09 23:50:10 +01:00
|
|
|
for (auto& m : matches)
|
|
|
|
res.push_back(m.candidate().str());
|
2015-05-26 19:38:48 +02:00
|
|
|
return res;
|
2014-12-23 14:54:09 +01:00
|
|
|
}
|
|
|
|
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
|
|
|
#endif // completion_hh_INCLUDED
|