dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

code.py (14778B)


      1 from talon import Context, Module, actions, app, imgui, registry, settings
      2 
      3 ctx = Context()
      4 mod = Module()
      5 mod.list("code_functions", desc="List of functions for active language")
      6 mod.list("code_types", desc="List of types for active language")
      7 mod.list("code_libraries", desc="List of libraries for active language")
      8 
      9 setting_private_function_formatter = mod.setting("code_private_function_formatter", str)
     10 setting_protected_function_formatter = mod.setting(
     11     "code_protected_function_formatter", str
     12 )
     13 setting_public_function_formatter = mod.setting("code_public_function_formatter", str)
     14 setting_private_variable_formatter = mod.setting("code_private_variable_formatter", str)
     15 setting_protected_variable_formatter = mod.setting(
     16     "code_protected_variable_formatter", str
     17 )
     18 setting_public_variable_formatter = mod.setting("code_public_variable_formatter", str)
     19 
     20 mod.tag("code_comment", desc="Tag for enabling generic comment commands")
     21 mod.tag("code_block_comment", desc="Tag for enabling generic block comment commands")
     22 mod.tag("code_operators", desc="Tag for enabling generic operator commands")
     23 mod.tag(
     24     "code_generic",
     25     desc="Tag for enabling other basic programming commands (loops, functions, etc)",
     26 )
     27 
     28 key = actions.key
     29 function_list = []
     30 library_list = []
     31 extension_lang_map = {
     32     ".asm": "assembly",
     33     ".bat": "batch",
     34     ".c": "c",
     35     ".cmake": "cmake",
     36     ".cpp": "cplusplus",
     37     ".cs": "csharp",
     38     ".gdb": "gdb",
     39     ".go": "go",
     40     ".h": "c",
     41     ".hpp": "cplusplus",
     42     ".java": "java",
     43     ".js": "javascript",
     44     ".jsx": "javascript",
     45     ".json": "json",
     46     ".lua": "lua",
     47     ".md": "markdown",
     48     ".pl": "perl",
     49     ".ps1": "powershell",
     50     ".py": "python",
     51     ".r": "r",
     52     ".rb": "ruby",
     53     ".s": "assembly",
     54     ".sh": "bash",
     55     ".snippets": "snippets",
     56     ".talon": "talon",
     57     ".ts": "typescript",
     58     ".tsx": "typescript",
     59     ".vba": "vba",
     60     ".vim": "vimscript",
     61     ".vimrc": "vimscript",
     62 }
     63 
     64 # flag indicates whether or not the title tracking is enabled
     65 forced_language = False
     66 
     67 
     68 @mod.capture(rule="{user.code_functions}")
     69 def code_functions(m) -> str:
     70     """Returns a function name"""
     71     return m.code_functions
     72 
     73 
     74 @mod.capture(rule="{user.code_types}")
     75 def code_types(m) -> str:
     76     """Returns a type"""
     77     return m.code_types
     78 
     79 
     80 @mod.capture(rule="{user.code_libraries}")
     81 def code_libraries(m) -> str:
     82     """Returns a type"""
     83     return m.code_libraries
     84 
     85 
     86 @ctx.action_class("code")
     87 class code_actions:
     88     def language():
     89         result = ""
     90         if not forced_language:
     91             file_extension = actions.win.file_ext()
     92 
     93             if file_extension and file_extension in extension_lang_map:
     94                 result = extension_lang_map[file_extension]
     95 
     96         # print("code.language: " + result)
     97         return result
     98 
     99 
    100 # create a mode for each defined language
    101 for __, lang in extension_lang_map.items():
    102     mod.mode(lang)
    103 
    104 
    105 @mod.action_class
    106 class Actions:
    107     def code_set_language_mode(language: str):
    108         """Sets the active language mode, and disables extension matching"""
    109         global forced_language
    110         actions.user.code_clear_language_mode()
    111         actions.mode.enable("user.{}".format(language))
    112         # app.notify("Enabled {} mode".format(language))
    113         forced_language = True
    114 
    115     def code_clear_language_mode():
    116         """Clears the active language mode, and re-enables code.language: extension matching"""
    117         global forced_language
    118         forced_language = False
    119 
    120         for __, lang in extension_lang_map.items():
    121             actions.mode.disable("user.{}".format(lang))
    122         # app.notify("Cleared language modes")
    123 
    124     def code_operator_indirection():
    125         """code_operator_indirection"""
    126 
    127     def code_operator_address_of():
    128         """code_operator_address_of (e.g., C++ & op)"""
    129 
    130     def code_operator_structure_dereference():
    131         """code_operator_structure_dereference (e.g., C++ -> op)"""
    132 
    133     def code_operator_lambda():
    134         """code_operator_lambda"""
    135 
    136     def code_operator_subscript():
    137         """code_operator_subscript (e.g., C++ [])"""
    138 
    139     def code_operator_assignment():
    140         """code_operator_assignment"""
    141 
    142     def code_operator_subtraction():
    143         """code_operator_subtraction"""
    144 
    145     def code_operator_subtraction_assignment():
    146         """code_operator_subtraction_equals"""
    147 
    148     def code_operator_addition():
    149         """code_operator_addition"""
    150 
    151     def code_operator_addition_assignment():
    152         """code_operator_addition_assignment"""
    153 
    154     def code_operator_multiplication():
    155         """code_operator_multiplication"""
    156 
    157     def code_operator_multiplication_assignment():
    158         """code_operator_multiplication_assignment"""
    159 
    160     def code_operator_exponent():
    161         """code_operator_exponent"""
    162 
    163     def code_operator_division():
    164         """code_operator_division"""
    165 
    166     def code_operator_division_assignment():
    167         """code_operator_division_assignment"""
    168 
    169     def code_operator_modulo():
    170         """code_operator_modulo"""
    171 
    172     def code_operator_modulo_assignment():
    173         """code_operator_modulo_assignment"""
    174 
    175     def code_operator_equal():
    176         """code_operator_equal"""
    177 
    178     def code_operator_not_equal():
    179         """code_operator_not_equal"""
    180 
    181     def code_operator_greater_than():
    182         """code_operator_greater_than"""
    183 
    184     def code_operator_greater_than_or_equal_to():
    185         """code_operator_greater_than_or_equal_to"""
    186 
    187     def code_operator_less_than():
    188         """code_operator_less_than"""
    189 
    190     def code_operator_less_than_or_equal_to():
    191         """code_operator_less_than_or_equal_to"""
    192 
    193     def code_operator_in():
    194         """code_operator_less_than_or_equal_to"""
    195 
    196     def code_operator_and():
    197         """codee_operator_and"""
    198 
    199     def code_operator_or():
    200         """code_operator_or"""
    201 
    202     def code_operator_bitwise_and():
    203         """code_operator_bitwise_and"""
    204 
    205     def code_operator_bitwise_and_assignment():
    206         """code_operator_and"""
    207 
    208     def code_operator_bitwise_or():
    209         """code_operator_bitwise_or"""
    210 
    211     def code_operator_bitwise_or_assignment():
    212         """code_operator_or_assignment"""
    213 
    214     def code_operator_bitwise_exclusive_or():
    215         """code_operator_bitwise_exclusive_or"""
    216 
    217     def code_operator_bitwise_exclusive_or_assignment():
    218         """code_operator_bitwise_exclusive_or_assignment"""
    219 
    220     def code_operator_bitwise_left_shift():
    221         """code_operator_bitwise_left_shift"""
    222 
    223     def code_operator_bitwise_left_shift_assignment():
    224         """code_operator_bitwise_left_shift_assigment"""
    225 
    226     def code_operator_bitwise_right_shift():
    227         """code_operator_bitwise_right_shift"""
    228 
    229     def code_operator_bitwise_right_shift_assignment():
    230         """code_operator_bitwise_right_shift_assignment"""
    231 
    232     def code_block():
    233         """Inserts equivalent of {\n} for the active language, and places the cursor appropriately"""
    234 
    235     def code_self():
    236         """Inserts the equivalent of "this" in C++ or self in python"""
    237 
    238     def code_null():
    239         """inserts null equivalent"""
    240 
    241     def code_is_null():
    242         """inserts check for == null"""
    243 
    244     def code_is_not_null():
    245         """inserts check for == null"""
    246 
    247     def code_state_in():
    248         """Inserts python "in" equivalent"""
    249 
    250     def code_state_if():
    251         """Inserts if statement"""
    252 
    253     def code_state_else_if():
    254         """Inserts else if statement"""
    255 
    256     def code_state_else():
    257         """Inserts else statement"""
    258 
    259     def code_state_do():
    260         """Inserts do statement"""
    261 
    262     def code_state_switch():
    263         """Inserts switch statement"""
    264 
    265     def code_state_case():
    266         """Inserts case statement"""
    267 
    268     def code_state_for():
    269         """Inserts for statement"""
    270 
    271     def code_state_for_each():
    272         """Inserts for each equivalent statement"""
    273 
    274     def code_state_go_to():
    275         """inserts go-to statement"""
    276 
    277     def code_state_while():
    278         """Inserts while statement"""
    279 
    280     def code_state_return():
    281         """Inserts return statement"""
    282 
    283     def code_break():
    284         """Inserts break statement"""
    285 
    286     def code_next():
    287         """Inserts next statement"""
    288 
    289     def code_true():
    290         """Insert True value"""
    291 
    292     def code_false():
    293         """Insert False value"""
    294 
    295     def code_try_catch():
    296         """Inserts try/catch. If selection is true, does so around the selecion"""
    297 
    298     def code_default_function(text: str):
    299         """Inserts function declaration"""
    300         actions.user.code_private_function(text)
    301 
    302     def code_private_function(text: str):
    303         """Inserts private function declaration"""
    304 
    305     def code_private_static_function(text: str):
    306         """Inserts private static function"""
    307 
    308     def code_protected_function(text: str):
    309         """Inserts protected function declaration"""
    310 
    311     def code_protected_static_function(text: str):
    312         """Inserts public function"""
    313 
    314     def code_public_function(text: str):
    315         """Inserts public function"""
    316 
    317     def code_public_static_function(text: str):
    318         """Inserts public function"""
    319 
    320     def code_private_function_formatter(name: str):
    321         """Inserts private function name with formatter"""
    322         actions.insert(
    323             actions.user.formatted_text(
    324                 name, settings.get("user.code_private_function_formatter")
    325             )
    326         )
    327 
    328     def code_protected_function_formatter(name: str):
    329         """inserts properly formatted private function name"""
    330         actions.insert(
    331             actions.user.formatted_text(
    332                 name, settings.get("user.code_protected_function_formatter")
    333             )
    334         )
    335 
    336     def code_public_function_formatter(name: str):
    337         """inserts properly formatted private function name"""
    338         actions.insert(
    339             actions.user.formatted_text(
    340                 name, settings.get("user.code_public_function_formatter")
    341             )
    342         )
    343 
    344     def code_private_variable_formatter(name: str):
    345         """inserts properly formatted private function name"""
    346         actions.insert(
    347             actions.user.formatted_text(
    348                 name, settings.get("user.code_private_variable_formatter")
    349             )
    350         )
    351 
    352     def code_protected_variable_formatter(name: str):
    353         """inserts properly formatted private function name"""
    354         actions.insert(
    355             actions.user.formatted_text(
    356                 name, settings.get("user.code_protected_variable_formatter")
    357             )
    358         )
    359 
    360     def code_public_variable_formatter(name: str):
    361         """inserts properly formatted private function name"""
    362         actions.insert(
    363             actions.user.formatted_text(
    364                 name, settings.get("user.code_public_variable_formatter")
    365             )
    366         )
    367 
    368     def code_comment():
    369         """Inserts comment at current cursor location"""
    370 
    371     def code_block_comment():
    372         """Block comment"""
    373 
    374     def code_block_comment_prefix():
    375         """Block comment start syntax"""
    376 
    377     def code_block_comment_suffix():
    378         """Block comment end syntax"""
    379 
    380     def code_type_definition():
    381         """code_type_definition (typedef)"""
    382 
    383     def code_typedef_struct():
    384         """code_typedef_struct (typedef)"""
    385 
    386     def code_type_class():
    387         """code_type_class"""
    388 
    389     def code_type_struct():
    390         """code_type_struct"""
    391 
    392     def code_include():
    393         """code_include"""
    394 
    395     def code_include_system():
    396         """code_include_system"""
    397 
    398     def code_include_local():
    399         """code_include_local"""
    400 
    401     def code_import():
    402         """import/using equivalent"""
    403 
    404     def code_from_import():
    405         """from import python equivalent"""
    406 
    407     def code_toggle_functions():
    408         """GUI: List functions for active language"""
    409         global function_list
    410         if gui_libraries.showing:
    411             gui_libraries.hide()
    412         if gui_functions.showing:
    413             function_list = []
    414             gui_functions.hide()
    415         else:
    416             update_function_list_and_freeze()
    417 
    418     def code_select_function(number: int, selection: str):
    419         """Inserts the selected function when the imgui is open"""
    420         if gui_functions.showing and number < len(function_list):
    421             actions.user.code_insert_function(
    422                 registry.lists["user.code_functions"][0][function_list[number]],
    423                 selection,
    424             )
    425 
    426     def code_insert_function(text: str, selection: str):
    427         """Inserts a function and positions the cursor appropriately"""
    428 
    429     def code_toggle_libraries():
    430         """GUI: List libraries for active language"""
    431         global library_list
    432         if gui_functions.showing:
    433             gui_functions.hide()
    434         if gui_libraries.showing:
    435             library_list = []
    436             gui_libraries.hide()
    437         else:
    438             update_library_list_and_freeze()
    439 
    440     def code_select_library(number: int, selection: str):
    441         """Inserts the selected library when the imgui is open"""
    442         if gui_libraries.showing and number < len(library_list):
    443             actions.user.code_insert_library(
    444                 registry.lists["user.code_libraries"][0][library_list[number]],
    445                 selection,
    446             )
    447 
    448     def code_insert_library(text: str, selection: str):
    449         """Inserts a library and positions the cursor appropriately"""
    450 
    451     def code_document_string():
    452         """Inserts a document string and positions the cursor appropriately"""
    453 
    454 
    455 def update_library_list_and_freeze():
    456     global library_list
    457     if "user.code_libraries" in registry.lists:
    458         library_list = sorted(registry.lists["user.code_libraries"][0].keys())
    459     else:
    460         library_list = []
    461 
    462     gui_libraries.show()
    463 
    464 
    465 def update_function_list_and_freeze():
    466     global function_list
    467     if "user.code_functions" in registry.lists:
    468         function_list = sorted(registry.lists["user.code_functions"][0].keys())
    469     else:
    470         function_list = []
    471 
    472     gui_functions.show()
    473 
    474 
    475 @imgui.open()
    476 def gui_functions(gui: imgui.GUI):
    477     gui.text("Functions")
    478     gui.line()
    479 
    480     # print(str(registry.lists["user.code_functions"]))
    481     for i, entry in enumerate(function_list, 1):
    482         if entry in registry.lists["user.code_functions"][0]:
    483             gui.text(
    484                 "{}. {}: {}".format(
    485                     i, entry, registry.lists["user.code_functions"][0][entry]
    486                 )
    487             )
    488 
    489 
    490 @imgui.open()
    491 def gui_libraries(gui: imgui.GUI):
    492     gui.text("Libraries")
    493     gui.line()
    494 
    495     for i, entry in enumerate(library_list, 1):
    496         gui.text(
    497             "{}. {}: {}".format(
    498                 i, entry, registry.lists["user.code_libraries"][0][entry]
    499             )
    500         )
    501 
    502 
    503 def commands_updated(_):
    504     if gui_functions.showing:
    505         update_function_list_and_freeze()
    506     if gui_libraries.showing:
    507         update_library_list_and_freeze()
    508 
    509 
    510 registry.register("update_commands", commands_updated)