snippets
The community/core/snippets directory contains the code for handling code snippets in Talon. It includes the logic for parsing, inserting, and managing snippets.
The snippets subdirectory contains a collection of .snippet files, each defining one or more code snippets. The README.md file provides a detailed explanation of the custom .snippet file format, which allows for multiple snippets per file, context-specific snippets, and variable tab stops within snippets. The snippets are language-aware, meaning that the code inserted can vary based on the selected language when using the snippet. Most of the snippets have multiple language versions included in the same file. Some of the more interesting snippets include:
tryStatement.snippet: Inserts atryblock for JavaScript, Java, and Python.global.snippet: Inserts a code block. It uses markdown code fence.constructorDeclaration.snippet: Inserts a constructor declaration for JavaScript, Java, and Python.commentBlock.snippet: Inserts a multi-line comment block for C, Javascript, Java, Python, and HTML.typescript.snippet: Contains a snippet for a Typescript interface declaration.javascript.snippet: Contains multiple JavaScript specific snippets.lua.snippet: Contains Lua specific snippets.python.snippet: Contains multiple Python specific snippets.html.snippet: Contains multiple HTML specific snippets.talon.snippet: Contains a Talon specific snippet for a voice command declaration.markdown.snippet: Contains a snippet for inserting a markdown link.
The other files in the directory provide the functionality to use the snippets.
-
snippets_insert_raw_text.pycontains the logic for inserting a snippet as raw text without editor support. It parses the snippet body to identify tab stops and variables that can be populated or used for cursor placement. -
snippets.pycontains the core logic for managing and using snippets. It defines Talon lists of snippets and wrapper snippets, parses snippets from.snippetfiles, handles context matching based on code language, and provides actions for getting and inserting snippets. This file uses thecode_languagesmodule fromcommunity/core/modesto determine the active language. This file also watches the snippet directory for changes to update the available snippets. It groups snippets by language and sets up the appropriate Talon context for each language. -
snippet_types.pydefines dataclasses to represent a snippet, its variables, insertion snippets, and wrapper snippets. -
snippets_insert.pydefines the Talon actions for inserting snippets, including the ability to insert a snippet by name and to substitute variables within the snippet. -
snippets_parser.pyprovides functionality for parsing the.snippetfiles, extracting contexts, variables, and snippet bodies. It handles the---delimiter to separate snippet documents,-to separate context and body, and:for key value pairs. It also handles tab stops, variables, and default values. It has error handling for invalid snippets. -
snippets.talondefines the Talon commands for using the snippets, includingsnip {user.snippet}for inserting a snippet andsnip {user.snippet_with_phrase} <user.text>for inserting a snippet that utilizes a phrase.
The files work together as follows:
.snippetfiles are stored in thesnippetssubdirectory, as well as in a user-defined directory. These files contain the raw text for snippets, as well as metadata like language, phrase, and insertion scope.snippets_parser.pyparses the.snippetfiles and extracts the individual snippets, storing them asSnippetobjects (defined insnippet_types.py).snippets.pywatches the snippets directory for file changes. It groups the parsed snippets by language and stores them in Talon lists. This allows Talon to make available language-specific snippets in code editing contexts, via thecode_languagesmodule incommunity/core/modes.- When a snippet is invoked by a Talon command (defined in
snippets.talon),snippets_insert.pyuses the snippet metadata to substitute variables and insert the snippet text usingsnippets_insert_raw_text.py, which will handle cursor placement in tab stops.