# `Drafter.Prompt.Completion`
[🔗](https://github.com/jaman/drafter/blob/v0.6.0/lib/drafter/prompt/completion.ex#L1)

Tab completion sources for text prompts.

A source is a list of words, or a function that takes the text before the cursor and
returns the candidates for the word being typed. The word being typed is the run of
characters other than whitespace that ends at the cursor, and a candidate replaces
all of it.

    iex> Drafter.Prompt.Completion.candidates(["start", "status", "stop"], "service sta")
    ["start", "status"]

    iex> Drafter.Prompt.Completion.common_prefix(["start", "status"])
    "sta"

`paths/1` builds a source that completes file names:

    Drafter.Prompt.text("Config file", complete: Drafter.Prompt.Completion.paths())

# `source`

```elixir
@type source() :: [String.t()] | (String.t() -&gt; [String.t()])
```

# `candidates`

```elixir
@spec candidates(source(), String.t()) :: [String.t()]
```

The candidates `source` offers for the text before the cursor.

A list offers its words that start with the word being typed.

# `commands`

```elixir
@spec commands(%{required(String.t()) =&gt; source()}) :: (String.t() -&gt; [String.t()])
```

A source for a command line: the first word completes to a command name and the
words after it complete from that command's own source.

`commands` maps each command name to the source for its arguments; use `[]` for a
command that takes none.

    iex> source = Drafter.Prompt.Completion.commands(%{"open" => ["inbox", "outbox"], "quit" => []})
    iex> source.("q")
    ["quit"]
    iex> source.("open in")
    ["inbox"]

# `common_prefix`

```elixir
@spec common_prefix([String.t()]) :: String.t()
```

The longest text every candidate starts with.

    iex> Drafter.Prompt.Completion.common_prefix(["lib/a.ex", "lib/b.ex"])
    "lib/"

    iex> Drafter.Prompt.Completion.common_prefix([])
    ""

# `paths`

```elixir
@spec paths(keyword()) :: (String.t() -&gt; [String.t()])
```

A source that completes the word being typed as a file path.

A candidate keeps the directory part as it was typed, so `~/Doc` completes to
`~/Documents/`. Directories end in `/`. Names starting with `.` are offered only
when the name being typed starts with `.`.

## Options

  * `:only` - `:directories` offers directories alone. Default `:all`.

# `word`

```elixir
@spec word(String.t()) :: String.t()
```

The word being typed: what follows the last whitespace in `before`.

    iex> Drafter.Prompt.Completion.word("git che")
    "che"

    iex> Drafter.Prompt.Completion.word("git ")
    ""

---

*Consult [api-reference.md](api-reference.md) for complete listing*
