pub struct Snippet {
pub name: String,
pub prefix: String,
pub body: Vec<String>,
pub description: Option<String>,
pub scope: Option<String>,
pub is_file_template: Option<bool>,
pub priority: Option<u32>,
}
Expand description
§The Snippet
🧩 Represents a VS Code snippet with all its properties and functionality. This structure allows you to create and manage code snippets for VS Code with rich customization options.
§Overview
- 🎯 Create snippets with prefix triggers and body content
- 🔧 Optional description and scope settings
- ⚡ Priority management for suggestion list
- 📦 Special templates to simplify development
§Usage
// Simple snippet creation
let snippet = Snippet::new("fn main", vec![
"fn main() {",
" println!(\"Hello, World!\");",
"}"
]);
// Using builder pattern
let snippet = Snippet::builder()
.set_prefix("fn test")
.set_body(vec![
"#[test]",
"fn test_$1() {",
" $0",
"}"
])
.set_description("Test function template")
.set_scope("rust")
.build()
.unwrap();
§Using Templates
// Text snippet
let text = Snippet::text("hello", "println!(\"Hello, world!\");")
.set_description("Prints 'Hello, world!'")
.build()
.unwrap();
// TODO comment
let todo = Snippet::todo_comment("todo", "TODO", Some("//"))
.build()
.unwrap();
// Function alias
let println = Snippet::fn_alias("pr", "println!")
.build()
.unwrap();
§Rust-specific Templates
// Requires feature = ["rust"]
// Rust macro alias
let macro_snippet = Snippet::rust_macro_alias("fmt", "format", None)
.set_description("format! macro")
.build()
.unwrap();
// Rust attribute alias
let derive = Snippet::rust_attr("der", "derive", vec!["Debug", "Clone"])
.set_description("Common derive attributes")
.build()
.unwrap();
§JSON Conversion
let snippet = Snippet::new("fn", vec!["function() {", " $0", "}"]);
let json = snippet.to_json().unwrap();
println!("{}", json);
§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
Fields§
§name: String
Unique identifier for the snippet (not serialized)
prefix: String
The trigger text for the snippet
body: Vec<String>
The actual content of the snippet
description: Option<String>
Optional description of what the snippet does
scope: Option<String>
Optional language scope (e.g., “rust”)
is_file_template: Option<bool>
Optional flag for file templates
priority: Option<u32>
Optional priority in suggestion list
Implementations§
Source§impl Snippet
The standart snippet templates
impl Snippet
The standart snippet templates
Sourcepub fn text<S: Into<String>>(prefix: S, text: S) -> SnippetBuilder
pub fn text<S: Into<String>>(prefix: S, text: S) -> SnippetBuilder
Creates a simple text snippet
Sourcepub fn todo_comment<S: Into<String>>(
prefix: S,
comment_name: &str,
comment_type: Option<&str>,
) -> SnippetBuilder
pub fn todo_comment<S: Into<String>>( prefix: S, comment_name: &str, comment_type: Option<&str>, ) -> SnippetBuilder
Creates various comment templates (TODO, NOTE, etc.)
Trait Implementations§
Source§impl From<SnippetBuilder> for Snippet
impl From<SnippetBuilder> for Snippet
Source§fn from(value: SnippetBuilder) -> Self
fn from(value: SnippetBuilder) -> Self
Converts to this type from the input type.
impl StructuralPartialEq for Snippet
Auto Trait Implementations§
impl Freeze for Snippet
impl RefUnwindSafe for Snippet
impl Send for Snippet
impl Sync for Snippet
impl Unpin for Snippet
impl UnwindSafe for Snippet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more