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

The following examples show how to use com.intellij.openapi.fileTypes.FileType#getDefaultExtension() . These examples are extracted from open source projects. 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
@Override
public PsiFile configureByText(final FileType fileType, @NonNls final String text) {
  assertInitialized();
  final String extension = fileType.getDefaultExtension();
  final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
  if (fileTypeManager.getFileTypeByExtension(extension) != fileType) {
    new WriteCommandAction(getProject()) {
      @Override
      protected void run(Result result) throws Exception {
        fileTypeManager.associateExtension(fileType, extension);
      }
    }.execute();
  }
  final String fileName = "aaa." + extension;
  return configureByText(fileName, text);
}
 
Example 2
Source Project: consulo   File: ScratchUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void updateFileExtension(@Nonnull Project project, @Nullable VirtualFile file) throws IOException {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  if (CommandProcessor.getInstance().getCurrentCommand() == null) {
    throw new AssertionError("command required");
  }

  if (file == null) return;
  Language language = LanguageUtil.getLanguageForPsi(project, file);
  FileType expected = getFileTypeFromName(file);
  FileType actual = language == null ? null : language.getAssociatedFileType();
  if (expected == actual || actual == null) return;
  String ext = actual.getDefaultExtension();
  if (StringUtil.isEmpty(ext)) return;

  String newName = PathUtil.makeFileName(file.getNameWithoutExtension(), ext);
  VirtualFile parent = file.getParent();
  newName = parent != null && parent.findChild(newName) != null ? PathUtil.makeFileName(file.getName(), ext) : newName;
  file.rename(ScratchUtil.class, newName);
}
 
Example 3
Source Project: json2java4idea   File: Extensions.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@CheckReturnValue
public static String remove(@Nonnull String fileName, @Nonnull FileType fileType) {
    final String extension = fileType.getDefaultExtension();
    if (fileName.endsWith(DELIMITER + extension)) {
        return fileName.substring(0, fileName.length() - (extension.length() + 1));
    }
    return fileName;
}
 
Example 4
Source Project: json2java4idea   File: Extensions.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@CheckReturnValue
public static String append(@Nonnull String fileName, @Nonnull FileType fileType) {
    final String extension = fileType.getDefaultExtension();
    if (fileName.endsWith(DELIMITER + extension)) {
        return fileName;
    }
    return fileName + DELIMITER + extension;
}
 
Example 5
Source Project: consulo   File: ExternalDiffToolUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static String getFileName(@Nonnull DiffContent content,
                                  @javax.annotation.Nullable String title,
                                  @javax.annotation.Nullable String windowTitle) {
  if (content instanceof EmptyContent) {
    return "no_content.tmp";
  }

  String fileName = content.getUserData(DiffUserDataKeysEx.FILE_NAME);

  if (fileName == null && content instanceof DocumentContent) {
    VirtualFile highlightFile = ((DocumentContent)content).getHighlightFile();
    fileName = highlightFile != null ? highlightFile.getName() : null;
  }

  if (fileName == null && content instanceof FileContent) {
    fileName = ((FileContent)content).getFile().getName();
  }

  if (!StringUtil.isEmptyOrSpaces(fileName)) {
    return fileName;
  }


  FileType fileType = content.getContentType();
  String ext = fileType != null ? fileType.getDefaultExtension() : null;
  if (StringUtil.isEmptyOrSpaces(ext)) ext = "tmp";

  String name = "";
  if (title != null && windowTitle != null) {
    name = title + "_" + windowTitle;
  }
  else if (title != null || windowTitle != null) {
    name = title != null ? title : windowTitle;
  }
  if (name.length() > 50) name = name.substring(0, 50);

  return PathUtil.suggestFileName(name + "." + ext, true, false);
}
 
Example 6
Source Project: consulo   File: FileReference.java    License: 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 7
private String chooseExtension() {
  DiffContent content = getContent();
  VirtualFile contentFile = content.getFile();
  String extension;
  if (contentFile != null) {
    extension = "." + contentFile.getExtension();
  }
  else {
    FileType contentType = content.getContentType();
    if (contentType == null) contentType = DiffUtil.chooseContentTypes(myRequest.getContents())[myIndex];
    extension = contentType != null ?  "." + contentType.getDefaultExtension() : null;
  }
  return extension;
}
 
Example 8
Source Project: consulo   File: RemoteFileInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void finished(@Nullable final FileType fileType) {
  final File localIOFile;

  synchronized (myLock) {
    LOG.debug("Downloading finished, size = " + myLocalFile.length() + ", file type=" + (fileType != null ? fileType.getName() : "null"));
    if (fileType != null) {
      String fileName = myLocalFile.getName();
      int dot = fileName.lastIndexOf('.');
      String extension = fileType.getDefaultExtension();
      if (dot == -1 || !extension.equals(fileName.substring(dot + 1))) {
        File newFile = FileUtil.findSequentNonexistentFile(myLocalFile.getParentFile(), fileName, extension);
        try {
          FileUtil.rename(myLocalFile, newFile);
          myLocalFile = newFile;
        }
        catch (IOException e) {
          LOG.debug(e);
        }
      }
    }

    localIOFile = myLocalFile;
  }

  VirtualFile localFile = WriteAction.compute(() -> {
    final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(localIOFile);
    if (file != null) {
      file.refresh(false, false);
    }
    return file;
  });
  LOG.assertTrue(localFile != null, "Virtual local file not found for " + localIOFile.getAbsolutePath());
  LOG.debug("Virtual local file: " + localFile + ", size = " + localFile.getLength());
  synchronized (myLock) {
    myLocalVirtualFile = localFile;
    myPrevLocalFile = null;
    myState = RemoteFileState.DOWNLOADED;
    myErrorMessage = null;
  }
  for (FileDownloadingListener listener : myListeners) {
    listener.fileDownloaded(localFile);
  }
}
 
Example 9
Source Project: consulo   File: CodeStyleAbstractPanel.java    License: Apache License 2.0 4 votes vote down vote up
@NonNls
protected String getFileTypeExtension(FileType fileType) {
  return fileType.getDefaultExtension();
}
 
Example 10
Source Project: consulo   File: ExtractIncludeFileBase.java    License: Apache License 2.0 4 votes vote down vote up
@NonNls
protected String getExtractExtension(final FileType extractFileType, final T first) {
  return extractFileType.getDefaultExtension();
}