pub struct SnippetsFile {
pub snippets: HashMap<String, Snippet>,
}
Expand description
§Snippets File Manager
📁 A file controller for managing VS Code snippets collections and their file operations.
§Overview
SnippetsFile
provides a convenient way to:
- 📦 Create and manage collections of code snippets
- 🔄 Serialize snippets to VS Code compatible format
- 💾 Save snippets to the filesystem
§Usage
§✨ Creating a new snippets file:
let file = SnippetsFile::new(vec![
Snippet::new("print", vec!["println!(\"$0\");"]),
Snippet::new("debug", vec!["dbg!($0);"])
]);
§➕ Adding snippets dynamically:
let mut file = SnippetsFile::new(vec![]);
// Add single snippet
file.add_snippet(Snippet::new("log", vec!["log::info!(\"$0\")"]));
// Add multiple snippets
file.add_snippets(vec![
Snippet::new("error", vec!["log::error!(\"$0\");"]),
Snippet::new("warn", vec!["log::warn!(\"$0\");"])
]);
§💾 Saving to file:
let file = SnippetsFile::new(vec![
Snippet::new("test", vec![
"#[test]",
"fn test_name() {",
" ",
"}"
])
]);
// Saves to VS Code snippets directory
file.write_to("./snippets/rust.code-snippets")?;
§📋 File Format
The snippets are saved in VS Code compatible JSON format:
{
"print": {
"prefix": "print",
"body": [
"println!(\"Hello\");"
],
"description": "Basic print statement"
}
}
§⚠️ Error Handling
The structure uses custom Result
type for error handling:
- 🔄 JSON serialization errors
- 📂 File system operation errors
- 📁 Directory creation errors
§User Snippets
📝 You can write this snippets to your VS Code custom user snippets folder Locales:
- 🗂️ Windows:
%APPDATA%/Code/User/snippets
- 🍎 MacOS:
~/Library/Application Support/Code/User/snippets
- 🐧 Linux:
~/.config/Code/User/snippets
💡 Or simply access it via VS Code:
- Press
Ctrl/Cmd + Shift + P
- Type “Snippets: Configure User Snippets”
- Select the language or create a new snippet file
Fields§
§snippets: HashMap<String, Snippet>
Implementations§
Source§impl SnippetsFile
impl SnippetsFile
Sourcepub fn new<Sn: Into<Snippet>>(snippets: impl IntoIterator<Item = Sn>) -> Self
pub fn new<Sn: Into<Snippet>>(snippets: impl IntoIterator<Item = Sn>) -> Self
Creates a new snippets file controller
Sourcepub fn add_snippet<S: Into<Snippet>>(&mut self, snippet: S)
pub fn add_snippet<S: Into<Snippet>>(&mut self, snippet: S)
Adds a new snippet to the collection
Sourcepub fn add_snippets<S: Into<Snippet>>(
&mut self,
snippets: impl IntoIterator<Item = S>,
)
pub fn add_snippets<S: Into<Snippet>>( &mut self, snippets: impl IntoIterator<Item = S>, )
Adds a new snippets to the collection
Trait Implementations§
Source§impl Clone for SnippetsFile
impl Clone for SnippetsFile
Source§fn clone(&self) -> SnippetsFile
fn clone(&self) -> SnippetsFile
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for SnippetsFile
impl Debug for SnippetsFile
Auto Trait Implementations§
impl Freeze for SnippetsFile
impl RefUnwindSafe for SnippetsFile
impl Send for SnippetsFile
impl Sync for SnippetsFile
impl Unpin for SnippetsFile
impl UnwindSafe for SnippetsFile
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