Skip to content

Tighten Talon marks type hints #1848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2023
Merged
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
11 changes: 6 additions & 5 deletions cursorless-talon/src/marks/decorated_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from talon import Module, actions, cron, fs

from ..csv_overrides import init_csv_and_watch_changes
from .mark_types import DecoratedSymbol

mod = Module()

Expand All @@ -28,9 +29,9 @@ def cursorless_grapheme(m) -> str:
@mod.capture(
rule="[{user.cursorless_hat_color}] [{user.cursorless_hat_shape}] <user.cursorless_grapheme>"
)
def cursorless_decorated_symbol(m) -> dict[str, Any]:
def cursorless_decorated_symbol(m) -> DecoratedSymbol:
"""A decorated symbol"""
hat_color = getattr(m, "cursorless_hat_color", "default")
hat_color: str = getattr(m, "cursorless_hat_color", "default")
try:
hat_style_name = f"{hat_color}-{m.cursorless_hat_shape}"
except AttributeError:
Expand Down Expand Up @@ -82,10 +83,10 @@ def cursorless_decorated_symbol(m) -> dict[str, Any]:
}
FALLBACK_COLOR_ENABLEMENT = DEFAULT_COLOR_ENABLEMENT

unsubscribe_hat_styles = None
unsubscribe_hat_styles: Any = None


def setup_hat_styles_csv(hat_colors: dict, hat_shapes: dict):
def setup_hat_styles_csv(hat_colors: dict[str, str], hat_shapes: dict[str, str]):
global unsubscribe_hat_styles

(
Expand Down Expand Up @@ -149,7 +150,7 @@ def setup_hat_styles_csv(hat_colors: dict, hat_shapes: dict):
slow_reload_job = None


def init_hats(hat_colors: dict, hat_shapes: dict):
def init_hats(hat_colors: dict[str, str], hat_shapes: dict[str, str]):
setup_hat_styles_csv(hat_colors, hat_shapes)

vscode_settings_path: Path = actions.user.vscode_settings_path().resolve()
Expand Down
22 changes: 10 additions & 12 deletions cursorless-talon/src/marks/lines_number.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this file uses a lot of confusing terminology 😅. Your changes are strictly an improvement, though, so I'm not sure I'd change anything in this PR, but it was def tough to follow what's happening in this file. I think maybe it has lost some of its readability over the course of a couple refactors

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any

from talon import Module

from ..targets.range_target import RangeConnective
from .mark_types import LineNumber, LineNumberMark, LineNumberType

mod = Module()

Expand All @@ -14,8 +14,8 @@
@dataclass
class CustomizableTerm:
cursorlessIdentifier: str
type: str
formatter: Callable
type: LineNumberType
formatter: Callable[[int], int]


# NOTE: Please do not change these dicts. Use the CSVs for customization.
Expand All @@ -35,15 +35,13 @@ class CustomizableTerm:
"[<user.cursorless_range_connective> <user.private_cursorless_number_small>]"
)
)
def cursorless_line_number(m) -> dict[str, Any]:
def cursorless_line_number(m) -> LineNumber:
direction = directions_map[m.cursorless_line_direction]
anchor = create_line_number_mark(
direction.type, direction.formatter(m.private_cursorless_number_small_list[0])
)
if len(m.private_cursorless_number_small_list) > 1:
numbers: list[int] = m.private_cursorless_number_small_list
anchor = create_line_number_mark(direction.type, direction.formatter(numbers[0]))
if len(numbers) > 1:
active = create_line_number_mark(
direction.type,
direction.formatter(m.private_cursorless_number_small_list[1]),
direction.type, direction.formatter(numbers[1])
)
range_connective: RangeConnective = m.cursorless_range_connective
return {
Expand All @@ -56,9 +54,9 @@ def cursorless_line_number(m) -> dict[str, Any]:
return anchor


def create_line_number_mark(line_number_type: str, line_number: int) -> dict[str, Any]:
def create_line_number_mark(type: LineNumberType, line_number: int) -> LineNumberMark:
return {
"type": "lineNumber",
"lineNumberType": line_number_type,
"lineNumberType": type,
"lineNumber": line_number,
}
6 changes: 3 additions & 3 deletions cursorless-talon/src/marks/mark.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any

from talon import Module

from .mark_types import Mark

mod = Module()


Expand All @@ -12,5 +12,5 @@
"<user.cursorless_line_number>" # row (ie absolute mod 100), up, down
)
)
def cursorless_mark(m) -> dict[str, Any]:
def cursorless_mark(m) -> Mark:
return m[0]
31 changes: 31 additions & 0 deletions cursorless-talon/src/marks/mark_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Literal, TypedDict, Union


class DecoratedSymbol(TypedDict):
type: Literal["decoratedSymbol"]
symbolColor: str
character: str


SimpleMark = dict[Literal["type"], str]

LineNumberType = Literal["modulo100", "relative"]


class LineNumberMark(TypedDict):
type: Literal["lineNumber"]
lineNumberType: LineNumberType
lineNumber: int


class LineNumberRange(TypedDict):
type: Literal["range"]
anchor: LineNumberMark
active: LineNumberMark
excludeAnchor: bool
excludeActive: bool


LineNumber = Union[LineNumberMark, LineNumberRange]

Mark = Union[DecoratedSymbol, SimpleMark, LineNumber]
4 changes: 3 additions & 1 deletion cursorless-talon/src/marks/simple_mark.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from talon import Module

from .mark_types import SimpleMark

mod = Module()

mod.list("cursorless_simple_mark", desc="Cursorless simple marks")
Expand All @@ -15,7 +17,7 @@


@mod.capture(rule="{user.cursorless_simple_mark}")
def cursorless_simple_mark(m) -> dict[str, str]:
def cursorless_simple_mark(m) -> SimpleMark:
return {
"type": simple_marks[m.cursorless_simple_mark],
}
8 changes: 5 additions & 3 deletions cursorless-talon/src/targets/target_types.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from dataclasses import dataclass
from typing import Literal, Optional, Union
from typing import Any, Literal, Optional, Union

from ..marks.mark_types import Mark

RangeTargetType = Literal["vertical"]


@dataclass
class PrimitiveTarget:
type = "primitive"
mark: Optional[dict]
modifiers: Optional[list[dict]]
mark: Optional[Mark]
modifiers: Optional[list[dict[str, Any]]]


@dataclass
Expand Down