[uberconf 2023] intellij dojo

Dan Hinojosa

For more, see the table of contents


Uses https://github.com/dhinojosa/intellij-dojo-workshop

General

  • Help > Keyboard Shortcuts PDF
  • Goal: get away from mouse
  • I tried to note both windows and mac since I use both. I followed along on Mac live
  • Presentation assistant shows shortcuts used [I used this one]
  • Key Promoter X – reminds you to use plugin [I turned this off; it was super annoying]

New projects

  • Projects typically created outside the IDE so just have to open the project
  • Open new project (or recent projects)
  • Can setup to open from command line by typing “idea .” – Open toolbox (not in IntelliJ; helps with downloading > bolt > settings > tools – shows where command line)

Navigation

  • Command or alt 1 – project menu – start typing name of file to get straight to it. Arrows to go thru nav menu.
  • Command or alt 2 – bookmarks
  • shift escape – close window – ex if have bookmarks open.
  • Command or alt 3 – find window
  • Command or alt 4 – run window
  • Command or alt 5 – debug window
  • Command or alt 6 – problems window “six is to fix”
  • Command or alt 7 – structure window (outline of file)
  • Command or alt 9 – git log
  • Command or alt 0 – commit window
  • Command or alt e – recent files list
  • Command shift e – recent edit locations (within file)
  • Command or control shift F12 – maximize/restore editor

Action keys

  • Command or control shift A – all actions – shows shortcut next to it so can type name and review shortcut
  • control control – run anything within project. type “?” to see what the choice are. ex: run maven test quickly. Also shows recent commands
  • shift shift – search everywhere ex: files, options in ide, even has a calculator

Navigation

  • control (or Alt windows) right/left – didn’t work for me on mac; ctrl tab lets me see tabs and pressing it repeatedly switches between them
  • ctrl F4 (was command w on my machine)

Finding items

  • command o/Ctrl N – go to class (supposed to be o for mac, but n works for me)
  • command shift o/ctrl shift n – go to file
  • command option o/ctrl option n – go to symbol
  • command/ctrl b – go to declaration
  • command/ctrl alt b – go to implementation
  • option/alt f7 – find usages
  • command/ctrl f7 – find in file

TDD Sample

  • command/ctrl n – create new – then choose what want ex: Java class. do again within class and can create test method automatically
  • F2/shift F2 – switch between problems (ex: compiler error). If no errors, says no errors found
  • alt/option enter – context sensitive actions ex: fix error, refactor
  • command/ctrl shift t – toggle between test and code under test
  • ctrl t (mac) ctrl alt shift t (win) – refactor this menu
  • command l – go to line

Formatting

  • command option/control alt l – format all code
  • control alt/option o – organize imports

Selecting code blocks

  • option down/ctrl shift w increase code block selected – ex: line -> section of code -> method -> class
  • option up/ctrl w increase code block selected

Editing

  • command/ctrl backspace – delete line
  • command/ctrl d – duplicate line
  • command/ctrl x – cut line
  • command/ctrl c – copy line
  • command/ctrl v paste line
  • command/ctrl shift v – paste from history; still available even after pasted
  • command/ctrl t – surround block/selection – didn’t work for me on mac
  • command option t/ctrl shift t – surround with – ex: wrap with if/try/etc

Code completion

  • pvsm – generate main method
  • postfix – write. sout or .var or the like at end of line if forgot something at beginning of line and fixes. Can add own with live templates
  • ctrl space – autocomplete
  • ctrl option/alt space – inaccessible members autocomplete; will import at same time
  • ctrl shift space – type sensitive complete – filter autocomplete based on the desired type
  • command/ctrl shift enter – complete statement – ex: add { for if statement
  • command/ctrl p – parameter info – click method and get pop up with the parameters for all overloads. Works when writing code or on filled in code
  • ctrl shift p – type info – useful on a stream to see return type. If ambiguous have to chose which expression from a list first

Moving

  • Command/ctrl shift up or down – move code up or down compared to other code

Docs

  • Ctrl Q – windows, Ctrl J – mac – pop up for docs
  • ctrl shift i win, command y – mac – pop up with source code/implementation. use right/left if multiple matches (ex: levels in hierarchy)
  • alt q windows, ctrl shift q mac – pop up with method name, press again for class. useful in long legacy code

Run/Debug

  • Contextually – main methods, tests , folder of tests etc
  • If create file named http, will run it. Uses specified format to specify get/post, content type, etc – https://www.jetbrains.com/help/idea/http-client-in-product-code-editor.html. Good if don’t need credentials
  • ctrl shift F10 win, ctrl shirt r mac – First time, run and mark context configuration. Like staking a claim
  • shift F10 win, ctrl r mac- run last config
  • alt shift f10 win, ctrl option r mac – mark context
  • Replace d with r for debug versions
  • In debugger, trace current stream chain lets you see each step of a stream and what is there. (click three dots in debugger to see it). Choose flat mode to see everything on one screen

Refactoring

  • ctrl t (mac) ctrl alt shift t (win) – refactor this menu – this one includes all the other refactorings. Including obscure ones like changing a method return type
  • F5 – copy item
  • F6 – move item
  • shift F6 – rename globally
  • command/alt delete – safe delete
  • ctrl alt n – inline
  • command option/ctrl alt m – extract method
  • command option/ctrl alt v – extract variable
  • command option/ctrl alt f – extract field
  • command option/ctrl alt c – extract constant
  • command option/ctrl alt p – extract parameter

Multicursor

  • shift command 8/alt shift insert win – start column selection mode.
  • hold shift down and get multiple cursors at that column
  • alt/option shift click – start multicursor mode – each click gives you a cursor
  • ctrl g mac, alt j windows – next instance of word giving you a cursor for each (select method/class/etc want to match first)
  • ctrl command g mac, alt shift j win – all instances of word giving cursor for each

HTML/XML editing

Version Control

  • Command/Ctrl 0 – changes window
  • Command/Ctrl K – commit
  • Command/Ctrl t – update project
  • command/ctrl shift k – push
  • ctrl v – mac, alt ` windows – version control quick help

Random stuff

  • If reference a closure variable (ex: effectively final variable used in a lambda), variable appears in purple.
  • Presentation mode – don’t need to mess with font manually when present
  • File and Code templates – for commonly used code

Side note

Chrome has a built in QR code generator – share the link and QR is an option

My take

It was good to get practice and learn more shortcuts. Also learned I had my keymap set to mac classic (that explains why some expected shortcuts hadn’t worked; fixed it). I like the windows/mac contrasts. Felt less magical seeing it presented together. (I use both).. Good mix of hands on and watching.

creating a new junit 5 project in gradle in intellij

I haven’t created a new JUnit 5/Gradle project in a IntelliJ in a long time. When doing so today, I ran into a number of problems. I wound up just starting over, but writing up what I encountered/learned.

Java 16 vs 17

IntelliJ supports Java 17. When I set up Gradle, it told me that Gradle doesn’t yet support Java 17. I confirmed on the Gradle website this is true. Ok that’s fair. Java 17 isn’t fully released for another month. While there might be an early adopter package that does work with Java 17, my focus here is JUnit 5, no, Java 17. So I went with Java 16.

String not found – “Package java.lang is declared in module java.base, which is not in the module graph”

This surprised me. I’m not using modules in this project. Or at least I don’t want to be, nor do I have a module-info.java file. I tried invalidating the IntelliJ cache and restarting the IDE. That didn’t work. I then tried deleting the .gradle and .idea directories and reopening IntelliJ. That worked. IntellIJ even asked if I wanted to trust the gradle project as it recreated one.

This approach lost all IntelliJ settings including Java version, gradle version and source/test directories.

Recreating the project

At this point, I decided to delete my build.gradle and settings.gradle files in addition to the .idea and .gradle directories. My plan was to start over. I then created a new Gradle project in the same directory. I created a dummy file and had IntellIJ create a JUnit 5 test for me. That updated the Gradle file in a way that the IDEA was happy with. Now to commit to git lest anything else happen.

opening multiple intellij modules in the same project and remembering them on reopen

This is the number one problem that has prevented me from using IntelliJ more and I finally know how to do it. (I’ve known how to open multiple modules for over a year. What I learned yesterday at the IntelliJ 20th anniversary conference was how to reopen that same set)

Note about terminology to Eclipse users

If your primary IDE is Eclipse, keep this difference in terminology in mind as you read this.

EclipseIntelliJ
WorkspaceProject
ProjectModule

Step 1: Create a dummy/grouping project

Create a new project. This project’s purpose is to contain the modules you want together and give it a distinct name. I’ve been keeping them in an IntelliJ folder in my home directory to avoid confusion. (Most of my stuff is under the git folder in my home directory). The idea of keeping them separate is so I know there’s not code in there.

Step 2: Add your modules

There are several ways of adding modules. Any of them are fine. I find the fastest for repeated adds is:

  • File > Project Structure > Modules
  • Click “+”
  • Choose “Import module”
  • Navigate to the build file (ex pom.xml) or .iml file for generic projects

Step 3: Rename the project (if needed)

On rare occasions, I noticed the project name got my first module name. I couldn’t reproduce this, but started double checking. This name is what will show up in your recents list.

  • File > Project Structure > Project
  • Enter a new name if needed

Testing

File > Open Recents and open another project. Then File > Open Recents and open this project. Admire how all your modules are back!

Seeing the modules on disk for the curious

In the dummy project, the .idea directory has a misc.xml file. I was told it contains all the modules. And it did when I tested on Windows

<list>
  <option value="$USER_HOME$/git/myProj/pom.xml" />
  <option value="$USER_HOME$/git/myProj2/pom.xml" />
</list>

When I tested on Mac, modules.xml had this info

<modules>
   <module fileurl="file://$USER_HOME$/git/myProj.xml" filepath="$USER_HOME$/git/myProj.xml" /> 
   <module fileurl="file://$USER_HOME$/git/myProj2.xml" filepath="$USER_HOME$/git/myProj2.xml" /> 

</modules>

And a note about “you are doing it wrong”

I am an Eclipse power user and an IntelliJ competent user. What holds me back is not using IntelliJ more at work because of not knowing how to do this. While I don’t want to replace my usage, I do want to be an IntelliJ power user and use Eclipse enough to not lose my skills! I think this is the thing that will do it!

When I’ve tried to find out how to add multiple modules and have them remembered multiple times in the past, I was told that I was “using IntelliJ wrong” and should have one project that I focus on. (I work on many sets of small related projects; that model doesn’t work for me.)

Yesterday, someone attempted to tell me that I should have one big project that I focus on at a time. (aka my problem doesn’t exist.) After explaining what I do, another user said he does that I do. The first user said something about religion. That seems like just it. There’s multiple religions. It’s fine to be passionate and a true believer in yours. But as part of society, accepting that other people believe differently is part of life.