Lesson 3: Code Completion#
In this lesson, you’ll learn to use IntelliSense-style auto-completion powered by the Java Language Server (jdtls).
Learning Objectives#
By the end of this lesson, you’ll be able to:
- Use auto-completion while typing
- Navigate completion suggestions
- Accept and dismiss completions
- Understand completion sources (LSP, snippets, buffer)
How Auto-Completion Works#
Neovim4j uses nvim-cmp for completion, which combines multiple sources:
- LSP (jdtls) - Intelligent Java completions
- Snippets - Common code patterns
- Buffer - Words from open files
- Path - File paths
Basic Completion Usage#
Auto-Trigger#
Start typing, and completions appear automatically:
public class Main {
public static void main(String[] args) {
System.out.
// ↑ Completions appear when you type the dot
}
}Manual Trigger#
If completions don’t appear automatically:
| Key | Action |
|---|---|
<Ctrl-Space> | Manually trigger completion |
<Ctrl-j> | Next suggestion |
<Ctrl-k> | Previous suggestion |
<Enter> | Accept suggestion |
<Ctrl-e> | Dismiss completion menu |
Navigating Suggestions#
When the completion menu is open:
<Ctrl-j>or<Down>- Move to next item<Ctrl-k>or<Up>- Move to previous item<Ctrl-d>- Scroll documentation down<Ctrl-u>- Scroll documentation up<Enter>- Accept selected item<Ctrl-e>- Close menu
Practice: Using Completions#
Example 1: Method Completion#
Create a new file or open Main.java:
package com.example;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names. // ← Type dot and wait
}
}When you type names., you’ll see:
add()remove()size()clear()- etc.
Try it:
- Type
names.ad - See completion narrow to
addmethods - Press
Enterto acceptadd(E e) - Fill in the parameter:
add("Alice")
Example 2: Creating Objects#
List<String> list = new Arr // ← Start typing ArrayListWhat happens:
- Type
new Arr - Completions show
ArrayList,Arrays, etc. - Select
ArrayList - Press
Enter - Auto-completes to
new ArrayList<>()
Example 3: Import Statements#
public class Main {
public static void main(String[] args) {
ArrayL // ← Type ArrayList (without import)
}
}What happens:
- Completion suggests
ArrayList - Accept it
- jdtls automatically adds:
import java.util.ArrayList;
Completion Symbols#
Completion items have icons indicating their type:
| Icon | Type |
|---|---|
| | Method |
| | Function |
| | Variable |
| | Field |
| | Class |
| | Interface |
| | Keyword |
| | Snippet |
| | Module |
Advanced Completion Features#
Snippet Completion#
Type common patterns and expand them:
Example: main snippet
- Type
main - Select the snippet from completions
- Press
Enter - Expands to full main method
Example: sout snippet
- Type
sout - Press
Enter - Expands to
System.out.println()
Example: for loop
- Type
for - Select snippet
- Tab through placeholders
Method Parameter Hints#
When calling methods, you’ll see parameter hints:
list.add( // Shows: add(E e) - parameter hint appearsCompletion Documentation#
Selected completion shows documentation in a popup window. Read it to understand:
- Method signatures
- Parameter types
- Return types
- Brief descriptions
Practice Exercises#
Exercise 1: String Methods#
Create this code and use completions:
String message = "Hello World";
message. // Explore available methodsTry finding:
length()- get string lengthtoUpperCase()- convert to uppercasesubstring(int beginIndex)- extract substringcontains(CharSequence s)- check if contains text
Exercise 2: List Operations#
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers. // Complete all these operations:
// add(10)
// add(20)
// get(0)
// size()
// remove(0)
}
}Use auto-completion for each method call.
Exercise 3: Creating Classes#
Start typing and let completion help:
public class Person {
private String name;
private int age;
// Use completion to generate:
// - Constructor
// - Getters
// - Setters
}Hint: Type get and see completion suggestions for getters.
Customization Tips#
If Completion is Too Aggressive#
Completions trigger automatically after 100ms. If you find this distracting:
- Open:
lua/plugins/completion.lua - Adjust
completion.autocompletedelay - Or disable auto-trigger entirely
Accepting with Tab#
By default:
Enteraccepts completionTabcan also be configured (check your config)
Signature Help#
When inside function parameters, press:
<Ctrl-k>To manually trigger signature help.
Common Issues#
Completions Not Appearing?#
Check LSP is running:
:LspInfoShould show
jdtlsattachedCheck completion plugin:
:checkhealth nvim-cmpRestart LSP:
:LspRestart
Wrong Completions?#
If completions seem incorrect:
- Make sure file is saved (
:w) - Wait for jdtls to finish indexing (first time can be slow)
- Check for syntax errors in your code
No Import Auto-Add?#
Make sure you’re accepting completions from the LSP source (they’ll have specific icons).
Tips for Effective Completion Usage#
- Let it flow - Don’t fight completions, let them guide you
- Learn patterns - Notice which completions appear for different contexts
- Read docs - The completion popup shows useful documentation
- Use snippets - They save time for boilerplate code
- Explore APIs - Type
.and browse what’s available
What’s Next?#
In Lesson 4: Creating Classes, you’ll learn multiple ways to create and manage Java classes.