How to make plugin hooking save?

Answered

I just want to make the plugin.

When I save the file (.dart), it checks code by regex and put the wavy line.

I research how to hook when I save

https://www.freecodecamp.org/news/how-to-create-an-intellij-plugin-lets-build-a-simple-dictionary-finder-6c5192b449c/

https://intellij-support.jetbrains.com/hc/en-us/community/posts/203374270-After-Save-Hooks?flash_digest=b37748c6c50e9e7deb387706be2e8bd72129c91f

I create a plugin project, I can't understand which for hook file by the selection of the menu below. 

I just want to know how to make the first file for the program  which hooking saving.

12 comments
Comment actions Permalink
Hi Vladimir Schneider

Thank you for replying.

I could put fileDocumentManagerListener inside of <extensions defaultExtensionNs="com.intellij">.

Do you mean this?

https://github.com/JetBrains/intellij-community/blob/ce81cfc8b19fadeecb30b03d09d449e595024920/platform/platform-impl/src/com/intellij/openapi/editor/impl/TrailingSpacesStripper.java

1
Comment actions Permalink

Yes, the defaultExtensionNs is required.

 

Yes the TrailingSpacesStripper.java uses FileDocumentListener. you don't need its particulars other than its implementation of the listener methods so you get an idea of what is expected by the IDE from extensions of this EP.

1
Comment actions Permalink

Vladimir Schneider

I added it in the plugin.xml

<extensions defaultExtensionNs="com.intellij">
<fileDocumentManagerListener id="your.implementation.id.here" implementation="SaveHook"/>
</extensions>

I create the SaveHook.java file in the src folder.

 

in the SaveHook.java I wrote like this. I guess it is the minimum code.

I wrote the code, "When I save, some error log will appear."

but, nothing happened.

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManagerListener;
import org.jetbrains.annotations.NotNull;

public final class SaveHook implements FileDocumentManagerListener {
@Override
public void beforeAllDocumentsSaving() {
Notifications.Bus.notify(
new Notification(
"sample",
"Hello Plugin!",
"hello.",
NotificationType.ERROR));
}

@Override
public void beforeDocumentSaving(@NotNull Document document) {
Notifications.Bus.notify(
new Notification(
"sample",
"Hello Plugin!",
"hello.",
NotificationType.ERROR));
}
}
1
Comment actions Permalink

"When I save, some error log will appear." It is a good sign because your hook is being called.

You will need to debug your code. You have to figure out what the error is because your plugin is causing it. 

You cannot do everything in your hook there are limitations on what the IDE expects not to be done on a save operation. Take a look at the stack trace for the exception and it will probably give you a good idea what you did wrong.

First, I would not try using notifications or other IDE API's at first. Just print a message to the console which does not depend on IDE state so will always succeed without causing exceptions and you will see the output in the console tab of your debug session in the IDE when debugging your plugin.

 

1
Comment actions Permalink

> "When I save, some error log will appear." It is a good sign because your hook is being called.

It just explained about my code. Actually, nothing has happened.

Error log which I said was this.

NotificationType.ERROR

I'm sorry for my English explanation was bad. in practice, no Exception and error have happened.

So, I can't find why it couldn't hook.

1
Comment actions Permalink

What do you think caused the Notification.ERROR to appear in the log, if not your hook?

Are you expecting a notification balloon?

Try to follow advice given. Do only System.out.println("beforeDocumentSaving") and System.out.println("beforeAllDocumentSaving") in your hook methods and nothing else. If it prints to the messages to the console then your hook is working.

You will have to figure out what you want to do in the hook and how you can do it within the IDE imposed limitations.

1
Comment actions Permalink

> Are you expecting a notification balloon?

Yes, I am. in the first, I want to try just save hooking.

I followed your advice.

It is my whole plugin.xml.

<idea-plugin>
<id>com.shinriyo.color</id>
<name>Plugin display name here</name>
<version>1.0</version>
<vendor email="shinriyo@gmail.com" url="http://shinriyo.hateblo.jp">shinriyo</vendor>

<description><![CDATA[
It just say colors format for Flutter.<br>
<em>most HTML tags may be used</em>
]]></description>

<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
]]>
</change-notes>

<idea-version since-build="173.0"/>

<extensions defaultExtensionNs="com.intellij">
<fileDocumentManagerListener id="hook" implementation="SaveHook"/>
</extensions>
</idea-plugin>

I put it in the src folder.

I wrote the printing code from yours.

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManagerListener;
import org.jetbrains.annotations.NotNull;

public final class SaveHook implements FileDocumentManagerListener {
@Override
public void beforeAllDocumentsSaving() {
System.out.println("beforeAllDocumentSaving");
}

@Override
public void beforeDocumentSaving(@NotNull Document document) {
System.out.println("beforeDocumentSaving");
}
}

I pushed [Run -> Debug 'Plugin'] on the menu bar.

And, new Intelli J and I save shortcut and also tried to chose [File -> Save All] on the menu.

But, nothing happened.

However, I also create a short-cut button Action program, it worked.

1
Comment actions Permalink

You can use FileDocumentManagerListener and register your implementation in plugin.xml:

<fileDocumentManagerListener id="your.implementation.id.here" implementation="your.implementation.fqn.here"/>

For an example you can look at intellij-community master branch in the TrailingSpacesStripper class or other classes which implement the FileDocumentManagerListener interface.

0
Comment actions Permalink

Try putting your hook into  "com.shinriyo" package. I don't know if IDE will accept extensions with null package.

If you set a break point in one of the hook methods does it break there?

Also, keep in mind that nothing is saved if no files are modified. So you have to modify a file and then invoke save or save all.

 

0
Comment actions Permalink

OK. I thought log appears in plugin's console, doesn't it?

I thought log appears int test IDE's console.

Because, in the action which extends AnAction case, the log appears test IDE's console.

I also succeeded in run Notifications.

Thank you.

 

At last, I reached the starting line!!

Next, I make the code that checks in the file by regex and put the wavy line in it.

 

0
Comment actions Permalink

The console output appears in the IDE which is debugging your plugin. Good luck.

0
Comment actions Permalink

"beforeFileContentReload" method has "VirtualFile file" parameter, but "beforeDocumentSaving" doesn't have the parameter.

So, I can't use it.

file.getExtension());

I want to get an extension name, because I get only ".dart" file.

And next, I want to draw a wavy line. but, I can't find it in the developing document.

 

0

Please sign in to leave a comment.