Sub topic 8
Writing Apex with code completion, navigation, and diagnostics
This chapter assumes you have installed VS Code, the Salesforce CLI, a Java JDK, and the Salesforce Extension Pack. It also assumes you have created a Salesforce DX project and authorized an org. If any of those are missing, finish them first. The Apex features below depend on the project layout and on Java.
How the Apex language server works
The Apex extension runs a language server. This is a separate Java process that reads your Apex files and answers questions from the editor: what can be typed here, where is this symbol defined, and is this line valid. VS Code sends requests to the server, and the server sends back results.
The server indexes the packageDirectories listed in sfdx-project.json. Apex files outside those directories are not indexed, so completion and navigation will not work for them. The first start can take a short while because the server builds this index.
Create a class to work with
Create force-app/main/default/classes/GreetingService.cls and add this code:
public with sharing class GreetingService {
public static String greet(String name) {
if (String.isBlank(name)) {
return 'Hello, world!';
}
return 'Hello, ' + name + '!';
}
}Save the file. The Apex extension also expects a companion GreetingService.cls-meta.xml file. If it is missing, the deploy step in a later chapter will fail. Creating the class with the command SFDX: Create Apex Class makes both files for you.
Code completion
Completion suggests names as you type. Try these in a new Apex file or a new method:
- Type
String.and wait. The list shows static methods such asisBlank,isNotBlank, andvalueOf. - Type
GreetingService.and the list showsgreet, which comes from your own class. - Press
Ctrl+Space(Cmd+Spacecan clash with Spotlight on macOS) to open the list manually. - Press
TaborEnterto accept a suggestion.
Completion covers system classes, your own classes, and SObject fields that the extension knows about. Field and SObject suggestions come from local metadata, so they can lag behind your org until you retrieve that metadata.
Navigation
Use these actions to move through a codebase:
| Action | Windows and Linux | macOS |
|---|---|---|
| Go to Definition | `F12` | `F12` |
| Peek Definition | `Alt+F12` | `Option+F12` |
| Find All References | `Shift+F12` | `Shift+F12` |
| Go to Symbol in File | `Ctrl+Shift+O` | `Cmd+Shift+O` |
| Go to Symbol in Workspace | `Ctrl+T` | `Cmd+T` |
The Outline view in the Explorer sidebar lists the methods and properties of the open class. This is a quick way to jump around a long file.
Diagnostics
Diagnostics are the red and yellow underlines in the editor. They also appear in the Problems panel, which you open with Ctrl+Shift+M (Cmd+Shift+M on macOS).
Try introducing an error. Change String.isBlank(name) to String.isBlnk(name) and save. The server underlines the call and reports that the method does not exist. Hover over the underline to read the message. Fix the typo and the marker disappears.
Common problems
- No completions and no outline. The server has not started. Open the Output panel, choose Apex Language Server from the dropdown, and look for a Java error. A missing JDK is the usual cause, and a later chapter covers the
salesforcedx-vscode-apex.java.homesetting. - Symbols from another class are not found. Check that the file sits under a directory listed in
packageDirectories. - Stale errors after a large change. Run
Developer: Reload Windowfrom the Command Palette to restart the server.
Exercise
You add a new class outside the force-app directory, and GreetingService. shows no completions for it. What should you check first, and why?
Show answer
Check whether the directory is listed under packageDirectories in sfdx-project.json. The language server only indexes source in those directories, so classes outside them are invisible to completion and navigation. Moving the file under force-app, or adding its directory to packageDirectories, fixes it.
Exercise
You misspell a method name and save the file, but no underline appears. Which panel do you check to see whether the language server is running, and what do you look for?
Show answer
Open the Output panel and select Apex Language Server. Look for a Java error, such as a missing runtime. If the server did not start, the editor cannot produce diagnostics.