init commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
list: user.code_common_function
|
||||
code.language: lua
|
||||
-
|
||||
|
||||
to number: "tonumber"
|
||||
I pairs: "ipairs"
|
||||
print: "print"
|
||||
print F: "printf"
|
||||
type: "type"
|
||||
assert: "assert"
|
||||
get meta table: "getmetatable"
|
||||
set meta table: "setmetatable"
|
||||
# io
|
||||
I O write: "io.write"
|
||||
I O read: "io.read"
|
||||
I O open: "io.open"
|
||||
# string
|
||||
format: "string.format"
|
||||
string G find: "string.gfind"
|
||||
string find: "string.strfind"
|
||||
string len: "string.strlen"
|
||||
string upper: "string.strupper"
|
||||
string lower: "string.strlower"
|
||||
string sub: "string.strsub"
|
||||
string G sub: "string.gsub"
|
||||
string match: "string.match"
|
||||
string G match: "string.gmatch"
|
||||
# table
|
||||
table unpack: "table.unpack"
|
||||
table insert: "table.insert"
|
||||
tabel get N: "table.getn"
|
||||
tabel sort: "table.sort"
|
||||
# math
|
||||
math max: "math.max"
|
||||
# json
|
||||
jason parse: "json.parse"
|
||||
# http
|
||||
H T T P get: "http.get"
|
||||
web get: "http.get"
|
||||
# os
|
||||
O S date: "os.date"
|
||||
O S time: "os.time"
|
||||
O S clock: "os.clock"
|
||||
O S rename: "os.rename"
|
||||
O S remove: "os.remove"
|
||||
O S getenv: "os.getenv"
|
||||
O S execute: "os.execute"
|
||||
@@ -0,0 +1,197 @@
|
||||
from talon import Context, Module, actions, settings
|
||||
|
||||
from ..tags.operators import Operators
|
||||
|
||||
mod = Module()
|
||||
ctx = Context()
|
||||
ctx.matches = r"""
|
||||
code.language: lua
|
||||
"""
|
||||
|
||||
mod.setting(
|
||||
"lua_version",
|
||||
type=float,
|
||||
default=5.1,
|
||||
desc="The default lua version to use. Dictates certain operators",
|
||||
)
|
||||
mod.tag("stylua", desc="Tag for stylua linting commands")
|
||||
|
||||
ctx.lists["user.code_libraries"] = {
|
||||
"bit": "bit",
|
||||
"I O": "io",
|
||||
"string": "string",
|
||||
"U T F eight": "utf8",
|
||||
"table": "table",
|
||||
"math": "math",
|
||||
"O S": "os",
|
||||
"debug": "debug",
|
||||
"L F S": "lfs",
|
||||
"socket": "socket",
|
||||
"H T T P": "http",
|
||||
"web": "http",
|
||||
"jason": "json",
|
||||
}
|
||||
|
||||
|
||||
@mod.capture(rule="{self.lua_functions}")
|
||||
def lua_functions(m) -> str:
|
||||
"Returns a string"
|
||||
return m.lua_functions
|
||||
|
||||
|
||||
###
|
||||
# code_operators_bitwise
|
||||
###
|
||||
|
||||
|
||||
# NOTE: < 5.3 assumes Lua BitOp usage
|
||||
# > 5.2 assumes native bitwise operators
|
||||
# TODO: Possibly add settings to define which library to use, as 5.2
|
||||
# includes bit32. Neovim uses luajit, which uses Lua BitOp
|
||||
def code_operator_bitwise_and():
|
||||
if settings.get("user.lua_version") > 5.2:
|
||||
actions.insert(" & ")
|
||||
else:
|
||||
actions.insert(" bit.band() ")
|
||||
|
||||
|
||||
def code_operator_bitwise_or():
|
||||
if settings.get("user.lua_version") > 5.2:
|
||||
actions.insert(" | ")
|
||||
else:
|
||||
actions.insert(" bit.bor() ")
|
||||
|
||||
|
||||
def code_operator_bitwise_exclusive_or():
|
||||
if settings.get("user.lua_version") > 5.2:
|
||||
actions.insert(" ~ ")
|
||||
else:
|
||||
actions.insert(" bit.xor() ")
|
||||
|
||||
|
||||
def code_operator_bitwise_left_shift():
|
||||
if settings.get("user.lua_version") > 5.2:
|
||||
actions.insert(" << ")
|
||||
else:
|
||||
actions.insert(" bit.lshift() ")
|
||||
|
||||
|
||||
def code_operator_bitwise_right_shift():
|
||||
if settings.get("user.lua_version") > 5.2:
|
||||
actions.insert(" >> ")
|
||||
else:
|
||||
actions.insert(" bit.rshift() ")
|
||||
|
||||
|
||||
operators = Operators(
|
||||
# code_operators_array
|
||||
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
|
||||
# code_operators_assignment
|
||||
ASSIGNMENT=" = ",
|
||||
# code_operators_bitwise
|
||||
BITWISE_AND=code_operator_bitwise_and,
|
||||
BITWISE_OR=code_operator_bitwise_or,
|
||||
BITWISE_EXCLUSIVE_OR=code_operator_bitwise_exclusive_or,
|
||||
BITWISE_LEFT_SHIFT=code_operator_bitwise_left_shift,
|
||||
BITWISE_RIGHT_SHIFT=code_operator_bitwise_right_shift,
|
||||
# code_operators_assignment
|
||||
MATH_SUBTRACT=" - ",
|
||||
MATH_ADD=" + ",
|
||||
MATH_MULTIPLY=" * ",
|
||||
MATH_DIVIDE=" / ",
|
||||
MATH_INTEGER_DIVIDE=" // ",
|
||||
MATH_MODULO=" % ",
|
||||
MATH_EXPONENT=" ^ ",
|
||||
MATH_EQUAL=" == ",
|
||||
MATH_NOT_EQUAL=" ~= ",
|
||||
MATH_GREATER_THAN=" > ",
|
||||
MATH_GREATER_THAN_OR_EQUAL=" >= ",
|
||||
MATH_LESS_THAN=" < ",
|
||||
MATH_LESS_THAN_OR_EQUAL=" <= ",
|
||||
MATH_AND=" and ",
|
||||
MATH_OR=" or ",
|
||||
)
|
||||
|
||||
|
||||
@ctx.action_class("user")
|
||||
class UserActions:
|
||||
def code_get_operators() -> Operators:
|
||||
return operators
|
||||
|
||||
# tag-related actions listed first, indicated by comment. corresponds to
|
||||
# the tag(): user.code_imperative style declaration in the language .talon
|
||||
# file
|
||||
|
||||
##
|
||||
# code_comment_block
|
||||
##
|
||||
def code_comment_block_prefix():
|
||||
actions.insert("--[[")
|
||||
|
||||
def code_comment_block_suffix():
|
||||
actions.insert("--]]")
|
||||
|
||||
##
|
||||
# code_data_bool
|
||||
##
|
||||
def code_insert_true():
|
||||
actions.insert("true")
|
||||
|
||||
def code_insert_false():
|
||||
actions.insert("false")
|
||||
|
||||
##
|
||||
# code_data_null
|
||||
##
|
||||
def code_insert_null():
|
||||
actions.insert("nil")
|
||||
|
||||
def code_insert_is_null():
|
||||
actions.insert(" == nil")
|
||||
|
||||
def code_insert_is_not_null():
|
||||
actions.insert(" ~= nil")
|
||||
|
||||
##
|
||||
# code_functions
|
||||
##
|
||||
def code_private_function(text: str):
|
||||
"""Inserts private function declaration"""
|
||||
result = "local function {}".format(
|
||||
actions.user.formatted_text(
|
||||
text, settings.get("user.code_private_function_formatter")
|
||||
)
|
||||
)
|
||||
|
||||
actions.insert("\n\nend")
|
||||
actions.key("up:2")
|
||||
actions.user.code_insert_function(result, None)
|
||||
|
||||
def code_public_function(text: str):
|
||||
result = "function {}".format(
|
||||
actions.user.formatted_text(
|
||||
text, settings.get("user.code_public_function_formatter")
|
||||
)
|
||||
)
|
||||
|
||||
actions.insert("\n\nend")
|
||||
actions.key("up:2")
|
||||
actions.user.code_insert_function(result, None)
|
||||
|
||||
def code_insert_function(text: str, selection: str):
|
||||
if selection:
|
||||
text = text + f"({selection})"
|
||||
else:
|
||||
text = text + "()"
|
||||
|
||||
actions.user.paste(text)
|
||||
actions.edit.left()
|
||||
|
||||
##
|
||||
# code_libraries
|
||||
##
|
||||
def code_insert_library(text: str, selection: str):
|
||||
substitutions = {"1": selection, "0": selection}
|
||||
actions.user.insert_snippet_by_name("importStatement", substitutions)
|
||||
|
||||
# non-tag related actions
|
||||
@@ -0,0 +1,67 @@
|
||||
code.language: lua
|
||||
-
|
||||
|
||||
tag(): user.code_imperative
|
||||
|
||||
tag(): user.code_comment_line
|
||||
tag(): user.code_comment_block
|
||||
tag(): user.code_data_bool
|
||||
tag(): user.code_data_null
|
||||
tag(): user.code_functions
|
||||
tag(): user.code_functions_common
|
||||
tag(): user.code_libraries
|
||||
tag(): user.code_operators_array
|
||||
tag(): user.code_operators_assignment
|
||||
tag(): user.code_operators_bitwise
|
||||
tag(): user.code_operators_math
|
||||
|
||||
# Use this tag if you use the stylua linter
|
||||
#tag(): user.stylua
|
||||
|
||||
settings():
|
||||
user.code_private_function_formatter = "SNAKE_CASE"
|
||||
user.code_public_function_formatter = "SNAKE_CASE"
|
||||
user.code_private_variable_formatter = "SNAKE_CASE"
|
||||
user.code_public_variable_formatter = "SNAKE_CASE"
|
||||
|
||||
state local: "local"
|
||||
state end: "end"
|
||||
state then: "then"
|
||||
state repeat: "repeat"
|
||||
state until: "until"
|
||||
state return (null | nil): "return nil"
|
||||
state return true: "return true"
|
||||
state return false: "return false"
|
||||
state return table: user.insert_between("return {", "}")
|
||||
state append string: " .. "
|
||||
|
||||
state label <user.text>:
|
||||
insert("::")
|
||||
user.insert_formatted(text, "SNAKE_CASE")
|
||||
insert("::")
|
||||
|
||||
require <user.code_libraries>:
|
||||
user.code_insert_library("", code_libraries)
|
||||
key(end enter)
|
||||
|
||||
state (variable | var) [<user.text>] [over]: user.code_public_variable_formatter(text)
|
||||
|
||||
state local (variable | var) [<user.text>] [over]:
|
||||
insert("local ")
|
||||
user.code_private_variable_formatter(text)
|
||||
|
||||
# for built in object methods, ex: foo:gsub()
|
||||
method <user.text>:
|
||||
insert(":")
|
||||
user.code_public_function_formatter(text)
|
||||
insert("()")
|
||||
edit.left()
|
||||
|
||||
self dot: "self."
|
||||
|
||||
index <user.word>: '["{word}"]'
|
||||
index (var | variable) <user.text>:
|
||||
var = user.formatted_text(text, "SNAKE_CASE")
|
||||
insert("[{var}]")
|
||||
|
||||
state return dick: user.insert_between("return {", "}")
|
||||
@@ -0,0 +1,6 @@
|
||||
tag: user.stylua
|
||||
-
|
||||
|
||||
lint ignore: "-- stylua: ignore"
|
||||
lint ignore start: "-- stylua: ignore start"
|
||||
lint ignore end: "-- stylua: ignore end"
|
||||
Reference in New Issue
Block a user