Java Code Examples for com.intellij.openapi.fileTypes.FileType#getName()

The following examples show how to use com.intellij.openapi.fileTypes.FileType#getName() . 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: LoadTextUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static CharSequence loadText(@Nonnull final VirtualFile file) {
  FileType type = file.getFileType();
  if (type.isBinary()) {
    final BinaryFileDecompiler decompiler = BinaryFileTypeDecompilers.INSTANCE.forFileType(type);
    if (decompiler != null) {
      CharSequence text = decompiler.decompile(file);
      try {
        StringUtil.assertValidSeparators(text);
      }
      catch (AssertionError e) {
        LOG.error(e);
      }
      return text;
    }

    throw new IllegalArgumentException("Attempt to load text for binary file which doesn't have a decompiler plugged in: " + file.getPresentableUrl() + ". File type: " + type.getName());
  }
  return loadText(file, UNLIMITED);
}
 
Example 2
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Loads content of given virtual file. If limit is {@value UNLIMITED} then full CharSequence will be returned. Else CharSequence
 * will be truncated by limit if it has bigger length.
 *
 * @param file  Virtual file for content loading
 * @param limit Maximum characters count or {@value UNLIMITED}
 * @return Full or truncated CharSequence with file content
 * @throws IllegalArgumentException for binary files
 */
@Nonnull
public static CharSequence loadText(@Nonnull final VirtualFile file, int limit) {
  FileType type = file.getFileType();
  if (type.isBinary()) throw new IllegalArgumentException("Attempt to load truncated text for binary file: " + file.getPresentableUrl() + ". File type: " + type.getName());

  if (file instanceof LightVirtualFile) {
    return limitCharSequence(((LightVirtualFile)file).getContent(), limit);
  }

  if (file.isDirectory()) {
    throw new AssertionError("'" + file.getPresentableUrl() + "' is a directory");
  }
  try {
    byte[] bytes = limit == UNLIMITED ? file.contentsToByteArray() : FileUtil.loadFirstAndClose(file.getInputStream(), limit);
    return getTextByBinaryPresentation(bytes, file);
  }
  catch (IOException e) {
    return ArrayUtil.EMPTY_CHAR_SEQUENCE;
  }
}
 
Example 3
Source File: OneFileAtProjectTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected LanguageVersion resolveLanguageVersion(@Nonnull FileType fileType) {
  if(fileType instanceof LanguageFileType) {
    return LanguageVersionUtil.findDefaultVersion(((LanguageFileType)fileType).getLanguage());
  }
  throw new IllegalArgumentException(fileType.getName() + " is not extends 'LanguageFileType'");
}
 
Example 4
Source File: FileReference.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
public String getNewFileTemplateName() {
  FileType fileType = FileTypeRegistry.getInstance().getFileTypeByFileName(myText);
  if (fileType != UnknownFileType.INSTANCE) {
    return fileType.getName() + " File." + fileType.getDefaultExtension();
  }
  return null;
}
 
Example 5
Source File: RegistrarMatchUtil.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@NotNull
public static Map<PhpToolboxProviderInterface, Set<JsonRegistrar>> getProviders(@NotNull PsiElement psiElement) {

    Collection<JsonRegistrar> registrars = ExtensionProviderUtil.getRegistrar(psiElement.getProject(), ApplicationManager.getApplication().getComponent(PhpToolboxApplicationService.class));
    if(registrars.size() == 0) {
        return Collections.emptyMap();
    }

    FileType fileType = psiElement.getContainingFile().getFileType();
    String fileTypeName = fileType.getName();

    Collection<PhpToolboxProviderInterface> providers = null;

    Map<PhpToolboxProviderInterface, Set<JsonRegistrar>> providerMatches = new HashMap<>();

    for (JsonRegistrar registrar : registrars) {

        if(registrar == null) {
            continue;
        }

        Collection<JsonSignature> signatures = registrar.getSignatures();
        if(!fileTypeName.equalsIgnoreCase(registrar.getLanguage()) || registrar.getProvider() == null || signatures.size() == 0) {
            continue;
        }

        // init providers
        if(providers == null) {
            providers = ExtensionProviderUtil.getProviders(psiElement.getProject());
        }

        Collection<PhpToolboxProviderInterface> matchedProviders = new ArrayList<>();
        for (PhpToolboxProviderInterface provider : providers) {
            String name = provider.getName();
            if (name.equals(registrar.getProvider())) {
                matchedProviders.add(provider);
            }
        }

        if(matchedProviders.size() == 0) {
            continue;
        }

        LanguageMatcherParameter parameter = null;

        for (LanguageRegistrarMatcherInterface matcher : ExtensionProviderUtil.REGISTRAR_MATCHER.getExtensions()) {

            if(!matcher.supports(psiElement.getContainingFile().getFileType())) {
                continue;
            }

            if(parameter == null) {
                parameter = new LanguageMatcherParameter(psiElement, registrar);
            }

            if(matcher.matches(parameter)) {
                for (PhpToolboxProviderInterface matchedProvider : matchedProviders) {
                    if(providerMatches.containsKey(matchedProvider)) {
                        providerMatches.get(matchedProvider).add(registrar);
                    }
                    providerMatches.put(matchedProvider, ContainerUtil.newHashSet(registrar));
                }

                break;
            }

        }

    }

    return providerMatches;
}
 
Example 6
Source File: RegistrarMatchUtil.java    From idea-php-toolbox with MIT License 4 votes vote down vote up
@NotNull
public static Map<PhpToolboxProviderInterface, Set<JsonRegistrar>> getProviders(@NotNull PsiElement psiElement) {

    Collection<JsonRegistrar> registrars = ExtensionProviderUtil.getRegistrar(psiElement.getProject(), ApplicationManager.getApplication().getComponent(PhpToolboxApplicationService.class));
    if(registrars.size() == 0) {
        return Collections.emptyMap();
    }

    FileType fileType = psiElement.getContainingFile().getFileType();
    String fileTypeName = fileType.getName();

    Collection<PhpToolboxProviderInterface> providers = null;

    Map<PhpToolboxProviderInterface, Set<JsonRegistrar>> providerMatches = new HashMap<>();

    for (JsonRegistrar registrar : registrars) {

        if(registrar == null) {
            continue;
        }

        Collection<JsonSignature> signatures = registrar.getSignatures();
        if(!fileTypeName.equalsIgnoreCase(registrar.getLanguage()) || registrar.getProvider() == null || signatures.size() == 0) {
            continue;
        }

        // init providers
        if(providers == null) {
            providers = ExtensionProviderUtil.getProviders(psiElement.getProject());
        }

        Collection<PhpToolboxProviderInterface> matchedProviders = new ArrayList<>();
        for (PhpToolboxProviderInterface provider : providers) {
            String name = provider.getName();
            if (name.equals(registrar.getProvider())) {
                matchedProviders.add(provider);
            }
        }

        if(matchedProviders.size() == 0) {
            continue;
        }

        LanguageMatcherParameter parameter = null;

        for (LanguageRegistrarMatcherInterface matcher : ExtensionProviderUtil.REGISTRAR_MATCHER.getExtensions()) {

            if(!matcher.supports(psiElement.getContainingFile().getFileType())) {
                continue;
            }

            if(parameter == null) {
                parameter = new LanguageMatcherParameter(psiElement, registrar);
            }

            if(matcher.matches(parameter)) {
                for (PhpToolboxProviderInterface matchedProvider : matchedProviders) {
                    if(providerMatches.containsKey(matchedProvider)) {
                        providerMatches.get(matchedProvider).add(registrar);
                    }
                    providerMatches.put(matchedProvider, ContainerUtil.newHashSet(registrar));
                }

                break;
            }

        }

    }

    return providerMatches;
}
 
Example 7
Source File: WakaTime.java    From jetbrains-wakatime with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static String getLanguage(final VirtualFile file) {
    FileType type = file.getFileType();
    if (type != null)
        return type.getName();
    return null;
}
 
Example 8
Source File: TemplateCommentPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
public TemplateCommentPanel(@Nonnull FileType fileType, @Nullable TemplateCommentPanel parentPanel, @Nonnull Project project) {
  this(fileType.getName(), fileType, parentPanel, project);
  myAllowBlock = FileTypeUtil.hasBlockComment(fileType);
  myCommenter = FileTypeUtil.getCommenter(fileType);
  myAllowSeparator = CopyrightUpdaters.INSTANCE.forFileType(fileType).isAllowSeparator();
}
 
Example 9
Source File: GotoFileAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected String textForFilterValue(@Nonnull FileType value) {
  return value.getName();
}
 
Example 10
Source File: GotoFileConfiguration.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected String nameForElement(FileType type) {
  return type.getName();
}