1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 20:41:49 +03:00

Extract shell_complete lambda as a proper function

This commit is contained in:
Maxime Coste 2013-12-30 14:20:05 +00:00
parent 56c3d9d137
commit 0b509735ca
3 changed files with 42 additions and 31 deletions

38
src/completion.cc Normal file
View File

@ -0,0 +1,38 @@
#include "completion.hh"
#include "file.hh"
#include "context.hh"
namespace Kakoune
{
Completions shell_complete(const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos)
{
ByteCount word_start = 0;
ByteCount word_end = 0;
bool first = true;
const ByteCount len = prefix.length();
for (ByteCount pos = 0; pos < cursor_pos;)
{
if (pos != 0)
first = false;
while (pos != len and is_blank(prefix[pos]))
++pos;
word_start = pos;
while (pos != len and not is_blank(prefix[pos]))
++pos;
word_end = pos;
}
Completions completions{word_start, word_end};
if (first)
completions.candidates = complete_command(prefix.substr(word_start, word_end),
cursor_pos - word_start);
else
completions.candidates = complete_filename(prefix.substr(word_start, word_end),
context.options()["ignored_files"].get<Regex>(),
cursor_pos - word_start);
return completions;
}
}

View File

@ -40,5 +40,8 @@ inline Completions complete_nothing(const Context& context, CompletionFlags,
return Completions(cursor_pos, cursor_pos);
}
Completions shell_complete(const Context& context, CompletionFlags,
const String&, ByteCount cursor_pos);
}
#endif // completion_hh_INCLUDED

View File

@ -421,38 +421,8 @@ void command(Context& context, int)
template<InsertMode mode>
void pipe(Context& context, int)
{
auto completer = [](const Context& context, CompletionFlags flags,
const String& prefix, ByteCount cursor_pos)
{
ByteCount word_start = 0;
ByteCount word_end = 0;
bool first = true;
const ByteCount len = prefix.length();
for (ByteCount pos = 0; pos < cursor_pos;)
{
if (pos != 0)
first = false;
while (pos != len and is_blank(prefix[pos]))
++pos;
word_start = pos;
while (pos != len and not is_blank(prefix[pos]))
++pos;
word_end = pos;
}
Completions completions{word_start, word_end};
if (first)
completions.candidates = complete_command(prefix.substr(word_start, word_end),
cursor_pos - word_start);
else
completions.candidates = complete_filename(prefix.substr(word_start, word_end),
context.options()["ignored_files"].get<Regex>(),
cursor_pos - word_start);
return completions;
};
const char* prompt = mode == InsertMode::Replace ? "pipe:" : "pipe (ins):";
context.input_handler().prompt(prompt, get_color("Prompt"), completer,
context.input_handler().prompt(prompt, get_color("Prompt"), shell_complete,
[](const String& cmdline, PromptEvent event, Context& context)
{
if (event != PromptEvent::Validate)