@Nullable
  private static PsiElement getElementToCopy(
      @Nullable final Editor editor, final DataContext dataContext) {
    PsiElement element = null;
    if (editor != null) {
      PsiReference reference = TargetElementUtilBase.findReference(editor);
      if (reference != null) {
        element = reference.getElement();
      }
    }

    if (element == null) {
      element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
    }
    if (element == null && editor == null) {
      VirtualFile virtualFile = PlatformDataKeys.VIRTUAL_FILE.getData(dataContext);
      Project project = PlatformDataKeys.PROJECT.getData(dataContext);
      if (virtualFile != null && project != null) {
        element = PsiManager.getInstance(project).findFile(virtualFile);
      }
    }
    if (element instanceof PsiFile && !((PsiFile) element).getViewProvider().isPhysical()) {
      return null;
    }

    for (QualifiedNameProvider provider : Extensions.getExtensions(QualifiedNameProvider.EP_NAME)) {
      PsiElement adjustedElement = provider.adjustElementToCopy(element);
      if (adjustedElement != null) return adjustedElement;
    }
    return element;
  }
 public void invoke(
     @NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
   editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
   PsiElement element = findTargetMember(file, editor);
   if (element == null) {
     element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
   }
   invokeOnElement(project, editor, element);
 }
 @Nullable
 private static PsiElement[] getPsiElements(DataContext dataContext) {
   PsiElement[] elements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
   if (elements == null) {
     final Object data = LangDataKeys.PSI_ELEMENT.getData(dataContext);
     if (data != null) {
       elements = new PsiElement[] {(PsiElement) data};
     } else {
       final Object data1 = LangDataKeys.PSI_FILE.getData(dataContext);
       if (data1 != null) {
         elements = new PsiElement[] {(PsiFile) data1};
       }
     }
   }
   return elements;
 }
  @Nullable
  private PsiElement getSymbolPsiElement(DataContext dataContext) {
    final PsiElement firstPsiElement = LangDataKeys.PSI_ELEMENT.getData(dataContext);
    return SymbolUtils.getSymbolElement(firstPsiElement);

    //        TODO: fix rename for JSON string values
    //        final Caret caret = LangDataKeys.CARET.getData(dataContext);
    //        final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(dataContext);
    //
    //        if(caret == null || psiFile == null) return null;
    //
    //        final int caretPosition = caret.getOffset();
    //        final PsiElement psiElement = psiFile.findElementAt(caretPosition);
    //
    //        return SymbolUtils.getSymbolElement(psiElement);
  }
  @Nullable
  private static PsiMethod getMethodImpl(final DataContext dataContext) {
    final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    if (project == null) return null;

    PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
    final PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false);

    if (method != null) {
      return method;
    }

    final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
    if (editor == null) {
      return null;
    }

    final PsiFile psiFile =
        PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null) {
      return null;
    }

    PsiDocumentManager.getInstance(project).commitAllDocuments();

    final int offset = editor.getCaretModel().getOffset();
    if (offset < 1) {
      return null;
    }

    element = psiFile.findElementAt(offset);
    if (!(element instanceof PsiWhiteSpace)) {
      return null;
    }

    element = psiFile.findElementAt(offset - 1);
    if (!(element instanceof PsiJavaToken)
        || ((PsiJavaToken) element).getTokenType() != JavaTokenType.SEMICOLON) {
      return null;
    }

    return PsiTreeUtil.getParentOfType(element, PsiMethod.class, false);
  }
  @Override
  public void invoke(
      @NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    PsiElement element = findTargetMember(file, editor);
    if (element == null) {
      element = LangDataKeys.PSI_ELEMENT.getData(dataContext);
    }

    if (element != null) {
      JetChangeSignatureDialog dialog =
          createDialog(
              element, file.findElementAt(editor.getCaretModel().getOffset()), project, editor);

      if (dialog != null) {
        dialog.show();
      }
    }
  }
  @Override
  protected void gotoActionPerformed(final AnActionEvent e) {
    final Project project = e.getData(PlatformDataKeys.PROJECT);
    if (project == null) return;

    PsiDocumentManager.getInstance(project).commitAllDocuments();

    final PsiElement psiElement = LangDataKeys.PSI_ELEMENT.getData(e.getDataContext());
    final PsiFile psiFile = LangDataKeys.PSI_FILE.getData(e.getDataContext());
    final VirtualFile virtualFile = PlatformDataKeys.VIRTUAL_FILE.getData(e.getDataContext());

    FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.goto.inspection");

    final GotoInspectionModel model = new GotoInspectionModel(project);
    showNavigationPopup(
        e,
        model,
        new GotoActionCallback<Object>() {
          @Override
          protected ChooseByNameFilter<Object> createFilter(@NotNull ChooseByNamePopup popup) {
            popup.setSearchInAnyPlace(true);
            return super.createFilter(popup);
          }

          @Override
          public void elementChosen(ChooseByNamePopup popup, final Object element) {
            ApplicationManager.getApplication()
                .invokeLater(
                    new Runnable() {
                      @Override
                      public void run() {
                        runInspection(
                            project,
                            (InspectionToolWrapper) element,
                            virtualFile,
                            psiElement,
                            psiFile);
                      }
                    });
          }
        });
  }
Exemplo n.º 8
0
 public Object getData(DataProvider dataProvider) {
   return LangDataKeys.PSI_ELEMENT.getData(dataProvider);
 }