Expand description
Β§The Snippets Module
π¦ This module provides a comprehensive toolkit for working with VS Code snippets. It allows creating, managing, and serializing code snippets with rich customization options.
Β§Components
Snippet
- Individual snippet representationSnippetBuilder
- Fluent builder for snippetsSnippetsFile
- Collection of snippets for VS Code
Β§Overview
- π Create and edit snippets with rich formatting
- π§ Customize triggers, descriptions, and scopes
- π¦ Manage collections of snippets
- πΎ Serialize/deserialize to VS Code format
Β§Examples
Β§π¨ Creating a Simple Snippet
use snippets::SnippetBuilder;
let snippet = SnippetBuilder::new()
.set_prefix("fn")
.add_line("fn ${1:name}() {")
.add_line(" ${0:// TODO: ${0}}")
.add_line("}")
.build()
.unwrap();
Β§π Managing Snippet Collections
use snippets::{ SnippetsFile, SnippetBuilder };
let mut file = SnippetsFile::new(vec![
SnippetBuilder::new()
.set_prefix("fn")
.set_body(vec![
"fn main() {",
" $0",
"}"
])
.set_scope("rust")
]);
// Add test snippet
file.add_snippet(
SnippetBuilder::new()
.set_prefix("test")
.set_body(vec![
"#[test]",
"fn test_${1:name}() {",
" $0",
"}"
])
.set_scope("rust")
.build()
.unwrap()
);
Β§Panics
The builderβs build()
method will panic if
prefix
is emptyname
is emptybody
is empty
Β§Notes
Β§The syntax of VS Code snippets:
- π Use
$0
to specify the final cursor position - π Use
$1
,$2
, etc. for tabstops - π Use
${1:default text}
for placeholders with default values - π Use
${1|one,two,three|}
for dropdown choices
Β§See Also
- π Structure
SnippetFile
- For more flexible snippet construction - π VS Code Snippet Guide
Re-exportsΒ§
pub use snippet::Snippet;
pub use snippet_builder::SnippetBuilder;
pub use snippets_file::SnippetsFile;