summaryrefslogtreecommitdiff
path: root/src/completion.cc
blob: 9fc3ed784998b17cd6c26f5c1bfbbf449648d4d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "completion.hh"
#include "file.hh"
#include "context.hh"

namespace Kakoune
{

Completions shell_complete(const Context& context, CompletionFlags flags,
                           StringView prefix, ByteCount cursor_pos)
{
    ByteCount word_start = 0;
    ByteCount word_end = 0;

    bool command = true;
    const ByteCount len = prefix.length();
    for (ByteCount pos = 0; pos < cursor_pos;)
    {
        command = (pos == 0 or prefix[pos-1] == ';' or prefix[pos-1] == '|' or
                   (pos > 1 and prefix[pos-1] == '&' and prefix[pos-2] == '&'));
        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 (command)
        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;
}

}