Skip to content

Commit

Permalink
Merge pull request #1808 from timbrel/fill-in-default-parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste authored Nov 19, 2023
2 parents f8be448 + 3a8007c commit a45861c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
31 changes: 23 additions & 8 deletions core/base_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from GitSavvy.core.ui_mixins.quick_panel import show_branch_panel
from GitSavvy.core import store

from typing import Any, Callable, Dict, Iterator, List, Literal, Protocol, TypeVar, Union
from typing import Callable, Dict, Iterator, List, Literal, Protocol, TypeVar, Union


class Kont(Protocol):
Expand All @@ -19,7 +19,7 @@ def __call__(self, val: object, **kw: object) -> None:


CommandT = TypeVar("CommandT", bound="GsCommand")
Args = Dict[str, Any]
Args = Dict[str, object]
ArgProvider = Callable[[CommandT, Args, Kont], None]


Expand Down Expand Up @@ -59,36 +59,51 @@ def run_(self, edit_token, args):
args = {}

present = args.keys()
args_with_defaults = {**default_args(self.run), **args}
for name in ordered_positional_args(self.run):
if name not in present and name in self.defaults:
sync_mode = Flag()
done = make_on_done_fn(
lambda: (
None
if sync_mode
else run_command(self, args)
else run_command(self, args_with_defaults)
),
args,
args_with_defaults,
name
)
with sync_mode.set():
self.defaults[name](self, args, done)
self.defaults[name](self, args_with_defaults, done)
if not done.called:
break
else:
return super().run_(edit_token, args)
return super().run_(edit_token, args_with_defaults)


@lru_cache()
def ordered_positional_args(fn):
# type: (Callable) -> List[str]
return [
name
for name, parameter in inspect.signature(fn).parameters.items()
for name, parameter in _signature(fn).parameters.items()
if parameter.default is inspect.Parameter.empty
]


def default_args(fn):
# type: (Callable) -> Dict[str, object]
return {
name: parameter.default
for name, parameter in _signature(fn).parameters.items()
if parameter.default is not inspect.Parameter.empty
}


@lru_cache()
def _signature(fn):
# type: (Callable) -> inspect.Signature
return inspect.signature(fn)


class Flag:
def __init__(self):
self._event = threading.Event()
Expand Down
13 changes: 7 additions & 6 deletions stubs/sublime_plugin.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sublime
from typing import Any, Dict, Optional, Tuple

WindowCommandR = Optional[Tuple[str, Optional[Dict[str, Any]]]]
Args = Optional[Dict[str, object]]

api_ready = ... # type: bool
application_command_classes = ... # type: Any
Expand Down Expand Up @@ -75,17 +76,17 @@ def on_post_window_command(window_id, name, args): ...

class Command:
def name(self): ...
def is_enabled_(self, args): ...
def is_enabled_(self, args: Args): ...
def is_enabled(self) -> bool: ...
def is_visible_(self, args): ...
def is_visible_(self, args: Args): ...
def is_visible(self): ...
def is_checked_(self, args): ...
def is_checked_(self, args: Args): ...
def is_checked(self): ...
def description_(self, args): ...
def description_(self, args: Args): ...
def description(self): ...
def filter_args(self, args): ...
def filter_args(self, args: Args) -> Args: ...
def want_event(self): ...
def run_(self, edit_token, args): ...
def run_(self, edit_token, args: Args): ...
def run(self, *args, **kwargs) -> None: ...

class ApplicationCommand(Command): ...
Expand Down

0 comments on commit a45861c

Please sign in to comment.