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

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#getFileType() . 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: TextOnlyTreeStructureProvider.java    From intellij-sdk-docs with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: SyntaxHighlighterOverEditorHighlighter.java    From consulo with Apache License 2.0 6 votes vote down vote up
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 3
Source File: VirtualFileImpl.java    From consulo with 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 4
Source File: MetadataContainerInfo.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
private static VirtualFile getContainerFile(VirtualFile fileContainer) {
  if (fileContainer.getFileType() == ARCHIVE) {
    return requireNonNull(JarFileSystem.getInstance().getLocalVirtualFileFor(fileContainer));
  } else {
    return fileContainer;
  }
}
 
Example 5
Source File: HaskellModuleIndex.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: CsvStripTrailingSpacesFilterFactory.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: ProjectViewPsiTreeChangeListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
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 File: PsiFileGistImpl.java    From consulo with 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
Source File: PsiUtilCore.java    From consulo with 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 10
Source File: FileBasedIndexImpl.java    From consulo with 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 11
Source File: Unity3dAssetEditorNotificationProvider.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: LocalFileExternalizer.java    From consulo with 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 13
Source File: LightFileDocumentManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean isBinaryWithoutDecompiler(VirtualFile file) {
  final FileType ft = file.getFileType();
  return ft.isBinary() && BinaryFileTypeDecompilers.INSTANCE.forFileType(ft) == null;
}
 
Example 14
Source File: ThriftDeclarationIndex.java    From intellij-thrift with Apache License 2.0 4 votes vote down vote up
@Override
public boolean acceptInput(VirtualFile file) {
  return file.getFileType() == ThriftFileType.INSTANCE;
}
 
Example 15
Source File: CmtFileEditorProvider.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@Override
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
    return file.getFileType() == CmtFileType.INSTANCE;
}
 
Example 16
Source File: CompileAction.java    From consulo with 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 17
Source File: MergeVersion.java    From consulo with 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 18
Source File: BinaryLightVirtualFile.java    From consulo with 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 19
Source File: GaugeUtil.java    From Intellij-Plugin with 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 20
Source File: PatchFileType.java    From consulo with 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;
}