Detecting if a file is being edited.

I am working on a custom plugin where I want to detect if a file is being opened for edit. Is there any way to do this?

8 comments
Comment actions Permalink

To check if a file is opened in an editor tab, you can check that FileEditorManager.getAllEditors(VirtualFile) returns a non-empty array. If you mean something else by "is being edited", you may want to use a different detection method.

0
Comment actions Permalink

Thanks Dmitry for reply.

In a project, when a user tries to edit a read only file, then a pop-up window is generated informing user to clear read only status. I want to do some custom operations for such files by a plugin. So, I am looking for any API which notifies such action.

There can be a thread which polls and gets list of open files through API you suggested and do operations. But I am looking if in some way, plugin can get notified.

0
Comment actions Permalink

Ah, this is a completely different API. You need to provide an implementation of the WritingAccessProvider interface.

0
Comment actions Permalink
wAccessProvider.java:
 
public class sdAccessProvider extends WritingAccessProvider{
    @NotNull
    @Override
    public
Collection<VirtualFile> requestWriting(final VirtualFile... files) {
        Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "request writing", NotificationType.INFORMATION));
        return Collections.emptyList();
    }

    @Override
    public boolean
isPotentiallyWritable(@NotNull final VirtualFile file) {
        
Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "isPotential", NotificationType.INFORMATION));
        return true;}}
     
plugin.xml:
 
<extensions defaultExtensionNs="com.intellij">
  <!-- Add your extensions here -->
  <writingAccessProvider implementation="wAccessProvider"/>

</extensions>
I have put above code in wAccessProvider.java and plugin.xml. Now when I am trying to edit some file then on each keypress, I am seeing the logs which I have put twice. Shouldn't these calls should be made only once.

Could you please give some detail on what these APIs definition should contain?


0
Comment actions Permalink

isPotentiallyWritable() needs to return true if the file is currently read-only (using whatever definition of "read-only" that is apprioriate for your system) but can be made writable.

requestWriting() is called with a set of files for which isPotentiallyWritable() returns true. It needs to perform the necessary action to make the files writable, and return an empty set if all files have been made writable, or a set of files which are still read-only.

0
Comment actions Permalink

4:20:30 PM Doing -isPotential
4:20:30 PM E:/plugin/test/src/Test
4:20:30 PM Doing -request writing
4:20:30 PM E:/plugin/test/src/Test
4:20:30 PM Doing -isPotential
4:20:30 PM E:/plugin/test/src/Test
4:20:30 PM Doing -request writing
4:20:30 PM E:/plugin/test/src/Test
4:20:30 PM Doing -isPotential
4:20:30 PM E:/plugin/test/src/Test



In isPotentiallyWritable(), I am always returing true and in requestWriting(), always returning an empty list.

Now on every character I type in "Test" file, I am getting above logs. That is twice, it calls isPotential and requestWriting and it shows this behavior for all subsequent character typing in the file. I am trying to understand:

1. Is it always going to call requestWriting() for a file for subsequent edit? Or just once and when requestWriting() returns with empty list, meaning file has been granted write access.
2. Why the calls are made twice on a typing a charcter in the file?

0
Comment actions Permalink

IntelliJ IDEA does not track whether the file is writable from the point of view of each WritingAccessProvider. Because of that, it will ask you before every operation.

Your current implementation is meaningless from the point of view of the API contract. Effectively, IDEA asks you "is this file writable?", you say "no", then it asks you "please make it writable", you say "OK I made it writable", then it asks again "is it writable?" and you again say "no".

The fact that this happens twice per character typed is just an implementation artifact.

0
Comment actions Permalink

In isPotentiallyWritable, I am returning true i.e. whether it is a read-only or not, it is writable. In requestWriting API, I am doing few operations and remove read-only status of the file.


Now, when I try to edit a read-only file through IDE, then I get pop-up window asking for clear read-only status and also "requestWriting" API is called in the background. At this point, I don't touch pop-up window and I can see in file properties that its read-only status is removed because of requestWriting API completion in background. And then I cancel the pop-up window. I am still trying to know

1. Shouldn't isPotentiallyWritable/requestWriting be called on for files which have read-only status and not for which have already got permission?
2. Why am I getting a pop-up window for clear read-only status when I am using a separate access provider in the plugin? Is there a way that I don't see this or a put a custom call in place of this?

0

Please sign in to leave a comment.