Java Code Examples for com.intellij.openapi.vfs.VirtualFile#getFileType()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#getFileType() . 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
public SyntaxHighlighterOverEditorHighlighter(SyntaxHighlighter _highlighter, VirtualFile file, Project project) {
  if (file.getFileType() == PlainTextFileType.INSTANCE) { // optimization for large files, PlainTextSyntaxHighlighterFactory is slow
    highlighter = new PlainSyntaxHighlighter();
    lexer = highlighter.getHighlightingLexer();
  } else {
    highlighter = _highlighter;
    LayeredLexer.ourDisableLayersFlag.set(Boolean.TRUE);
    EditorHighlighter editorHighlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(project, file);

    try {
      if (editorHighlighter instanceof LayeredLexerEditorHighlighter) {
        lexer = new LexerEditorHighlighterLexer(editorHighlighter, false);
      }
      else {
        lexer = highlighter.getHighlightingLexer();
      }
    }
    finally {
      LayeredLexer.ourDisableLayersFlag.set(null);
    }
  }
}
 
Example 2
Source Project: consulo   File: VirtualFileImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public FileType getFileType() {
  if (myFileInfo == null) {
    return super.getFileType();
  }

  VirtualFile localFile = myFileInfo.getLocalFile();
  if (localFile != null) {
    return localFile.getFileType();
  }
  FileType fileType = super.getFileType();
  if (myInitialFileType == null) {
    myInitialFileType = fileType;
  }
  return fileType;
}
 
Example 3
@NotNull
@Override
public Collection<AbstractTreeNode<?>> modify(@NotNull AbstractTreeNode<?> parent,
                                           @NotNull Collection<AbstractTreeNode<?>> children,
                                           ViewSettings settings) {
  ArrayList<AbstractTreeNode<?>> nodes = new ArrayList<>();
  for (AbstractTreeNode<?> child : children) {
    if (child instanceof PsiFileNode) {
      VirtualFile file = ((PsiFileNode) child).getVirtualFile();
      if (file != null && !file.isDirectory() && !(file.getFileType() instanceof PlainTextFileType)) {
        continue;
      }
    }
    nodes.add(child);
  }
  return nodes;
}
 
Example 4
Source Project: consulo   File: FileBasedIndexImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private List<ID<?, ?>> getAffectedIndexCandidates(@Nonnull VirtualFile file) {
  if (file.isDirectory()) {
    return isProjectOrWorkspaceFile(file, null) ? Collections.emptyList() : myIndicesForDirectories;
  }
  FileType fileType = file.getFileType();
  if (isProjectOrWorkspaceFile(file, fileType)) return Collections.emptyList();

  return getState().getFileTypesForIndex(fileType);
}
 
Example 5
private static VirtualFile getContainerFile(VirtualFile fileContainer) {
  if (fileContainer.getFileType() == ARCHIVE) {
    return requireNonNull(JarFileSystem.getInstance().getLocalVirtualFileFor(fileContainer));
  } else {
    return fileContainer;
  }
}
 
Example 6
@Override
public boolean acceptInput(@NotNull VirtualFile file) {
    //noinspection ObjectEquality
    return file.getFileType() == HaskellFileType.INSTANCE
            && file.isInLocalFileSystem();
    // to avoid renaming modules that are somewhere in a lib folder
    // and added as a library. Can get nasty otherwise.
}
 
Example 7
protected void childrenChanged(PsiElement parent, final boolean stopProcessingForThisModificationCount) {
  if (parent instanceof PsiDirectory && isFlattenPackages()) {
    addSubtreeToUpdateByRoot();
    return;
  }

  long newModificationCount = myModificationTracker.getModificationCount();
  if (newModificationCount == myModificationCount) return;
  if (stopProcessingForThisModificationCount) {
    myModificationCount = newModificationCount;
  }

  while (true) {
    if (parent == null) break;
    if (parent instanceof PsiFile) {
      VirtualFile virtualFile = ((PsiFile)parent).getVirtualFile();
      if (virtualFile != null && virtualFile.getFileType() != PlainTextFileType.INSTANCE) {
        // adding a class within a file causes a new node to appear in project view => entire dir should be updated
        parent = ((PsiFile)parent).getContainingDirectory();
        if (parent == null) break;
      }
    }
    else if (parent instanceof PsiDirectory && ScratchUtil.isScratch(((PsiDirectory)parent).getVirtualFile())) {
      addSubtreeToUpdateByRoot();
      break;
    }

    if (addSubtreeToUpdateByElementFile(parent)) {
      break;
    }

    if (parent instanceof PsiFile || parent instanceof PsiDirectory) break;
    parent = parent.getParent();
  }
}
 
Example 8
Source Project: consulo   File: PsiFileGistImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static PsiFile getPsiFile(@Nonnull Project project, @Nonnull VirtualFile file) {
  PsiFile psi = PsiManager.getInstance(project).findFile(file);
  if (!(psi instanceof PsiFileImpl) || ((PsiFileImpl)psi).isContentsLoaded()) {
    return psi;
  }

  FileType fileType = file.getFileType();
  if (!(fileType instanceof LanguageFileType)) return null;

  return FileContentImpl.createFileFromText(project, psi.getViewProvider().getContents(), (LanguageFileType)fileType, file, file.getName());
}
 
Example 9
@RequiredReadAction
@Nullable
@Override
public EditorNotificationPanel createNotificationPanel(@Nonnull VirtualFile file, @Nonnull FileEditor fileEditor)
{
	if(file.getFileType() != Unity3dYMLAssetFileType.INSTANCE || !ArrayUtil.contains(file.getExtension(), Unity3dAssetFileTypeDetector.ourAssetExtensions))
	{
		return null;
	}
	final String uuid = Unity3dAssetUtil.getGUID(myProject, file);
	if(uuid == null)
	{
		return null;
	}
	MultiMap<VirtualFile, Unity3dYMLAsset> map = Unity3dYMLAsset.findAssetAsAttach(myProject, file);
	if(map.isEmpty())
	{
		return null;
	}

	PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
	if(psiFile == null)
	{
		return null;
	}

	final EditorNotificationPanel panel = new EditorNotificationPanel();
	panel.text("Used asset...");
	panel.createActionLabel("Find usages...", () -> FindManager.getInstance(myProject).findUsages(psiFile));
	return panel;
}
 
Example 10
Source Project: consulo   File: PsiUtilCore.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tries to find PSI file for a virtual file and throws assertion error with debug info if it is null.
 */
@Nonnull
public static PsiFile getPsiFile(@Nonnull Project project, @Nonnull VirtualFile file) {
  PsiManager psiManager = PsiManager.getInstance(project);
  PsiFile psi = psiManager.findFile(file);
  if (psi != null) return psi;
  FileType fileType = file.getFileType();
  FileViewProvider viewProvider = psiManager.findViewProvider(file);
  Document document = FileDocumentManager.getInstance().getDocument(file);
  boolean ignored = !(file instanceof LightVirtualFile) && FileTypeRegistry.getInstance().isFileIgnored(file);
  VirtualFile vDir = file.getParent();
  PsiDirectory psiDir = vDir == null ? null : PsiManager.getInstance(project).findDirectory(vDir);
  FileIndexFacade indexFacade = FileIndexFacade.getInstance(project);
  StringBuilder sb = new StringBuilder();
  sb.append("valid=").append(file.isValid()).
          append(" isDirectory=").append(file.isDirectory()).
          append(" hasDocument=").append(document != null).
          append(" length=").append(file.getLength());
  sb.append("\nproject=").append(project.getName()).
          append(" default=").append(project.isDefault()).
          append(" open=").append(project.isOpen());;
  sb.append("\nfileType=").append(fileType.getName()).append("/").append(fileType.getClass().getName());
  sb.append("\nisIgnored=").append(ignored);
  sb.append(" underIgnored=").append(indexFacade.isUnderIgnored(file));
  sb.append(" inLibrary=").append(indexFacade.isInLibrarySource(file) || indexFacade.isInLibraryClasses(file));
  sb.append(" parentDir=").append(vDir == null ? "no-vfs" : vDir.isDirectory() ? "has-vfs-dir" : "has-vfs-file").
          append("/").append(psiDir == null ? "no-psi" : "has-psi");
  sb.append("\nviewProvider=").append(viewProvider == null ? "null" : viewProvider.getClass().getName());
  if (viewProvider != null) {
    List<PsiFile> files = viewProvider.getAllFiles();
    sb.append(" language=").append(viewProvider.getBaseLanguage().getID());
    sb.append(" physical=").append(viewProvider.isPhysical());
    sb.append(" rootCount=").append(files.size());
    for (PsiFile o : files) {
      sb.append("\n  root=").append(o.getLanguage().getID()).append("/").append(o.getClass().getName());
    }
  }
  LOG.error("no PSI for file '" + file.getName() + "'", new Attachment(file.getPresentableUrl(), sb.toString()));
  throw new AssertionError();
}
 
Example 11
@NotNull
@Override
public StripTrailingSpacesFilter createFilter(@Nullable Project project, @NotNull Document document) {
    VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
    if (project != null &&
            virtualFile != null &&
            virtualFile.getFileType() instanceof LanguageFileType &&
            ((LanguageFileType) virtualFile.getFileType()).getLanguage().isKindOf(CsvLanguage.INSTANCE) &&
            CsvEditorSettings.getInstance().getKeepTrailingSpaces()) {
        return StripTrailingSpacesFilter.NOT_ALLOWED;
    }
    return StripTrailingSpacesFilter.ALL_LINES;
}
 
Example 12
Source Project: consulo   File: PatchFileType.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isPatchFile(@javax.annotation.Nullable VirtualFile vFile) {
  return vFile != null && vFile.getFileType() == PatchFileType.INSTANCE;
}
 
Example 13
Source Project: Intellij-Plugin   File: GaugeUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isGaugeFile(VirtualFile file) {
    return file.getFileType() instanceof SpecFileType || file.getFileType() instanceof ConceptFileType;
}
 
Example 14
Source Project: consulo   File: BinaryLightVirtualFile.java    License: Apache License 2.0 4 votes vote down vote up
public BinaryLightVirtualFile(VirtualFile original, final byte[] content, long modificationStamp) {
  this(original.getName(), original.getFileType(), content, modificationStamp);
}
 
Example 15
Source Project: consulo   File: MergeVersion.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public FileType getContentType() {
  VirtualFile file = getFile();
  if (file == null) return PlainTextFileType.INSTANCE;
  return file.getFileType();
}
 
Example 16
Source Project: consulo   File: LocalFileExternalizer.java    License: Apache License 2.0 4 votes vote down vote up
static boolean canExternalizeAsFile(VirtualFile file) {
  if (file == null || file.isDirectory()) return false;
  FileType fileType = file.getFileType();
  if (fileType.isBinary() && fileType != UnknownFileType.INSTANCE) return false;
  return true;
}
 
Example 17
Source Project: consulo   File: CompileAction.java    License: Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
public void update(@Nonnull AnActionEvent event) {
  super.update(event);
  Presentation presentation = event.getPresentation();
  if (!presentation.isEnabled()) {
    return;
  }
  DataContext dataContext = event.getDataContext();

  presentation.setText(ActionsBundle.actionText(IdeActions.ACTION_COMPILE));
  presentation.setVisible(true);

  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    presentation.setEnabled(false);
    return;
  }

  final Module module = dataContext.getData(LangDataKeys.MODULE_CONTEXT);

  final VirtualFile[] files = getCompilableFiles(project, dataContext.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY));
  if (module == null && files.length == 0) {
    presentation.setEnabled(false);
    presentation.setVisible(!ActionPlaces.isPopupPlace(event.getPlace()));
    return;
  }

  String elementDescription = null;
  if (module != null) {
    elementDescription = CompilerBundle.message("action.compile.description.module", module.getName());
  }
  else {
    PsiPackage aPackage = null;
    if (files.length == 1) {
      final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(files[0]);
      if (directory != null) {
        aPackage = PsiPackageManager.getInstance(project).findAnyPackage(directory);
      }
    }
    else {
      PsiElement element = dataContext.getData(LangDataKeys.PSI_ELEMENT);
      if (element instanceof PsiPackage) {
        aPackage = (PsiPackage)element;
      }
    }

    if (aPackage != null) {
      String name = aPackage.getQualifiedName();
      if (name.length() == 0) {
        //noinspection HardCodedStringLiteral
        name = "<default>";
      }
      elementDescription = "'" + name + "'";
    }
    else if (files.length == 1) {
      final VirtualFile file = files[0];
      FileType fileType = file.getFileType();
      if (CompilerManager.getInstance(project).isCompilableFileType(fileType) || isCompilableResourceFile(project, file)) {
        elementDescription = "'" + file.getName() + "'";
      }
      else {
        if (!ActionPlaces.MAIN_MENU.equals(event.getPlace())) {
          // the action should be invisible in popups for non-java files
          presentation.setEnabled(false);
          presentation.setVisible(false);
          return;
        }
      }
    }
    else {
      elementDescription = CompilerBundle.message("action.compile.description.selected.files");
    }
  }

  if (elementDescription == null) {
    presentation.setEnabled(false);
    return;
  }

  presentation.setText(createPresentationText(elementDescription), true);
  presentation.setEnabled(true);
}
 
Example 18
@Override
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
    return file.getFileType() == CmtFileType.INSTANCE;
}
 
Example 19
@Override
public boolean acceptInput(VirtualFile file) {
  return file.getFileType() == ThriftFileType.INSTANCE;
}
 
Example 20
private static boolean isBinaryWithoutDecompiler(VirtualFile file) {
  final FileType ft = file.getFileType();
  return ft.isBinary() && BinaryFileTypeDecompilers.INSTANCE.forFileType(ft) == null;
}