// Path: src/io/github/jhsx/jet/JetLanguage.java
// public class JetLanguage extends Language implements TemplateLanguage {
//     public static final Language INSTANCE = new JetLanguage();
// 
//     private JetLanguage() {
//         super("JetTemplateLanguage");
//     }
// 
//     public static LanguageFileType getDefaultTemplateLang() {
//         return StdFileTypes.HTML;
//     }
// 
//     @NotNull
//     @Override
//     public String getDisplayName() {
//         return "Jet template engine";
//     }
// 
//     @Nullable
//     @Override
//     public LanguageFileType getAssociatedFileType() {
//         return JetFileType.INSTANCE;
//     }
// 
//     @Override
//     public boolean isCaseSensitive() {
//         return true;
//     }
// }


import javax.swing.*;
import java.nio.charset.Charset;
import com.intellij.icons.AllIcons;
import com.intellij.lang.Language;
import com.intellij.openapi.fileTypes.CharsetUtil;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.TemplateLanguageFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.templateLanguages.TemplateDataLanguageMappings;
import io.github.jhsx.jet.JetLanguage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
    @Override
    public String getCharset(@NotNull VirtualFile file, @NotNull byte[] content) {
        return CharsetToolkit.UTF8;
    }


    public Charset extractCharsetFromFileContent(@Nullable final Project project,
                                                 @Nullable final VirtualFile file,
                                                 @NotNull final CharSequence content) {
        LanguageFileType associatedFileType = getAssociatedFileType(file, project);

        if (associatedFileType == null) {
            return null;
        }

        return CharsetUtil.extractCharsetFromFileContent(project, file, associatedFileType, content);
    }

    private static LanguageFileType getAssociatedFileType(VirtualFile file, Project project) {
        if (project == null) {
            return null;
        }
        Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(file);

        LanguageFileType associatedFileType = null;
        if (language != null) {
            associatedFileType = language.getAssociatedFileType();
        }

        if (language == null || associatedFileType == null) {
// Path: src/io/github/jhsx/jet/JetLanguage.java
// public class JetLanguage extends Language implements TemplateLanguage {
//     public static final Language INSTANCE = new JetLanguage();
// 
//     private JetLanguage() {
//         super("JetTemplateLanguage");
//     }
// 
//     public static LanguageFileType getDefaultTemplateLang() {
//         return StdFileTypes.HTML;
//     }
// 
//     @NotNull
//     @Override
//     public String getDisplayName() {
//         return "Jet template engine";
//     }
// 
//     @Nullable
//     @Override
//     public LanguageFileType getAssociatedFileType() {
//         return JetFileType.INSTANCE;
//     }
// 
//     @Override
//     public boolean isCaseSensitive() {
//         return true;
//     }
// }


// Path: src/io/github/jhsx/jet/lang/file/JetFileType.java
import javax.swing.*;
import java.nio.charset.Charset;
import com.intellij.icons.AllIcons;
import com.intellij.lang.Language;
import com.intellij.openapi.fileTypes.CharsetUtil;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.TemplateLanguageFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.templateLanguages.TemplateDataLanguageMappings;
import io.github.jhsx.jet.JetLanguage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

    @Override
    public String getCharset(@NotNull VirtualFile file, @NotNull byte[] content) {
        return CharsetToolkit.UTF8;
    }


    public Charset extractCharsetFromFileContent(@Nullable final Project project,
                                                 @Nullable final VirtualFile file,
                                                 @NotNull final CharSequence content) {
        LanguageFileType associatedFileType = getAssociatedFileType(file, project);

        if (associatedFileType == null) {
            return null;
        }

        return CharsetUtil.extractCharsetFromFileContent(project, file, associatedFileType, content);
    }

    private static LanguageFileType getAssociatedFileType(VirtualFile file, Project project) {
        if (project == null) {
            return null;
        }
        Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(file);

        LanguageFileType associatedFileType = null;
        if (language != null) {
            associatedFileType = language.getAssociatedFileType();
        }

        if (language == null || associatedFileType == null) {
            associatedFileType = JetLanguage.getDefaultTemplateLang();