init commit
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
from talon import Context, Module, actions, settings
|
||||
|
||||
from ..tags.operators import Operators
|
||||
|
||||
mod = Module()
|
||||
|
||||
ctx = Context()
|
||||
ctx.matches = r"""
|
||||
code.language: c
|
||||
"""
|
||||
|
||||
ctx.lists["self.c_pointers"] = {
|
||||
"pointer": "*",
|
||||
"pointer to pointer": "**",
|
||||
}
|
||||
|
||||
ctx.lists["self.stdint_signed"] = {
|
||||
"signed": "",
|
||||
"unsigned": "u",
|
||||
"you": "u",
|
||||
}
|
||||
|
||||
ctx.lists["self.c_signed"] = {
|
||||
"signed": "signed",
|
||||
"unsigned": "unsigned",
|
||||
}
|
||||
|
||||
ctx.lists["self.c_keywords"] = {
|
||||
"static": "static",
|
||||
"volatile": "volatile",
|
||||
"register": "register",
|
||||
}
|
||||
|
||||
ctx.lists["self.stdint_types"] = {
|
||||
"character": "int8_t",
|
||||
"char": "int8_t",
|
||||
"short": "int16_t",
|
||||
"long": "int32_t",
|
||||
"long long": "int64_t",
|
||||
"int": "int32_t",
|
||||
"integer": "int32_t",
|
||||
"void": "void",
|
||||
"double": "double",
|
||||
"struct": "struct",
|
||||
"struck": "struct",
|
||||
"num": "enum",
|
||||
"union": "union",
|
||||
"float": "float",
|
||||
}
|
||||
|
||||
ctx.lists["self.c_types"] = {
|
||||
"character": "char",
|
||||
"char": "char",
|
||||
"short": "short",
|
||||
"long": "long",
|
||||
"int": "int",
|
||||
"integer": "int",
|
||||
"void": "void",
|
||||
"double": "double",
|
||||
"struct": "struct",
|
||||
"struck": "struct",
|
||||
"num": "enum",
|
||||
"union": "union",
|
||||
"float": "float",
|
||||
}
|
||||
|
||||
ctx.lists["user.code_libraries"] = {
|
||||
"assert": "assert.h",
|
||||
"type": "ctype.h",
|
||||
"error": "errno.h",
|
||||
"float": "float.h",
|
||||
"limits": "limits.h",
|
||||
"locale": "locale.h",
|
||||
"math": "math.h",
|
||||
"set jump": "setjmp.h",
|
||||
"signal": "signal.h",
|
||||
"arguments": "stdarg.h",
|
||||
"definition": "stddef.h",
|
||||
"input": "stdio.h",
|
||||
"output": "stdio.h",
|
||||
"library": "stdlib.h",
|
||||
"string": "string.h",
|
||||
"time": "time.h",
|
||||
"standard int": "stdint.h",
|
||||
}
|
||||
|
||||
mod.list("c_pointers", desc="Common C pointers")
|
||||
mod.list("c_signed", desc="Common C datatype signed modifiers")
|
||||
mod.list("c_keywords", desc="C keywords")
|
||||
mod.list("c_types", desc="Common C types")
|
||||
mod.list("stdint_types", desc="Common stdint C types")
|
||||
mod.list("stdint_signed", desc="Common stdint C datatype signed modifiers")
|
||||
|
||||
|
||||
@mod.capture(rule="{self.c_pointers}")
|
||||
def c_pointers(m) -> str:
|
||||
"Returns a string"
|
||||
return m.c_pointers
|
||||
|
||||
|
||||
@mod.capture(rule="{self.c_signed}")
|
||||
def c_signed(m) -> str:
|
||||
"Returns a string"
|
||||
return m.c_signed
|
||||
|
||||
|
||||
@mod.capture(rule="{self.c_keywords}")
|
||||
def c_keywords(m) -> str:
|
||||
"Returns a string"
|
||||
return m.c_keywords
|
||||
|
||||
|
||||
@mod.capture(rule="{self.c_types}")
|
||||
def c_types(m) -> str:
|
||||
"Returns a string"
|
||||
return m.c_types
|
||||
|
||||
|
||||
@mod.capture(rule="{self.stdint_types}")
|
||||
def stdint_types(m) -> str:
|
||||
"Returns a string"
|
||||
return m.stdint_types
|
||||
|
||||
|
||||
@mod.capture(rule="{self.stdint_signed}")
|
||||
def stdint_signed(m) -> str:
|
||||
"Returns a string"
|
||||
return m.stdint_signed
|
||||
|
||||
|
||||
@mod.capture(rule="[<self.c_signed>] <self.c_types> [<self.c_pointers>+]")
|
||||
def c_cast(m) -> str:
|
||||
"Returns a string"
|
||||
return "(" + " ".join(list(m)) + ")"
|
||||
|
||||
|
||||
@mod.capture(rule="[<self.stdint_signed>] <self.stdint_types> [<self.c_pointers>+]")
|
||||
def stdint_cast(m) -> str:
|
||||
"Returns a string"
|
||||
return "(" + "".join(list(m)) + ")"
|
||||
|
||||
|
||||
@mod.capture(rule="[<self.c_signed>] <self.c_types> [<self.c_pointers>]")
|
||||
def c_variable(m) -> str:
|
||||
"Returns a string"
|
||||
return " ".join(list(m))
|
||||
|
||||
|
||||
operators = Operators(
|
||||
SUBSCRIPT=lambda: actions.user.insert_between("[", "]"),
|
||||
ASSIGNMENT=" = ",
|
||||
ASSIGNMENT_ADDITION=" += ",
|
||||
ASSIGNMENT_SUBTRACTION=" -= ",
|
||||
ASSIGNMENT_MULTIPLICATION=" *= ",
|
||||
ASSIGNMENT_DIVISION=" /= ",
|
||||
ASSIGNMENT_MODULO=" %= ",
|
||||
ASSIGNMENT_INCREMENT="++",
|
||||
ASSIGNMENT_BITWISE_AND=" &= ",
|
||||
ASSIGNMENT_BITWISE_OR=" |= ",
|
||||
ASSIGNMENT_BITWISE_EXCLUSIVE_OR=" ^= ",
|
||||
ASSIGNMENT_BITWISE_LEFT_SHIFT=" <<= ",
|
||||
ASSIGNMENT_BITWISE_RIGHT_SHIFT=" >>= ",
|
||||
BITWISE_AND=" & ",
|
||||
BITWISE_OR=" | ",
|
||||
BITWISE_NOT="~",
|
||||
BITWISE_EXCLUSIVE_OR=" ^ ",
|
||||
BITWISE_LEFT_SHIFT=" << ",
|
||||
BITWISE_RIGHT_SHIFT=" >> ",
|
||||
MATH_SUBTRACT=" - ",
|
||||
MATH_ADD=" + ",
|
||||
MATH_MULTIPLY=" * ",
|
||||
MATH_DIVIDE=" / ",
|
||||
MATH_MODULO=" % ",
|
||||
MATH_EQUAL=" == ",
|
||||
MATH_NOT_EQUAL=" != ",
|
||||
MATH_GREATER_THAN=" > ",
|
||||
MATH_GREATER_THAN_OR_EQUAL=" >= ",
|
||||
MATH_LESS_THAN=" < ",
|
||||
MATH_LESS_THAN_OR_EQUAL=" <= ",
|
||||
MATH_AND=" && ",
|
||||
MATH_OR=" || ",
|
||||
MATH_NOT="!",
|
||||
POINTER_INDIRECTION="*",
|
||||
POINTER_ADDRESS_OF="&",
|
||||
POINTER_STRUCTURE_DEREFERENCE="->",
|
||||
)
|
||||
|
||||
|
||||
@ctx.action_class("user")
|
||||
class UserActions:
|
||||
def code_get_operators() -> Operators:
|
||||
return operators
|
||||
|
||||
def code_insert_null():
|
||||
actions.auto_insert("NULL")
|
||||
|
||||
def code_insert_is_null():
|
||||
actions.auto_insert(" == NULL ")
|
||||
|
||||
def code_insert_is_not_null():
|
||||
actions.auto_insert(" != NULL")
|
||||
|
||||
def code_insert_true():
|
||||
actions.auto_insert("true")
|
||||
|
||||
def code_insert_false():
|
||||
actions.auto_insert("false")
|
||||
|
||||
def code_insert_function(text: str, selection: str):
|
||||
if selection:
|
||||
text = text + f"({selection})"
|
||||
else:
|
||||
text = text + "()"
|
||||
|
||||
actions.user.paste(text)
|
||||
actions.edit.left()
|
||||
|
||||
# TODO - it would be nice that you integrate that types from c_cast
|
||||
# instead of defaulting to void
|
||||
def code_private_function(text: str):
|
||||
"""Inserts private function declaration"""
|
||||
result = "void {}".format(
|
||||
actions.user.formatted_text(
|
||||
text, settings.get("user.code_private_function_formatter")
|
||||
)
|
||||
)
|
||||
|
||||
actions.user.code_insert_function(result, None)
|
||||
|
||||
def code_private_static_function(text: str):
|
||||
"""Inserts private static function"""
|
||||
result = "static void {}".format(
|
||||
actions.user.formatted_text(
|
||||
text, settings.get("user.code_private_function_formatter")
|
||||
)
|
||||
)
|
||||
|
||||
actions.user.code_insert_function(result, None)
|
||||
|
||||
def code_insert_library(text: str, selection: str):
|
||||
actions.user.paste(f"#include <{text}>")
|
||||
@@ -0,0 +1,86 @@
|
||||
code.language: c
|
||||
-
|
||||
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_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
|
||||
tag(): user.code_operators_pointer
|
||||
|
||||
settings():
|
||||
user.code_private_function_formatter = "SNAKE_CASE"
|
||||
user.code_protected_function_formatter = "SNAKE_CASE"
|
||||
user.code_public_function_formatter = "SNAKE_CASE"
|
||||
user.code_private_variable_formatter = "SNAKE_CASE"
|
||||
user.code_protected_variable_formatter = "SNAKE_CASE"
|
||||
user.code_public_variable_formatter = "SNAKE_CASE"
|
||||
|
||||
# NOTE: migrated from generic, as they were only used here, though once cpp support is added, perhaps these should be migrated to a tag together with the commands below
|
||||
state include: user.insert_snippet_by_name("importStatement")
|
||||
state include system: user.insert_snippet_by_name("includeSystemStatement")
|
||||
state include local: user.insert_snippet_by_name("includeLocalStatement")
|
||||
state type deaf: insert("typedef ")
|
||||
state type deaf struct: user.insert_snippet_by_name("typedefStructDeclaration")
|
||||
|
||||
# XXX - create a preprocessor tag for these, as they will match cpp, etc
|
||||
state define: user.insert_snippet_by_name("preprocessorDefineStatement")
|
||||
state (undefine | undeaf): user.insert_snippet_by_name("preprocessorUndefineStatement")
|
||||
state if (define | deaf): user.insert_snippet_by_name("preprocessorIfDefineStatement")
|
||||
[state] define <user.text>$:
|
||||
user.insert_snippet_by_name_with_phrase("preprocessorDefineStatement", text)
|
||||
[state] (undefine | undeaf) <user.text>$:
|
||||
user.insert_snippet_by_name_with_phrase("preprocessorUndefineStatement", text)
|
||||
[state] if (define | deaf) <user.text>$:
|
||||
user.insert_snippet_by_name_with_phrase("preprocessorIfDefineStatement", text)
|
||||
|
||||
# XXX - preprocessor instead of pre?
|
||||
state pre if: user.insert_snippet_by_name("preprocessorIfStatement")
|
||||
state error: user.insert_snippet_by_name("preprocessorErrorStatement")
|
||||
state pre else if: user.insert_snippet_by_name("preprocessorElseIfStatement")
|
||||
state pre end: user.insert_snippet_by_name("preprocessorEndIfStatement")
|
||||
state pragma: user.insert_snippet_by_name("preprocessorPragmaStatement")
|
||||
state default: "default:\nbreak;"
|
||||
|
||||
#control flow
|
||||
#best used with a push like command
|
||||
#the below example may not work in editors that automatically add the closing brace
|
||||
#if so uncomment the two lines and comment out the rest accordingly
|
||||
push braces:
|
||||
edit.line_end()
|
||||
#insert("{")
|
||||
#key(enter)
|
||||
insert("{}")
|
||||
edit.left()
|
||||
key(enter)
|
||||
key(enter)
|
||||
edit.up()
|
||||
|
||||
# Declare variables or structs etc.
|
||||
# Ex. * int myList
|
||||
<user.c_variable> <phrase>:
|
||||
insert("{c_variable} ")
|
||||
insert(user.formatted_text(phrase, "PRIVATE_CAMEL_CASE,NO_SPACES"))
|
||||
|
||||
<user.c_variable> <user.letter>: insert("{c_variable} {letter} ")
|
||||
|
||||
# Ex. (int *)
|
||||
cast to <user.c_cast>: "{c_cast}"
|
||||
standard cast to <user.stdint_cast>: "{stdint_cast}"
|
||||
<user.c_types>: "{c_types}"
|
||||
<user.c_pointers>: "{c_pointers}"
|
||||
<user.c_keywords>: "{c_keywords}"
|
||||
<user.c_signed>: "{c_signed} "
|
||||
standard <user.stdint_types>: "{stdint_types}"
|
||||
int main: user.insert_between("int main(", ")")
|
||||
|
||||
include <user.code_libraries>:
|
||||
user.code_insert_library(code_libraries, "")
|
||||
key(end enter)
|
||||
@@ -0,0 +1,47 @@
|
||||
list: user.code_common_function
|
||||
code.language: c
|
||||
-
|
||||
|
||||
alloc ah: alloca
|
||||
ay to eye: atoi
|
||||
ef close: fclose
|
||||
ef open: fopen
|
||||
ef read: fread
|
||||
ef write: fwrite
|
||||
em map: mmap
|
||||
em un map: munmap
|
||||
es en print eff: sprintf
|
||||
es print eff: sprintf
|
||||
exit
|
||||
free
|
||||
get char: getchar
|
||||
get op: getopt
|
||||
is digit: isdigit
|
||||
ma map: mmap
|
||||
malloc
|
||||
mem copy: memcpy
|
||||
mem set: memset
|
||||
print eff: printf
|
||||
re alloc: realloc
|
||||
see alloc: calloc
|
||||
set jump: setjmp
|
||||
signal
|
||||
size of: sizeof
|
||||
stir cat: strcat
|
||||
string cat: strcat
|
||||
stir comp: strcmp
|
||||
stir copy: strcpy
|
||||
stir dupe: strdup
|
||||
string dupe: strdup
|
||||
stir elle cat: strlcat
|
||||
stir l cat: strlcat
|
||||
stir elle copy: strlcpy
|
||||
stir l copy: strlcpy
|
||||
stir en cat: strncat
|
||||
stir en copy: strncpy
|
||||
stir en comp: strncmp
|
||||
stir len: strlen
|
||||
string len: strlen
|
||||
stir to int: strtoint
|
||||
stir to unsigned int: strtouint
|
||||
string char: strchr
|
||||
Reference in New Issue
Block a user