Search in sources :

Example 1 with FileNameMatcher

use of com.intellij.openapi.fileTypes.FileNameMatcher in project intellij-community by JetBrains.

the class Utils method buildPattern.

@NotNull
private static String buildPattern(FileType fileType) {
    final StringBuilder result = new StringBuilder();
    final List<FileNameMatcher> associations = FileTypeManager.getInstance().getAssociations(fileType);
    for (FileNameMatcher matcher : associations) {
        if (result.length() != 0)
            result.append(",");
        result.append(matcher.getPresentableString());
    }
    if (associations.size() > 1) {
        result.insert(0, "{").append("}");
    }
    return result.toString();
}
Also used : FileNameMatcher(com.intellij.openapi.fileTypes.FileNameMatcher) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with FileNameMatcher

use of com.intellij.openapi.fileTypes.FileNameMatcher in project intellij-community by JetBrains.

the class FileTypeAssocTable method findAssociatedFileType.

@Nullable
T findAssociatedFileType(@NotNull FileNameMatcher matcher) {
    if (matcher instanceof ExtensionFileNameMatcher) {
        return myExtensionMappings.get(((ExtensionFileNameMatcher) matcher).getExtension());
    }
    if (matcher instanceof ExactFileNameMatcher) {
        final ExactFileNameMatcher exactFileNameMatcher = (ExactFileNameMatcher) matcher;
        Map<CharSequence, T> mapToUse = exactFileNameMatcher.isIgnoreCase() ? myExactFileNameAnyCaseMappings : myExactFileNameMappings;
        return mapToUse.get(exactFileNameMatcher.getFileName());
    }
    for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) {
        if (matcher.equals(mapping.getFirst()))
            return mapping.getSecond();
    }
    return null;
}
Also used : ExactFileNameMatcher(com.intellij.openapi.fileTypes.ExactFileNameMatcher) ExtensionFileNameMatcher(com.intellij.openapi.fileTypes.ExtensionFileNameMatcher) FileNameMatcher(com.intellij.openapi.fileTypes.FileNameMatcher) ExtensionFileNameMatcher(com.intellij.openapi.fileTypes.ExtensionFileNameMatcher) ExactFileNameMatcher(com.intellij.openapi.fileTypes.ExactFileNameMatcher) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with FileNameMatcher

use of com.intellij.openapi.fileTypes.FileNameMatcher in project intellij-community by JetBrains.

the class FileTypeAssocTable method removeAssociation.

boolean removeAssociation(@NotNull FileNameMatcher matcher, @NotNull T type) {
    if (matcher instanceof ExtensionFileNameMatcher) {
        String extension = ((ExtensionFileNameMatcher) matcher).getExtension();
        if (myExtensionMappings.get(extension) == type) {
            myExtensionMappings.remove(extension);
            return true;
        }
        return false;
    }
    if (matcher instanceof ExactFileNameMatcher) {
        final ExactFileNameMatcher exactFileNameMatcher = (ExactFileNameMatcher) matcher;
        String fileName = exactFileNameMatcher.getFileName();
        final Map<CharSequence, T> mapToUse = exactFileNameMatcher.isIgnoreCase() ? myExactFileNameAnyCaseMappings : myExactFileNameMappings;
        if (mapToUse.get(fileName) == type) {
            mapToUse.remove(fileName);
            return true;
        }
        return false;
    }
    List<Pair<FileNameMatcher, T>> copy = new ArrayList<>(myMatchingMappings);
    for (Pair<FileNameMatcher, T> assoc : copy) {
        if (matcher.equals(assoc.getFirst())) {
            myMatchingMappings.remove(assoc);
            return true;
        }
    }
    return false;
}
Also used : ExactFileNameMatcher(com.intellij.openapi.fileTypes.ExactFileNameMatcher) ExtensionFileNameMatcher(com.intellij.openapi.fileTypes.ExtensionFileNameMatcher) FileNameMatcher(com.intellij.openapi.fileTypes.FileNameMatcher) ExtensionFileNameMatcher(com.intellij.openapi.fileTypes.ExtensionFileNameMatcher) ExactFileNameMatcher(com.intellij.openapi.fileTypes.ExactFileNameMatcher) Pair(com.intellij.openapi.util.Pair)

Example 4 with FileNameMatcher

use of com.intellij.openapi.fileTypes.FileNameMatcher in project intellij-community by JetBrains.

the class TemplateDataLanguagePatterns method getState.

@Override
public Element getState() {
    Element state = new Element("x");
    for (final Language language : TemplateDataLanguageMappings.getTemplateableLanguages()) {
        final List<FileNameMatcher> matchers = myAssocTable.getAssociations(language);
        if (!matchers.isEmpty()) {
            final Element child = new Element("pattern");
            state.addContent(child);
            child.setAttribute("value", StringUtil.join(matchers, fileNameMatcher -> fileNameMatcher.getPresentableString(), SEPARATOR));
            child.setAttribute("lang", language.getID());
        }
    }
    return state;
}
Also used : Language(com.intellij.lang.Language) Storage(com.intellij.openapi.components.Storage) FileTypeManager(com.intellij.openapi.fileTypes.FileTypeManager) StringUtil(com.intellij.openapi.util.text.StringUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) NonNls(org.jetbrains.annotations.NonNls) THashMap(gnu.trove.THashMap) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) FileNameMatcher(com.intellij.openapi.fileTypes.FileNameMatcher) Function(com.intellij.util.Function) State(com.intellij.openapi.components.State) FileTypeAssocTable(com.intellij.openapi.fileTypes.impl.FileTypeAssocTable) Element(org.jdom.Element) Language(com.intellij.lang.Language) Element(org.jdom.Element) FileNameMatcher(com.intellij.openapi.fileTypes.FileNameMatcher)

Example 5 with FileNameMatcher

use of com.intellij.openapi.fileTypes.FileNameMatcher in project intellij-community by JetBrains.

the class ApproveRemovedMappingsActivity method runActivity.

@Override
public void runActivity(@NotNull final Project project) {
    final Map<FileNameMatcher, Pair<FileType, Boolean>> map = ((FileTypeManagerImpl) FileTypeManager.getInstance()).getRemovedMappings();
    if (!map.isEmpty()) {
        UIUtil.invokeAndWaitIfNeeded(new Runnable() {

            @Override
            public void run() {
                for (Iterator<Map.Entry<FileNameMatcher, Pair<FileType, Boolean>>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
                    Map.Entry<FileNameMatcher, Pair<FileType, Boolean>> entry = iterator.next();
                    if (entry.getValue().getSecond()) {
                        continue;
                    }
                    final FileNameMatcher matcher = entry.getKey();
                    final FileType fileType = entry.getValue().getFirst();
                    Notification notification = new Notification("File type recognized", "File type recognized", "File extension " + matcher.getPresentableString() + " was reassigned to " + fileType.getName() + " <a href='revert'>Revert</a>", NotificationType.WARNING, new NotificationListener.Adapter() {

                        @Override
                        protected void hyperlinkActivated(@NotNull Notification notification, @NotNull HyperlinkEvent e) {
                            ApplicationManager.getApplication().runWriteAction(() -> {
                                FileTypeManager.getInstance().associate(PlainTextFileType.INSTANCE, matcher);
                                map.put(matcher, Pair.create(fileType, true));
                            });
                            notification.expire();
                        }
                    });
                    Notifications.Bus.notify(notification, project);
                    ApplicationManager.getApplication().runWriteAction(() -> FileTypeManager.getInstance().associate(fileType, matcher));
                    iterator.remove();
                }
            }
        });
    }
}
Also used : HyperlinkEvent(javax.swing.event.HyperlinkEvent) FileNameMatcher(com.intellij.openapi.fileTypes.FileNameMatcher) NotNull(org.jetbrains.annotations.NotNull) Notification(com.intellij.notification.Notification) PlainTextFileType(com.intellij.openapi.fileTypes.PlainTextFileType) FileType(com.intellij.openapi.fileTypes.FileType) Iterator(java.util.Iterator) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair)

Aggregations

FileNameMatcher (com.intellij.openapi.fileTypes.FileNameMatcher)5 ExactFileNameMatcher (com.intellij.openapi.fileTypes.ExactFileNameMatcher)2 ExtensionFileNameMatcher (com.intellij.openapi.fileTypes.ExtensionFileNameMatcher)2 Pair (com.intellij.openapi.util.Pair)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Language (com.intellij.lang.Language)1 Notification (com.intellij.notification.Notification)1 PersistentStateComponent (com.intellij.openapi.components.PersistentStateComponent)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 State (com.intellij.openapi.components.State)1 Storage (com.intellij.openapi.components.Storage)1 FileType (com.intellij.openapi.fileTypes.FileType)1 FileTypeManager (com.intellij.openapi.fileTypes.FileTypeManager)1 PlainTextFileType (com.intellij.openapi.fileTypes.PlainTextFileType)1 FileTypeAssocTable (com.intellij.openapi.fileTypes.impl.FileTypeAssocTable)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Function (com.intellij.util.Function)1 THashMap (gnu.trove.THashMap)1