Skip to content

Commit

Permalink
Resurrect go language support (#1690)
Browse files Browse the repository at this point in the history
Trying to keep it simple for a start

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Riley <com-github@sabi.net>
  • Loading branch information
3 people authored Jan 19, 2025
1 parent 72853e3 commit d1c8855
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 0 deletions.
243 changes: 243 additions & 0 deletions lang/go/go.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
from talon import Context, Module, actions, settings

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

# Primitive Types
ctx.lists["self.code_type"] = {
"boolean": "bool",
"int": "int",
"float": "float",
"byte": "byte",
"double": "double",
"short": "short",
"long": "long",
"char": "char",
"string": "string",
"rune": "rune",
"void": "void",
"channel": "channel",
}

ctx.lists["user.code_keyword"] = {
"break": "break",
"continue": "continue",
"struct": "struct",
"type": "type",
"return": "return",
"package": "package",
"import": "import",
"null": "nil",
"nil": "nil",
"true": "true",
"false": "false",
"defer": "defer",
"go": "go",
"if": "if",
"else": "else",
"switch": "switch",
"select": "select",
"const": "const",
}

ctx.lists["user.code_common_function"] = {
# golang buildin functions
"append": "append",
"length": "len",
"make": "make",
# formatting
"format print": "fmt.Printf",
"format sprint": "fmt.Sprintf",
"format print line": "fmt.Println",
# time
"time hour": "time.Hour",
"time minute": "time.Minute",
"time second": "time.Second",
"time millisecond": "time.Millisecond",
"time microsecond": "time.Microsecond",
"time nanosecond": "time.Nanosecond",
# IO
"buf I O": "bufio.",
# strings
"string convert": "strconv.",
"string convert to int": "strconv.AtoI",
}


@ctx.action_class("user")
class UserActions:
def code_operator_lambda():
actions.insert(" -> ")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

def code_operator_bitwise_and_assignment():
actions.insert(" &= ")

def code_operator_increment():
actions.insert("++")

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

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

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

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

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

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

def code_operator_indirection():
actions.insert("*")

def code_operator_address_of():
actions.insert("&")

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

def code_operator_object_accessor():
actions.insert(".")

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

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

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

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.insert("else ")
actions.key("enter")

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

def code_state_case():
actions.user.insert_between("case ", ":")

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

# There is no while keyword in go. Closest approximation is a for loop.
def code_state_while():
actions.user.insert_between("for ", " ")

def code_break():
actions.insert("break")

def code_next():
actions.insert("continue")

def code_insert_true():
actions.insert("true")

def code_insert_false():
actions.insert("false")

def code_import():
actions.insert("import ")

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

def code_comment_line_prefix():
actions.insert("// ")

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

def code_private_function(text: str):
"""Inserts private function declaration"""
result = "func {}".format(
actions.user.formatted_text(
text, settings.get("user.code_private_function_formatter")
)
)

actions.user.code_insert_function(result, None)
37 changes: 37 additions & 0 deletions lang/go/go.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
code.language: go
-

tag(): user.code_imperative

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_operators_pointer

settings():
user.code_private_function_formatter = "PRIVATE_CAMEL_CASE"
user.code_protected_function_formatter = "PRIVATE_CAMEL_CASE"
user.code_public_function_formatter = "PUBLIC_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"

(variadic | spread): "..."
declare: " := "
channel (receive | send): " <- "

[state] if (err | error):
insert("if err != nil {")
key("enter")

[state] if not (err | error):
insert("if err == nil {")
key("enter")

0 comments on commit d1c8855

Please sign in to comment.