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

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#equals() . 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: PubRoot.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns true if the project has a module for the "android" directory.
 */
public boolean hasAndroidModule(Project project) {
  final VirtualFile androidDir = getAndroidDir();
  if (androidDir == null) {
    return false;
  }

  for (Module module : ModuleManager.getInstance(project).getModules()) {
    for (VirtualFile contentRoot : ModuleRootManager.getInstance(module).getContentRoots()) {
      if (contentRoot.equals(androidDir)) {
        return true;
      }
    }
  }
  return false;
}
 
Example 2
Source File: NativeEditorNotificationProvider.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static VirtualFile findRootDir(@NotNull VirtualFile file, @Nullable VirtualFile projectDir) {
  if (projectDir == null) {
    return null;
  }
  // Return the top-most parent of file that is a child of the project directory.
  VirtualFile parent = file.getParent();
  if (projectDir.equals(parent)) {
    return null;
  }
  VirtualFile root = parent;
  while (parent != null) {
    parent = parent.getParent();
    if (projectDir.equals(parent)) {
      return root;
    }
    root = parent;
  }
  return null;
}
 
Example 3
Source File: PersistentRootConfigComponent.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
private boolean isValidRoot(final VirtualFile file) {
    if (project.isDisposed()) {
        // Err on the side of caution
        return true;
    }
    ProjectLevelVcsManager mgr = ProjectLevelVcsManager.getInstance(project);
    if (mgr == null) {
        // Err on the side of caution
        return true;
    }
    for (VcsRoot root : mgr.getAllVcsRoots()) {
        if (root == null || root.getVcs() == null || root.getPath() == null) {
            continue;
        }
        if (!P4VcsKey.VCS_NAME.equals(root.getVcs().getKeyInstanceMethod().getName())) {
            continue;
        }
        if (file.equals(root.getPath())) {
            return true;
        }
    }
    // This can happen if the passed-in file is not yet registered in
    // the VCS root path.
    return false;
}
 
Example 4
Source File: PubRoot.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns true if the project has a module for the "android" directory.
 */
public boolean hasAndroidModule(Project project) {
  final VirtualFile androidDir = getAndroidDir();
  if (androidDir == null) {
    return false;
  }

  for (Module module : ModuleManager.getInstance(project).getModules()) {
    for (VirtualFile contentRoot : ModuleRootManager.getInstance(module).getContentRoots()) {
      if (contentRoot.equals(androidDir)) {
        return true;
      }
    }
  }
  return false;
}
 
Example 5
Source File: JSGraphQLEndpointImportUtil.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
public static String getImportName(VirtualFile entryFileDir, VirtualFile virtualFile, String name) {
    if(entryFileDir != null) {
        VirtualFile parentDir = virtualFile.getParent();
        while(parentDir != null && !parentDir.equals(entryFileDir)) {
            name = parentDir.getName() + '/' + name;
            parentDir = parentDir.getParent();
        }
    }
    return name;
}
 
Example 6
Source File: PubRoot.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns true if the PubRoot is an ancestor of the given file.
 */
public boolean contains(@NotNull VirtualFile file) {
  VirtualFile dir = file.getParent();
  while (dir != null) {
    if (dir.equals(root)) {
      return true;
    }
    dir = dir.getParent();
  }
  return false;
}
 
Example 7
Source File: EditorHistoryManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private synchronized HistoryEntry getEntry(@Nonnull VirtualFile file) {
  for (int i = myEntriesList.size() - 1; i >= 0; i--) {
    final HistoryEntry entry = myEntriesList.get(i);
    VirtualFile entryFile = entry.getFile();
    if (file.equals(entryFile)) {
      return entry;
    }
  }
  return null;
}
 
Example 8
Source File: DesktopEditorWindow.java    From consulo with Apache License 2.0 5 votes vote down vote up
private int calcIndexToSelect(VirtualFile fileBeingClosed, final int fileIndex) {
  final int currentlySelectedIndex = myTabbedPane.getSelectedIndex();
  if (currentlySelectedIndex != fileIndex) {
    // if the file being closed is not currently selected, keep the currently selected file open
    return currentlySelectedIndex;
  }
  UISettings uiSettings = UISettings.getInstance();
  if (uiSettings.getActiveMruEditorOnClose()) {
    // try to open last visited file
    final VirtualFile[] histFiles = EditorHistoryManager.getInstance(getManager().getProject()).getFiles();
    for (int idx = histFiles.length - 1; idx >= 0; idx--) {
      final VirtualFile histFile = histFiles[idx];
      if (histFile.equals(fileBeingClosed)) {
        continue;
      }
      final DesktopEditorWithProviderComposite editor = findFileComposite(histFile);
      if (editor == null) {
        continue; // ????
      }
      final int histFileIndex = findComponentIndex(editor.getComponent());
      if (histFileIndex >= 0) {
        // if the file being closed is located before the hist file, then after closing the index of the histFile will be shifted by -1
        return histFileIndex;
      }
    }
  }
  else if (uiSettings.getActiveRigtEditorOnClose() && fileIndex + 1 < myTabbedPane.getTabCount()) {
    return fileIndex + 1;
  }

  // by default select previous neighbour
  if (fileIndex > 0) {
    return fileIndex - 1;
  }
  // do nothing
  return -1;
}
 
Example 9
Source File: ContentEntryEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public ContentFolder getFolder(@Nonnull final VirtualFile file) {
  final ContentEntry contentEntry = getContentEntry();
  if (contentEntry == null) {
    return null;
  }
  for (ContentFolder contentFolder : contentEntry.getFolders(ContentFolderScopes.all())) {
    final VirtualFile f = contentFolder.getFile();
    if (f != null && f.equals(file)) {
      return contentFolder;
    }
  }
  return null;
}
 
Example 10
Source File: FlutterExternalIdeActionGroup.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected static boolean isWithinIOsDirectory(@NotNull VirtualFile file, @NotNull Project project) {
  final VirtualFile baseDir = project.getBaseDir();
  if (baseDir == null) {
    return false;
  }
  VirtualFile candidate = file;
  while (candidate != null && !baseDir.equals(candidate)) {
    if (isIOsDirectory(candidate)) {
      return true;
    }
    candidate = candidate.getParent();
  }
  return false;
}
 
Example 11
Source File: FlutterExternalIdeActionGroup.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected static boolean isWithinAndroidDirectory(@NotNull VirtualFile file, @NotNull Project project) {
  final VirtualFile baseDir = project.getBaseDir();
  if (baseDir == null) {
    return false;
  }
  VirtualFile candidate = file;
  while (candidate != null && !baseDir.equals(candidate)) {
    if (isAndroidDirectory(candidate)) {
      return true;
    }
    candidate = candidate.getParent();
  }
  return false;
}
 
Example 12
Source File: CopyTargetAction.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void update(AnActionEvent e) {
  VirtualFile buckFile = findBuckFileFromActionEvent(e);
  if (buckFile != null && buckFile.equals(e.getData(CommonDataKeys.VIRTUAL_FILE))) {
    e.getPresentation().setEnabledAndVisible(isActionEventInRuleBody(e));
  } else {
    e.getPresentation().setEnabledAndVisible(buckFile != null);
  }
}
 
Example 13
Source File: FileTreeUtil.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param path   child path
 * @param parent parent path
 * @return < 0 if the path is not under the parent, 0 if the path is the parent, or the
 *      number of directories under the parent the path is.
 */
public static int getPathDepth(@NotNull VirtualFile path, @Nullable VirtualFile parent) {
    if (parent == null) {
        return -1;
    }
    if (path.equals(parent)) {
        return 0;
    }
    List<VirtualFile> tree = getTreeTo(path, parent);
    if (tree == null) {
        return -1;
    }
    return tree.size() - 1;
}
 
Example 14
Source File: JsonBuilderService.java    From intellij-swagger with MIT License 5 votes vote down vote up
private void consumeLocalReference(
    final TextNode ref,
    final VirtualFile containingFile,
    Consumer<ResolvedRef> refConsumer,
    final VirtualFile specFile) {

  // We only need to inline references if we are in a referenced file.
  if (!containingFile.equals(specFile)) {
    final String jsonPointer = StringUtils.substringAfter(ref.asText(), "#");

    final ResolvedRef resolvedRef = resolveJsonPointer(jsonPointer, containingFile);

    consumeResolvedRef(refConsumer, specFile, resolvedRef);
  }
}
 
Example 15
Source File: Unity3dProjectImportUtil.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
private static void importAfterDefines(@Nonnull final Project project,
									   @Nullable final Sdk sdk,
									   final boolean runValidator,
									   @Nonnull ProgressIndicator indicator,
									   @Nullable UnityOpenFilePostHandlerRequest requestor,
									   @Nullable UnitySetDefines setDefines)
{
	try
	{
		Collection<String> defines = null;
		if(setDefines != null)
		{
			VirtualFile maybeProjectDir = LocalFileSystem.getInstance().findFileByPath(setDefines.projectPath);
			if(maybeProjectDir != null && maybeProjectDir.equals(project.getBaseDir()))
			{
				defines = new TreeSet<>(Arrays.asList(setDefines.defines));
			}
		}

		importOrUpdate(project, sdk, null, indicator, defines);
	}
	finally
	{
		project.putUserData(ourInProgressFlag, null);
	}

	if(runValidator)
	{
		UnityPluginFileValidator.runValidation(project);
	}

	if(requestor != null)
	{
		UIUtil.invokeLaterIfNeeded(() -> UnityOpenFilePostHandler.openFile(project, requestor));
	}
}
 
Example 16
Source File: DuplicatesInspectionBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ProblemDescriptor[] checkFile(@Nonnull final PsiFile psiFile, @Nonnull final InspectionManager manager, final boolean isOnTheFly) {
  final VirtualFile virtualFile = psiFile.getVirtualFile();
  if (!(virtualFile instanceof VirtualFileWithId) || /*!isOnTheFly || */!DuplicatesIndex.ourEnabled) return ProblemDescriptor.EMPTY_ARRAY;
  final DuplicatesProfile profile = DuplicatesIndex.findDuplicatesProfile(psiFile.getFileType());
  if (profile == null) return ProblemDescriptor.EMPTY_ARRAY;


  final FileASTNode node = psiFile.getNode();
  boolean usingLightProfile = profile instanceof LightDuplicateProfile &&
                              node.getElementType() instanceof ILightStubFileElementType &&
                              DuplicatesIndex.ourEnabledLightProfiles;
  final Project project = psiFile.getProject();
  DuplicatedCodeProcessor<?> processor;
  if (usingLightProfile) {
    processor = processLightDuplicates(node, virtualFile, (LightDuplicateProfile)profile, project);
  }
  else {
    processor = processPsiDuplicates(psiFile, virtualFile, profile, project);
  }
  if (processor == null) return null;

  final SmartList<ProblemDescriptor> descriptors = new SmartList<>();
  final VirtualFile baseDir = project.getBaseDir();
  for (Map.Entry<Integer, TextRange> entry : processor.reportedRanges.entrySet()) {
    final Integer offset = entry.getKey();
    if (!usingLightProfile && processor.fragmentSize.get(offset) < MIN_FRAGMENT_SIZE) continue;
    final VirtualFile file = processor.reportedFiles.get(offset);
    String path = null;

    if (file.equals(virtualFile)) {
      path = "this file";
    }
    else if (baseDir != null) {
      path = VfsUtilCore.getRelativePath(file, baseDir);
    }
    if (path == null) {
      path = file.getPath();
    }
    String message = "Found duplicated code in " + path;

    PsiElement targetElement = processor.reportedPsi.get(offset);
    TextRange rangeInElement = entry.getValue();
    final int offsetInOtherFile = processor.reportedOffsetInOtherFiles.get(offset);

    LocalQuickFix fix = isOnTheFly ? createNavigateToDupeFix(file, offsetInOtherFile) : null;
    long hash = processor.fragmentHash.get(offset);

    int hash2 = (int)(hash >> 32);
    LocalQuickFix viewAllDupesFix = isOnTheFly && hash != 0 ? createShowOtherDupesFix(virtualFile, offset, (int)hash, hash2) : null;

    boolean onlyExtractable = Registry.is("duplicates.inspection.only.extractable");
    LocalQuickFix extractMethodFix =
      (isOnTheFly || onlyExtractable) && hash != 0 ? createExtractMethodFix(targetElement, rangeInElement, (int)hash, hash2) : null;
    if (onlyExtractable) {
      if (extractMethodFix == null) return null;
      if (!isOnTheFly) extractMethodFix = null;
    }

    ProblemDescriptor descriptor = manager
      .createProblemDescriptor(targetElement, rangeInElement, message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly, fix,
                               viewAllDupesFix, extractMethodFix);
    descriptors.add(descriptor);
  }

  return descriptors.isEmpty() ? null : descriptors.toArray(ProblemDescriptor.EMPTY_ARRAY);
}
 
Example 17
Source File: DuplicatesInspectionBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean process(@Nonnull VirtualFile file, TIntArrayList list) {
  for(int i = 0, len = list.size(); i < len; i+=2) {
    ProgressManager.checkCanceled();

    if (list.getQuick(i + 1) != myHash2) continue;
    int offset = list.getQuick(i);

    if (myFileIndex.isInSourceContent(virtualFile)) {
      if (!myFileIndex.isInSourceContent(file)) return true;
      if (!TestSourcesFilter.isTestSources(virtualFile, project) && TestSourcesFilter.isTestSources(file, project)) return true;
      if (mySkipGeneratedCode) {
        if (!myFileWithinGeneratedCode && GeneratedSourcesFilter.isGeneratedSourceByAnyFilter(file, project)) return true;
      }
    } else if (myFileIndex.isInSourceContent(file)) {
      return true;
    }

    final int startOffset = getStartOffset(myNode);
    final int endOffset = getEndOffset(myNode);
    if (file.equals(virtualFile) && offset >= startOffset && offset < endOffset) continue;

    PsiElement target = getPsi(myNode);
    TextRange rangeInElement = getRangeInElement(myNode);

    Integer fragmentStartOffsetInteger = startOffset;
    SortedMap<Integer,TextRange> map = reportedRanges.subMap(fragmentStartOffsetInteger, endOffset);
    int newFragmentSize = !map.isEmpty() ? 0:1;

    Iterator<Integer> iterator = map.keySet().iterator();
    while(iterator.hasNext()) {
      Integer next = iterator.next();
      iterator.remove();
      reportedFiles.remove(next);
      reportedOffsetInOtherFiles.remove(next);
      reportedPsi.remove(next);
      newFragmentSize += fragmentSize.remove(next);
    }

    reportedRanges.put(fragmentStartOffsetInteger, rangeInElement);
    reportedFiles.put(fragmentStartOffsetInteger, file);
    reportedOffsetInOtherFiles.put(fragmentStartOffsetInteger, offset);
    reportedPsi.put(fragmentStartOffsetInteger, target);
    fragmentSize.put(fragmentStartOffsetInteger, newFragmentSize);
    if (newFragmentSize >= MIN_FRAGMENT_SIZE || isLightProfile()) {
      fragmentHash.put(fragmentStartOffsetInteger, (myHash & 0xFFFFFFFFL) | ((long)myHash2 << 32));
    }
    return false;
  }
  return true;
}
 
Example 18
Source File: XDebuggerManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void updateExecutionPoint(@Nonnull VirtualFile file, boolean navigate) {
  if (file.equals(myExecutionPointHighlighter.getCurrentFile())) {
    myExecutionPointHighlighter.update(navigate);
  }
}
 
Example 19
Source File: OpenAndroidModule.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void openOrImportProject(@NotNull VirtualFile projectFile,
                                        @Nullable Project project,
                                        @Nullable VirtualFile sourceFile,
                                        boolean forceOpenInNewFrame) {
  // This is very similar to AndroidOpenFileAction.openOrImportProject().
  if (canImportAsGradleProject(projectFile)) {
    VirtualFile target = findGradleTarget(projectFile);
    if (target != null) {
      Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
      if (openProjects.length > 0) {
        int exitCode = forceOpenInNewFrame ? GeneralSettings.OPEN_PROJECT_NEW_WINDOW : confirmOpenNewProject(false);
        if (exitCode == GeneralSettings.OPEN_PROJECT_SAME_WINDOW) {
          Project toClose = ((project != null) && !project.isDefault()) ? project : openProjects[openProjects.length - 1];
          if (!closeAndDispose(toClose)) {
            return;
          }
        }
        else if (exitCode != GeneralSettings.OPEN_PROJECT_NEW_WINDOW) {
          return;
        }
      }

      GradleProjectImporter gradleImporter = GradleProjectImporter.getInstance();
      gradleImporter.importAndOpenProjectCore(null, true, projectFile);
      for (Project proj : ProjectManager.getInstance().getOpenProjects()) {
        if (projectFile.equals(proj.getBaseDir()) || projectFile.equals(proj.getProjectFile())) {
          if (sourceFile != null && !sourceFile.isDirectory()) {
            OpenFileAction.openFile(sourceFile, proj);
          }
          break;
        }
      }
      return;
    }
  }
  Project newProject = openOrImport(projectFile.getPath(), project, false);
  if (newProject != null) {
    setLastOpenedFile(newProject, projectFile);
    if (sourceFile != null && !sourceFile.isDirectory()) {
      OpenFileAction.openFile(sourceFile, newProject);
    }
  }
}
 
Example 20
Source File: ProjectLevelVcsManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean isFileInBaseDir(final VirtualFile file) {
  VirtualFile parent = file.getParent();
  return !file.isDirectory() && parent != null && parent.equals(myProject.getBaseDir());
}