@Override
  public boolean canHandle(RunConfiguration configuration, Project project) {
    if (!(configuration instanceof JUnitConfiguration)) {
      return false;
    } else {
      JUnitConfiguration.Data data = ((JUnitConfiguration) configuration).getPersistentData();

      String mainClassName = data.getMainClassName();
      String methodName = data.getMethodName();

      JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
      PsiClass referencedClass =
          psiFacade.findClass(mainClassName, GlobalSearchScope.allScope(project));
      PsiFile containingFile = referencedClass.getContainingFile();

      FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
      if (methodName == null) {
        fileEditorManager.openFile(containingFile.getVirtualFile(), true);
      } else {
        PsiMethod[] methodsByName = referencedClass.findMethodsByName(methodName, false);

        fileEditorManager.openEditor(
            new OpenFileDescriptor(
                project, containingFile.getVirtualFile(), methodsByName[0].getTextOffset()),
            true);
      }
      return true;
    }
  }
  /**
   * Scroll to the error specified by the given tree path, or do nothing if no error is specified.
   *
   * @param treePath the tree path to scroll to.
   */
  private void scrollToError(final TreePath treePath) {
    final DefaultMutableTreeNode treeNode =
        (DefaultMutableTreeNode) treePath.getLastPathComponent();
    if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode)) {
      return;
    }

    final ResultTreeNode nodeInfo = (ResultTreeNode) treeNode.getUserObject();
    if (nodeInfo.getFile() == null || nodeInfo.getProblem() == null) {
      return; // no problem here :-)
    }

    final VirtualFile virtualFile = nodeInfo.getFile().getVirtualFile();
    if (virtualFile == null || !virtualFile.exists()) {
      return;
    }

    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    final FileEditor[] editor = fileEditorManager.openFile(virtualFile, true);

    if (editor.length > 0 && editor[0] instanceof TextEditor) {
      final LogicalPosition problemPos =
          new LogicalPosition(Math.max(lineFor(nodeInfo) - 1, 0), Math.max(columnFor(nodeInfo), 0));

      final Editor textEditor = ((TextEditor) editor[0]).getEditor();
      textEditor.getCaretModel().moveToLogicalPosition(problemPos);
      textEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
    }
  }
 private void openFilesInEditor(final List<FilePath> allCommittedFilePathsInSelection) {
   FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
   boolean areTabsAllowed =
       UISettings.getInstance().EDITOR_TAB_LIMIT >= allCommittedFilePathsInSelection.size();
   if (areTabsAllowed)
     allCommittedFilePathsInSelection
         .stream()
         .filter(filePath -> filePath != null)
         .forEach(filepath -> fileEditorManager.openFile(filepath.getVirtualFile(), true));
   else
     displayErrorMessage(
         "Tab limit reached",
         "Please increase Tab Limit for the editor in\nFile -> Settings -> Editor -> General -> Editor Tabs");
 }
Пример #4
0
  private static boolean activatePsiElementIfOpen(
      @NotNull PsiElement elt, boolean searchForOpen, boolean requestFocus) {
    if (!elt.isValid()) return false;
    elt = elt.getNavigationElement();
    final PsiFile file = elt.getContainingFile();
    if (file == null || !file.isValid()) return false;

    VirtualFile vFile = file.getVirtualFile();
    if (vFile == null) return false;

    if (!EditorHistoryManager.getInstance(elt.getProject()).hasBeenOpen(vFile)) return false;

    final FileEditorManager fem = FileEditorManager.getInstance(elt.getProject());
    if (!fem.isFileOpen(vFile)) {
      fem.openFile(vFile, requestFocus, searchForOpen);
    }

    final TextRange range = elt.getTextRange();
    if (range == null) return false;

    final FileEditor[] editors = fem.getEditors(vFile);
    for (FileEditor editor : editors) {
      if (editor instanceof TextEditor) {
        final Editor text = ((TextEditor) editor).getEditor();
        final int offset = text.getCaretModel().getOffset();

        if (range.containsOffset(offset)) {
          // select the file
          fem.openFile(vFile, requestFocus, searchForOpen);
          return true;
        }
      }
    }

    return false;
  }