After familiarizing yourself with the official tutorial, do you have an itch to implement a functional plugin yourself? Let’s see how other people implement plug-ins, and then we’ll build our own plug-in based on their plug-ins.

I choose plug-in is the Active Tab is plugins.jetbrains.com/plugin/9562 Highlighter home page…

The function of this plug-in is very simple. It can mark the TAB of the currently selected file with a special color, and can select different colors.

However, I don’t think this plugin is enough for my needs. I want something like the following: I can change the colors of the 4 most recently opened files, and the more recently opened files get darker.

ActiveTabHighlighterPlugin we download the source code to see what is the function of its implementation. Download the code

git clone https://github.com/tobszarny/ActiveTabHighlighterPlugin.git
Copy the code

After opening the project, we first look at plugin.

<extensions defaultExtensionNs="com.intellij">
    <! -- Startup -->
    <postStartupActivity implementation="com.tobszarny.intellij.plugin.activetabhighlighter.ActiveTabHighlighterStartupActivity"/>

    <editorTabColorProvider
            implementation="com.tobszarny.intellij.plugin.activetabhighlighter.editor.CustomEditorTabColorProvider"/>
    <projectConfigurable groupId="tools" displayName="Active Tab Highlighter Plugin"
                             id="preference.HighlighterSettingsConfigurable"
                             instance="com.tobszarny.intellij.plugin.activetabhighlighter.config.HighlighterSettingsConfigurable"/>
    <projectService serviceImplementation="com.tobszarny.intellij.plugin.activetabhighlighter.config.HighlighterSettingsConfig"/>
</extensions>
Copy the code

ActiveTabHighlighterPlugin are implemented by extending the four extensions, we analyze them one by one

postStartupActivity

PostStartupActivity is executed immediately after a project is opened. You can guess ActiveTabHighlighterStartupActivity used for initialization.

Let’s look at the implementation of this class

The class which is crucial to monitor the two message queue, which is FileEditorManagerListener IDEA built-in queue, responsible for the new file is opened, closed, notify the listener when switching.

HighlighterSettingsChangeListener is ActiveTabHighlighterPlugin write their own queue, the main function is to monitor palette changes after treatment. Both of the queue to monitor implementation is TabHighlighterFileEditorListener class, realize the function of add color to the TAB two key method is to highlight and unhighlight, a highlighting, highlighting a cancelled.

The input parameter file represents the file that needs to be highlighted.

This class also implements the FileEditorManagerListener selectionChanged method and custom settingsChanged method.

The implementation idea is simple: highlight focus file every time you switch to focus file, and unhighlight out-of-focus files.

So to achieve the desired function, we only need to use a queue to retain the sequence of files in the focus, and add TAB colors to the files in the sequence.

Here’s how I did it

I also modified the hightlight method to handle the color brightness according to the input parameter I.

Also remember to change the initialization location to add the currently focused file to the list

At this point, our main functionality has been implemented. Let’s talk about the other three extensions

editorTabColorProvider

It’s not explained on the website, but there are comments in the code. It gets the color of the TAB.

I have observed that the extensions do this: If your project is managed by Git, the Git plugin will also change the color of the TAB (by default, the text will turn blue). If you do not add editorTabColorProvider, git will change the color of the TAB to unhighlighted every time git changes colors.

ProjectConfigurable and projectService

Standard collocations are used to save configurations, in this case to implement the palette, and to save palette information.

The final implementation can be seen here: gitee.com/kagami1/myp… Just pack it up.

Other problems

In the process of plug-in development, we often need to draw our own UI. The UI design tool of IDEA can basically meet our needs. Create a GUI Form with new

Sometimes when you right-click, you will find that this option is not available. You need to check the UI Designer plug-in in the plug-in list and restart it.

The relevant data

The IDEA of plug-in website: plugins.jetbrains.com/docs/intell…

The Extension Point List:plugins.jetbrains.com/docs/intell…

IDEA sample project address: github.com/JetBrains/i…

Live Templates function description: www.jetbrains.com/help/idea/t…