@Override
    public final void actionPerformed(final AnActionEvent event) {
      final DataContext dataContext = event.getDataContext();
      final HierarchyBrowserBaseEx browser =
          (HierarchyBrowserBaseEx) dataContext.getData(myBrowserDataKey);
      if (browser == null) return;

      final PsiElement selectedElement = browser.getSelectedElement();
      if (selectedElement == null || !browser.isApplicableElement(selectedElement)) return;

      final String currentViewType = browser.myCurrentViewType;
      Disposer.dispose(browser);
      final HierarchyProvider provider =
          BrowseHierarchyActionBase.findProvider(
              myProviderLanguageExtension,
              selectedElement,
              selectedElement.getContainingFile(),
              event.getDataContext());
      final HierarchyBrowser newBrowser =
          BrowseHierarchyActionBase.createAndAddToPanel(
              selectedElement.getProject(), provider, selectedElement);
      ApplicationManager.getApplication()
          .invokeLater(
              () ->
                  ((HierarchyBrowserBaseEx) newBrowser)
                      .changeView(correctViewType(browser, currentViewType)));
    }
  private void updateEditorText() {
    disposeNonTextEditor();

    final PsiElement elt = myElements[myIndex].getNavigationElement();
    Project project = elt.getProject();
    PsiFile psiFile = getContainingFile(elt);
    final VirtualFile vFile = psiFile.getVirtualFile();
    if (vFile == null) return;
    final FileEditorProvider[] providers =
        FileEditorProviderManager.getInstance().getProviders(project, vFile);
    for (FileEditorProvider provider : providers) {
      if (provider instanceof TextEditorProvider) {
        updateTextElement(elt);
        myBinarySwitch.show(myViewingPanel, TEXT_PAGE_KEY);
        break;
      } else if (provider.accept(project, vFile)) {
        myCurrentNonTextEditorProvider = provider;
        myNonTextEditor = myCurrentNonTextEditorProvider.createEditor(project, vFile);
        myBinaryPanel.removeAll();
        myBinaryPanel.add(myNonTextEditor.getComponent());
        myBinarySwitch.show(myViewingPanel, BINARY_PAGE_KEY);
        break;
      }
    }
  }
Exemplo n.º 3
0
 @Nullable
 public String getInfo() {
   try {
     return generateInfo(myTargetElement, myElementAtPointer);
   } catch (IndexNotReadyException e) {
     showDumbModeNotification(myTargetElement.getProject());
     return null;
   }
 }
Exemplo n.º 4
0
 @Override
 @NotNull
 public DocInfo getInfo() {
   AccessToken token = ReadAction.start();
   try {
     return generateInfo(myTargetElement, myElementAtPointer);
   } catch (IndexNotReadyException e) {
     showDumbModeNotification(myTargetElement.getProject());
     return DocInfo.EMPTY;
   } finally {
     token.finish();
   }
 }
 private void doWriteAction(final ImportCandidateHolder item) {
   PsiElement src = item.getImportable();
   new WriteCommandAction(
       src.getProject(), PyBundle.message("ACT.CMD.use.import"), myTarget.getContainingFile()) {
     @Override
     protected void run(@NotNull Result result) throws Throwable {
       doIt(item);
     }
   }.execute();
   if (myOnDoneCallback != null) {
     myOnDoneCallback.run();
   }
 }
 @Override
 public void actionPerformed(AnActionEvent e) {
   PsiElement element = myElements[myIndex];
   PsiElement navigationElement = element.getNavigationElement();
   PsiFile file = getContainingFile(navigationElement);
   if (file == null) return;
   VirtualFile virtualFile = file.getVirtualFile();
   if (virtualFile == null) return;
   Project project = element.getProject();
   FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
   OpenFileDescriptor descriptor =
       new OpenFileDescriptor(project, virtualFile, navigationElement.getTextOffset());
   fileEditorManager.openTextEditor(descriptor, myFocusEditor);
 }
 private void addToExistingImport(PyImportElement src) {
   final PyElementGenerator gen = PyElementGenerator.getInstance(myTarget.getProject());
   // did user choose 'import' or 'from import'?
   PsiElement parent = src.getParent();
   if (parent instanceof PyFromImportStatement) {
     // add another import element right after the one we got
     PsiElement newImportElement = gen.createImportElement(LanguageLevel.getDefault(), myName);
     parent.add(newImportElement);
   } else { // just 'import'
     // all we need is to qualify our target
     myTarget.replace(
         gen.createExpressionFromText(
             LanguageLevel.forElement(myTarget), src.getVisibleName() + "." + myName));
   }
 }
  public static String getNewText(PsiElement elt) {
    Project project = elt.getProject();
    PsiFile psiFile = getContainingFile(elt);

    final Document doc = PsiDocumentManager.getInstance(project).getDocument(psiFile);
    if (doc == null) return null;

    final ImplementationTextSelectioner implementationTextSelectioner =
        LanguageImplementationTextSelectioner.INSTANCE.forLanguage(elt.getLanguage());
    int start = implementationTextSelectioner.getTextStartOffset(elt);
    final int end = implementationTextSelectioner.getTextEndOffset(elt);

    final int lineStart = doc.getLineStartOffset(doc.getLineNumber(start));
    final int lineEnd =
        end < doc.getTextLength()
            ? doc.getLineEndOffset(doc.getLineNumber(end))
            : doc.getTextLength();
    return doc.getCharsSequence().subSequence(lineStart, lineEnd).toString();
  }
 private void addImportStatement(ImportCandidateHolder item) {
   final Project project = myTarget.getProject();
   final PyElementGenerator gen = PyElementGenerator.getInstance(project);
   AddImportHelper.ImportPriority priority =
       AddImportHelper.getImportPriority(myTarget, item.getFile());
   PsiFile file = myTarget.getContainingFile();
   InjectedLanguageManager manager = InjectedLanguageManager.getInstance(project);
   if (manager.isInjectedFragment(file)) {
     file = manager.getTopLevelFile(myTarget);
   }
   // We are trying to import top-level module or package which thus cannot be qualified
   if (isRoot(item.getFile())) {
     if (myImportLocally) {
       AddImportHelper.addLocalImportStatement(myTarget, myName);
     } else {
       AddImportHelper.addImportStatement(file, myName, item.getAsName(), priority, null);
     }
   } else {
     final QualifiedName path = item.getPath();
     final String qualifiedName = path != null ? path.toString() : "";
     if (myUseQualifiedImport) {
       String nameToImport = qualifiedName;
       if (item.getImportable() instanceof PsiFileSystemItem) {
         nameToImport += "." + myName;
       }
       if (myImportLocally) {
         AddImportHelper.addLocalImportStatement(myTarget, nameToImport);
       } else {
         AddImportHelper.addImportStatement(file, nameToImport, item.getAsName(), priority, null);
       }
       myTarget.replace(
           gen.createExpressionFromText(
               LanguageLevel.forElement(myTarget), qualifiedName + "." + myName));
     } else {
       if (myImportLocally) {
         AddImportHelper.addLocalFromImportStatement(myTarget, qualifiedName, myName);
       } else {
         AddImportHelper.addFromImportStatement(
             file, qualifiedName, myName, item.getAsName(), priority, null);
       }
     }
   }
 }
 /**
  * Alters either target (by qualifying a name) or source (by explicitly importing the name).
  *
  * @return true if action succeeded
  */
 public boolean execute() {
   // check if the tree is sane
   PsiDocumentManager.getInstance(myTarget.getProject()).commitAllDocuments();
   if (!myTarget.isValid()) return false;
   if ((myTarget instanceof PyQualifiedExpression)
       && ((((PyQualifiedExpression) myTarget).isQualified())))
     return false; // we cannot be qualified
   for (ImportCandidateHolder item : mySources) {
     if (!item.getImportable().isValid()) return false;
     if (!item.getFile().isValid()) return false;
     if (item.getImportElement() != null && !item.getImportElement().isValid()) return false;
   }
   if (mySources.isEmpty()) {
     return false;
   }
   // act
   if (mySources.size() > 1) {
     selectSourceAndDo();
   } else doWriteAction(mySources.get(0));
   return true;
 }
Exemplo n.º 11
0
  @Override
  public JComponent getPreviewComponent(@NotNull PsiElement element) {
    final PsiNewExpression psiNewExpression =
        PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);

    if (psiNewExpression != null) {
      final PsiJavaCodeReferenceElement referenceElement =
          PsiTreeUtil.getChildOfType(psiNewExpression, PsiJavaCodeReferenceElement.class);

      if (referenceElement != null) {
        final PsiReference reference = referenceElement.getReference();

        if (reference != null) {
          final PsiElement psiElement = reference.resolve();

          if (psiElement instanceof PsiClass
              && "java.awt.Color".equals(((PsiClass) psiElement).getQualifiedName())) {
            final PsiExpressionList argumentList = psiNewExpression.getArgumentList();

            if (argumentList != null) {
              final PsiExpression[] expressions = argumentList.getExpressions();
              int[] values = ArrayUtil.newIntArray(expressions.length);
              float[] values2 = new float[expressions.length];
              int i = 0;
              int j = 0;

              final PsiConstantEvaluationHelper helper =
                  JavaPsiFacade.getInstance(element.getProject()).getConstantEvaluationHelper();
              for (final PsiExpression each : expressions) {
                final Object o = helper.computeConstantExpression(each);
                if (o instanceof Integer) {
                  values[i] = ((Integer) o).intValue();
                  if (expressions.length != 1) {
                    values[i] = values[i] > 255 ? 255 : values[i] < 0 ? 0 : values[i];
                  }

                  i++;
                } else if (o instanceof Float) {
                  values2[j] = ((Float) o).floatValue();
                  values2[j] = values2[j] > 1 ? 1 : values2[j] < 0 ? 0 : values2[j];
                  j++;
                }
              }

              Color c = null;
              if (i == expressions.length) {
                if (i == 1 && values[0] > 255) {
                  c = new Color(values[0]);
                } else {
                  switch (values.length) {
                    case 1:
                      c = new Color(values[0]);
                      break;
                    case 3:
                      c = new Color(values[0], values[1], values[2]);
                      break;
                    case 4:
                      c = new Color(values[0], values[1], values[2], values[3]);
                      break;
                    default:
                      break;
                  }
                }
              } else if (j == expressions.length) {
                switch (values2.length) {
                  case 3:
                    c = new Color(values2[0], values2[1], values2[2]);
                    break;
                  case 4:
                    c = new Color(values2[0], values2[1], values2[2], values2[3]);
                    break;
                  default:
                    break;
                }
              }

              if (c != null) {
                return new ColorPreviewComponent(c);
              }
            }
          }
        }
      }
    }

    if (ColorChooserIntentionAction.isInsideDecodeOrGetColorMethod(element)) {
      final String color = StringUtil.unquoteString(element.getText());
      try {
        return new ColorPreviewComponent(Color.decode(color));
      } catch (NumberFormatException ignore) {
      }
    }

    if (PlatformPatterns.psiElement(PsiIdentifier.class)
        .withParent(PlatformPatterns.psiElement(PsiReferenceExpression.class))
        .accepts(element)) {
      final PsiReference reference = element.getParent().getReference();

      if (reference != null) {
        final PsiElement psiElement = reference.resolve();

        if (psiElement instanceof PsiField) {
          if ("java.awt.Color"
              .equals(((PsiField) psiElement).getContainingClass().getQualifiedName())) {
            final String colorName =
                ((PsiField) psiElement).getName().toLowerCase().replace("_", "");
            final String hex = ColorSampleLookupValue.getHexCodeForColorName(colorName);
            return new ColorPreviewComponent(Color.decode("0x" + hex.substring(1)));
          }
        }
      }
    }

    if (PlatformPatterns.psiElement()
        .withParent(PlatformPatterns.psiElement(PsiLiteralExpression.class))
        .accepts(element)) {
      final PsiLiteralExpression psiLiteralExpression = (PsiLiteralExpression) element.getParent();
      if (psiLiteralExpression != null) {
        return ImagePreviewComponent.getPreviewComponent(psiLiteralExpression);
      }
    }

    return null;
  }