Skip to content

CommandCompleter

🛈 DocStrings

Main command completion implementation.

Source code in src/slashed/completion.py
class CommandCompleter:
    """Main command completion implementation."""

    def __init__(self, commands: dict[str, BaseCommand]) -> None:
        """Initialize command completer.

        Args:
            commands: Mapping of command names to command instances
        """
        self._commands = commands
        self._global_providers: list[CompletionProvider] = []

    def add_global_provider(self, provider: CompletionProvider) -> None:
        """Add a global completion provider."""
        self._global_providers.append(provider)

    async def get_completions(
        self,
        context: CompletionContext[Any],
    ) -> AsyncIterator[CompletionItem]:
        """Get completions for the current context."""
        # If at start of command, complete command names
        logger.debug("Getting completions for command %s", context.command_name)
        if not context.command_name:
            word = context.current_word.lstrip("/")  # Remove slash for matching
            matching_commands = [name for name in self._commands if name.startswith(word)]

            # If exactly one match, complete it
            if len(matching_commands) == 1:
                meta = self._commands[matching_commands[0]].description
                cmd = matching_commands[0]
                yield CompletionItem(text=cmd, metadata=meta, kind="command")
            # If multiple matches, show all
            elif len(matching_commands) > 1:
                for name in matching_commands:
                    meta = self._commands[name].description
                    yield CompletionItem(text=name, metadata=meta, kind="command")
            return
        # Get command-specific completions
        # Get command-specific completions
        command = self._commands.get(context.command_name)
        if command and (completer := command.get_completer()):
            logger.debug("Fetching completions for command %s", command.name)
            async for item in completer.get_completions(context):
                yield item

        # Get global completions
        for provider in self._global_providers:
            logger.debug("Fetching completions for provider %r", provider)
            async for item in provider.get_completions(context):
                yield item

__init__

__init__(commands: dict[str, BaseCommand]) -> None

Initialize command completer.

Parameters:

Name Type Description Default
commands dict[str, BaseCommand]

Mapping of command names to command instances

required
Source code in src/slashed/completion.py
def __init__(self, commands: dict[str, BaseCommand]) -> None:
    """Initialize command completer.

    Args:
        commands: Mapping of command names to command instances
    """
    self._commands = commands
    self._global_providers: list[CompletionProvider] = []

add_global_provider

add_global_provider(provider: CompletionProvider) -> None

Add a global completion provider.

Source code in src/slashed/completion.py
def add_global_provider(self, provider: CompletionProvider) -> None:
    """Add a global completion provider."""
    self._global_providers.append(provider)

get_completions async

get_completions(context: CompletionContext[Any]) -> AsyncIterator[CompletionItem]

Get completions for the current context.

Source code in src/slashed/completion.py
async def get_completions(
    self,
    context: CompletionContext[Any],
) -> AsyncIterator[CompletionItem]:
    """Get completions for the current context."""
    # If at start of command, complete command names
    logger.debug("Getting completions for command %s", context.command_name)
    if not context.command_name:
        word = context.current_word.lstrip("/")  # Remove slash for matching
        matching_commands = [name for name in self._commands if name.startswith(word)]

        # If exactly one match, complete it
        if len(matching_commands) == 1:
            meta = self._commands[matching_commands[0]].description
            cmd = matching_commands[0]
            yield CompletionItem(text=cmd, metadata=meta, kind="command")
        # If multiple matches, show all
        elif len(matching_commands) > 1:
            for name in matching_commands:
                meta = self._commands[name].description
                yield CompletionItem(text=name, metadata=meta, kind="command")
        return
    # Get command-specific completions
    # Get command-specific completions
    command = self._commands.get(context.command_name)
    if command and (completer := command.get_completer()):
        logger.debug("Fetching completions for command %s", command.name)
        async for item in completer.get_completions(context):
            yield item

    # Get global completions
    for provider in self._global_providers:
        logger.debug("Fetching completions for provider %r", provider)
        async for item in provider.get_completions(context):
            yield item

:fa-brands-github: Show source on GitHub