boolean removeAllAssociations(@NotNull T type) { boolean changed = removeAssociationsFromMap(myExtensionMappings, type, false); changed = removeAssociationsFromMap(myExactFileNameAnyCaseMappings, type, changed); changed = removeAssociationsFromMap(myExactFileNameMappings, type, changed); List<Pair<FileNameMatcher, T>> copy = new ArrayList<Pair<FileNameMatcher, T>>(myMatchingMappings); for (Pair<FileNameMatcher, T> assoc : copy) { if (assoc.getSecond() == type) { myMatchingMappings.remove(assoc); changed = true; } } return changed; }
@Nullable public 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; }
private static boolean filenameMatchesPrefixOrType(final String fileName, final String prefix, final FileType[] suitableFileTypes, final int invocationCount) { final boolean prefixMatched = prefix.length() == 0 || StringUtil.startsWithIgnoreCase(fileName, prefix); if (prefixMatched && (suitableFileTypes.length == 0 || invocationCount > 2)) return true; if (prefixMatched) { final String extension = FileUtilRt.getExtension(fileName); if (extension.length() == 0) return false; for (final FileType fileType : suitableFileTypes) { for (final FileNameMatcher matcher : FileTypeManager.getInstance().getAssociations(fileType)) { if (FileNameMatcherEx.acceptsCharSequence(matcher, fileName)) return true; } } } return false; }
@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, new Function<FileNameMatcher, String>() { @Override public String fun(FileNameMatcher fileNameMatcher) { return fileNameMatcher.getPresentableString(); } }, SEPARATOR)); child.setAttribute("lang", language.getID()); } } return state; }
@Nullable private static PsiFile findPyFileInDir(PsiDirectory dir, String referencedName) { PsiFile file = dir.findFile(referencedName + PyNames.DOT_PY); if (file == null) { final List<FileNameMatcher> associations = FileTypeManager.getInstance().getAssociations(PythonFileType.INSTANCE); for (FileNameMatcher association : associations) { if (association instanceof ExtensionFileNameMatcher) { file = dir.findFile(referencedName + "." + ((ExtensionFileNameMatcher)association).getExtension()); if (file != null) break; } } } if (file != null && FileUtil.getNameWithoutExtension(file.getName()).equals(referencedName)) { return file; } return null; }
private FileTypeAssocTable(Map<String, T> extensionMappings, Map<String, T> exactFileNameMappings, Map<String, T> exactFileNameAnyCaseMappings, List<Pair<FileNameMatcher, T>> matchingMappings) { myExtensionMappings = new THashMap<String, T>(extensionMappings); myExactFileNameMappings = new THashMap<String, T>(exactFileNameMappings); myExactFileNameAnyCaseMappings = new THashMap<String, T>(exactFileNameAnyCaseMappings, CaseInsensitiveStringHashingStrategy.INSTANCE) { @Override public T remove(Object key) { T removed = super.remove(key); myHasAnyCaseExactMappings = size() > 0; return removed; } @Override public T put(String key, T value) { T result = super.put(key, value); myHasAnyCaseExactMappings = true; return result; } }; myMatchingMappings = new ArrayList<Pair<FileNameMatcher, T>>(matchingMappings); }
public void addAssociation(FileNameMatcher matcher, T type) { if (matcher instanceof ExtensionFileNameMatcher) { myExtensionMappings.put(((ExtensionFileNameMatcher)matcher).getExtension(), type); } else if (matcher instanceof ExactFileNameMatcher) { final ExactFileNameMatcher exactFileNameMatcher = (ExactFileNameMatcher)matcher; if (exactFileNameMatcher.isIgnoreCase()) { myExactFileNameAnyCaseMappings.put(exactFileNameMatcher.getFileName(), type); } else { myExactFileNameMappings.put(exactFileNameMatcher.getFileName(), type); } } else { myMatchingMappings.add(new Pair<FileNameMatcher, T>(matcher, type)); } }
public boolean removeAllAssociations(T type) { boolean changed = removeAssociationsFromMap(myExtensionMappings, type, false); changed = removeAssociationsFromMap(myExactFileNameAnyCaseMappings, type, changed); changed = removeAssociationsFromMap(myExactFileNameMappings, type, changed); List<Pair<FileNameMatcher, T>> copy = new ArrayList<Pair<FileNameMatcher, T>>(myMatchingMappings); for (Pair<FileNameMatcher, T> assoc : copy) { if (assoc.getSecond() == type) { myMatchingMappings.remove(assoc); changed = true; } } return changed; }
@Nullable public T findAssociatedFileType(@NotNull @NonNls String fileName) { T t = myExactFileNameMappings.get(fileName); if (t != null) return t; if (myHasAnyCaseExactMappings) { // even hash lookup with case insensitive hasher is costly for isIgnored checks during compile t = myExactFileNameAnyCaseMappings.get(fileName); if (t != null) return t; } //noinspection ForLoopReplaceableByForEach for (int i = 0, n = myMatchingMappings.size(); i < n; i++) { final Pair<FileNameMatcher, T> mapping = myMatchingMappings.get(i); if (mapping.getFirst().accept(fileName)) return mapping.getSecond(); } return myExtensionMappings.get(StringUtil.toLowerCase(FileUtilRt.getExtension(fileName))); }
@Nullable public T findAssociatedFileType(final FileNameMatcher matcher) { if (matcher instanceof ExtensionFileNameMatcher) { return myExtensionMappings.get(((ExtensionFileNameMatcher)matcher).getExtension()); } if (matcher instanceof ExactFileNameMatcher) { final ExactFileNameMatcher exactFileNameMatcher = (ExactFileNameMatcher)matcher; if (exactFileNameMatcher.isIgnoreCase()) { return myExactFileNameAnyCaseMappings.get(exactFileNameMatcher.getFileName()); } else { return myExactFileNameMappings.get(exactFileNameMatcher.getFileName()); } } for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) { if (matcher.equals(mapping.getFirst())) return mapping.getSecond(); } return null; }
private static boolean filenameMatchesPrefixOrType(final String fileName, final String prefix, final FileType[] suitableFileTypes, final int invocationCount) { final boolean prefixMatched = prefix.length() == 0 || StringUtil.startsWithIgnoreCase(fileName, prefix); if (prefixMatched && (suitableFileTypes.length == 0 || invocationCount > 2)) return true; if (prefixMatched) { final String extension = FileUtilRt.getExtension(fileName); if (extension.length() == 0) return false; for (final FileType fileType : suitableFileTypes) { final List<FileNameMatcher> matchers = FileTypeManager.getInstance().getAssociations(fileType); for (final FileNameMatcher matcher : matchers) { if (matcher.accept(fileName)) return true; } } } return false; }
private FileTypeAssocTable(Map<CharSequence, T> extensionMappings, Map<CharSequence, T> exactFileNameMappings, Map<CharSequence, T> exactFileNameAnyCaseMappings, List<Pair<FileNameMatcher, T>> matchingMappings) { myExtensionMappings = new THashMap<CharSequence, T>(extensionMappings, CharSequenceHashingStrategy.CASE_INSENSITIVE); myExactFileNameMappings = new THashMap<CharSequence, T>(exactFileNameMappings, CharSequenceHashingStrategy.CASE_SENSITIVE); myExactFileNameAnyCaseMappings = new THashMap<CharSequence, T>(exactFileNameAnyCaseMappings, CharSequenceHashingStrategy.CASE_INSENSITIVE) { @Override public T remove(Object key) { T removed = super.remove(key); myHasAnyCaseExactMappings = size() > 0; return removed; } @Override public T put(CharSequence key, T value) { T result = super.put(key, value); myHasAnyCaseExactMappings = true; return result; } }; myMatchingMappings = new ArrayList<Pair<FileNameMatcher, T>>(matchingMappings); }
@Nullable public T findAssociatedFileType(@Nonnull @NonNls CharSequence fileName) { T t = myExactFileNameMappings.get(fileName); if (t != null) return t; if (myHasAnyCaseExactMappings) { // even hash lookup with case insensitive hasher is costly for isIgnored checks during compile t = myExactFileNameAnyCaseMappings.get(fileName); if (t != null) return t; } //noinspection ForLoopReplaceableByForEach for (int i = 0, n = myMatchingMappings.size(); i < n; i++) { final Pair<FileNameMatcher, T> mapping = myMatchingMappings.get(i); if (mapping.getFirst().accept(fileName)) return mapping.getSecond(); } return myExtensionMappings.get(FileUtilRt.getExtension(fileName)); }
private static boolean filenameMatchesPrefixOrType(final String fileName, final String prefix, final FileType[] suitableFileTypes, final int invocationCount) { final boolean prefixMatched = prefix.length() == 0 || StringUtil.startsWithIgnoreCase(fileName, prefix); if (prefixMatched && (suitableFileTypes.length == 0 || invocationCount > 2)) return true; if (prefixMatched) { final String extension = FileUtilRt.getExtension(fileName); if (extension.length() == 0) return false; for (final FileType fileType : suitableFileTypes) { for (final FileNameMatcher matcher : FileTypeManager.getInstance().getAssociations(fileType)) { if (matcher.accept(fileName)) return true; } } } return false; }
@Override public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) { fileTypeConsumer.consume(EmojiResourceFileType.INSTANCE, new FileNameMatcher() { @Override public boolean accept(@NotNull String s) { return s.equals(".emojirc"); } @NotNull @Override public String getPresentableString() { return ".emojirc"; } }); }
public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) { super.createFileTypes(fileTypeConsumer); fileTypeConsumer.consume(CloudSlangFileType.INSTANCE, new FileNameMatcher() { @Override public boolean accept(@NonNls @NotNull String s) { return CloudSlangFileUtils.isCloudSlangFile(s); } @NotNull @Override public String getPresentableString() { return "sl;sl.yaml;sl.yml;prop.sl"; } }); }
@NotNull private List<FileNameMatcher> parse(@Nullable String semicolonDelimited) { if (semicolonDelimited == null) { return Collections.emptyList(); } StringTokenizer tokenizer = new StringTokenizer(semicolonDelimited, FileTypeConsumer.EXTENSION_DELIMITER, false); ArrayList<FileNameMatcher> list = new ArrayList<>(); while (tokenizer.hasMoreTokens()) { list.add(new ExtensionFileNameMatcher(tokenizer.nextToken().trim())); } return list; }
boolean match(String filename) { for (FileNameMatcher matcher : matchers) { if (matcher.accept(filename)) { return true; } } return false; }
@NotNull public FileNameMatcher createMatcher(@NotNull String pattern) { if (pattern.startsWith("*.") && pattern.indexOf('*', 2) < 0 && pattern.indexOf('.', 2) < 0 && pattern.indexOf('?', 2) < 0) { return new ExtensionFileNameMatcher(pattern.substring(2).toLowerCase()); } if (pattern.contains("*") || pattern.contains("?")) { return new WildcardFileNameMatcher(pattern); } return new ExactFileNameMatcher(pattern); }
private FileTypeAssocTable(@NotNull Map<CharSequence, T> extensionMappings, @NotNull Map<CharSequence, T> exactFileNameMappings, @NotNull Map<CharSequence, T> exactFileNameAnyCaseMappings, @NotNull List<Pair<FileNameMatcher, T>> matchingMappings) { myExtensionMappings = new THashMap<CharSequence, T>(Math.max(10, extensionMappings.size()), 0.5f, CharSequenceHashingStrategy.CASE_INSENSITIVE); myExtensionMappings.putAll(extensionMappings); myExactFileNameMappings = new THashMap<CharSequence, T>(Math.max(10, exactFileNameMappings.size()), 0.5f, CharSequenceHashingStrategy.CASE_SENSITIVE); myExactFileNameMappings.putAll(exactFileNameMappings); myExactFileNameAnyCaseMappings = new THashMap<CharSequence, T>(Math.max(10, exactFileNameAnyCaseMappings.size()), 0.5f, CharSequenceHashingStrategy.CASE_INSENSITIVE); myExactFileNameAnyCaseMappings.putAll(exactFileNameAnyCaseMappings); myMatchingMappings = new ArrayList<Pair<FileNameMatcher, T>>(matchingMappings); }
public boolean isAssociatedWith(@NotNull T type, @NotNull FileNameMatcher matcher) { if (matcher instanceof ExtensionFileNameMatcher || matcher instanceof ExactFileNameMatcher) { return findAssociatedFileType(matcher) == type; } for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) { if (matcher.equals(mapping.getFirst()) && type == mapping.getSecond()) return true; } return false; }
public void addAssociation(FileNameMatcher matcher, T type) { if (matcher instanceof ExtensionFileNameMatcher) { myExtensionMappings.put(((ExtensionFileNameMatcher)matcher).getExtension(), type); } else if (matcher instanceof ExactFileNameMatcher) { final ExactFileNameMatcher exactFileNameMatcher = (ExactFileNameMatcher)matcher; Map<CharSequence, T> mapToUse = exactFileNameMatcher.isIgnoreCase() ? myExactFileNameAnyCaseMappings : myExactFileNameMappings; mapToUse.put(exactFileNameMatcher.getFileName(), type); } else { myMatchingMappings.add(Pair.create(matcher, type)); } }
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<Pair<FileNameMatcher, T>>(myMatchingMappings); for (Pair<FileNameMatcher, T> assoc : copy) { if (matcher.equals(assoc.getFirst())) { myMatchingMappings.remove(assoc); return true; } } return false; }
public boolean hasAssociationsFor(@NotNull T fileType) { if (myExtensionMappings.values().contains(fileType) || myExactFileNameMappings.values().contains(fileType) || myExactFileNameAnyCaseMappings.values().contains(fileType)) { return true; } for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) { if (mapping.getSecond() == fileType) { return true; } } return false; }
public static FileNameMatcher[] fileNameMatchers() { return new FileNameMatcher[] { new ExtensionFileNameMatcher(CODE_EXTENSION), new ExtensionFileNameMatcher(FS_CODE_EXTENSION), new ExtensionFileNameMatcher(HEADER_EXTENSION), }; }
@Override public void createFileTypes(@NotNull final FileTypeConsumer consumer) { FileNameMatcher[] matchers = ProjectViewStorageManager.VALID_EXTENSIONS .stream() .map(ExtensionFileNameMatcher::new) .toArray(ExtensionFileNameMatcher[]::new); consumer.consume(ProjectViewFileType.INSTANCE, matchers); }
@Override public void createFileTypes(@NotNull final FileTypeConsumer consumer) { ImmutableList<FileNameMatcher> fileNameMatchers = ImmutableList.<FileNameMatcher>builder() .addAll(BuildSystemProvider.defaultBuildSystem().buildLanguageFileTypeMatchers()) .add() .build(); consumer.consume(BuildFileType.INSTANCE, fileNameMatchers.toArray(new FileNameMatcher[0])); }
/** * Consumes the file type with only the specified extensions. * * @param fileType The file type. * @param extensions The file extensions, without a leading '.'. */ public void consume(final FileType fileType, @NonNls final String... extensions) { Preconditions.checkNotNull(fileType); Preconditions.checkNotNull(extensions); final FileNameMatcher[] matchers = new FileNameMatcher[extensions.length]; for (int i = 0; i < extensions.length; i++) { matchers[i] = new ExtensionFileNameMatcher(extensions[i]); } consume(fileType, matchers); }
/** * Consumes the file type with only the specified file name matchers. * * @param fileType The file type. * @param matchers The file name matchers. */ public void consume(final FileType fileType, final FileNameMatcher... matchers) { Preconditions.checkNotNull(fileType); Preconditions.checkNotNull(matchers); this.consumer.consume(fileType, matchers); }
/** * Registers the given file type with IntelliJ IDEA. * * @param fileType The file type to register. */ public static void register(final IMetaborgFileType fileType) { FileTypeManagerEx.getInstanceEx().registerFileType(fileType); // Associate the file type with its file extensions. final FileTypeManager fileTypeManager = FileTypeManager.getInstance(); for (final String ext : fileType.getExtensions()) { final FileNameMatcher matcher = new ExtensionFileNameMatcher(ext); fileTypeManager.associate(fileType, matcher); } }
@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); } } }
public boolean isAssociatedWith(T type, FileNameMatcher matcher) { if (matcher instanceof ExtensionFileNameMatcher || matcher instanceof ExactFileNameMatcher) { return findAssociatedFileType(matcher) == type; } for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) { if (matcher.equals(mapping.getFirst()) && type == mapping.getSecond()) return true; } return false; }
public boolean removeAssociation(FileNameMatcher matcher, 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; final Map<String, T> mapToUse; String fileName = exactFileNameMatcher.getFileName(); if (exactFileNameMatcher.isIgnoreCase()) { mapToUse = myExactFileNameAnyCaseMappings; } else { mapToUse = myExactFileNameMappings; } if(mapToUse.get(fileName) == type) { mapToUse.remove(fileName); return true; } return false; } List<Pair<FileNameMatcher, T>> copy = new ArrayList<Pair<FileNameMatcher, T>>(myMatchingMappings); for (Pair<FileNameMatcher, T> assoc : copy) { if (matcher.equals(assoc.getFirst())) { myMatchingMappings.remove(assoc); return true; } } return false; }
public boolean hasAssociationsFor(final T fileType) { if (myExtensionMappings.values().contains(fileType)) return true; if (myExactFileNameMappings.values().contains(fileType)) return true; if (myExactFileNameAnyCaseMappings.values().contains(fileType)) return true; for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) { if (mapping.getSecond() == fileType) return true; } return false; }