command_manager.hh (4753B)
1 #ifndef command_manager_hh_INCLUDED 2 #define command_manager_hh_INCLUDED 3 4 #include "completion.hh" 5 #include "array_view.hh" 6 #include "shell_manager.hh" 7 #include "parameters_parser.hh" 8 #include "string.hh" 9 #include "optional.hh" 10 #include "utils.hh" 11 #include "hash_map.hh" 12 #include "function.hh" 13 14 namespace Kakoune 15 { 16 17 class Context; 18 using CommandParameters = ConstArrayView<String>; 19 using CommandFunc = Function<void (const ParametersParser& parser, 20 Context& context, 21 const ShellContext& shell_context)>; 22 23 using CommandCompleter = Function<Completions (const Context& context, 24 CommandParameters, 25 size_t, ByteCount)>; 26 27 using CommandHelper = Function<String (const Context& context, CommandParameters)>; 28 29 enum class CommandFlags 30 { 31 None = 0, 32 Hidden = 1, 33 }; 34 constexpr bool with_bit_ops(Meta::Type<CommandFlags>) { return true; } 35 36 struct CommandInfo { String name, info; }; 37 38 struct Token 39 { 40 enum class Type 41 { 42 Raw, 43 RawQuoted, 44 Expand, 45 ShellExpand, 46 RegisterExpand, 47 OptionExpand, 48 ValExpand, 49 ArgExpand, 50 FileExpand, 51 CommandSeparator 52 }; 53 54 Type type; 55 ByteCount pos; 56 String content; 57 bool terminated = false; 58 }; 59 60 struct ParseState 61 { 62 StringView str; 63 const char* pos; 64 65 operator bool() const { return pos != str.end(); } 66 }; 67 68 class CommandParser 69 { 70 public: 71 CommandParser(StringView command_line); 72 Optional<Token> read_token(bool throw_on_unterminated); 73 74 const char* pos() const { return m_state.pos; } 75 bool done() const { return not m_state; } 76 77 private: 78 ParseState m_state; 79 }; 80 81 class CommandManager : public Singleton<CommandManager> 82 { 83 public: 84 void execute(StringView command_line, Context& context, 85 const ShellContext& shell_context = ShellContext{}); 86 87 void execute_single_command(CommandParameters params, 88 Context& context, 89 const ShellContext& shell_context); 90 91 92 Optional<CommandInfo> command_info(const Context& context, 93 StringView command_line) const; 94 95 bool command_defined(StringView command_name) const; 96 97 void register_command(String command_name, CommandFunc func, 98 String docstring, 99 ParameterDesc param_desc, 100 CommandFlags flags = CommandFlags::None, 101 CommandHelper helper = CommandHelper(), 102 CommandCompleter completer = CommandCompleter()); 103 104 void set_command_completer(StringView command_name, CommandCompleter completer); 105 106 Completions complete_command_name(const Context& context, StringView query) const; 107 108 bool module_defined(StringView module_name) const; 109 110 void register_module(String module_name, String commands); 111 112 void load_module(StringView module_name, Context& context); 113 HashSet<String> loaded_modules() const; 114 115 Completions complete_module_name(StringView query) const; 116 117 struct Completer 118 { 119 Completions operator()(const Context& context, 120 StringView command_line, ByteCount cursor_pos); 121 122 private: 123 String m_last_complete_command; 124 CommandCompleter m_command_completer; 125 }; 126 127 struct NestedCompleter 128 { 129 Completions operator()(const Context& context, CommandParameters params, 130 size_t token_to_complete, ByteCount pos_in_token); 131 132 private: 133 String m_last_complete_command; 134 CommandCompleter m_command_completer; 135 }; 136 137 private: 138 struct Command 139 { 140 CommandFunc func; 141 String docstring; 142 ParameterDesc param_desc; 143 CommandFlags flags; 144 CommandHelper helper; 145 CommandCompleter completer; 146 }; 147 using CommandMap = HashMap<String, Command, MemoryDomain::Commands>; 148 CommandMap m_commands; 149 int m_command_depth = 0; 150 151 struct Module 152 { 153 enum class State 154 { 155 Registered, 156 Loading, 157 Loaded 158 }; 159 State state = State::Registered; 160 String commands; 161 }; 162 using ModuleMap = HashMap<String, Module, MemoryDomain::Commands>; 163 ModuleMap m_modules; 164 }; 165 166 String expand(StringView str, const Context& context, 167 const ShellContext& shell_context = ShellContext{}); 168 169 String expand(StringView str, const Context& context, 170 const ShellContext& shell_context, 171 const FunctionRef<String (String)>& postprocess); 172 173 } 174 175 #endif // command_manager_hh_INCLUDED