Skip to content

Commit

Permalink
Add basic kotlin support (talonhub#1408)
Browse files Browse the repository at this point in the history
- Borrowed from
[here](okonomichiyaki@6542dc3)

Addresses talonhub#1406

---------

Co-authored-by: okonomichiyaki <74801510+okonomichiyaki@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jeff Knaus <knaus.jeff@gmail.com>
  • Loading branch information
4 people authored Apr 15, 2024
1 parent 93731ec commit 801afdd
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/modes/language_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"javascript": "js",
"javascriptreact": "jsx",
# 'json': 'json',
"kotlin": "kt",
"lua": "lua",
"markdown": "md",
# 'perl': 'pl',
Expand Down
183 changes: 183 additions & 0 deletions lang/kotlin/kotlin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
from talon import Context, Module, actions, settings

ctx = Context()
mod = Module()
ctx.matches = r"""
code.language: kotlin
"""

# Java Modifies
ctx.lists["user.code_keyword"] = {
"var": "var",
"val": "val",
"lateinit": "lateinit",
"public": "public",
"private": "private",
"protected": "protected",
"static": "static",
"synchronized": "synchronized",
"volatile": "volatile",
"transient": "transient",
"abstract": "abstract",
"interface": "interface",
"final": "final",
}


@ctx.action_class("user")
class UserActions:
def code_comment_line_prefix():
actions.insert("// ")

def code_operator_lambda():
actions.auto_insert(" -> ")

def code_operator_subscript():
actions.user.insert_between("[", "]")

def code_operator_assignment():
actions.auto_insert(" = ")

def code_operator_subtraction():
actions.auto_insert(" - ")

def code_operator_subtraction_assignment():
actions.auto_insert(" -= ")

def code_operator_addition():
actions.auto_insert(" + ")

def code_operator_addition_assignment():
actions.auto_insert(" += ")

def code_operator_multiplication():
actions.auto_insert(" * ")

def code_operator_multiplication_assignment():
actions.auto_insert(" *= ")

def code_operator_exponent():
actions.auto_insert(" ^ ")

def code_operator_division():
actions.auto_insert(" / ")

def code_operator_division_assignment():
actions.auto_insert(" /= ")

def code_operator_modulo():
actions.auto_insert(" % ")

def code_operator_modulo_assignment():
actions.auto_insert(" %= ")

def code_operator_equal():
actions.auto_insert(" == ")

def code_operator_not_equal():
actions.auto_insert(" != ")

def code_operator_greater_than():
actions.auto_insert(" > ")

def code_operator_greater_than_or_equal_to():
actions.auto_insert(" >= ")

def code_operator_less_than():
actions.auto_insert(" < ")

def code_operator_less_than_or_equal_to():
actions.auto_insert(" <= ")

def code_operator_and():
actions.auto_insert(" && ")

def code_operator_or():
actions.auto_insert(" || ")

def code_operator_bitwise_and():
actions.auto_insert(" & ")

def code_operator_bitwise_or():
actions.auto_insert(" | ")

def code_operator_bitwise_exclusive_or():
actions.auto_insert(" ^ ")

def code_operator_bitwise_left_shift():
actions.auto_insert(" << ")

def code_operator_bitwise_left_shift_assignment():
actions.auto_insert(" <<= ")

def code_operator_bitwise_right_shift():
actions.auto_insert(" >> ")

def code_operator_bitwise_right_shift_assignment():
actions.auto_insert(" >>= ")

def code_self():
actions.auto_insert("this")

def code_insert_null():
actions.insert("null")

def code_insert_is_null():
actions.insert(" == null")

def code_insert_is_not_null():
actions.insert(" != null")

def code_state_if():
actions.user.insert_between("if (", ") ")

def code_state_else_if():
actions.user.insert_between("else if (", ") ")

def code_state_else():
actions.user.insert_between(" else {", "}")
actions.key("enter")

def code_state_switch():
actions.user.insert_between("switch (", ") ")

def code_state_case():
actions.insert("case \nbreak;")
actions.edit.up()

def code_state_for():
actions.user.insert_between("for (", ") ")

def code_state_while():
actions.user.insert_between("while (", ") ")

def code_define_class():
actions.auto_insert("class ")

def code_state_return():
actions.insert("return ")

def code_insert_function(text: str, selection: str):
text += f"({selection or ''})"
actions.user.paste(text)
actions.edit.left()

def code_default_function(text: str):
result = "fun {}".format(
actions.user.formatted_text(
text, settings.get("user.code_protected_function_formatter")
)
)
actions.user.code_insert_function(result, None)

def code_public_function(text: str):
actions.user.code_default_function(text)

def code_private_function(text: str):
"""Inserts private function declaration"""
result = "private fun {}".format(
actions.user.formatted_text(
text, settings.get("user.code_private_function_formatter")
)
)
actions.user.code_insert_function(result, None)
25 changes: 25 additions & 0 deletions lang/kotlin/kotlin.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
code.language: kotlin
-
tag(): user.code_imperative
tag(): user.code_object_oriented

tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_data_bool
tag(): user.code_data_null
tag(): user.code_functions
tag(): user.code_libraries
tag(): user.code_operators_array
tag(): user.code_operators_assignment
tag(): user.code_operators_bitwise
tag(): user.code_operators_lambda
tag(): user.code_operators_math
tag(): user.code_keywords

settings():
user.code_private_function_formatter = "PRIVATE_CAMEL_CASE"
user.code_protected_function_formatter = "PRIVATE_CAMEL_CASE"
user.code_public_function_formatter = "PRIVATE_CAMEL_CASE"
user.code_private_variable_formatter = "PRIVATE_CAMEL_CASE"
user.code_protected_variable_formatter = "PRIVATE_CAMEL_CASE"
user.code_public_variable_formatter = "PRIVATE_CAMEL_CASE"

0 comments on commit 801afdd

Please sign in to comment.