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

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

Introduction

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

Prototype

public abstract void removeAssociation(@NotNull FileType type, @NotNull FileNameMatcher matcher);

Source Link

Usage

From source file:com.facebook.buck.intellij.ideabuck.file.BuckFileUtil.java

License:Apache License

public static void setBuckFileType() {
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
        @Override//  w  w w.  j av  a2 s .  co  m
        public void run() {
            FileTypeManager fileTypeManager = FileTypeManagerImpl.getInstance();

            FileType fileType = fileTypeManager
                    .getFileTypeByFileName(BuckFileType.INSTANCE.getDefaultExtension());

            // Remove all FileType associations for BUCK files that are not BuckFileType
            while (!(fileType instanceof BuckFileType || fileType instanceof UnknownFileType)) {
                List<FileNameMatcher> fileNameMatchers = fileTypeManager.getAssociations(fileType);

                for (FileNameMatcher fileNameMatcher : fileNameMatchers) {
                    if (fileNameMatcher.accept(BuckFileType.INSTANCE.getDefaultExtension())) {
                        fileTypeManager.removeAssociation(fileType, fileNameMatcher);
                    }
                }

                fileType = fileTypeManager.getFileTypeByFileName(BuckFileType.INSTANCE.getDefaultExtension());
            }

            // Use a simple BinaryFileStubBuilder, that doesn't offer stubbing
            BinaryFileStubBuilders.INSTANCE.addExplicitExtension(fileType, new BinaryFileStubBuilder() {
                @Override
                public boolean acceptsFile(VirtualFile virtualFile) {
                    return false;
                }

                @Nullable
                @Override
                public Stub buildStubTree(FileContent fileContent) {
                    return null;
                }

                @Override
                public int getStubVersion() {
                    return 0;
                }
            });
        }
    });
}

From source file:com.twitter.intellij.pants.quickfix.TypeAssociationFix.java

License:Apache License

@Override
public void invoke(@NotNull final Project project, Editor editor, PsiFile psiFile)
        throws IncorrectOperationException {
    FileTypeManager manager = FileTypeManager.getInstance();
    FileType type = manager.getFileTypeByFileName(psiFile.getName());
    // Remove the BUILD file matcher from the wrong type then add it to PythonFileType
    for (FileNameMatcher matcher : manager.getAssociations(type)) {
        if (matcher.accept(psiFile.getName())) {
            manager.removeAssociation(type, matcher);
        }/* w  w w.jav  a  2s.  c om*/
    }
}