Useful IntelliJ IDEA Feature: Code Snippets Collection

2019/11/25

Introduction

Today I want to share a really handy little IntelliJ feature: code bookmarks. I’m not sure if you’ve run into this kind of frustration before: you take over a project, and for historical reasons some common operations were never abstracted into shared methods, so every time you need them you have to dig through the code forever; or you spot a particularly slick piece of code written by a teammate and think the idea might help in your own business logic later, so you make a mental note… and then when you actually need it, you’ve completely forgotten where it was; or when reading source code, there’s a certain spot you need to revisit again and again, compare back and forth, and look at in context….

In these situations, jumping around the code can be pretty annoying, especially after enough time has passed… your mind just goes blank…. So I want to share IntelliJ’s code bookmark feature. It’s actually very simple, and I’ve also included the official JetBrains tutorial in the references.

Code Bookmarks

  1. Right-click the line number area in the editor, then choose to set a bookmark to save that piece of code (I mapped it to the F3 shortcut)

image-20210528152849640

Bookmark code
  1. Add a note to the bookmarked code. After bookmarking, it will show up as a little check mark. Right-click that check mark and you’ll see an option to edit the description (I mapped it to Shift+F3)

image-20210528154010434

Set a note for the code
  1. To view the bookmarks, find favorites in the IDEA sidebar. Inside, you’ll see the code you just bookmarked. By default, it’s under bookmarks, but you can also create your own bookmark groups, which is super convenient. (I mapped it to cmd+2)

找到收藏夹

Find the favorites panel
  1. As you can see above, IDEA is already pretty smart. I didn’t set a note for it earlier, and it automatically extracted my annotation as the description. Now let me set a note and take another look

image-20210528154454738

After setting a note

References

  1. https://www.jetbrains.com/help/idea/2021.1/managing-your-project-favorites.htm

All articles in this blog, unless otherwise stated, are licensed under @Oreoft . Please indicate the source when reprinting!

Table of Contents