Using Snippets

Snippets are useful pieces of code you can save, share, reuse, modify, integrate into future projects to improve productivity and reduce time spent looking through old resources.

With the Pieces for JetBrains Plugin, you can save all kinds of code snippets, and easily find them and edit them whenever, wherever, and however you need.

Let’s take a look at some examples:


SQL Query Templates

An SQL query to retrieve data from a database with filter conditions.

Save time with databases by reusing common queries like inserts, updates, or data retrieval.

Boilerplate Code

An HTML boilerplate template for creating new webpages.

Standardize project structure of different projects by starting with a clean and organized template.

Error-Handling Code Blocks

A try-catch block template in JavaScript for handling API call errors.

Streamline error management by quickly inserting predefined error-handling patterns for more reliable code.

Regex Patterns

A regex pattern to validate email addresses.

Quickly implement validation in forms to make sure users correctly enter formatted data.

Shell Scripts

A shell script to automate server maintenance tasks like clearing logs and updating packages.

Automate system administration tasks on Unix / Linus servers so you spend less time on manual upkeep.


Streamlining Development with Snippet Management

The Pieces for JetBrains Plugin connects you with your development environment, so you can focus on what matters—building great software—while empowering you with tools for efficient code reuse.

  • Improved Productivity: Focus on solving new problems rather than resolving old ones.

  • Contextual Awareness: Snippets enriched with metadata, descriptions, and links to relevant searches help you remember the origin, purpose, and ideal usage scenarios for each piece of code.

  • Code Standardization: Sharing best practices and reusable components helps developers ensure uniform coding practices and conventions are available across different teams and projects.

By using the Pieces for JetBrains Plugin, you can capture and manage these snippets to streamline daily tasks and enhance your workflow in several ways:

Save & Organize

Users can save snippets by highlighting the desired code, right-clicking, and selecting the Save to Pieces option.

You can also do this without needing to open up the right-click menu—just use the ⌥+⌘+p (macOS) or ctrl+shift+p (Windows/Linux) shortcut while your code is highlighted.

This guarantees that important pieces of code, such as code fragments, functions, configuration files, and command-line instructions, are always at hand—just when you need them.

Additionally, each snippet is enriched with context, enabling you to insert well-documented, relevant snippets where they make sense within your projects.

Pieces can also intelligently identify and save snippets based on re-use patterns in your JetBrains workflow. This is an automatically enabled setting called Suggested Snippets.

Whenever Pieces saves a snippet, you'll see a confirmation in the bottom right corner of your screen that says Saved a Piece!.

Your snippet is now saved in Pieces with auto-generated metadata!

Search & Reuse

You can search your entire collection of saved Pieces code snippets inside of your preferred JetBrains IDE, so the code you need is always on hand.

To do this, open the Search Everywhere window and switch to the Pieces tab by using ⌘+shift+p (macOS) or ctrl+shift+p (Windows/Linux).

Simply enter a search query, and any relating snippets will appear in the dropdown list—then you can hit enter and view your snippet (with all of it’s saved context) rendered in markdown directly inside your editor.

Edit & Update

You can easily edit and update code snippets directly from your IDE without opening a separate window or navigating to the Pieces for Developers Desktop App.

To do so, open the Pieces from the tool window on the left-hand side of your JetBrains IDE, which will reveal a list of saved snippets. You can then right-click a snippet, which opens that snippet up in a new window.

Simply make your changes and save the snippet using ⌘+s (macOS) or ctrl+s (Windows/Linux).

Sharing

The Sharing Snippets feature allows you to share code snippets with your team or across projects directly from your JetBrains IDE.

To share a snippet, you can right-click on some highlighted code and hover over the Pieces option, then select Share via Pieces Link.

You can also share a snippet by right-clicking a snippet that’s listed in the left-hand toolbar window in any of your JetBrains IDEs and clicking Share via Pieces Link.

Exploring Materials

With the Explored Materials feature, you can gain deeper insights into code snippets without saving them permanently to your repository. This allows you to quickly analyze and document code within your JetBrains IDE.

Users can activate this feature by highlighting a desired code snippet, right-clicking, and selecting the Explore with Pieces Insights option.

This loads a detailed view in the side panel, displaying additional context and metadata for the selected snippet, like the snippet language, related tags, authors, and links.

Related snippets are code snippets that are similar to the code you're currently working on.

Pieces is capable of identifying related code snippets, even if you're interested in code that isn't originally part of the Pieces snippet.

To access related materials, you can highlight a code snippet within your editor, right-click, and select Find Related Materials in Pieces.

This will trigger a success message and open up the saved snippets window on the left-hand side of your IDE, with the snippets related to your highlighted code available.

Use Cases

With Pieces for VS Code Extension, your snippets become a robust library of reusable code that you can organize, search, edit, and share across projects.

Here are some examples of how you can use them:

Reusable Scheduling Functions

Imagine you’re frequently working on projects that involve scheduling tasks at regular intervals.

Instead of rewriting the same logic for each new project, you can save an evergreen snippet to your Pieces repository.

This allows you to reuse a well-crafted scheduling function across multiple applications that’s useful in a variety of scenarios:

package main

import (
	"fmt"
	"time"
)

// A reusable function to schedule tasks at a specified interval
func scheduleTask(interval time.Duration, task func()) {
	ticker := time.NewTicker(interval)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			task()
		}
	}
}

File System Monitoring

If you frequently work on projects that involve monitoring file system changes, you can save a helpful snippet to your Pieces repository that does exactly that, using generic and reusable naming conventions:

package main

import (
	"fmt"
	"log"
	"github.com/fsnotify/fsnotify"
)

// A reusable function to monitor file system changes
func watchFileChanges(path string) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	err = watcher.Add(path)
	if err != nil {
		log.Fatal(err)
	}

	for {
		select {
		case event := <-watcher.Events:
			if event.Op&fsnotify.Write == fsnotify.Write {
				fmt.Println("Modified file:", event.Name)
			}
		case err := <-watcher.Errors:
			fmt.Println("Error:", err)
		}
	}
}

Additional Snippet Scenarios

Read up on some additional scenarios and use cases for Pieces Snippets and see how the Pieces for JetBrains Plugin could benefit you and your workflow.



Download the Pieces for JetBrains Plugin today!
Updated on