FileTypeDetector not called for the .txt file, should I use FileTypeOverrider instead?
Answered
I'm creating a custom language support plugin. Source files have .txt extension, so an only way to differ them from simple txt is detecting whether they have necessary type using their content.
I register my file type in the following way:
<fileType name="DSF Source" implementationClass="com.onpositive.dsfedit.language.DSFFileType"
fieldName="INSTANCE" language="DSF" extensions="txt" />
File type detector is registered as following:
fileTypeDetector implementation="com.onpositive.dsfedit.language.DSFFileTypeDetector" order="first"/
Code:
public class DSFFileTypeDetector implements FileTypeRegistry.FileTypeDetector {
@Override
public @Nullable FileType detect(@NotNull VirtualFile file, @NotNull ByteSequence firstBytes, @Nullable CharSequence firstCharsIfText) {
if (firstCharsIfText.length() > 10 && firstCharsIfText.charAt(0) == 'A' || firstCharsIfText.charAt(0) == 'I') {
return DSFFileType.INSTANCE;
}
return null;
}
@Override
public int getVersion() {
return 0;
}
}
Breakpoint inside `detect` isn't hit, even it I remove `extensions="txt"` from the first declaration
After some debugging I've found `fleTypeOverrider` extension, which is fired OK and defines my file type by content well. but it's result doesn't seem to be cached, so I'm caching them myself using file path as a key.
Am I doing smth wrong with fileTypeDetector, or fileTypeOverrider should be used in such a case?
Please sign in to leave a comment.
Could you please link your plugin's sources. If not possible, please specify your target platform version.
NP, it's opensource: https://github.com/32kda/dsf-editor
Sorry for delay, reproduced problem.
It seems there is problem when extension is already matched to existing filetype (*.txt), changing test data file extension to e.g. test.xtxt and detector works as expected. For sure, do not mix overrider and detector at the same time. Please watch https://youtrack.jetbrains.com/issue/IDEA-241309.
Thanks for response, watching
Anyway - if Overrider isn't for inner use or smth like that, would just use it.
Best regards,
KDA