ChoiceCompleter
Base classes
| Name |
Children |
Inherits |
CompletionProvider slashed.completion Base class for completion providers. |
|
|
⋔ Inheritance diagram
graph TD
308201287["completers.ChoiceCompleter"]
2851341576["completion.CompletionProvider"]
925645236["abc.ABC"]
2751680898["builtins.object"]
2851341576 --> 308201287
925645236 --> 2851341576
2751680898 --> 925645236
🛈 DocStrings
Bases: CompletionProvider
Provides completion from a fixed set of choices.
Source code in src/slashed/completers.py
| class ChoiceCompleter(CompletionProvider):
"""Provides completion from a fixed set of choices."""
def __init__(
self,
choices: Sequence[str] | dict[str, str],
ignore_case: bool = True,
) -> None:
"""Initialize choice completer.
Args:
choices: Sequence of choices or mapping of choice -> description
ignore_case: Whether to do case-insensitive matching
"""
self.ignore_case = ignore_case
if isinstance(choices, dict):
self.choices = list(choices.keys())
self.descriptions = choices
else:
self.choices = list(choices)
self.descriptions = {}
async def get_completions(
self,
context: CompletionContext[Any],
) -> AsyncIterator[CompletionItem]:
"""Get matching choices."""
word = context.current_word
if self.ignore_case:
word = word.lower()
matches = (c for c in self.choices if c.lower().startswith(word))
else:
matches = (c for c in self.choices if c.startswith(word))
for choice in matches:
meta = self.descriptions.get(choice)
yield CompletionItem(text=choice, metadata=meta, kind="choice")
|
__init__
__init__(choices: Sequence[str] | dict[str, str], ignore_case: bool = True) -> None
Initialize choice completer.
Parameters:
| Name |
Type |
Description |
Default |
choices
|
Sequence[str] | dict[str, str]
|
Sequence of choices or mapping of choice -> description
|
required
|
ignore_case
|
bool
|
Whether to do case-insensitive matching
|
True
|
Source code in src/slashed/completers.py
| def __init__(
self,
choices: Sequence[str] | dict[str, str],
ignore_case: bool = True,
) -> None:
"""Initialize choice completer.
Args:
choices: Sequence of choices or mapping of choice -> description
ignore_case: Whether to do case-insensitive matching
"""
self.ignore_case = ignore_case
if isinstance(choices, dict):
self.choices = list(choices.keys())
self.descriptions = choices
else:
self.choices = list(choices)
self.descriptions = {}
|
get_completions
async
Get matching choices.
Source code in src/slashed/completers.py
| async def get_completions(
self,
context: CompletionContext[Any],
) -> AsyncIterator[CompletionItem]:
"""Get matching choices."""
word = context.current_word
if self.ignore_case:
word = word.lower()
matches = (c for c in self.choices if c.lower().startswith(word))
else:
matches = (c for c in self.choices if c.startswith(word))
for choice in matches:
meta = self.descriptions.get(choice)
yield CompletionItem(text=choice, metadata=meta, kind="choice")
|
:fa-brands-github: Show source on GitHub