In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to write an IDEA plug-in to achieve event monitoring", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write an IDEA plug-in to monitor events.
How to listen for project or module change events
The first is project-level event monitoring. To add a project management event listener, we need to implement the ProjectManagerListener interface, which has four methods with the following source code.
Public interface ProjectManagerListener extends EventListener {default void projectOpened (@ NotNull Project project) {} default void projectClosed (@ NotNull Project project) {} default void projectClosing (@ NotNull Project project) {} default void projectClosingBeforeSave (@ NotNull Project project) {}}
ProjectOpened: this method is called back when the project is opened
ProjectClosingBeforeSave: callback when the project is closed, before starting to save the project, or before calling the FileDocumentManager#saveAllDocuments method to save all files
ProjectClosing: callback after projectClosingBeforeSave method
ProjectClosed: unlike projectClosing, projectClosed is called back when the project has been closed and called when the ProjectManagerImpl#closeProject method executes to the last line of code.
With the project management event listener, how do we register the listener?
There are two ways, one is to register code, and the other is to register in the plugin.xml plug-in configuration file.
Code registration can call ProjectManager.getInstance (). AddProjectManagerListener (); method registration, but the drawback of this method registration is that the project open event cannot be listened to, the projectOpened method will not be called, and the project should actually be open when we can call this method to register the listener.
Therefore, we can only register the project management listener by modifying the plugin.xml configuration file. The configuration code is as follows:
Topic: fill in the event subject, which is similar to the Topic in message middleware, except that the interface class name of the event listener is entered here.
Class: add the name of the implementation class for the interface
When we register a custom project management event listener with IDEA, we can register other event listeners through the project management event listener, such as registration module listeners, this is because the module's event trigger is triggered only after the project open event is triggered. Therefore, any other event listener can be registered in the projectOpened method.
The registration module event listener code is as follows:
Project.getMessageBus () .connect () .subscribe (ProjectTopics.MODULES, new ModuleListener () {})
The subscribe method requires two parameters:
Topic: topic. Available values can be found in the source code of the ProjectTopics class, including PROJECT_ROOTS and MODULES
Handler: event handler, listener. When topic is MODULES, a ModuleListener is required.
The definition of ModuleListener interface is as follows:
Public interface ModuleListener extends EventListener {default void moduleAdded (@ NotNull Project project, @ NotNull Module module) {} default void beforeModuleRemoved (@ NotNull Project project, @ NotNull Module module) {} default void moduleRemoved (@ NotNull Project project, @ NotNull Module module) {} default void modulesRenamed (@ NotNull Project project, @ NotNull List modules, @ NotNull Function oldNameProvider) {}}
ModuleAdded: called when the add module is complete
BeforeModuleRemoved: called before the module is removed
ModuleRemoved: called when the module is removed
ModulesRenamed: called when the module changes its name
How to listen for file editing events
Through the study of the previous two articles, we have learned what PSI is, that a file corresponds to a PsiFile, a PsiFile itself is a PsiElement, is made up of many PsiElement, and each PsiElement can have a child PsiElement.
Therefore, listening for file change events is actually listening for structural change events of the PSI tree. We need to register the PsiTreeChangeListener through PsiManager, as follows.
PsiManager.getInstance (project). AddPsiTreeChangeListener (new PsiTreeChangeListener () {/ /. }, FILES::clear)
As for the timing of registration, depending on the situation, you can register when Service is initialized, when AnAction triggers, or in the projectOpened event method.
There are many methods defined by PsiTreeChangeListener API, which can be divided into two types of events, one is before event, the other is after event. The source code of API is as follows.
Public interface PsiTreeChangeListener extends EventListener {void beforeChildAddition (@ NotNull PsiTreeChangeEvent event); void beforeChildRemoval (@ NotNull PsiTreeChangeEvent event); void beforeChildReplacement (@ NotNull PsiTreeChangeEvent event); void beforeChildMovement (@ NotNull PsiTreeChangeEvent event); void beforeChildrenChange (@ NotNull PsiTreeChangeEvent event); void beforePropertyChange (@ NotNull PsiTreeChangeEvent event); void childAdded (@ NotNull PsiTreeChangeEvent event); void childRemoved (@ NotNull PsiTreeChangeEvent event); void childReplaced (@ NotNull PsiTreeChangeEvent event); void childrenChanged (@ NotNull PsiTreeChangeEvent event); void childMoved (@ NotNull PsiTreeChangeEvent event); void propertyChanged (@ NotNull PsiTreeChangeEvent event);}
ChildrenChanged: called when the content of a child element changes
ChildReplaced: the child element is called when it is replaced, and the childReplaced event is triggered with the childrenChanged event.
ChildAdded: called when child elements are added, and childAdded events are also triggered with childReplaced, childrenChanged, or events
ChildRemoved: called when child elements are removed, and triggering childRemoved events is also accompanied by childReplaced and childrenChanged events
PropertyChanged: called when the property is changed, such as changing the file name
At this point, I believe you have a deeper understanding of "how to write an IDEA plug-in to achieve event monitoring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.