insert_completer.hh (3082B)
1 #ifndef insert_completer_hh_INCLUDED 2 #define insert_completer_hh_INCLUDED 3 4 #include "option.hh" 5 #include "display_buffer.hh" 6 #include "vector.hh" 7 #include "utils.hh" 8 9 #include "optional.hh" 10 11 namespace Kakoune 12 { 13 14 struct SelectionList; 15 struct Key; 16 class Context; 17 class OptionManager; 18 class FaceRegistry; 19 20 struct InsertCompleterDesc 21 { 22 enum Mode 23 { 24 Word, 25 Option, 26 Filename, 27 Line 28 }; 29 30 bool operator==(const InsertCompleterDesc& other) const = default; 31 32 Mode mode; 33 Optional<String> param; 34 }; 35 36 using InsertCompleterDescList = Vector<InsertCompleterDesc, MemoryDomain::Options>; 37 38 String option_to_string(const InsertCompleterDesc& opt); 39 InsertCompleterDesc option_from_string(Meta::Type<InsertCompleterDesc>, StringView str); 40 41 inline StringView option_type_name(Meta::Type<InsertCompleterDesc>) 42 { 43 return "completer"; 44 } 45 46 using CompletionCandidate = std::tuple<String, String, String>; 47 using CompletionList = PrefixedList<String, CompletionCandidate>; 48 49 inline StringView option_type_name(Meta::Type<CompletionList>) 50 { 51 return "completions"; 52 } 53 54 struct InsertCompletion 55 { 56 struct Candidate 57 { 58 String completion; 59 String on_select; 60 DisplayLine menu_entry; 61 62 bool operator==(const Candidate& other) const { return completion == other.completion; } 63 auto operator<=>(const Candidate& other) const { return completion <=> other.completion; } 64 }; 65 using CandidateList = Vector<Candidate, MemoryDomain::Completion>; 66 67 CandidateList candidates; 68 BufferCoord begin; 69 BufferCoord end; 70 size_t timestamp = 0; 71 72 bool is_valid() const { return not candidates.empty(); } 73 }; 74 75 class InsertCompleter : public OptionWatcher 76 { 77 public: 78 InsertCompleter(Context& context); 79 InsertCompleter(const InsertCompleter&) = delete; 80 InsertCompleter& operator=(const InsertCompleter&) = delete; 81 ~InsertCompleter(); 82 83 void select(int index, bool relative, FunctionRef<void (Key)> record_key); 84 void update(bool allow_implicit); 85 void try_accept(); 86 void reset(); 87 88 void explicit_file_complete(); 89 void explicit_word_buffer_complete(); 90 void explicit_word_all_complete(); 91 void explicit_line_buffer_complete(); 92 void explicit_line_all_complete(); 93 94 bool has_candidate_selected() const; 95 96 private: 97 bool setup_ifn(); 98 99 template<typename Func> 100 bool try_complete(Func complete_func); 101 void on_option_changed(const Option& opt) override; 102 103 void menu_show(); 104 105 Context& m_context; 106 OptionManager& m_options; 107 const FaceRegistry& m_faces; 108 InsertCompletion m_completions; 109 Vector<BufferRange> m_inserted_ranges; 110 int m_current_candidate = -1; 111 bool m_enabled = true; 112 113 using CompleteFunc = InsertCompletion (const SelectionList& sels, 114 const OptionManager& options, 115 const FaceRegistry& faces); 116 CompleteFunc* m_explicit_completer = nullptr; 117 }; 118 119 } 120 121 #endif // insert_completer_hh_INCLUDED