8. Line Marker Provider
Line markers help annotate code with icons on the gutter. These markers can provide navigation targets to related code.
Define a Line Marker Provider
A line marker provider annotates usages of Simple Language properties within Java code and provides navigation to the definition of these properties. The visual marker is a Simple Language icon in the gutter of the Editor window.
The Simple Language marker provider subclasses
RelatedItemLineMarkerProvider. For this example, override the
collectNavigationMarkers() method to collect usage of a Simple Language key and separators:
GutterIconDescriptor allows configuring gutter icons to be shown via .
Best Practices for Implementing Line Marker Providers
This section addresses important details about implementing a marker provider.
collectNavigationMarkers() method should:
Only return line marker information consistent with the element passed into the method. For example, do not return a class marker if
getLineMarkerInfo()was called with an element that corresponds to a method.
Return line marker information for the appropriate element at the correct scope of the PSI tree. Return leaf elements only - i.e., the smallest possible elements. For example, do not return method marker for
PsiMethod. Instead, return it for the
PsiIdentifierwhich contains the name of the method.
What happens when a
LineMarkerProvider returns marker information for a
PsiElement that is a higher node in the PSI tree? For example, if
MyWrongLineMarkerProvider() erroneously returns a
PsiMethod instead of a
The consequences of the
MyWrongLineMarkerProvider() implementation have to do with how the IntelliJ Platform performs inspections. For performance reasons, inspection, and specifically the
LineMarkersPass queries all
LineMarkerProviders in two phases:
The first pass is for all elements visible in the Editor window,
The second pass is for the rest of the elements in the file.
If providers return nothing for either area, the line markers get cleared. However, if a method like
actionPerformed() is not completely visible in the Editor window (as shown in the image above,) and
MyWrongLineMarkerProvider() returns marker info for the
PsiMethod instead of
The first pass removes line marker info because whole
The second pass tries to add a line marker because
MyWrongLineMarkerProvider()is called for the
As a result, the line marker icon would blink annoyingly. To fix this problem for this case, rewrite
MyWrongLineMarkerProvider to return info for
PsiIdentifier instead of
PsiMethod as shown below:
Register the Line Marker Provider
SimpleLineMarkerProvider implementation is registered with the IntelliJ Platform in the plugin configuration file using the
com.intellij.codeInsight.lineMarkerProvider extension point.
Run the Project
Run the plugin by using the Gradle
Open the Java Test file. Now the icon appears next to line 3 on the gutter. A user can click on the icon to navigate to the property definition.