Example usage for com.intellij.openapi.fileTypes FileTypeManager associateExtension

List of usage examples for com.intellij.openapi.fileTypes FileTypeManager associateExtension

Introduction

In this page you can find the example usage for com.intellij.openapi.fileTypes FileTypeManager associateExtension.

Prototype

public final void associateExtension(@NotNull FileType type, @NotNull @NonNls String extension) 

Source Link

Document

Adds an extension to the list of extensions associated with a file type.

Usage

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected PsiFile configureByText(final FileType fileType, @NonNls final String text,
        @Nullable String _extension) throws Exception {
    final String extension = _extension == null ? fileType.getDefaultExtension() : _extension;

    File dir = createTempDirectory();
    final File tempFile = FileUtil.createTempFile(dir, "aaa", "." + extension, true);
    final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
    if (fileTypeManager.getFileTypeByExtension(extension) != fileType) {
        new WriteCommandAction(getProject()) {
            @Override/*w w  w  .j  a  v a 2s . c om*/
            protected void run(Result result) throws Exception {
                fileTypeManager.associateExtension(fileType, extension);
            }
        }.execute();
    }
    final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempFile);
    assert vFile != null;
    VfsUtil.saveText(vFile, text);

    final VirtualFile vdir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(dir);

    PsiTestUtil.addSourceRoot(myModule, vdir);

    configureByExistingFile(vFile);

    assertEquals(fileType, myFile.getVirtualFile().getFileType());
    return myFile;
}

From source file:com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.java

License:Apache License

@Override
public PsiFile configureByText(final FileType fileType, @NonNls final String text) {
    assertInitialized();//from   w w w.j av  a  2 s  .c o m
    final String extension = fileType.getDefaultExtension();
    final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
    if (fileTypeManager.getFileTypeByExtension(extension) != fileType) {
        new WriteCommandAction(getProject()) {
            @Override
            protected void run(Result result) throws Exception {
                fileTypeManager.associateExtension(fileType, extension);
            }
        }.execute();
    }
    final String fileName = "aaa." + extension;
    return configureByText(fileName, text);
}