kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

completion.hh (2538B)


      1 #ifndef completion_hh_INCLUDED
      2 #define completion_hh_INCLUDED
      3 
      4 #include <algorithm>
      5 
      6 #include "units.hh"
      7 #include "string.hh"
      8 #include "vector.hh"
      9 #include "ranked_match.hh"
     10 
     11 namespace Kakoune
     12 {
     13 
     14 class Context;
     15 class Regex;
     16 
     17 using CandidateList = Vector<String, MemoryDomain::Completion>;
     18 
     19 struct Completions
     20 {
     21     enum class Flags
     22     {
     23         None = 0,
     24         Quoted = 0b1,
     25         Menu = 0b10,
     26         NoEmpty = 0b100
     27     };
     28 
     29     constexpr friend bool with_bit_ops(Meta::Type<Flags>) { return true; }
     30 
     31     CandidateList candidates;
     32     ByteCount start;
     33     ByteCount end;
     34     Flags flags = Flags::None;
     35 
     36     Completions()
     37         : start(0), end(0) {}
     38 
     39     Completions(ByteCount start, ByteCount end)
     40         : start(start), end(end) {}
     41 
     42     Completions(ByteCount start, ByteCount end, CandidateList candidates, Flags flags = Flags::None)
     43         : candidates(std::move(candidates)), start(start), end(end), flags{flags} {}
     44 };
     45 
     46 inline Completions complete_nothing(const Context&, StringView, ByteCount cursor_pos)
     47 {
     48     return {cursor_pos, cursor_pos};
     49 }
     50 
     51 enum class FilenameFlags
     52 {
     53     None = 0,
     54     OnlyDirectories = 1 << 0,
     55     Expand = 1 << 1
     56 };
     57 constexpr bool with_bit_ops(Meta::Type<FilenameFlags>) { return true; }
     58 
     59 CandidateList complete_filename(StringView prefix, const Regex& ignore_regex,
     60                                 ByteCount cursor_pos = -1,
     61                                 FilenameFlags flags = FilenameFlags::None);
     62 
     63 CandidateList complete_command(StringView prefix, ByteCount cursor_pos = -1);
     64 
     65 Completions shell_complete(const Context& context, StringView, ByteCount cursor_pos);
     66 
     67 inline Completions offset_pos(Completions completion, ByteCount offset)
     68 {
     69     return {completion.start + offset, completion.end + offset,
     70             std::move(completion.candidates), completion.flags};
     71 }
     72 
     73 template<typename Container>
     74 CandidateList complete(StringView query, ByteCount cursor_pos,
     75                        const Container& container)
     76 {
     77     using std::begin;
     78     static_assert(not std::is_same<decltype(*begin(container)), String>::value,
     79                   "complete require long lived strings, not temporaries");
     80 
     81     query = query.substr(0, cursor_pos);
     82     Vector<RankedMatch> matches;
     83     for (const auto& str : container)
     84     {
     85         if (RankedMatch match{str, query})
     86             matches.push_back(match);
     87     }
     88     std::sort(matches.begin(), matches.end());
     89     CandidateList res;
     90     for (auto& m : matches)
     91         res.push_back(m.candidate().str());
     92     return res;
     93 }
     94 
     95 }
     96 #endif // completion_hh_INCLUDED