Completions functions take a Context parameter
This commit is contained in:
parent
39797f87dc
commit
a712dd5bbe
|
@ -229,7 +229,8 @@ void CommandManager::execute(const String& command_line,
|
||||||
execute_single_command(params, context);
|
execute_single_command(params, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
Completions CommandManager::complete(const String& command_line, size_t cursor_pos)
|
Completions CommandManager::complete(const Context& context,
|
||||||
|
const String& command_line, size_t cursor_pos)
|
||||||
{
|
{
|
||||||
TokenPosList pos_info;
|
TokenPosList pos_info;
|
||||||
TokenList tokens = parse(command_line, &pos_info);
|
TokenList tokens = parse(command_line, &pos_info);
|
||||||
|
@ -279,13 +280,14 @@ Completions CommandManager::complete(const String& command_line, size_t cursor_p
|
||||||
std::vector<String> params;
|
std::vector<String> params;
|
||||||
for (auto token_it = tokens.begin()+1; token_it != tokens.end(); ++token_it)
|
for (auto token_it = tokens.begin()+1; token_it != tokens.end(); ++token_it)
|
||||||
params.push_back(token_it->content());
|
params.push_back(token_it->content());
|
||||||
result.candidates = command_it->second.completer(params,
|
result.candidates = command_it->second.completer(context, params,
|
||||||
token_to_complete - 1,
|
token_to_complete - 1,
|
||||||
cursor_pos_in_token);
|
cursor_pos_in_token);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
CandidateList PerArgumentCommandCompleter::operator()(const CommandParameters& params,
|
CandidateList PerArgumentCommandCompleter::operator()(const Context& context,
|
||||||
|
const CommandParameters& params,
|
||||||
size_t token_to_complete,
|
size_t token_to_complete,
|
||||||
size_t pos_in_token) const
|
size_t pos_in_token) const
|
||||||
{
|
{
|
||||||
|
@ -297,7 +299,7 @@ CandidateList PerArgumentCommandCompleter::operator()(const CommandParameters& p
|
||||||
|
|
||||||
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](argument, pos_in_token);
|
return m_completers[token_to_complete](context, argument, pos_in_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,19 +26,22 @@ using CommandParameters = memoryview<String>;
|
||||||
typedef std::function<void (const CommandParameters&,
|
typedef std::function<void (const CommandParameters&,
|
||||||
const Context& context)> Command;
|
const Context& context)> Command;
|
||||||
|
|
||||||
typedef std::function<CandidateList (const CommandParameters&,
|
typedef std::function<CandidateList (const Context& context,
|
||||||
|
const CommandParameters&,
|
||||||
size_t, size_t)> CommandCompleter;
|
size_t, size_t)> CommandCompleter;
|
||||||
|
|
||||||
class PerArgumentCommandCompleter
|
class PerArgumentCommandCompleter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::function<CandidateList (const String&, size_t)> ArgumentCompleter;
|
typedef std::function<CandidateList (const Context&,
|
||||||
|
const String&, size_t)> ArgumentCompleter;
|
||||||
typedef memoryview<ArgumentCompleter> ArgumentCompleterList;
|
typedef memoryview<ArgumentCompleter> ArgumentCompleterList;
|
||||||
|
|
||||||
PerArgumentCommandCompleter(const ArgumentCompleterList& completers)
|
PerArgumentCommandCompleter(const ArgumentCompleterList& completers)
|
||||||
: m_completers(completers.begin(), completers.end()) {}
|
: m_completers(completers.begin(), completers.end()) {}
|
||||||
|
|
||||||
CandidateList operator()(const CommandParameters& params,
|
CandidateList operator()(const Context& context,
|
||||||
|
const CommandParameters& params,
|
||||||
size_t token_to_complete,
|
size_t token_to_complete,
|
||||||
size_t pos_in_token) const;
|
size_t pos_in_token) const;
|
||||||
|
|
||||||
|
@ -52,7 +55,8 @@ public:
|
||||||
void execute(const String& command_line, const Context& context,
|
void execute(const String& command_line, const Context& context,
|
||||||
const EnvVarMap& env_vars = EnvVarMap());
|
const EnvVarMap& env_vars = EnvVarMap());
|
||||||
|
|
||||||
Completions complete(const String& command_line, size_t cursor_pos);
|
Completions complete(const Context& context,
|
||||||
|
const String& command_line, size_t cursor_pos);
|
||||||
|
|
||||||
bool command_defined(const String& command_name) const;
|
bool command_defined(const String& command_name) const;
|
||||||
|
|
||||||
|
|
|
@ -506,7 +506,7 @@ void define_command(const CommandParameters& params, const Context& context)
|
||||||
if (parser.has_option("shell-completion"))
|
if (parser.has_option("shell-completion"))
|
||||||
{
|
{
|
||||||
String shell_cmd = parser.option_value("shell-completion");
|
String shell_cmd = parser.option_value("shell-completion");
|
||||||
auto completer = [=](const CommandParameters& params,
|
auto completer = [=](const Context& context, const CommandParameters& params,
|
||||||
size_t token_to_complete, size_t pos_in_token)
|
size_t token_to_complete, size_t pos_in_token)
|
||||||
{
|
{
|
||||||
EnvVarMap vars = params_to_env_var_map(params);
|
EnvVarMap vars = params_to_env_var_map(params);
|
||||||
|
@ -759,16 +759,17 @@ void register_commands()
|
||||||
cm.register_command("wq!", write_and_quit<true>);
|
cm.register_command("wq!", write_and_quit<true>);
|
||||||
|
|
||||||
PerArgumentCommandCompleter buffer_completer({
|
PerArgumentCommandCompleter buffer_completer({
|
||||||
[](const String& prefix, size_t cursor_pos)
|
[](const Context& context, const String& prefix, size_t cursor_pos)
|
||||||
{ return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
|
{ return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
|
||||||
});
|
});
|
||||||
cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer);
|
cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer);
|
||||||
cm.register_commands({ "db", "delbuf" }, delete_buffer, buffer_completer);
|
cm.register_commands({ "db", "delbuf" }, delete_buffer, buffer_completer);
|
||||||
|
|
||||||
cm.register_commands({ "ah", "addhl" }, add_highlighter,
|
cm.register_commands({ "ah", "addhl" }, add_highlighter,
|
||||||
[](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)
|
[](const Context& context, const CommandParameters& params,
|
||||||
|
size_t token_to_complete, size_t pos_in_token)
|
||||||
{
|
{
|
||||||
Window& w = main_context.window();
|
Window& w = context.window();
|
||||||
const String& arg = token_to_complete < params.size() ?
|
const String& arg = token_to_complete < params.size() ?
|
||||||
params[token_to_complete] : String();
|
params[token_to_complete] : String();
|
||||||
if (token_to_complete == 1 and params[0] == "-group")
|
if (token_to_complete == 1 and params[0] == "-group")
|
||||||
|
@ -779,9 +780,10 @@ void register_commands()
|
||||||
return CandidateList();
|
return CandidateList();
|
||||||
});
|
});
|
||||||
cm.register_commands({ "rh", "rmhl" }, rm_highlighter,
|
cm.register_commands({ "rh", "rmhl" }, rm_highlighter,
|
||||||
[](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)
|
[](const Context& context, const CommandParameters& params,
|
||||||
|
size_t token_to_complete, size_t pos_in_token)
|
||||||
{
|
{
|
||||||
Window& w = main_context.window();
|
Window& w = context.window();
|
||||||
const String& arg = token_to_complete < params.size() ?
|
const String& arg = token_to_complete < params.size() ?
|
||||||
params[token_to_complete] : String();
|
params[token_to_complete] : String();
|
||||||
if (token_to_complete == 1 and params[0] == "-group")
|
if (token_to_complete == 1 and params[0] == "-group")
|
||||||
|
@ -792,9 +794,10 @@ void register_commands()
|
||||||
return w.highlighters().complete_id(arg, pos_in_token);
|
return w.highlighters().complete_id(arg, pos_in_token);
|
||||||
});
|
});
|
||||||
cm.register_commands({ "af", "addfilter" }, add_filter,
|
cm.register_commands({ "af", "addfilter" }, add_filter,
|
||||||
[](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)
|
[](const Context& context, const CommandParameters& params,
|
||||||
|
size_t token_to_complete, size_t pos_in_token)
|
||||||
{
|
{
|
||||||
Window& w = main_context.window();
|
Window& w = context.window();
|
||||||
const String& arg = token_to_complete < params.size() ?
|
const String& arg = token_to_complete < params.size() ?
|
||||||
params[token_to_complete] : String();
|
params[token_to_complete] : String();
|
||||||
if (token_to_complete == 1 and params[0] == "-group")
|
if (token_to_complete == 1 and params[0] == "-group")
|
||||||
|
@ -805,9 +808,10 @@ void register_commands()
|
||||||
return CandidateList();
|
return CandidateList();
|
||||||
});
|
});
|
||||||
cm.register_commands({ "rf", "rmfilter" }, rm_filter,
|
cm.register_commands({ "rf", "rmfilter" }, rm_filter,
|
||||||
[](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)
|
[](const Context& context, const CommandParameters& params,
|
||||||
|
size_t token_to_complete, size_t pos_in_token)
|
||||||
{
|
{
|
||||||
Window& w = main_context.window();
|
Window& w = context.window();
|
||||||
const String& arg = token_to_complete < params.size() ?
|
const String& arg = token_to_complete < params.size() ?
|
||||||
params[token_to_complete] : String();
|
params[token_to_complete] : String();
|
||||||
if (token_to_complete == 1 and params[0] == "-group")
|
if (token_to_complete == 1 and params[0] == "-group")
|
||||||
|
@ -834,22 +838,22 @@ void register_commands()
|
||||||
[](const CommandParameters& params, const Context& context)
|
[](const CommandParameters& params, const Context& context)
|
||||||
{ set_option(GlobalOptionManager::instance(), params, context); },
|
{ set_option(GlobalOptionManager::instance(), params, context); },
|
||||||
PerArgumentCommandCompleter({
|
PerArgumentCommandCompleter({
|
||||||
[](const String& prefix, size_t cursor_pos)
|
[](const Context& context, const String& prefix, size_t cursor_pos)
|
||||||
{ return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); }
|
{ return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); }
|
||||||
}));
|
}));
|
||||||
cm.register_commands({ "setb", "setbuffer" },
|
cm.register_commands({ "setb", "setbuffer" },
|
||||||
[](const CommandParameters& params, const Context& context)
|
[](const CommandParameters& params, const Context& context)
|
||||||
{ set_option(context.buffer().option_manager(), params, context); },
|
{ set_option(context.buffer().option_manager(), params, context); },
|
||||||
PerArgumentCommandCompleter({
|
PerArgumentCommandCompleter({
|
||||||
[](const String& prefix, size_t cursor_pos)
|
[](const Context& context, const String& prefix, size_t cursor_pos)
|
||||||
{ return main_context.buffer().option_manager().complete_option_name(prefix, cursor_pos); }
|
{ return context.buffer().option_manager().complete_option_name(prefix, cursor_pos); }
|
||||||
}));
|
}));
|
||||||
cm.register_commands({ "setw", "setwindow" },
|
cm.register_commands({ "setw", "setwindow" },
|
||||||
[](const CommandParameters& params, const Context& context)
|
[](const CommandParameters& params, const Context& context)
|
||||||
{ set_option(context.window().option_manager(), params, context); },
|
{ set_option(context.window().option_manager(), params, context); },
|
||||||
PerArgumentCommandCompleter({
|
PerArgumentCommandCompleter({
|
||||||
[](const String& prefix, size_t cursor_pos)
|
[](const Context& context, const String& prefix, size_t cursor_pos)
|
||||||
{ return main_context.window().option_manager().complete_option_name(prefix, cursor_pos); }
|
{ return context.window().option_manager().complete_option_name(prefix, cursor_pos); }
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,8 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
CandidateList complete_filename(const String& prefix,
|
CandidateList complete_filename(const Context& context,
|
||||||
|
const String& prefix,
|
||||||
size_t cursor_pos)
|
size_t cursor_pos)
|
||||||
{
|
{
|
||||||
String real_prefix = prefix.substr(0, cursor_pos);
|
String real_prefix = prefix.substr(0, cursor_pos);
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
|
class Context;
|
||||||
|
|
||||||
typedef std::vector<String> CandidateList;
|
typedef std::vector<String> CandidateList;
|
||||||
|
|
||||||
struct Completions
|
struct Completions
|
||||||
|
@ -24,12 +26,15 @@ struct Completions
|
||||||
: start(start), end(end) {}
|
: start(start), end(end) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
CandidateList complete_filename(const String& prefix,
|
CandidateList complete_filename(const Context& context,
|
||||||
|
const String& prefix,
|
||||||
size_t cursor_pos = -1);
|
size_t cursor_pos = -1);
|
||||||
|
|
||||||
typedef std::function<Completions (const String&, size_t)> Completer;
|
typedef std::function<Completions (const Context&,
|
||||||
|
const String&, size_t)> Completer;
|
||||||
|
|
||||||
inline Completions complete_nothing(const String&, size_t cursor_pos)
|
inline Completions complete_nothing(const Context& context,
|
||||||
|
const String&, size_t cursor_pos)
|
||||||
{
|
{
|
||||||
return Completions(cursor_pos, cursor_pos);
|
return Completions(cursor_pos, cursor_pos);
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,7 +172,7 @@ void do_command(const Context& context)
|
||||||
auto cmdline = prompt(":", context,
|
auto cmdline = prompt(":", context,
|
||||||
std::bind(&CommandManager::complete,
|
std::bind(&CommandManager::complete,
|
||||||
&CommandManager::instance(),
|
&CommandManager::instance(),
|
||||||
_1, _2));
|
_1, _2, _3));
|
||||||
|
|
||||||
CommandManager::instance().execute(cmdline, context);
|
CommandManager::instance().execute(cmdline, context);
|
||||||
}
|
}
|
||||||
|
|
|
@ -261,7 +261,7 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet
|
||||||
{
|
{
|
||||||
if (current_completion == -1)
|
if (current_completion == -1)
|
||||||
{
|
{
|
||||||
completions = completer(result, cursor_pos);
|
completions = completer(context, result, cursor_pos);
|
||||||
if (completions.candidates.empty())
|
if (completions.candidates.empty())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user