com.intellij.openapi.fileTypes.FileNameMatcher Java Examples

The following examples show how to use com.intellij.openapi.fileTypes.FileNameMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TemplateDataLanguagePatterns.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static Element writeRemovedMapping(@Nonnull String fileTypeName, @Nonnull FileNameMatcher matcher, boolean specifyTypeName, boolean approved) {
  Element mapping = new Element(ELEMENT_REMOVED_MAPPING);
  if (matcher instanceof ExtensionFileNameMatcher) {
    mapping.setAttribute(AbstractFileType.ATTRIBUTE_EXT, ((ExtensionFileNameMatcher)matcher).getExtension());
  }
  else if (AbstractFileType.writePattern(matcher, mapping)) {
    return null;
  }
  if (approved) {
    mapping.setAttribute(ATTRIBUTE_APPROVED, "true");
  }
  if (specifyTypeName) {
    mapping.setAttribute(ATTRIBUTE_TYPE, fileTypeName);
  }

  return mapping;
}
 
Example #3
Source File: FilePathCompletionContributor.java    From consulo with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 6 votes vote down vote up
boolean removeAssociation(@Nonnull FileNameMatcher matcher, @Nonnull 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;
  }

  return myMatchingMappings.removeIf(assoc -> matcher.equals(assoc.getFirst()));
}
 
Example #5
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
T findAssociatedFileType(@Nonnull FileNameMatcher matcher) {
  if (matcher instanceof ExtensionFileNameMatcher) {
    return findByExtension(((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;
}
 
Example #6
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
static List<RemovedMapping> readRemovedMappings(@Nonnull Element e) {
  List<Element> children = e.getChildren(ELEMENT_REMOVED_MAPPING);
  if (children.isEmpty()) {
    return Collections.emptyList();
  }

  List<RemovedMapping> result = new ArrayList<>();
  for (Element mapping : children) {
    String ext = mapping.getAttributeValue(AbstractFileType.ATTRIBUTE_EXT);
    FileNameMatcher matcher = ext == null ? FileTypeManager.parseFromString(mapping.getAttributeValue(AbstractFileType.ATTRIBUTE_PATTERN)) : new ExtensionFileNameMatcher(ext);
    boolean approved = Boolean.parseBoolean(mapping.getAttributeValue(ATTRIBUTE_APPROVED));
    String fileTypeName = mapping.getAttributeValue(ATTRIBUTE_TYPE);
    if (fileTypeName == null) continue;

    RemovedMapping removedMapping = new RemovedMapping(matcher, fileTypeName, approved);
    result.add(removedMapping);
  }
  return result;
}
 
Example #7
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
boolean isAssociatedWith(@Nonnull T type, @Nonnull 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;
}
 
Example #8
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private FileTypeAssocTable(@Nonnull Map<? extends CharSequence, ? extends T> extensionMappings,
                           @Nonnull Map<? extends CharSequence, ? extends T> exactFileNameMappings,
                           @Nonnull Map<? extends CharSequence, T> exactFileNameAnyCaseMappings,
                           @Nonnull List<? extends Pair<FileNameMatcher, T>> matchingMappings) {
  myExtensionMappings = new THashMap<>(Math.max(10, extensionMappings.size()), 0.5f, CharSequenceHashingStrategy.CASE_INSENSITIVE);
  myExtensionMappings.putAll(extensionMappings);
  myExactFileNameMappings = new THashMap<>(Math.max(10, exactFileNameMappings.size()), 0.5f, CharSequenceHashingStrategy.CASE_SENSITIVE);
  myExactFileNameMappings.putAll(exactFileNameMappings);
  myExactFileNameAnyCaseMappings = new THashMap<>(Math.max(10, exactFileNameAnyCaseMappings.size()), 0.5f, CharSequenceHashingStrategy.CASE_INSENSITIVE);
  myExactFileNameAnyCaseMappings.putAll(exactFileNameAnyCaseMappings);
  myMatchingMappings = new ArrayList<>(matchingMappings);
}
 
Example #9
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
List<RemovedMapping> retrieveUnapprovedMappings() {
  List<RemovedMapping> result = new ArrayList<>();
  for (Iterator<Map.Entry<FileNameMatcher, RemovedMapping>> it = myRemovedMappings.entrySet().iterator(); it.hasNext(); ) {
    Map.Entry<FileNameMatcher, RemovedMapping> next = it.next();
    if (!next.getValue().isApproved()) {
      result.add(next.getValue());
      it.remove();
    }
  }
  return result;
}
 
Example #10
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void addAssociation(@Nonnull FileNameMatcher matcher, @Nonnull 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));
  }
}
 
Example #11
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 5 votes vote down vote up
List<FileNameMatcher> getMappingsForFileType(@Nonnull String name) {
  List<FileNameMatcher> result = new ArrayList<>();
  for (RemovedMapping mapping : myRemovedMappings.values()) {
    if (mapping.myFileTypeName.equals(name)) {
      result.add(mapping.myFileNameMatcher);
    }
  }
  return result;
}
 
Example #12
Source File: ProtostuffPluginController.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #13
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 5 votes vote down vote up
void saveRemovedMappingsForFileType(@Nonnull Element map, @Nonnull String fileTypeName, @Nonnull Set<? extends FileNameMatcher> associations, boolean specifyTypeName) {
  for (FileNameMatcher matcher : associations) {
    Element content = writeRemovedMapping(fileTypeName, matcher, specifyTypeName, isApproved(matcher));
    if (content != null) {
      map.addContent(content);
    }
  }
}
 
Example #14
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
boolean hasAssociationsFor(@Nonnull T fileType) {
  if (myExtensionMappings.containsValue(fileType) || myExactFileNameMappings.containsValue(fileType) || myExactFileNameAnyCaseMappings.containsValue(fileType)) {
    return true;
  }
  for (Pair<FileNameMatcher, T> mapping : myMatchingMappings) {
    if (mapping.getSecond() == fileType) {
      return true;
    }
  }
  return false;
}
 
Example #15
Source File: FileTypeAssocTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
Map<FileNameMatcher, T> getRemovedMappings(@Nonnull FileTypeAssocTable<T> newTable, @Nonnull Collection<? extends T> keys) {
  Map<FileNameMatcher, T> map = new HashMap<>();
  for (T key : keys) {
    List<FileNameMatcher> associations = getAssociations(key);
    associations.removeAll(newTable.getAssociations(key));
    for (FileNameMatcher matcher : associations) {
      map.put(matcher, key);
    }
  }
  return map;
}
 
Example #16
Source File: PluginsAdvertiser.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isMyFeature(String extensionValue, UnknownExtension feature) {
  if (feature.getExtensionKey().equals(FileTypeFactory.FILE_TYPE_FACTORY_EP.getName())) {
    FileNameMatcher matcher = createMatcher(extensionValue);
    return matcher != null && matcher.accept(feature.getValue());
  }
  else {
    return extensionValue.equals(feature.getValue());
  }
}
 
Example #17
Source File: PluginsAdvertiser.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * for correct specification - see hub impl
 */
@Nullable
public static FileNameMatcher createMatcher(@Nonnull String extensionValue) {
  if (extensionValue.length() < 2) {
    return null;
  }

  List<String> values = StringUtil.split(extensionValue, "|");

  String id = values.get(0);
  if (id.length() != 1) {
    return null;
  }


  FileNameMatcherFactory factory = FileNameMatcherFactory.getInstance();
  String value = values.get(1);

  char idChar = id.charAt(0);

  switch (idChar) {
    case '?':
      return factory.createWildcardFileNameMatcher(value);
    case '*':
      return factory.createExtensionFileNameMatcher(value);
    case '!':
      return factory.createExactFileNameMatcher(value, true);
    case 'ยก':
      return factory.createExactFileNameMatcher(value, false);
    default:
      return null;
  }
}
 
Example #18
Source File: TypeAssociationFix.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@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.acceptsCharSequence(psiFile.getName())) {
      manager.removeAssociation(type, matcher);
    }
  }
}
 
Example #19
Source File: BuildFileTypeFactory.java    From intellij with Apache License 2.0 5 votes vote down vote up
@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]));
}
 
Example #20
Source File: ProjectViewFileTypeFactory.java    From intellij with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: BuildSystemProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** Returns the list of file types recognized as build system files. */
default ImmutableList<FileNameMatcher> buildLanguageFileTypeMatchers() {
  ImmutableList.Builder<FileNameMatcher> list = ImmutableList.builder();
  possibleBuildFileNames().forEach(s -> list.add(new ExactFileNameMatcher(s)));
  possibleWorkspaceFileNames().forEach(s -> list.add(new ExactFileNameMatcher(s)));
  list.add(new ExtensionFileNameMatcher("bzl"));
  return list.build();
}
 
Example #22
Source File: ProtostuffPluginController.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
boolean match(String filename) {
    for (FileNameMatcher matcher : matchers) {
        if (matcher.accept(filename)) {
            return true;
        }
    }
    return false;
}
 
Example #23
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
void approveRemoval(@Nonnull String fileTypeName, @Nonnull FileNameMatcher matcher) {
  myRemovedMappings.put(matcher, new RemovedMapping(matcher, fileTypeName, true));
}
 
Example #24
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
void removeMatching(@Nonnull BiPredicate<? super FileNameMatcher, ? super String> predicate) {
  myRemovedMappings.entrySet().removeIf(next -> predicate.test(next.getValue().myFileNameMatcher, next.getValue().myFileTypeName));
}
 
Example #25
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean isApproved(@Nonnull FileNameMatcher matcher) {
  RemovedMapping mapping = myRemovedMappings.get(matcher);
  return mapping != null && mapping.isApproved();
}
 
Example #26
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
boolean hasRemovedMapping(@Nonnull FileNameMatcher matcher) {
  return myRemovedMappings.containsKey(matcher);
}
 
Example #27
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void add(@Nonnull FileNameMatcher matcher, @Nonnull String fileTypeName, boolean approved) {
  myRemovedMappings.put(matcher, new RemovedMapping(matcher, fileTypeName, approved));
}
 
Example #28
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
public FileNameMatcher getFileNameMatcher() {
  return myFileNameMatcher;
}
 
Example #29
Source File: RemovedMappingTracker.java    From consulo with Apache License 2.0 4 votes vote down vote up
private RemovedMapping(@Nonnull FileNameMatcher matcher, @Nonnull String name, boolean approved) {
  myFileNameMatcher = matcher;
  myFileTypeName = name;
  myApproved = approved;
}
 
Example #30
Source File: FileTypeBean.java    From consulo with Apache License 2.0 4 votes vote down vote up
public List<FileNameMatcher> getMatchers() {
  return new ArrayList<>(myMatchers);
}