Skip to content
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

lang/go: redo from scratch #1313

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions BREAKING_CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ applied given the delay between changes being submitted and the time they were r
and merged.

---
* 2023-11-11 Old go bindings were removed and new ones were added that follow the standards for other languages
* 2023-06-06 Deprecate `go marks` command for VSCode. Use 'bar marks' instead.
* 2023-02-04 Deprecate `murder` command for i3wm. Use 'win kill' instead.
* 2022-12-11 Deprecate user.insert_with_history. Just use `user.add_phrase_to_history(text);
Expand Down
20 changes: 20 additions & 0 deletions lang/go/code-common-functions.talon-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
list: user.code_common_function
Copy link
Collaborator

Choose a reason for hiding this comment

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

This it the list of predeclared functions, but many of them aren't very common. (Also: make?)

These are the top ten identifiers across a pretty large Go corpus:

  4.62%   4.62% 10517712 err
  3.14%   7.75% 7149343 nil
  2.64%  10.40% 6019367 string
  2.57%  12.97% 5859233 t
  1.13%  14.09% 2564886 error
  0.99%  15.09% 2267076 c
  0.91%  16.00% 2081426 name
  0.87%  16.87% 1982235 s
  0.83%  17.70% 1881744 _
  0.81%  18.50% 1835017 ctx

len is #16 in the list. append is #35. close is #96. println is #197 (and note that that includes uses of fmt.Println!). clear is #1725. imag doesn't even make the top 5000.

I'd rather not worry about the built-ins and choose based on use.

My 2c: I'd keep len, append, close, cap, clear, delete, max, min, new, panic, and recover. And ditch the rest.

And consider maybe adding important std calls, such as errors.New, fmt.Errorf, fmt.Printf, and the like. If it'd be helpful, I can pull usage stats on those.

FWIW, here's what I have for package fmt (some of which are pretty metal):

# fmt
fmt dot print: fmt.Print
thumped up print: fmt.Print

fmt dot print line: fmt.Println
thumped up print line: fmt.Println

fmt dot print ef: fmt.Printf
thumped up print ef: fmt.Printf
fmt dot print dev: fmt.Printf
thumped up print dev: fmt.Printf
fmt dot print death: fmt.Printf
thumped up print death: fmt.Printf

# # this is how conformer D spells/hears "error eff"
fmt dot erf: fmt.Errorf
thumped up erf: fmt.Errorf

fmt dot sprintf: fmt.Sprintf
thumped up sprintf: fmt.Sprintf
fmt dot sprint dev: fmt.Sprintf
thumped up sprint dev: fmt.Sprintf
fmt dot sprint death: fmt.Sprintf
thumped up sprint death: fmt.Sprintf

Also, there are common identifiers that are hard to pronounce but aren't function calls, like req, resp, cmd. How do those fit in here?

Choose a reason for hiding this comment

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

apologies if i'm misunderstanding the context here

'make' is necessary to initialize maps, slices, chans. its pretty essential

what is the Go corpus you looked at?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, my parenthetical was saying we need make. :)

The corpus: https://github.com/mvdan/corpus

Copy link

@poetworrier poetworrier Nov 15, 2023

Choose a reason for hiding this comment

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

oh man, i worried about that, sorry! The comment I was referring to is My 2c: I'd keep len, append, close, cap, clear, delete, max, min, new, panic, and recover. And ditch the rest.
kinda interesting make doesnt show up that often then?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah. In particular, I'm surprised that new shows up more than make.

Of course, that's a list of all identifiers, not just functions.

code.language: go
-

length: len
append: append
close: close
capacity: cap
clear: clear
complex: complex
delete: delete
imaginary: imag
max: max
min: min
new: new
panic: panic
print: print
print line: println
real: real
recover: recover
36 changes: 36 additions & 0 deletions lang/go/code-types.talon-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
list: user.code_type
code.language: go
-

bool: bool
boolean: bool

byte: byte
bite: byte

complex sixty four: complex64
complex six four: complex64
complex one twenty eight: complex128
complex one two eight: complex128

number: int
integer: int
int: int
integer eight: int8
integer sixteen: int16
integer thirty two: int32
integer sixty four: int64
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe these are more commonly pronounced int sixty four. Here's most of my types talon-list:

int sixty four: int64
int sixty four: int64
you int sixty four: uint64
int thirty two: int32
you int thirty two: uint32
int sixteen: int16
you int sixteen: uint16
int eight: int8
you int eight: uint8
float thirty two: float32
float sixty four: float64
complex sixty four: complex64
complex one twenty eight: complex128
you int pointer: uintptr
you int: uint


float: float64
float thirty two: float32
float sixty four: float64

character: rune
rune: rune

string: string

machine word: uintptr
raw pointer: uintptr

comparable: comparable
211 changes: 211 additions & 0 deletions lang/go/go.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
from talon import Context, Module, actions

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


ctx.lists["user.code_type"] = {}
Xe marked this conversation as resolved.
Show resolved Hide resolved


def base_function(text: str, visibility: str):
"""Inserts a public function definition, this assumes a lot about how your editor works"""
result = f"func {actions.user.formatted_text(text, visibility)}() {{\n\n}}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This variable seems unused.


actions.user.insert_between(f"func {text}() {{\n", "")


@ctx.action_class("user")
class UserActions:
def code_insert_true():
actions.auto_insert(" true ")

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

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

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

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

def code_comment_documentation():
"""inserts godoc syntax"""
actions.key("up")
actions.auto_insert("// ")

def code_default_function(text: str):
if text == "main" or text == "mane":
actions.user.code_private_function("main")
else:
actions.user.code_public_function(text)

def code_public_function(text: str):
base_function(text, "PUBLIC_CAMEL_CASE")

def code_private_function(text: str):
base_function(text, "PRIVATE_CAMEL_CASE")

def code_insert_function(text: str, selection: str):
actions.insert(text)
actions.insert("()")
actions.key("left")
actions.insert(selection)
Comment on lines +54 to +57
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be simplified to

Suggested change
actions.insert(text)
actions.insert("()")
actions.key("left")
actions.insert(selection)
actions.user.insert_between(f"{text}(", ")")
actions.insert(selection)

or potentially this may have the same side effect in most cases:

Suggested change
actions.insert(text)
actions.insert("()")
actions.key("left")
actions.insert(selection)
actions.user.insert_between(f"{text}({selection}", ")")


def code_block():
actions.user.insert_between("{\n", "")

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.user.code_block()

def code_state_while():
actions.user.code_state_for()

def code_state_infinite_loop():
actions.insert("for ")
actions.user.code_block()

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

def code_state_for_each():
actions.user.insert_between("for _, ", " := range {")

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

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

def code_state_go_to():
actions.insert("goto ")

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

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

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

def code_import():
actions.user.insert_between("import (", ")")
actions.insert("\n")

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

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

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

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

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

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

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

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

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

def code_operator_bitwise_or_assignment():
actions.auto_insert(" |= ")

def code_operator_bitwise_exclusive_or_assignment():
actions.auto_insert(" ^= ")

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

def code_operator_bitwise_right_shift_assignment():
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_right_shift():
actions.auto_insert(" >> ")

def code_operator_lambda():
actions.user.insert_between("func() {", "}")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

def code_operator_structure_dereference():
actions.auto_insert(".")
Loading