Java Code Examples for com.intellij.usages.rules.UsageInFile

The following examples show how to use com.intellij.usages.rules.UsageInFile. 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
private Color getBackgroundColor(boolean isSelected, Usage usage) {
  Color fileBgColor = null;
  if (isSelected) {
    fileBgColor = UIUtil.getListSelectionBackground();
  } else {
    VirtualFile virtualFile =
        usage instanceof UsageInFile ? ((UsageInFile) usage).getFile() : null;
    if (virtualFile != null) {
      Project project = myUsageView.getProject();
      PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
      if (psiFile != null && psiFile.isValid()) {
        final Color color = FileColorManager.getInstance(project).getRendererBackground(psiFile);
        if (color != null) fileBgColor = color;
      }
    }
  }
  return fileBgColor;
}
 
Example 2
Source Project: consulo   Source File: DirectoryGroupingRule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@javax.annotation.Nullable
public UsageGroup groupUsage(@Nonnull Usage usage) {
  if (usage instanceof UsageInFile) {
    UsageInFile usageInFile = (UsageInFile)usage;
    VirtualFile file = usageInFile.getFile();
    if (file != null) {
      if (file instanceof VirtualFileWindow) {
        file = ((VirtualFileWindow)file).getDelegate();
      }
      VirtualFile dir = file.getParent();
      if (dir == null) return null;
      return getGroupForFile(dir);
    }
  }
  return null;
}
 
Example 3
Source Project: consulo   Source File: NonCodeUsageGroupingRule.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public UsageGroup groupUsage(@Nonnull Usage usage) {
  if (usage instanceof UsageInFile) {
    VirtualFile file = ((UsageInFile)usage).getFile();
    if (file != null) {
      if (GeneratedSourcesFilter.isGenerated(myProject, file)) {
        return UsageInGeneratedCodeGroup.INSTANCE;
      }
    }
  }
  if (usage instanceof PsiElementUsage) {
    if (usage instanceof UsageInfo2UsageAdapter) {
      final UsageInfo usageInfo = ((UsageInfo2UsageAdapter)usage).getUsageInfo();
      if (usageInfo.isDynamicUsage()) {
        return DynamicUsageGroup.INSTANCE;
      }
    }
    if (((PsiElementUsage)usage).isNonCodeUsage()) {
      return NonCodeUsageGroup.INSTANCE;
    }
    else {
      return CodeUsageGroup.INSTANCE;
    }
  }
  return null;
}
 
Example 4
Source Project: consulo   Source File: ReplaceInProjectManager.java    License: Apache License 2.0 6 votes vote down vote up
private boolean ensureUsagesWritable(ReplaceContext replaceContext, Collection<? extends Usage> selectedUsages) {
  Set<VirtualFile> readOnlyFiles = null;
  for (final Usage usage : selectedUsages) {
    final VirtualFile file = ((UsageInFile)usage).getFile();

    if (file != null && !file.isWritable()) {
      if (readOnlyFiles == null) readOnlyFiles = new HashSet<>();
      readOnlyFiles.add(file);
    }
  }

  if (readOnlyFiles != null) {
    ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(readOnlyFiles);
  }

  if (hasReadOnlyUsages(selectedUsages)) {
    int result = Messages.showOkCancelDialog(replaceContext.getUsageView().getComponent(), FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"),
                                             FindBundle.message("find.replace.occurrences.in.read.only.files.title"), Messages.getWarningIcon());
    if (result != Messages.OK) {
      return false;
    }
  }
  return true;
}
 
Example 5
Source Project: consulo   Source File: ShowUsagesTableCellRenderer.java    License: Apache License 2.0 6 votes vote down vote up
private Color getBackgroundColor(boolean isSelected, Usage usage) {
  Color fileBgColor = null;
  if (isSelected) {
    fileBgColor = UIUtil.getListSelectionBackground();
  }
  else {
    VirtualFile virtualFile = usage instanceof UsageInFile ? ((UsageInFile)usage).getFile() : null;
    if (virtualFile != null) {
      Project project = myUsageView.getProject();
      PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
      if (psiFile != null && psiFile.isValid()) {
        final Color color = FileColorManager.getInstance(project).getRendererBackground(psiFile);
        if (color != null) fileBgColor = color;
      }
    }
  }
  return fileBgColor;
}
 
Example 6
private Color getBackgroundColor(boolean isSelected, Usage usage) {
  Color fileBgColor = null;
  if (isSelected) {
    fileBgColor = UIUtil.getListSelectionBackground();
  }
  else {
    VirtualFile virtualFile = usage instanceof UsageInFile ? ((UsageInFile)usage).getFile() : null;
    if (virtualFile != null) {
      Project project = myUsageView.getProject();
      PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
      if (psiFile != null && psiFile.isValid()) {
        final Color color = FileColorManager.getInstance(project).getRendererBackground(psiFile);
        if (color != null) fileBgColor = color;
      }
    }
  }
  return fileBgColor;
}
 
Example 7
Source Project: intellij   Source File: BuildFileGroupingRule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public UsageGroup getParentGroupFor(Usage usage, UsageTarget[] targets) {
  // give the delegate a chance to refuse the usage
  UsageGroup base = delegate.getParentGroupFor(usage, targets);
  if (base == null || !(usage instanceof UsageInFile)) {
    return null;
  }
  VirtualFile vf = ((UsageInFile) usage).getFile();
  if (vf == null || vf.getFileType() != BuildFileType.INSTANCE) {
    return base;
  }
  return new FileUsageGroup(project, vf) {
    String name;

    @Override
    public void update() {
      super.update();
      if (isValid()) {
        name = BuildFile.getBuildFileString(project, vf.getPath());
      }
    }

    @Override
    public String getPresentableName() {
      return name;
    }

    @Override
    public String getText(UsageView view) {
      return name;
    }
  };
}
 
Example 8
Source Project: consulo   Source File: FileGroupingRule.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public UsageGroup getParentGroupFor(@Nonnull Usage usage, @Nonnull UsageTarget[] targets) {
  VirtualFile virtualFile;
  if (usage instanceof UsageInFile && (virtualFile = ((UsageInFile)usage).getFile()) != null) {
    return new FileUsageGroup(myProject, virtualFile);
  }
  return null;
}
 
Example 9
Source Project: consulo   Source File: UsageViewManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isInScope(@Nonnull Usage usage, @Nonnull SearchScope searchScope) {
  PsiElement element = null;
  VirtualFile file =
          usage instanceof UsageInFile ? ((UsageInFile)usage).getFile() : usage instanceof PsiElementUsage ? PsiUtilCore.getVirtualFile(element = ((PsiElementUsage)usage).getElement()) : null;
  if (file != null) {
    return isFileInScope(file, searchScope);
  }
  return element != null && (searchScope instanceof EverythingGlobalScope || searchScope instanceof ProjectScopeImpl || searchScope instanceof ProjectAndLibrariesScope);
}
 
Example 10
Source Project: consulo   Source File: ReplaceInProjectManager.java    License: Apache License 2.0 4 votes vote down vote up
private boolean replaceUsages(@Nonnull ReplaceContext replaceContext, @Nonnull Collection<Usage> usages) {
  if (!ensureUsagesWritable(replaceContext, usages)) {
    return true;
  }

  int[] replacedCount = {0};
  final boolean[] success = {true};

  success[0] &= ((ApplicationEx)ApplicationManager.getApplication())
          .runWriteActionWithCancellableProgressInDispatchThread(FindBundle.message("find.replace.all.confirmation.title"), myProject, null, indicator -> {
            indicator.setIndeterminate(false);
            int processed = 0;
            VirtualFile lastFile = null;

            for (final Usage usage : usages) {
              ++processed;
              indicator.checkCanceled();
              indicator.setFraction((float)processed / usages.size());

              if (usage instanceof UsageInFile) {
                VirtualFile virtualFile = ((UsageInFile)usage).getFile();
                if (virtualFile != null && !virtualFile.equals(lastFile)) {
                  indicator.setText2(virtualFile.getPresentableUrl());
                  lastFile = virtualFile;
                }
              }

              ProgressManager.getInstance().executeNonCancelableSection(() -> {
                try {
                  if (replaceUsage(usage, replaceContext.getFindModel(), replaceContext.getExcludedSetCached(), false)) {
                    replacedCount[0]++;
                  }
                }
                catch (FindManager.MalformedReplacementStringException ex) {
                  markAsMalformedReplacement(replaceContext, usage);
                  success[0] = false;
                }
              });
            }
          });

  replaceContext.getUsageView().removeUsagesBulk(usages);
  reportNumberReplacedOccurrences(myProject, replacedCount[0]);
  return success[0];
}
 
Example 11
Source Project: consulo   Source File: UsageListCellRenderer.java    License: Apache License 2.0 4 votes vote down vote up
public static VirtualFile getVirtualFile(final Usage usage) {
  return usage instanceof UsageInFile ? ((UsageInFile)usage).getFile() : null;
}