Skip to content

Commit 4448d1a

Browse files
FireChickenProductivitypre-commit-ci[bot]nriley
authored
Use described functions for operators (#1964)
The git dif on this was not pretty. I needed to use app.register("ready", on_ready) to define operators to allow working with an action. The lua actions could probably be better described. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Nicholas Riley <[email protected]>
1 parent cf04a80 commit 4448d1a

File tree

15 files changed

+65
-18
lines changed

15 files changed

+65
-18
lines changed

core/described_functions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file provides support for defining functions that have an associated description
2+
# Talon's version of Python does not allow getting the code of a function through reflection, so we are instead allowing associating doc strings with lambdas and functions as a way to apply descriptions to the community help system
3+
4+
5+
from typing import Callable
6+
7+
from talon import actions
8+
9+
10+
def create_described_function(function: Callable, description: str) -> Callable:
11+
"""Creates a function with an associated doc string. Primarily intended to be used with lambdas"""
12+
function.__doc__ = description
13+
return function
14+
15+
16+
def create_described_insert_between(before: str, after: str) -> Callable:
17+
"""Creates a described function for calling actions.user.insert_between"""
18+
return create_described_function(
19+
lambda: actions.user.insert_between(before, after),
20+
f"wraps the cursor with {before} and {after}",
21+
)

core/help/help.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ def update_operators_text():
121121
# If the operator is implemented as text insertion,
122122
# display the operator text
123123
operator = operators.get(operator_text)
124-
if type(operator) == str:
124+
if isinstance(operator, str):
125125
text = ": " + operator
126+
# If a documentation string is available, use that
127+
elif doc_string := getattr(operator, "__doc__", None):
128+
text = ": " + doc_string
126129
# Otherwise display the operator name from list
127130
else:
128131
has_operator_without_text_implementation = True

lang/c/c.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import Context, Module, actions, settings
22

3+
from ...core.described_functions import create_described_insert_between
34
from ..tags.operators import Operators
45

56
mod = Module()
@@ -140,7 +141,7 @@ def c_variable(m) -> str:
140141

141142

142143
operators = Operators(
143-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
144+
SUBSCRIPT=create_described_insert_between("[", "]"),
144145
ASSIGNMENT=" = ",
145146
ASSIGNMENT_ADDITION=" += ",
146147
ASSIGNMENT_SUBTRACTION=" -= ",

lang/csharp/csharp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import Context, actions, settings
22

3+
from ...core.described_functions import create_described_insert_between
34
from ..tags.operators import Operators
45

56
ctx = Context()
@@ -9,7 +10,7 @@
910

1011
operators = Operators(
1112
# code_operators_array
12-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
13+
SUBSCRIPT=create_described_insert_between("[", "]"),
1314
# code_operators_assignment
1415
ASSIGNMENT=" = ",
1516
ASSIGNMENT_ADDITION=" += ",

lang/go/go.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import Context, Module, actions, settings
22

3+
from ...core.described_functions import create_described_insert_between
34
from ..tags.operators import Operators
45

56
ctx = Context()
@@ -47,7 +48,7 @@
4748

4849
operators = Operators(
4950
# code_operators_array
50-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
51+
SUBSCRIPT=create_described_insert_between("[", "]"),
5152
# code_operators_assignment
5253
ASSIGNMENT=" = ",
5354
ASSIGNMENT_ADDITION=" += ",

lang/java/java.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import Context, Module, actions, settings
22

3+
from ...core.described_functions import create_described_insert_between
34
from ..tags.operators import Operators
45

56
ctx = Context()
@@ -91,7 +92,7 @@
9192

9293
operators = Operators(
9394
# code_operators_array
94-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
95+
SUBSCRIPT=create_described_insert_between("[", "]"),
9596
# code_operators_assignment
9697
ASSIGNMENT=" = ",
9798
ASSIGNMENT_SUBTRACTION=" -= ",

lang/javascript/javascript.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import Context, Module, actions, settings
22

3+
from ...core.described_functions import create_described_insert_between
34
from ..tags.operators import Operators
45

56
mod = Module()
@@ -60,7 +61,7 @@
6061

6162
operators = Operators(
6263
# code_operators_array
63-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
64+
SUBSCRIPT=create_described_insert_between("[", "]"),
6465
# code_operators_assignment
6566
ASSIGNMENT=" = ",
6667
ASSIGNMENT_OR=" ||= ",

lang/kotlin/kotlin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import Context, Module, actions, settings
22

3+
from ...core.described_functions import create_described_insert_between
34
from ..tags.operators import Operators
45

56
ctx = Context()
@@ -44,7 +45,7 @@
4445

4546
operators = Operators(
4647
# code_operators_array
47-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
48+
SUBSCRIPT=create_described_insert_between("[", "]"),
4849
# code_operators_assignment
4950
ASSIGNMENT=" = ",
5051
ASSIGNMENT_ADDITION=" += ",

lang/lua/lua.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from talon import Context, Module, actions, settings
22

3+
from ...core.described_functions import (
4+
create_described_function,
5+
create_described_insert_between,
6+
)
37
from ..tags.operators import Operators
48

59
mod = Module()
@@ -46,37 +50,43 @@ def lua_functions(m) -> str:
4650

4751
# NOTE: < 5.3 assumes Lua BitOp usage
4852
# > 5.2 assumes native bitwise operators
53+
# ALSO NOTE: The documentation strings for these functions are used by help operators
4954
# TODO: Possibly add settings to define which library to use, as 5.2
5055
# includes bit32. Neovim uses luajit, which uses Lua BitOp
5156
def code_operator_bitwise_and():
57+
"Insert & or library call based on user.lua_version"
5258
if settings.get("user.lua_version") > 5.2:
5359
actions.insert(" & ")
5460
else:
5561
actions.insert(" bit.band() ")
5662

5763

5864
def code_operator_bitwise_or():
65+
"Insert | or library call based on user.lua_version"
5966
if settings.get("user.lua_version") > 5.2:
6067
actions.insert(" | ")
6168
else:
6269
actions.insert(" bit.bor() ")
6370

6471

6572
def code_operator_bitwise_exclusive_or():
73+
"Insert ~ or library call based on user.lua_version"
6674
if settings.get("user.lua_version") > 5.2:
6775
actions.insert(" ~ ")
6876
else:
6977
actions.insert(" bit.xor() ")
7078

7179

7280
def code_operator_bitwise_left_shift():
81+
"Insert << or library call based on user.lua_version"
7382
if settings.get("user.lua_version") > 5.2:
7483
actions.insert(" << ")
7584
else:
7685
actions.insert(" bit.lshift() ")
7786

7887

7988
def code_operator_bitwise_right_shift():
89+
"Insert >> or library call based on user.lua_version"
8090
if settings.get("user.lua_version") > 5.2:
8191
actions.insert(" >> ")
8292
else:
@@ -85,7 +95,7 @@ def code_operator_bitwise_right_shift():
8595

8696
operators = Operators(
8797
# code_operators_array
88-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
98+
SUBSCRIPT=create_described_insert_between("[", "]"),
8999
# code_operators_assignment
90100
ASSIGNMENT=" = ",
91101
# code_operators_bitwise

lang/python/python.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from talon import Context, Module, actions, settings
44

5+
from ...core.described_functions import create_described_insert_between
56
from ..tags.operators import Operators
67

78
mod = Module()
@@ -123,7 +124,7 @@
123124

124125
operators = Operators(
125126
# code_operators_array
126-
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
127+
SUBSCRIPT=create_described_insert_between("[", "]"),
127128
# code_operators_assignment
128129
ASSIGNMENT=" = ",
129130
ASSIGNMENT_SUBTRACTION=" -= ",
@@ -145,7 +146,7 @@
145146
BITWISE_LEFT_SHIFT=" << ",
146147
BITWISE_RIGHT_SHIFT=" >> ",
147148
# code_operators_lambda
148-
LAMBDA=lambda: actions.user.insert_between("lambda ", ": "),
149+
LAMBDA=create_described_insert_between("lambda ", ": "),
149150
# code_operators_math
150151
MATH_SUBTRACT=" - ",
151152
MATH_ADD=" + ",

0 commit comments

Comments
 (0)