Skip to content

CompletionContext

Base classes

Name Children Inherits
Generic
typing
Abstract base class for generic types.

⋔ Inheritance diagram

graph TD
  3154893150["completion.CompletionContext"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  3481597255 --> 3154893150
  2751680898 --> 3481597255

🛈 DocStrings

Context for completion operations.

Class Type Parameters:

Name Bound or Constraints Description Default
TCommandData

Type of the data in the associated CommandContext. Used when completions need to access the command context data.

required
Source code in src/slashed/completion.py
class CompletionContext[TCommandData]:
    """Context for completion operations.

    Type Parameters:
        TCommandData: Type of the data in the associated CommandContext. Used when
                     completions need to access the command context data.
    """

    def __init__(
        self,
        document: Document,
        command_context: CommandContext[TCommandData] | None = None,
    ) -> None:
        """Initialize completion context.

        Args:
            document: Current document being completed
            command_context: Optional command execution context
        """
        self._document = document
        self._command_context = command_context
        self._command_name: str | None = None
        self._args: list[str] = []
        self._current_word: str = ""
        self._arg_position: int = 0
        self._parse_document()

    @property
    def command_context(self) -> CommandContext[TCommandData]:
        """Get command execution context.

        Returns:
            Command context

        Raises:
            RuntimeError: If command context is not set
        """
        if self._command_context is None:
            msg = "No command context available"
            raise RuntimeError(msg)
        return self._command_context

    def has_command_context(self) -> bool:
        """Check if command context is available."""
        return self._command_context is not None

    def _parse_document(self) -> None:
        """Parse document into command and arguments."""
        text = self._document.text.lstrip()

        if not text.startswith("/"):
            self._command_name = None
            self._args = []
            self._current_word = self._document.get_word_before_cursor()
            self._arg_position = 0
            return

        parts = text[1:].split()
        self._command_name = parts[0] if parts else None
        self._args = parts[1:]
        self._current_word = self._document.get_word_before_cursor()
        self._arg_position = len(text[: self._document.cursor_position].split()) - 1
        logger.info(
            "Parsed command %s with args %s. Current word: %s (position %d)",
            self._command_name,
            self._args,
            self._current_word,
            self._arg_position,
        )

    @property
    def command_name(self) -> str | None:
        """Get current command name if any."""
        return self._command_name

    @property
    def current_word(self) -> str:
        """Get word being completed."""
        return self._current_word

    @property
    def arg_position(self) -> int:
        """Get current argument position."""
        return self._arg_position

    @property
    def command_args(self) -> list[str]:
        """Get current command arguments."""
        return self._args

arg_position property

arg_position: int

Get current argument position.

command_args property

command_args: list[str]

Get current command arguments.

command_context property

command_context: CommandContext[TCommandData]

Get command execution context.

Returns:

Type Description
CommandContext[TCommandData]

Command context

Raises:

Type Description
RuntimeError

If command context is not set

command_name property

command_name: str | None

Get current command name if any.

current_word property

current_word: str

Get word being completed.

__init__

__init__(document: Document, command_context: CommandContext[TCommandData] | None = None) -> None

Initialize completion context.

Parameters:

Name Type Description Default
document Document

Current document being completed

required
command_context CommandContext[TCommandData] | None

Optional command execution context

None
Source code in src/slashed/completion.py
def __init__(
    self,
    document: Document,
    command_context: CommandContext[TCommandData] | None = None,
) -> None:
    """Initialize completion context.

    Args:
        document: Current document being completed
        command_context: Optional command execution context
    """
    self._document = document
    self._command_context = command_context
    self._command_name: str | None = None
    self._args: list[str] = []
    self._current_word: str = ""
    self._arg_position: int = 0
    self._parse_document()

has_command_context

has_command_context() -> bool

Check if command context is available.

Source code in src/slashed/completion.py
def has_command_context(self) -> bool:
    """Check if command context is available."""
    return self._command_context is not None

:fa-brands-github: Show source on GitHub