EditorTabColorProvider not called at startup

已回答

I use an implementation of EditorTabColorProvider to colorize tabs that contain Java Swing controls. This is working well when I open a new tab. But when I close IntelliJ and restart it, the coloring is gone and only comes back, when I reopen the tabs.

I did set a breakpoint and saw that the method "getEditorTabColor" never gets called at startup, only when manually opening a file.

This is how I register my provider:

<editorTabColorProvider implementation="Tcp" order="first" />

How can I restore coloring at startup without having to reopen all tabs?

0

Please clarify how you do achieve "that contain Java Swing controls" in your implementation. The problem could maybe be solved by implementing com.intellij.openapi.project.DumbAware additionally, but then you must not access indexes in your implementation.

0

Here is the relevant part of my code:

 

public class Tcp implements EditorTabColorProvider {
@Nullable
@Override
public Color getEditorTabColor(@NotNull Project project, @NotNull VirtualFile virtualFile) {

PsiFile psifile = PsiManager.getInstance(project).findFile(virtualFile);

if (psifile instanceof PsiJavaFile) {
PsiJavaFile psiJavaFile = (PsiJavaFile) psifile;
String PackageName = psiJavaFile.getPackageName();
if (PackageName.contains("foo.bar")) { // Precondition to save some time
for (PsiClass cl : psiJavaFile.getClasses()) {
if (isSwingControl(cl)) {
return new JBColor(new Color(245,225,215), new Color(1,2,3));
}
}
}
}

return null;
}

private boolean isSwingControl(PsiClass psiClass) {
// here I basically check the class (and its super classes, provided by InheritanceUtil),
// if the qualified name matches "java.awt.Component"
}
}
0
Avatar
Permanently deleted user

Please try

public class Tcp implements EditorTabColorProvider, DumbAware {...
0

Then it throws an IndexNotReadyException when calling InheritanceUtil.getSuperClasses()

0
Avatar
Permanently deleted user

OK, I see. While DumbService.isDumb(project) you should return null

0

Then I'm back at my original problem. The initial call will return immediately because Index is not loaded yet and it does not get called any more until I close and reopen the Tab.

Is there any way to force the editor to call the color provider? Then I could register a listerner which is fired when the project is loaded and manually call the colorizing process

0
Avatar
Permanently deleted user

Our own implementations don't depend on indexes. Unfortunately there is no "direct" listener but there are two reasons to update background color:

  1. Any changes happen in UISettings
  2. VirtualFile is renamed/moved.
    Sure we should update background color just after indexes are build (https://youtrack.jetbrains.com/issue/IDEA-263296)
0
Avatar
Permanently deleted user

Update: IDEA-263296 is fixed in master branch. This example works for me:

<extensions defaultExtensionNs="com.intellij">
<editorTabColorProvider implementation="api.TestColorProvider" order="first"/>
</extensions>
package api;

import com.intellij.openapi.fileEditor.impl.EditorTabColorProvider;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.util.InheritanceUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

public class TestColorProvider implements EditorTabColorProvider {
@Nullable
@Override
public Color getEditorTabColor(@NotNull Project project, @NotNull VirtualFile virtualFile) {
if (DumbService.isDumb(project)) return null;
try {
PsiFile psifile = PsiManager.getInstance(project).findFile(virtualFile);
if (psifile instanceof PsiJavaFile) {
PsiJavaFile psiJavaFile = (PsiJavaFile) psifile;
for (PsiClass cl : psiJavaFile.getClasses()) {
if (InheritanceUtil.isInheritor(cl, "java.awt.Component")) {
return Color.RED;
}
}
}
} catch (Exception e) {
return Color.BLACK;
}
return null;
}
}
0

Wow, that was fixed quickly. Thank you.

2

请先登录再写评论。