Why is local history turned off for files detected via FileTypeDetector?

My plugin has a FileTypeDetector to detect the file type of files without extension, based on the content / shebang line.

A user reported that local history is disabled for such files and I can confirm this.
I would like to enable the local history for detected files.
Internally, IdeaGateway.isVersioned(VirtualFile, boolean) returns false because WorkspaceFileIndex.isInContent(VirtualFile) reports false for this file, even though it’s in the local file system and it‘s inside a content root.

What’s the reason that local history is turned off and how could I turn it on?

Thank you!

‘Show Local History’ action is disabled if com.intellij.history.integration.ui.actions.ShowHistoryAction::isActionEnabled returns false, which calls isFileVersioned from ShowLocalHistoryUtil.kt, which calls IdeaGateway.areContentChangesVersioned, which checks FileType::isBinary. So maybe the file type assigned by your detector is marked as binary? Local history indeed isn’t supported for binary files.

@nik Thank you, that was helpful.
Unfortunately, I think that it’s still a problem.

At demo-file-type-detector I created a minimal file type detector:

class DemoFileTypeDetector : FileTypeDetector {
    override fun detect(file: VirtualFile, firstBytes: ByteSequence, firstCharsIfText: CharSequence?): FileType? {
        return when {
            file.name.endsWith("no_local_history") -> JsonFileType.INSTANCE
            else -> null
        }
    }
}

IdeaGateway#areContentChangesVersioned(java.lang.String) indeed returns false because FileTypeManager.getInstance().getFileTypeByFileName(fileName).isBinary() returns true.

Unfortunately, isBinary relies on file-type associations and the file extension, but by design none of this is available for files handled by a FileTypeDetector.
AFAIK a detector is only called if there’s no association and not extension.

As a consequence, unless I misunderstand the design, there’s never local history for files detected by a FileTypeDetector.

Could you perhaps suggest a way to enable local history for detected files?
I couldn’t find such a way.

Thank you!

Hmm, you’re right. I don’t see a way to enable local history for detected files. BTW, we have the same problem in the platform: if you open a file without extension with a shebang line, its file type will be detected, but local history won’t be available for it.

To fix this issue, we need to detect the file type by VirtualFile, not by its name in areContentChangesVersioned. However, this may affect performance, so it requires additional considerations. Feel free to create an issue about that in YouTrack.

1 Like

Thank you for your reply.
I’ve created https://youtrack.jetbrains.com/issue/IJPL-177645 on YouTrack.