@NotNull
  public static Charset detectCharset(
      @NotNull VirtualFile virtualFile, @NotNull byte[] content, @NotNull FileType fileType) {
    Charset charset = null;

    Trinity<Charset, CharsetToolkit.GuessedEncoding, byte[]> guessed =
        guessFromContent(virtualFile, content, content.length);
    if (guessed != null && guessed.first != null) {
      charset = guessed.first;
    } else {
      String charsetName = fileType.getCharset(virtualFile, content);

      if (charsetName == null) {
        Charset specifiedExplicitly = EncodingRegistry.getInstance().getEncoding(virtualFile, true);
        if (specifiedExplicitly != null) {
          charset = specifiedExplicitly;
        }
      } else {
        charset = CharsetToolkit.forName(charsetName);
      }
    }

    if (charset == null) {
      charset = EncodingRegistry.getInstance().getDefaultCharset();
    }
    if (fileType.getName().equals("Properties")
        && EncodingRegistry.getInstance().isNative2Ascii(virtualFile)) {
      charset = Native2AsciiCharset.wrap(charset);
    }
    virtualFile.setCharset(charset);
    return charset;
  }
 /** {@inheritDoc} */
 @Override
 public int compare(final FileType o1, final FileType o2) {
   if (o1 == o2) {
     return 0;
   }
   if (o1 == FileTypes.UNKNOWN) {
     return 1;
   }
   if (o2 == FileTypes.UNKNOWN) {
     return -1;
   }
   if (o1.isBinary() && !o2.isBinary()) {
     return 1;
   }
   if (!o1.isBinary() && o2.isBinary()) {
     return -1;
   }
   return o1.getName().compareToIgnoreCase(o2.getName());
 }
 @Nullable
 public static CompletionData getCompletionDataByFileType(FileType fileType) {
   for (CompletionDataEP ep : Extensions.getExtensions(CompletionDataEP.EP_NAME)) {
     if (ep.fileType.equals(fileType.getName())) {
       return ep.getHandler();
     }
   }
   final NotNullLazyValue<CompletionData> lazyValue = ourCustomCompletionDatas.get(fileType);
   return lazyValue == null ? null : lazyValue.getValue();
 }
Example #4
0
  public void initComponent() {
    // auto-associate .awl files as xml if not otherwise mapped
    FileType awlType = FileTypeManager.getInstance().getFileTypeByExtension("awl");
    if (awlType.getName().endsWith("UnknownFileType") || awlType.getName().equals("UNKNOWN")) {
      FileType htmType = FileTypeManager.getInstance().getFileTypeByExtension("xml");
      FileTypeManager.getInstance().registerFileType(htmType, new String[] {"awl"});
    }

    // auto-associate .oss files as text if not otherwise mapped
    FileType ossType = FileTypeManager.getInstance().getFileTypeByExtension("oss");
    if (ossType.getName().endsWith("UnknownFileType") || ossType.getName().equals("UNKNOWN")) {
      FileType txtType = FileTypeManager.getInstance().getFileTypeByExtension("txt");
      FileTypeManager.getInstance().registerFileType(txtType, new String[] {"oss"});
    }

    // Load our bundled live templates
    InputStream is = getClass().getResourceAsStream("/ariba/ideplugin/idea/livetemplates.xml");
    if (is != null) {
      loadTemplates(is, "AribaWeb");
    }
  }
 @Override
 @Nullable
 public WordsScanner getCacheBuilder(@NotNull FileType fileType) {
   final WordsScanner scanner = myMap.get(fileType);
   if (scanner != null) {
     return scanner;
   }
   for (CacheBuilderEP ep : Extensions.getExtensions(CacheBuilderEP.EP_NAME)) {
     if (ep.getFileType().equals(fileType.getName())) {
       return ep.getWordsScanner();
     }
   }
   return null;
 }
 @NotNull
 @Override
 public String getName() {
   return fileType.getName();
 }
  @NotNull
  public static CharSequence loadText(@NotNull final VirtualFile file) {
    if (file instanceof LightVirtualFile) {
      return ((LightVirtualFile) file).getContent();
    }

    if (file.isDirectory()) {
      throw new AssertionError("'" + file.getPresentableUrl() + "' is a directory");
    }

    FileType fileType = file.getFileType();
    if (fileType.isBinary()) {
      final BinaryFileDecompiler decompiler =
          BinaryFileTypeDecompilers.INSTANCE.forFileType(fileType);
      if (decompiler != null) {
        CharSequence text;

        Application app = ApplicationManager.getApplication();
        if (app != null
            && app.isDispatchThread()
            && !app.isWriteAccessAllowed()
            && !ourDecompileProgressStarted) {
          final Ref<CharSequence> result = Ref.create(ArrayUtil.EMPTY_CHAR_SEQUENCE);
          final Ref<Throwable> error = Ref.create();

          ourDecompileProgressStarted = true;
          try {
            ProgressManager.getInstance()
                .run(
                    new Task.Modal(null, "Decompiling " + file.getName(), true) {
                      @Override
                      public void run(@NotNull ProgressIndicator indicator) {
                        indicator.setIndeterminate(true);
                        try {
                          result.set(
                              ApplicationUtil.runWithCheckCanceled(
                                  new Callable<CharSequence>() {
                                    @Override
                                    public CharSequence call() {
                                      return decompiler.decompile(file);
                                    }
                                  },
                                  indicator));
                        } catch (Throwable t) {
                          error.set(t);
                        }
                      }
                    });
          } finally {
            ourDecompileProgressStarted = false;
          }

          ExceptionUtil.rethrowUnchecked(error.get());
          text = result.get();
        } else {
          text = decompiler.decompile(file);
        }

        StringUtil.assertValidSeparators(text);
        return text;
      }

      throw new IllegalArgumentException(
          "Attempt to load text for binary file which doesn't have a decompiler plugged in: "
              + file.getPresentableUrl()
              + ". File type: "
              + fileType.getName());
    }

    try {
      byte[] bytes = file.contentsToByteArray();
      return getTextByBinaryPresentation(bytes, file);
    } catch (IOException e) {
      return ArrayUtil.EMPTY_CHAR_SEQUENCE;
    }
  }
  @Nullable
  public static Icon getIcon(VirtualFile file, int flags, @Nullable Project project) {
    if (file.isDirectory()) return DIRECTORY;

    FileType type = file.getFileType();
    String typeString = type.getName().toLowerCase();
    String extension = file.getExtension();

    if (extension != null) extension = extension.toLowerCase();
    else extension = "";

    switch (typeString) {
      case "objectivec":
        {
          switch (extension) {
            case "h":
            case "hpp":
            case "pch":
              return HEADER;
            case "cpp":
              return CPPFILE;
            case "c":
              return CFILE;
            case "mm":
            case "m":
              return MFILE;
          }

          return CFILE;
        }
      case "c#":
        return CSHARP;
      case "c++":
        return CPPFILE;

      case "json":
        if (file.getName().equalsIgnoreCase("package.json")) return NPM;
        if (file.getName().equalsIgnoreCase("bower.json")) return BOWER;

        return JSON;
      case "xml":
      case "plist":
        return XML;

      case "html":
      case "xhtml":
        return HTML;
      case "css":
        return CSS;
      case "yaml":
        return YAML;
      case "jade":
        return JADE;

      case "javascript":
      case "ecmascript 6":
        if (file.getName().equalsIgnoreCase("gruntfile.js")) return GRUNT;
        if (file.getName().equalsIgnoreCase("gulpfile.js")) return GULP;

        return JAVASCRIPT;
      case "coffeescript":
      case "literal coffeescript":
        return COFFEE;
      case "ruby":
      case "podfile":
        return RUBY;
      case "python":
        return PYTHON;
      case "php":
        return PHP;
      case "haskell":
        return HASKELL;
      case "java":
        return JAVA;

      case "images":
        return IMAGE;
      case "sql":
        return SQL;

      case "cmakelists.txt":
        return null; // Use the default CMake icon

      case "strings":
        return TEXT;

      case "git file":
        return GIT;

      case "npm file":
        return NPM;

      case "markdown":
        return MARKDOWN;

      case "bash":
        return SHELL;

      case "plain_text":
        {
          Icon icon = getIconForExtension(extension);
          if (icon != null) return icon;

          return TEXT;
        }

      case "unknown":
        Icon icon = getIconForExtension(extension);
        if (icon != null) return icon;

      default:
        return type.isBinary() ? BINARY : ANY;
    }
  }
 @Override
 protected String textForFilterValue(@NotNull FileType value) {
   return value.getName();
 }