Skip to content

python

The community/lang/python directory contains files that provide support for Python development in Talon. It primarily defines code actions, lists, and grammar to make writing Python code easier and faster using voice commands.

The most important files in this directory are:

  • python.py: This file defines the core logic for Python support. It includes:

    • A context that activates when the code language is set to Python.
    • Several lists (ctx.lists) that provide vocabulary for common Python constructs. These include:
      • user.code_common_function: Common Python functions like print, len, and range.
      • user.python_docstring_fields: Fields used in Python docstrings like :param:, :return:, and :type:.
      • user.code_type: Python data types such as int, str, list, and dict.
      • user.code_keyword: Python keywords like class, return, import, True, and False.
      • user.python_exception: A comprehensive list of Python exceptions.
    • An action class (UserActions) that implements the following:
      • Actions to insert code operators (e.g., +, -, *, /, =, ==, !=).
      • Actions to insert code structures such as if, elif, else, for, while, and match/case.
      • Actions to insert common keywords like class, return, import, None, True, and False.
      • Actions to insert function definitions (both public and private).
      • Actions to insert type hints for variables and return types.
      • Actions to insert break and continue statements.
      • Actions to insert comments and docstrings.
  • python.talon: This file defines the Talon grammar for Python. It includes:

    • Tags for Python (e.g., user.code_imperative, user.code_object_oriented, user.code_comment_documentation).
    • Settings to control how function names and variable names are formatted.
    • Grammar rules that map voice commands to actions defined in python.py. For example:
      • state def inserts def.
      • state try inserts try:\n.
      • state raise {user.python_exception} inserts a raise statement with the selected exception.
      • dock string calls the user.code_comment_documentation() action to insert a docstring.
      • dock {user.python_docstring_fields} inserts a docstring field.
      • dock type {user.code_type} inserts a type hint in a docstring.
      • dock returns type {user.code_type} inserts a return type hint in a docstring.
      • import <user.code_libraries> inserts an import statement for a library and moves the cursor to the end of the line.

Together, these files provide a comprehensive set of tools for coding in Python with Talon. The python.py file defines the logic and available actions, while python.talon maps voice commands to those actions and sets up the grammar. The lists in python.py provide the necessary vocabulary for these commands to function.