Add support for hidden commands, that will not show on completion

This commit is contained in:
Maxime Coste 2013-11-12 19:38:19 +00:00
parent 5a4650aecc
commit 0244112af4
4 changed files with 43 additions and 19 deletions

View File

@ -668,6 +668,7 @@ def can also takes some flags:
completion candidate per line.
* +-allow-override+: allow the new command to replace an exisiting one
with the same name.
* +-hidden+: do not show the command in command name completions
Using shell expansion permits to define complex commands or to access
kakoune state:

View File

@ -18,17 +18,19 @@ bool CommandManager::command_defined(const String& command_name) const
void CommandManager::register_command(String command_name,
Command command,
CommandFlags flags,
CommandCompleter completer)
{
m_commands[command_name] = { std::move(command), std::move(completer) };
m_commands[command_name] = { std::move(command), flags, std::move(completer) };
}
void CommandManager::register_commands(memoryview<String> command_names,
Command command,
CommandFlags flags,
CommandCompleter completer)
{
for (auto command_name : command_names)
m_commands[command_name] = { command, completer };
m_commands[command_name] = { command, flags, completer };
}
struct parse_error : runtime_error
@ -337,7 +339,9 @@ Completions CommandManager::complete(const Context& context, CompletionFlags fla
for (auto& command : m_commands)
{
if (prefix_match(command.first, prefix))
if (command.second.flags & CommandFlags::Hidden)
continue;
if ( prefix_match(command.first, prefix))
result.candidates.push_back(command.first);
}
std::sort(result.candidates.begin(), result.candidates.end());

View File

@ -21,6 +21,19 @@ using CommandCompleter = std::function<CandidateList (const Context& context,
CompletionFlags,
CommandParameters,
size_t, ByteCount)>;
enum class CommandFlags
{
None = 0,
Hidden = 1,
};
constexpr CommandFlags operator|(CommandFlags lhs, CommandFlags rhs)
{
return (CommandFlags)((int)lhs | (int)rhs);
}
constexpr bool operator&(CommandFlags lhs, CommandFlags rhs)
{
return (bool)((int)lhs & (int)rhs);
}
class PerArgumentCommandCompleter
{
@ -55,12 +68,12 @@ public:
bool command_defined(const String& command_name) const;
void register_command(String command_name,
Command command,
void register_command(String command_name, Command command,
CommandFlags flags = CommandFlags::None,
CommandCompleter completer = CommandCompleter());
void register_commands(memoryview<String> command_names,
Command command,
void register_commands(memoryview<String> command_names, Command command,
CommandFlags flags = CommandFlags::None,
CommandCompleter completer = CommandCompleter());
private:
@ -69,6 +82,7 @@ private:
struct CommandDescriptor
{
Command command;
CommandFlags flags;
CommandCompleter completer;
};
std::unordered_map<String, CommandDescriptor> m_commands;

View File

@ -333,6 +333,7 @@ void define_command(CommandParameters params, Context& context)
{ "shell-params", false },
{ "allow-override", false },
{ "file-completion", false },
{ "hidden", false },
{ "shell-completion", true } },
ParametersParser::Flags::None,
2, 2);
@ -344,6 +345,10 @@ void define_command(CommandParameters params, Context& context)
not parser.has_option("allow-override"))
throw runtime_error("command '" + cmd_name + "' already defined");
CommandFlags flags = CommandFlags::None;
if (parser.has_option("hidden"))
flags = CommandFlags::Hidden;
String commands = parser[1];
Command cmd;
if (parser.has_option("env-params"))
@ -397,7 +402,7 @@ void define_command(CommandParameters params, Context& context)
return split(output, '\n');
};
}
CommandManager::instance().register_command(cmd_name, cmd, completer);
CommandManager::instance().register_command(cmd_name, cmd, flags, completer);
}
void echo_message(CommandParameters params, Context& context)
@ -809,9 +814,9 @@ void register_commands()
[](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos)
{ return complete_filename(prefix, context.options()["ignored_files"].get<Regex>(), cursor_pos); }
});
cm.register_commands({ "e", "edit" }, edit<false>, filename_completer);
cm.register_commands({ "e!", "edit!" }, edit<true>, filename_completer);
cm.register_commands({ "w", "write" }, write_buffer, filename_completer);
cm.register_commands({ "e", "edit" }, edit<false>, CommandFlags::None, filename_completer);
cm.register_commands({ "e!", "edit!" }, edit<true>, CommandFlags::None, filename_completer);
cm.register_commands({ "w", "write" }, write_buffer, CommandFlags::None, filename_completer);
cm.register_commands({ "wa", "writeall" }, write_all_buffers);
cm.register_commands({ "q", "quit" }, quit<false>);
cm.register_commands({ "q!", "quit!" }, quit<true>);
@ -822,19 +827,19 @@ void register_commands()
[](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos)
{ return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
});
cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer);
cm.register_commands({ "db", "delbuf" }, delete_buffer<false>, buffer_completer);
cm.register_commands({ "db!", "delbuf!" }, delete_buffer<true>, buffer_completer);
cm.register_commands({ "b", "buffer" }, show_buffer, CommandFlags::None, buffer_completer);
cm.register_commands({ "db", "delbuf" }, delete_buffer<false>, CommandFlags::None, buffer_completer);
cm.register_commands({ "db!", "delbuf!" }, delete_buffer<true>, CommandFlags::None, buffer_completer);
cm.register_commands({"nb", "namebuf"}, set_buffer_name);
auto get_highlighters = [](const Context& c) -> HighlighterGroup& { return c.window().highlighters(); };
cm.register_commands({ "ah", "addhl" }, add_highlighter, group_add_completer<HighlighterRegistry>(get_highlighters));
cm.register_commands({ "rh", "rmhl" }, rm_highlighter, group_rm_completer(get_highlighters));
cm.register_commands({ "ah", "addhl" }, add_highlighter, CommandFlags::None, group_add_completer<HighlighterRegistry>(get_highlighters));
cm.register_commands({ "rh", "rmhl" }, rm_highlighter, CommandFlags::None, group_rm_completer(get_highlighters));
cm.register_command("hook", add_hook);
cm.register_command("rmhooks", rm_hooks);
cm.register_command("source", exec_commands_in_file, filename_completer);
cm.register_command("source", exec_commands_in_file, CommandFlags::None, filename_completer);
cm.register_command("exec", exec_string);
cm.register_command("eval", eval_string);
@ -849,7 +854,7 @@ void register_commands()
cm.register_command("echo", echo_message);
cm.register_command("debug", write_debug_message);
cm.register_command("set", set_option,
cm.register_command("set", set_option, CommandFlags::None,
[](const Context& context, CompletionFlags, CommandParameters params, size_t token_to_complete, ByteCount pos_in_token)
{
if (token_to_complete == 0)
@ -873,7 +878,7 @@ void register_commands()
cm.register_commands({"ca", "colalias"}, define_color_alias);
cm.register_commands({"nc", "nameclient"}, set_client_name);
cm.register_command("cd", change_working_directory, filename_completer);
cm.register_command("cd", change_working_directory, CommandFlags::None, filename_completer);
cm.register_command("map", map_key);
}
}