formatting changes in command_manager.cc
This commit is contained in:
parent
341311c442
commit
20d20ae1dc
|
@ -23,7 +23,11 @@ void CommandManager::register_command(String command_name,
|
||||||
CommandFlags flags,
|
CommandFlags flags,
|
||||||
CommandCompleter completer)
|
CommandCompleter completer)
|
||||||
{
|
{
|
||||||
m_commands[command_name] = { std::move(command), std::move(docstring), std::move(param_desc), flags, std::move(completer) };
|
m_commands[command_name] = { std::move(command),
|
||||||
|
std::move(docstring),
|
||||||
|
std::move(param_desc),
|
||||||
|
flags,
|
||||||
|
std::move(completer) };
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandManager::register_commands(memoryview<String> command_names,
|
void CommandManager::register_commands(memoryview<String> command_names,
|
||||||
|
@ -34,7 +38,11 @@ void CommandManager::register_commands(memoryview<String> command_names,
|
||||||
CommandCompleter completer)
|
CommandCompleter completer)
|
||||||
{
|
{
|
||||||
kak_assert(not command_names.empty());
|
kak_assert(not command_names.empty());
|
||||||
m_commands[command_names[0]] = { std::move(command), std::move(docstring), std::move(param_desc), flags, completer };
|
m_commands[command_names[0]] = { std::move(command),
|
||||||
|
std::move(docstring),
|
||||||
|
std::move(param_desc),
|
||||||
|
flags,
|
||||||
|
completer };
|
||||||
for (size_t i = 1; i < command_names.size(); ++i)
|
for (size_t i = 1; i < command_names.size(); ++i)
|
||||||
m_aliases[command_names[i]] = command_names[0];
|
m_aliases[command_names[i]] = command_names[0];
|
||||||
}
|
}
|
||||||
|
@ -190,7 +198,8 @@ Token parse_percent_token(const String& line, ByteCount& pos)
|
||||||
String type_name = line.substr(type_start, pos - type_start);
|
String type_name = line.substr(type_start, pos - type_start);
|
||||||
|
|
||||||
if (throw_on_unterminated and pos == length)
|
if (throw_on_unterminated and pos == length)
|
||||||
throw parse_error{"expected a string delimiter after '%" + type_name + "'"};
|
throw parse_error{"expected a string delimiter after '%" +
|
||||||
|
type_name + "'"};
|
||||||
|
|
||||||
Token::Type type = token_type<throw_on_unterminated>(type_name);
|
Token::Type type = token_type<throw_on_unterminated>(type_name);
|
||||||
static const std::unordered_map<char, char> matching_delimiters = {
|
static const std::unordered_map<char, char> matching_delimiters = {
|
||||||
|
@ -239,12 +248,14 @@ TokenList parse(const String& line)
|
||||||
token_start = ++pos;
|
token_start = ++pos;
|
||||||
String token = get_until_delimiter(line, pos, delimiter);
|
String token = get_until_delimiter(line, pos, delimiter);
|
||||||
if (throw_on_unterminated and pos == length)
|
if (throw_on_unterminated and pos == length)
|
||||||
throw unterminated_string(String{delimiter}, String{delimiter});
|
throw unterminated_string(String{delimiter},
|
||||||
|
String{delimiter});
|
||||||
result.emplace_back(Token::Type::Raw, token_start,
|
result.emplace_back(Token::Type::Raw, token_start,
|
||||||
pos, std::move(token));
|
pos, std::move(token));
|
||||||
}
|
}
|
||||||
else if (line[pos] == '%')
|
else if (line[pos] == '%')
|
||||||
result.push_back(parse_percent_token<throw_on_unterminated>(line, pos));
|
result.push_back(
|
||||||
|
parse_percent_token<throw_on_unterminated>(line, pos));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (pos != length and
|
while (pos != length and
|
||||||
|
@ -257,12 +268,13 @@ TokenList parse(const String& line)
|
||||||
auto token = line.substr(token_start, pos - token_start);
|
auto token = line.substr(token_start, pos - token_start);
|
||||||
static const Regex regex{R"(\\([ \t;\n]))"};
|
static const Regex regex{R"(\\([ \t;\n]))"};
|
||||||
result.emplace_back(Token::Type::Raw, token_start, pos,
|
result.emplace_back(Token::Type::Raw, token_start, pos,
|
||||||
boost::regex_replace(token, regex, "\\1"));
|
boost::regex_replace(token, regex,
|
||||||
|
"\\1"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_command_separator(line[pos]))
|
if (is_command_separator(line[pos]))
|
||||||
result.push_back(Token{ Token::Type::CommandSeparator, pos, pos+1 });
|
result.emplace_back(Token::Type::CommandSeparator, pos, pos+1);
|
||||||
|
|
||||||
++pos;
|
++pos;
|
||||||
}
|
}
|
||||||
|
@ -305,21 +317,22 @@ String eval_token(const Token& token, Context& context,
|
||||||
memoryview<String> shell_params,
|
memoryview<String> shell_params,
|
||||||
const EnvVarMap& env_vars)
|
const EnvVarMap& env_vars)
|
||||||
{
|
{
|
||||||
|
auto& content = token.content();
|
||||||
switch (token.type())
|
switch (token.type())
|
||||||
{
|
{
|
||||||
case Token::Type::ShellExpand:
|
case Token::Type::ShellExpand:
|
||||||
return ShellManager::instance().eval(token.content(), context,
|
return ShellManager::instance().eval(content, context, shell_params,
|
||||||
shell_params, env_vars);
|
env_vars);
|
||||||
case Token::Type::RegisterExpand:
|
case Token::Type::RegisterExpand:
|
||||||
if (token.content().length() != 1)
|
if (content.length() != 1)
|
||||||
throw runtime_error("wrong register name: " + token.content());
|
throw runtime_error("wrong register name: " + content);
|
||||||
return RegisterManager::instance()[token.content()[0]].values(context)[0];
|
return RegisterManager::instance()[content[0]].values(context)[0];
|
||||||
case Token::Type::OptionExpand:
|
case Token::Type::OptionExpand:
|
||||||
return context.options()[token.content()].get_as_string();
|
return context.options()[content].get_as_string();
|
||||||
case Token::Type::RawEval:
|
case Token::Type::RawEval:
|
||||||
return eval(token.content(), context, shell_params, env_vars);
|
return eval(content, context, shell_params, env_vars);
|
||||||
case Token::Type::Raw:
|
case Token::Type::Raw:
|
||||||
return token.content();
|
return content;
|
||||||
default: kak_assert(false);
|
default: kak_assert(false);
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
@ -333,7 +346,8 @@ struct command_not_found : runtime_error
|
||||||
: runtime_error(command + " : no such command") {}
|
: runtime_error(command + " : no such command") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
CommandManager::CommandMap::const_iterator CommandManager::find_command(const String& name) const
|
CommandManager::CommandMap::const_iterator
|
||||||
|
CommandManager::find_command(const String& name) const
|
||||||
{
|
{
|
||||||
auto it = m_aliases.find(name);
|
auto it = m_aliases.find(name);
|
||||||
const String& cmd_name = it == m_aliases.end() ? name : it->second;
|
const String& cmd_name = it == m_aliases.end() ? name : it->second;
|
||||||
|
@ -351,7 +365,10 @@ void CommandManager::execute_single_command(CommandParameters params,
|
||||||
auto command_it = find_command(params[0]);
|
auto command_it = find_command(params[0]);
|
||||||
if (command_it == m_commands.end())
|
if (command_it == m_commands.end())
|
||||||
throw command_not_found(params[0]);
|
throw command_not_found(params[0]);
|
||||||
command_it->second.command(ParametersParser(param_view, command_it->second.param_desc), context);
|
|
||||||
|
ParametersParser parameter_parser(param_view,
|
||||||
|
command_it->second.param_desc);
|
||||||
|
command_it->second.command(parameter_parser, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandManager::execute(const String& command_line,
|
void CommandManager::execute(const String& command_line,
|
||||||
|
@ -375,7 +392,8 @@ void CommandManager::execute(const String& command_line,
|
||||||
else if (it->type() == Token::Type::ShellExpand)
|
else if (it->type() == Token::Type::ShellExpand)
|
||||||
{
|
{
|
||||||
auto shell_tokens = parse<true>(eval_token(*it, context,
|
auto shell_tokens = parse<true>(eval_token(*it, context,
|
||||||
shell_params, env_vars));
|
shell_params,
|
||||||
|
env_vars));
|
||||||
it = tokens.erase(it);
|
it = tokens.erase(it);
|
||||||
for (auto& token : shell_tokens)
|
for (auto& token : shell_tokens)
|
||||||
it = ++tokens.insert(it, std::move(token));
|
it = ++tokens.insert(it, std::move(token));
|
||||||
|
@ -386,12 +404,13 @@ void CommandManager::execute(const String& command_line,
|
||||||
it -= shell_tokens.size() + 1;
|
it -= shell_tokens.size() + 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
params.push_back(eval_token(*it, context, shell_params, env_vars));
|
params.push_back(eval_token(*it, context, shell_params,
|
||||||
|
env_vars));
|
||||||
}
|
}
|
||||||
execute_single_command(params, context);
|
execute_single_command(params, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<String, String> CommandManager::command_info(const String& command_line) const
|
CommandInfo CommandManager::command_info(const String& command_line) const
|
||||||
{
|
{
|
||||||
TokenList tokens = parse<false>(command_line);
|
TokenList tokens = parse<false>(command_line);
|
||||||
size_t cmd_idx = 0;
|
size_t cmd_idx = 0;
|
||||||
|
@ -402,7 +421,8 @@ std::pair<String, String> CommandManager::command_info(const String& command_lin
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<String, String> res;
|
std::pair<String, String> res;
|
||||||
if (cmd_idx == tokens.size() or tokens[cmd_idx].type() != Token::Type::Raw)
|
if (cmd_idx == tokens.size() or
|
||||||
|
tokens[cmd_idx].type() != Token::Type::Raw)
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
auto cmd = find_command(tokens[cmd_idx].content());
|
auto cmd = find_command(tokens[cmd_idx].content());
|
||||||
|
@ -422,8 +442,10 @@ std::pair<String, String> CommandManager::command_info(const String& command_lin
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Completions CommandManager::complete(const Context& context, CompletionFlags flags,
|
Completions CommandManager::complete(const Context& context,
|
||||||
const String& command_line, ByteCount cursor_pos)
|
CompletionFlags flags,
|
||||||
|
const String& command_line,
|
||||||
|
ByteCount cursor_pos)
|
||||||
{
|
{
|
||||||
TokenList tokens = parse<false>(command_line);
|
TokenList tokens = parse<false>(command_line);
|
||||||
|
|
||||||
|
@ -446,7 +468,8 @@ Completions CommandManager::complete(const Context& context, CompletionFlags fla
|
||||||
(tok_idx == cmd_idx and (tok_idx == tokens.size() or
|
(tok_idx == cmd_idx and (tok_idx == tokens.size() or
|
||||||
tokens[tok_idx].type() == Token::Type::Raw)))
|
tokens[tok_idx].type() == Token::Type::Raw)))
|
||||||
{
|
{
|
||||||
ByteCount cmd_start = tok_idx == tokens.size() ? cursor_pos
|
const bool is_end_token = tok_idx == tokens.size();
|
||||||
|
ByteCount cmd_start = is_end_token ? cursor_pos
|
||||||
: tokens[tok_idx].begin();
|
: tokens[tok_idx].begin();
|
||||||
Completions result(cmd_start, cursor_pos);
|
Completions result(cmd_start, cursor_pos);
|
||||||
String prefix = command_line.substr(cmd_start,
|
String prefix = command_line.substr(cmd_start,
|
||||||
|
@ -476,12 +499,14 @@ Completions CommandManager::complete(const Context& context, CompletionFlags fla
|
||||||
case Token::Type::OptionExpand:
|
case Token::Type::OptionExpand:
|
||||||
{
|
{
|
||||||
Completions result(start , cursor_pos);
|
Completions result(start , cursor_pos);
|
||||||
result.candidates = context.options().complete_option_name(tokens[tok_idx].content(), cursor_pos_in_token);
|
result.candidates = context.options().complete_option_name(
|
||||||
|
tokens[tok_idx].content(), cursor_pos_in_token);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
case Token::Type::ShellExpand:
|
case Token::Type::ShellExpand:
|
||||||
{
|
{
|
||||||
Completions shell_completions = shell_complete(context, flags, tokens[tok_idx].content(), cursor_pos_in_token);
|
Completions shell_completions = shell_complete(
|
||||||
|
context, flags, tokens[tok_idx].content(), cursor_pos_in_token);
|
||||||
shell_completions.start += start;
|
shell_completions.start += start;
|
||||||
shell_completions.end += start;
|
shell_completions.end += start;
|
||||||
return shell_completions;
|
return shell_completions;
|
||||||
|
@ -494,16 +519,18 @@ Completions CommandManager::complete(const Context& context, CompletionFlags fla
|
||||||
const String& command_name = tokens[cmd_idx].content();
|
const String& command_name = tokens[cmd_idx].content();
|
||||||
|
|
||||||
auto command_it = find_command(command_name);
|
auto command_it = find_command(command_name);
|
||||||
if (command_it == m_commands.end() or not command_it->second.completer)
|
if (command_it == m_commands.end() or
|
||||||
|
not command_it->second.completer)
|
||||||
return Completions();
|
return Completions();
|
||||||
|
|
||||||
std::vector<String> params;
|
std::vector<String> params;
|
||||||
for (auto token_it = tokens.begin() + cmd_idx + 1; token_it != tokens.end(); ++token_it)
|
for (auto token_it = tokens.begin() + cmd_idx + 1;
|
||||||
|
token_it != tokens.end(); ++token_it)
|
||||||
params.push_back(token_it->content());
|
params.push_back(token_it->content());
|
||||||
if (tok_idx == tokens.size())
|
if (tok_idx == tokens.size())
|
||||||
params.push_back("");
|
params.push_back("");
|
||||||
Completions completions = command_it->second.completer(context, flags, params,
|
Completions completions = command_it->second.completer(
|
||||||
tok_idx - cmd_idx - 1,
|
context, flags, params, tok_idx - cmd_idx - 1,
|
||||||
cursor_pos_in_token);
|
cursor_pos_in_token);
|
||||||
completions.start += start;
|
completions.start += start;
|
||||||
completions.end += start;
|
completions.end += start;
|
||||||
|
@ -519,7 +546,8 @@ Completions PerArgumentCommandCompleter::operator()(const Context& context,
|
||||||
CompletionFlags flags,
|
CompletionFlags flags,
|
||||||
CommandParameters params,
|
CommandParameters params,
|
||||||
size_t token_to_complete,
|
size_t token_to_complete,
|
||||||
ByteCount pos_in_token) const
|
ByteCount pos_in_token)
|
||||||
|
const
|
||||||
{
|
{
|
||||||
if (token_to_complete >= m_completers.size())
|
if (token_to_complete >= m_completers.size())
|
||||||
return Completions{};
|
return Completions{};
|
||||||
|
@ -529,7 +557,8 @@ Completions PerArgumentCommandCompleter::operator()(const Context& context,
|
||||||
|
|
||||||
const String& argument = token_to_complete < params.size() ?
|
const String& argument = token_to_complete < params.size() ?
|
||||||
params[token_to_complete] : String();
|
params[token_to_complete] : String();
|
||||||
return m_completers[token_to_complete](context, flags, argument, pos_in_token);
|
return m_completers[token_to_complete](context, flags, argument,
|
||||||
|
pos_in_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,8 @@ private:
|
||||||
std::vector<ArgumentCompleter> m_completers;
|
std::vector<ArgumentCompleter> m_completers;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using CommandInfo = std::pair<String, String>;
|
||||||
|
|
||||||
class CommandManager : public Singleton<CommandManager>
|
class CommandManager : public Singleton<CommandManager>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -67,7 +69,7 @@ public:
|
||||||
Completions complete(const Context& context, CompletionFlags flags,
|
Completions complete(const Context& context, CompletionFlags flags,
|
||||||
const String& command_line, ByteCount cursor_pos);
|
const String& command_line, ByteCount cursor_pos);
|
||||||
|
|
||||||
std::pair<String, String> command_info(const String& command_line) const;
|
CommandInfo command_info(const String& command_line) const;
|
||||||
|
|
||||||
bool command_defined(const String& command_name) const;
|
bool command_defined(const String& command_name) const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user