Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions st3/sublime_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
from .view_utils import new_view, close_view, LineEnding # noqa: F401
from .activity_indicator import ActivityIndicator # noqa: F401
from .window_utils import new_window, close_window # noqa: F401
from .input_generator import ( # noqa: F401
input_generator, GenericListInputHandler, GenericTextInputHandler
)
70 changes: 70 additions & 0 deletions st3/sublime_lib/input_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from sublime_plugin import CommandInputHandler, Command, ListInputHandler, TextInputHandler

from ._compat.typing import Callable, Generator, Optional, Union
from functools import wraps

FancyInputHandler = Union['GenericListInputHandler', 'GenericTextInputHandler']
GeneratorType = Generator[FancyInputHandler, object, None]


__all__ = ['input_generator', 'GenericListInputHandler', 'GenericTextInputHandler']


def input_generator(
input_function: Callable[[Command, dict], GeneratorType],
) -> Callable[[Command, dict], Optional[FancyInputHandler]]:
@wraps(input_function)
def wrapped(self: Command, args: dict) -> Optional[FancyInputHandler]:
return _next_input(input_function(self, args), None)

return wrapped


def _next_input(gen: GeneratorType, value: object) -> Optional[FancyInputHandler]:
try:
next_input_handler = gen.send(value)
next_input_handler._gen = gen
return next_input_handler
except StopIteration:
return None


class GenericListInputHandler(ListInputHandler):
_gen = None # type: GeneratorType

def __init__(
self,
*,
name: str,
items: list
) -> None:
super().__init__()
self._name = name
self._items = items

def name(self) -> str:
return self._name

def list_items(self) -> list:
return self._items

def next_input(self, args: dict) -> Optional[CommandInputHandler]:
return _next_input(self._gen, args.get(self._name, None))


class GenericTextInputHandler(TextInputHandler):
_gen = None # type: GeneratorType

def __init__(
self,
*,
name: str
) -> None:
super().__init__()
self._name = name

def name(self) -> str:
return self._name

def next_input(self, args: dict) -> Optional[CommandInputHandler]:
return _next_input(self._gen, args.get(self._name, None))
114 changes: 114 additions & 0 deletions stubs/sublime_plugin.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import sublime
from typing import Optional, Union, List, Dict, Tuple


class CommandInputHandler():
def name(self) -> str: ...
def next_input(self, args: dict) -> Optional[CommandInputHandler]: ...
def placeholder(self) -> str: ...
def initial_text(self) -> str: ...
def preview(self, arg: dict) -> Union[str, sublime.Html]: ...
def validate(self, arg: dict) -> bool: ...
def cancel(self) -> None: ...
def confirm(self, arg: dict) -> None: ...


class BackInputHandler(CommandInputHandler):
pass


class TextInputHandler(CommandInputHandler):
def description(self, text: str) -> str: ...


class ListInputHandler(CommandInputHandler):
def list_items(self) -> list: ...
def description(self, v: object, text: str) -> str: ...


class Command():
def is_enabled(self) -> bool: ...
def is_visible(self) -> bool: ...
def is_checked(self) -> bool: ...
def description(self) -> str: ...
def input(self, args: dict) -> Optional[CommandInputHandler]: ...
def input_description(self) -> str: ...


class ApplicationCommand(Command):
def run(self) -> None: ...


class WindowCommand(Command):
window: sublime.Window

def run(self) -> None: ...


class TextCommand(Command):
view: sublime.View

def run(self, edit: sublime.Edit) -> None: ...
def want_event(self) -> bool: ...


class EventListener():
def on_new(self, view: sublime.View) -> None: ...
def on_new_async(self, view: sublime.View) -> None: ...
def on_clone(self, view: sublime.View) -> None: ...
def on_clone_async(self, view: sublime.View) -> None: ...
def on_load(self, view: sublime.View) -> None: ...
def on_load_async(self, view: sublime.View) -> None: ...
def on_pre_close(self, view: sublime.View) -> None: ...
def on_close(self, view: sublime.View) -> None: ...
def on_pre_save(self, view: sublime.View) -> None: ...
def on_pre_save_async(self, view: sublime.View) -> None: ...
def on_post_save(self, view: sublime.View) -> None: ...
def on_post_save_async(self, view: sublime.View) -> None: ...
def on_modified(self, view: sublime.View) -> None: ...
def on_modified_async(self, view: sublime.View) -> None: ...
def on_selection_modified(self, view: sublime.View) -> None: ...
def on_selection_modified_async(self, view: sublime.View) -> None: ...
def on_activated(self, view: sublime.View) -> None: ...
def on_activated_async(self, view: sublime.View) -> None: ...
def on_deactivated(self, view: sublime.View) -> None: ...
def on_deactivated_async(self, view: sublime.View) -> None: ...
def on_hover(self, view: sublime.View, point: int, hover_zone: int) -> None: ...
def on_query_context(self, view: sublime.View, key: str, operator: int, operand: str, match_all: bool) -> Optional[None]: ...
def on_query_completions(self, view: sublime.View, prefix: str, locations: List[int]) -> Union[None, list, tuple]: ...
def on_text_command(self, view: sublime.View, command_name: str, args: dict) -> Tuple[str, dict]: ...
def on_post_text_command(self, view: sublime.View, command_name: str, args: dict) -> None: ...
def on_window_command(self, view: sublime.Window, command_name: str, args: dict) -> Tuple[str, dict]: ...
def on_post_window_command(self, view: sublime.Window, command_name: str, args: dict) -> None: ...


class ViewEventListener():
view: sublime.View

@classmethod
def is_applicable(cls, settings: sublime.Settings) -> bool: ...

@classmethod
def applies_to_primary_view_only(cls) -> bool: ...

def on_load(self) -> None: ...
def on_load_async(self) -> None: ...
def on_pre_close(self) -> None: ...
def on_close(self) -> None: ...
def on_pre_save(self) -> None: ...
def on_pre_save_async(self) -> None: ...
def on_post_save(self) -> None: ...
def on_post_save_async(self) -> None: ...
def on_modified(self) -> None: ...
def on_modified_async(self) -> None: ...
def on_selection_modified(self) -> None: ...
def on_selection_modified_async(self) -> None: ...
def on_activated_modified(self) -> None: ...
def on_activated_modified_async(self) -> None: ...
def on_deactivated_modified(self) -> None: ...
def on_deactivated_modified_async(self) -> None: ...
def on_hover(self, point: int, hover_zone: int) -> None: ...
def on_query_context(self, key: str, operator: int, operand: str, match_all: bool) -> Optional[None]: ...
def on_query_completions(self, prefix: str, locations: List[int]) -> Union[None, list, tuple]: ...
def on_text_command(self, command_name: str, args: dict) -> Tuple[str, dict]: ...
def on_post_text_command(self, command_name: str, args: dict) -> None: ...