Developing an IntelliJ IDEA Plugin for Smart Code Completion and Formatting
This article explains how to design, implement, debug, and publish an IntelliJ IDEA plugin that automatically inserts semicolons and braces, formats code, and streamlines development workflow using a chain‑of‑responsibility architecture triggered by Ctrl+Enter, complete with code examples and deployment steps.
As developers accumulate years of experience, the need to customize the IDE to improve team efficiency, enforce consistent code style, and eliminate repetitive tasks such as manually adding semicolons and braces becomes increasingly urgent.
The proposed solution is an IntelliJ plugin that, after typing a line of code, simply presses Ctrl+Enter to automatically complete line endings, format the current line, and handle block exits, thereby freeing developers to focus on business logic.
Plugin features include intelligent end‑of‑line completion, automatic line formatting, and block‑exit handling, eliminating the need to repeatedly type ; or {} .
IntelliJ platform overview outlines its core modules—Base Platform, Action System, PSI, VFS, GUI, Editor Basics, Plugin, and Others—providing the foundation for building extensions.
Plugin architecture uses META-INF\plugin.xml to declare actions and extensions. Actions extend AnAction or EditorAction , and are registered in the <actions> section. The core logic follows a chain‑of‑responsibility pattern defined by the IEnterHandler interface, with concrete handlers such as BaseAppendSemicolonHandler , BaseAppendBracketHandler , and OnReturnEnterHandler :
public interface IEnterHandler {
boolean isEnabledForCase(EnterEvent event);
boolean execute(EnterEvent event);
} public class SuperEnterAction extends EditorAction {
public SuperEnterAction() {
super(new Handler());
}
private static class Handler extends EditorWriteActionHandler {
@Override
public boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
return getEnterHandler().isEnabled(editor, caret, dataContext);
}
@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
// custom logic
}
}
} private EnterManager enterManager = new EnterManager();
private void initHandlers() {
enterManager.addHandler(new OnConditionEnterHandler());
// ... other handlers ...
enterManager.addHandler(new FinalEnterHandler());
}Debugging can be performed via DevKit (using the IDE’s Debug button) or via Gradle tasks such as intellij\runIde or intellij\runIdea , with source attachment for IntelliJ SDK to step through platform code.
Publishing is done either through DevKit’s “Build | Prepare Plugin Module for Deployment” or Gradle tasks like buildPlugin , patchPluginXml , prepareSandbox , and publishPlugin , producing either a single‑jar or a zip package depending on external dependencies.
The plugin works throughout the class lifecycle—class declarations, method definitions, control‑flow statements, and assignment expressions—by pressing Ctrl+Enter , as demonstrated by several animated GIFs.
Reference: IntelliJ official documentation.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.