Is it possible to associate file type automatically if it is already associated?

Answered

Hi. I am creating plugin for *.eo files. Most users have already associated *.eo files as txt long time ago. My plugin doesn't work for these people because plugin doesn't see .eo files. It is possible to fix it manually if you go to preferences and remove association *.eo with *.txt. Then plugin starts highlight code and etc.

But is it possible to remove eo to txt association programatically? May be there are other solutions? Hope my question is clear

0
11 comments

Hi Stepa,

It is possible by implementing com.intellij.psi.LanguageSubstitutor and registering in com.intellij.lang.substitutor EP.

0

Couldn't find clear documentation. Should it look like this?

public class EOSubstitutor extends LanguageSubstitutor {
@Nullable
@Override
public Language getLanguage(@NotNull VirtualFile virtualFile, @NotNull Project project) {
if (virtualFile.getExtension() == null) {
return null;
}
if (virtualFile.getExtension().equals("eo")) {
return EOLanguage.INSTANCE;
}
return null;
}
}

Here I tried to associate *.eo files with EO languages and this code is reachable but it doesn't work

0

If you already associated *.eo with text files and after that installed my plugin still occurs this problem

I expected to see *.eo in right tab (even if *.eo are already associated with text files)

0
  1. What do you mean that this code is reachable? Is the EOLanguage.INSTANCE returned?
  2. Can you show your fileType extension registration and implementation?
  3. Are your EO files opened as EO language files or text files?
0

1. I mean that EOLanguage.INSTANCE returned

2. Implementation FileType

public class EOFileType extends LanguageFileType {
public static final String FILE_EXTENSION = "eo";
public static final EOFileType INSTANCE = new EOFileType();

protected EOFileType() {
super(EOLanguage.INSTANCE);
}

@NotNull
@Override
public String getName() {
return "EO";
}

@NotNull
@Override
public String getDescription() {
return "EO";
}

@NotNull
@Override
public String getDefaultExtension() {
return FILE_EXTENSION;
}

@Nullable
@Override
public Icon getIcon() {
return Icons.EO_ICON;
}
}

FileTypeFactory:

public class EOFileTypeFactory extends FileTypeFactory {
@Override
public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) {
fileTypeConsumer.consume(EOFileType.INSTANCE, EOFileType.FILE_EXTENSION);
}
}

Registration:

<fileTypeFactory implementation="org.eolang.jetbrains.EOFileTypeFactory"/>

 

3. EO files opened but associated with text. It means that user has done this before downloading my plugin:

0

By the way, I tried to reproduce this problem (IntelliJ 2020.3.2). I associated *.eo with text and then installed my plugin) I got message:

But my colleges with other versions don't receive this message and *.eo files are still associated with text

0

What is the reason for using fileTypeFactory? It is deprecated. Could you please switch to fileType as the deprecation comment suggests and see if the problem is solved?

0

I upgraded org.jetbrains.intellij to 1.2.0 and removed fileTypeFactory but problem still appears.

Source: https://github.com/yasamprom/eo-intellij-plugin-copy2 

0

Please try with FileTypeManager, e.g. 

FileTypeManager.getInstance().removeAssociation(FileTypes.PLAIN_TEXT, ...);
0

Could you please provide more examples or link to documentation? I tried to add this code in PluginContoller class into projectOpened method but I don't see my logs in idea.logs

Why isn't this code reachable?

FileTypeManager fileTypeManager = FileTypeManager.getInstance();
fileTypeManager.removeAssociation(FileTypes.PLAIN_TEXT, new FileNameMatcher() {
@Override
public boolean accept(@NotNull String s) {
LOG.info(s + " CHECKING FILE");
return s.endsWith(".eo");
}

@NotNull
@Override
public String getPresentableString() {
return null;
}
});
0

Well, I found this solution:

FileTypeManager fileTypeManager = FileTypeManager.getInstance();
Runnable r1 = ()->fileTypeManager.removeAssociatedExtension(FileTypes.PLAIN_TEXT, "eo");
Runnable r2 = ()->fileTypeManager.associateExtension(EOFileType.INSTANCE, "eo");
WriteCommandAction.runWriteCommandAction(project, r1);
WriteCommandAction.runWriteCommandAction(project, r2);

I inserted this code into initComponent in PluginController class

0

Please sign in to leave a comment.