17. Commenter
A commenter enables the user to comment-out a line of code at the cursor or selected code automatically.
The Commenter
defines support for “Comment with Line Comment” and “Comment with Block Comment” actions.
17.1. Define a Commenter
The Simple Language commenter subclasses Commenter
.
This commenter defines the line comment prefix as “#”.
// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.sdk.language;
import com.intellij.lang.Commenter;
import org.jetbrains.annotations.Nullable;
public class SimpleCommenter implements Commenter {
@Nullable
@Override
public String getLineCommentPrefix() {
return "#";
}
@Nullable
@Override
public String getBlockCommentPrefix() {
return "";
}
@Nullable
@Override
public String getBlockCommentSuffix() {
return null;
}
@Nullable
@Override
public String getCommentedBlockCommentPrefix() {
return null;
}
@Nullable
@Override
public String getCommentedBlockCommentSuffix() {
return null;
}
}
17.2. Register the Commenter
The SimpleCommenter
implementation is registered with the IntelliJ Platform in the plugin configuration file using the com.intellij.lang.commenter
extension point.
<extensions defaultExtensionNs="com.intellij">
<lang.commenter language="Simple" implementationClass="org.intellij.sdk.language.SimpleCommenter"/>
</extensions>
17.3. Run the Project
Open the example Simple Language properties file in the IDE Development Instance.
Place the cursor at the website
line.
Select Code | Comment with Line Comment.
The line is converted to a comment.
Select Code | Comment with Line Comment again, and the comment is converted back to active code.