mirror of
https://github.com/mistralai/mistral-vibe
synced 2026-04-26 01:24:55 +02:00
Co-Authored-By: Quentin Torroba <quentin.torroba@mistral.ai> Co-Authored-By: Laure Hugo <laure.hugo@mistral.ai> Co-Authored-By: Benjamin Trom <benjamin.trom@mistral.ai> Co-Authored-By: Mathias Gesbert <mathias.gesbert@ext.mistral.ai> Co-Authored-By: Michel Thomazo <michel.thomazo@mistral.ai> Co-Authored-By: Clément Drouin <clement.drouin@mistral.ai> Co-Authored-By: Vincent Guilloux <vincent.guilloux@mistral.ai> Co-Authored-By: Valentin Berard <val@mistral.ai> Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Sequence
|
|
from typing import Protocol
|
|
|
|
from textual import events
|
|
|
|
from vibe.cli.autocompletion.base import CompletionResult
|
|
|
|
|
|
class CompletionController(Protocol):
|
|
def can_handle(self, text: str, cursor_index: int) -> bool: ...
|
|
|
|
def on_text_changed(self, text: str, cursor_index: int) -> None: ...
|
|
|
|
def on_key(
|
|
self, event: events.Key, text: str, cursor_index: int
|
|
) -> CompletionResult: ...
|
|
|
|
def reset(self) -> None: ...
|
|
|
|
|
|
class MultiCompletionManager:
|
|
def __init__(self, controllers: Sequence[CompletionController]) -> None:
|
|
self._controllers = list(controllers)
|
|
self._active: CompletionController | None = None
|
|
|
|
def on_text_changed(self, text: str, cursor_index: int) -> None:
|
|
candidate = None
|
|
for controller in self._controllers:
|
|
if controller.can_handle(text, cursor_index):
|
|
candidate = controller
|
|
break
|
|
|
|
if candidate is None:
|
|
if self._active is not None:
|
|
self._active.reset()
|
|
self._active = None
|
|
return
|
|
|
|
if candidate is not self._active:
|
|
if self._active is not None:
|
|
self._active.reset()
|
|
self._active = candidate
|
|
|
|
candidate.on_text_changed(text, cursor_index)
|
|
|
|
def on_key(
|
|
self, event: events.Key, text: str, cursor_index: int
|
|
) -> CompletionResult:
|
|
if self._active is None:
|
|
return CompletionResult.IGNORED
|
|
return self._active.on_key(event, text, cursor_index)
|
|
|
|
def reset(self) -> None:
|
|
if self._active is not None:
|
|
self._active.reset()
|
|
self._active = None
|