We often reuse the same blocks of code in several parts of the projects we are working on. Jetbrains IDEs have live templates, but there is no easy way to share them with other team members or share them across IDEs.
In this article, we present the Codiga Coding Assistant plugin that lets you create, share, search and imports code snippets within your IDE. Code Snippets you write with Codiga are reusable across IDEs and work seamlessly on all JetBrains IDEs or VS Code.
Install the Codiga IntelliJ Plugin
First, install the Codiga plugin extension in IntelliJ. Go to the preferences of IntelliJ first and go to the PlugIns section. Look for the
Codiga plugin in the marketplace and click install.
The hardest part is almost done! Next, let's set up your API token set up!
Setup your Codiga API token
First, head to Codiga and create your API token. This token will link your Codiga account with IntelliJ.
Once you get the API token, enter it in IntelliJ. Go to the preferences. Under the
Tools section, find the
Codiga sub-section and enter your API token.
IMPORTANT: do not forget to click on
Apply before testing the API connection.
Search Java Code Snippets in IntelliJ using comments
Open IntelliJ and start a comment with your search keywords. For example, enter the following comment in a Java file.
// read file
The Codiga plugin will query the Codiga Code Snippet engine for the snippet based on the keywords you entered.
The plugin suggests different code snippets based on your suggestions. You can browse the suggestions using
ALT + ] (next snippet) or
ALT + [ (previous snippet). Use
TAB to insert a snippet and
ESC to remove suggestions.
IMPORTANT: the Codiga engine uses extract the comment content to get the keywords. If you create a code snippet on Codiga and want to make them searchable, make sure you add keywords to your code snippets.
Search Java Code Snippets in IntelliJ using embedded search
Sometimes, you prefer a search interface to browse all code snippets. Open the code snippets panel in IntelliJ. The panel finds the language in the editor and suggests code snippets based on the packages being used in your environment.
Enter your search request. You can then go on the button to preview the snippet and if the snippet is what you need, click to insert it into your code.
IMPORTANT: the Codiga engine uses all the words in the comments to find snippets based on their keywords. If you create a code snippet on Codiga and want to make them searchable, make sure you add keywords to your code snippets.
Import a Java Code Snippet in IntelliJ using shortcuts
A shortcut is a short set of words that triggers the snippet and inserts it into your IDE. It is very useful when you repeat the same code very often.
Some code snippets have a shortcut (you can see the shortcut in the code snippet panel). Shortcuts are associated when you create the snippets. They are a great way to associate a code snippet with a short keyword.
Start typing a
. in your editor, followed by the shortcut you want to use. Codiga will then suggests shortcuts as you type. Select the shortcut in the suggestions to insert it in the editor.
Create Java Code Snippets in IntelliJ
You can also create code snippets from IntelliJ. Select the code you want to share in IntelliJ, make a right click and select
Create Codiga Snippet. The plugin opens your browser to create the snippet.
After creating a snippet, you can associate the following elements:
- keywords: keywords are used to index the snippet and make them searchable in the snippet search, either in the panel or in the comments.
- shortcut: shortcuts make snippets easy to find. Typing a
.followed by the keyword will suggest the snippet directly in your editor.
Share your Java Code Snippets on the Codiga Hub
When creating a code snippet on Codiga, the snippet is either public or private. Public snippets are shared on the Codiga Hub, a central repository for code snippets.
If you create code snippets for Java, your snippets will be available and indexed on the Codiga Hub. As you create more snippets, your Codiga karma will increase, unlocking badges and rewards.
Supporting all JetBrains products
The Codiga extension is also available for:
Creating code snippets in IntelliJ has never been so easy with the Codiga extension. Code Snippets are shared across the Codiga community on the Codiga Hub and reusable across platforms (such as other JetBrains IDEs and VS Code).