Is this the correct way of opening a file in the editor

Hi everyone !

I am experiencing a strange behaviour, I am developing a plugin and while I need to open a file located in the plugin directory I get : Slow operations are prohibited on EDT

I am wondering if this is the right way of doing it :

VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(bookmarkFile.path());
if (virtualFile != null) {
  ApplicationManager.getApplication().runReadAction(() -> {
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile);
    FileEditorManager.getInstance(project).openEditor(descriptor, false);
  });
}

The plugin works, I do not get that error a lot, it’s from time to time.
I tried without the ApplicationManager.getApplication().runReadAction(() -> { but the same, I am running out of options (I tried the invokeLater too) because this is needed to be run on EDT at the end.

I would really appreciate any kind of help :grinning_face:

Thank you

2025-03-24 11:00:55,627 [ 43703] SEVERE - #c.i.u.SlowOperations - IntelliJ IDEA 2023.3.8 Build #IC-233.15619.7
2025-03-24 11:00:55,627 [ 43703] SEVERE - #c.i.u.SlowOperations - JDK: 17.0.12; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2025-03-24 11:00:55,627 [ 43703] SEVERE - #c.i.u.SlowOperations - OS: Mac OS X

.findFileByPath() traverses the virtual file system, which is considered “slow” and thus should not be done in EDT. You need to switch to BGT first.

In my plugin, I do this (the actual code can be found here):

@Service(Service.Level.PROJECT)
class CoroutineService(override val scope: CoroutineScope)

project.service<CoroutineService>().scope.launch {
    val virtualFile = withBackgroundProgress(project, indicatorTitle, cancellable = true) {
        LocalFileSystem.getInstance().findFileByPath(somePath)
    }

    withContext(Dispatchers.EDT) {
        FileEditorManager.getInstance(project).openEditor(virtualFile!!, false)
    }
}

I’m not sure what the equivalent Java code would be though.

1 Like

Thank you very much !
The Java code is now looking like this :

ApplicationManager.getApplication().executeOnPooledThread(() -> {
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(bookmarkFile.path());
    if (virtualFile != null) {
        ApplicationManager.getApplication().invokeLater(() -> {
            if(!project.isDisposed()) {
                OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile);
                FileEditorManager.getInstance(project).openEditor(descriptor, false);
            }
        });
     }
});

But still get the error pointing (mostly when starting the project and the first time I invoke the code, it does not appear to do that later. Maybe because it does not have enough ressource ?

FileEditorManager.getInstance(project).openEditor(descriptor, false);

[…] mostly when starting the project and the first time I invoke the code, it does not appear to do that later.

This is a log-only error. If I recall correctly, it is reported once per IDE session.

As for why your code still doesn’t work, I’m not sure; I have never written threading code in Java.

1 Like

I will try to do that class in Kotlin and see if it’s better based on what you provided.

Thank you again !