cpp
This directory provides robust support for editing C++ code using voice commands. It defines structured vocabularies for standard library components, native C++ keywords, namespace resolution, and complex type formatting rules.
These components coordinate with Talon's programming language features to enable efficient typing of variables, nested namespaces, templates, pointers, references, and system header inclusions.
Core Architecture and Syntax Resolution
The behavior is driven by two main files that implement C++ grammar parsing and command mappings:
- cpp.py: The underlying Python logic. It establishes the
code.language: cppcontext, declares Talon lists and captures, and overrides default user actions. - cpp.talon: The user-facing voice command mappings. It activates when editing C++ files, enabling specific syntax structures like headers, variables, namespaces, and C++-specific keywords.
Advanced Type and Pointer Formatting
One of the primary challenges of writing C++ by voice is managing pointers (*), references (&), arrays ([]), and their associated whitespace. In cpp.py, the format_type() helper handles these distinct rules depending on how they are used:
- Stand-alone Types: Using
<user.code_type>outputs types with pointer annotations flush against the type (e.g.,int*). - Variable Declarations: The command
var <user.variable_type> <user.text>in cpp.talon parses spacing specifically for declarations (e.g., "var short pointer reference high score" formats toshort* &high_score).
This logic uses pointer and reference keywords registered in cpp_pointers.talon-list.
C++ Standard Library Prefixing
Talon captures phrases combining a prefix (such as "stood" or "standard") with standard types, functions, or constants to automatically output them in their scoped (std::) form.
For instance, cpp.py implements three captures to handle standard library mappings:
* cpp_standard_type: Combines cpp_standard_prefix.talon-list and cpp_standard_type.talon-list (e.g., "stood vector" becomes std::vector).
* cpp_standard_function: Combines prefixes with cpp_standard_function.talon-list (e.g., "stood make unique" inserts std::make_unique).
* cpp_standard_constant: Combines prefixes with cpp_standard_constant.talon-list (e.g., "stood see out" yields std::cout).
Vocabularies and Lists
A large set of .talon-list files provides targeted vocabularies for language structures, avoiding collision with other active languages:
- code_keyword.talon-list: Standard C++ keywords (e.g.,
class,constexpr,noexcept,nullptr). - code_keyword_unprefixed.talon-list: Special C++ modifiers that are applied without common formatting prefixes (e.g.,
const,virtual,explicit). - cpp_namespace.talon-list: Commonly referenced namespaces (e.g.,
std,boost,chrono,filesystem,fmt). The capture rulecpp_namespace_listin cpp.py chains these together (e.g., "from standard chrono" outputsstd::chrono::). - cpp_standard_header.talon-list: System library headers. This is used by commands in cpp.talon to insert standard includes, such as
include algorithmtyping#include <algorithm>. - code_common_function.talon-list: Common C-style and global system calls frequently used in C++ files (e.g.,
memcpy,printf,sizeof,exit).
Action Implementations
Inside cpp.py, standard programming actions are overridden in the UserActions class to emit valid C++ syntax:
- Booleans & Null: Maps true/false to
true/false, and null actions tonullptr. - Object Accessors: Maps the generic object accessor to
.and self accessors tothis->. - Operators: Incorporates the base operators defined under the common C programming interface.