protected int getCaretOffset() {
   RangeMarker r;
   if (myLocalMarker != null) {
     final PsiReference reference = myExpr != null ? myExpr.getReference() : null;
     if (reference != null && reference.resolve() == myLocalVariable) {
       r = myExprMarker;
     } else {
       r = myLocalMarker;
     }
   } else {
     r = myExprMarker;
   }
   return r != null ? r.getStartOffset() : 0;
 }
  public AddModuleDependencyFix(
      Module currentModule, VirtualFile classVFile, PsiClass[] classes, PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    final Project project = psiElement.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      final Module classModule = fileIndex.getModuleForFile(virtualFile);
      if (classModule != null
          && classModule != currentModule
          && !ModuleRootManager.getInstance(currentModule).isDependsOn(classModule)) {
        myModules.add(classModule);
      }
    }
    myCurrentModule = currentModule;
    myClassVFile = classVFile;
    myClasses = classes;
    myReference = reference;
  }
示例#3
0
  private static List<PsiElement> resolve(final PsiReference ref) {
    // IDEA-56727 try resolve first as in GotoDeclarationAction
    PsiElement resolvedElement = ref.resolve();

    if (resolvedElement == null && ref instanceof PsiPolyVariantReference) {
      List<PsiElement> result = new ArrayList<PsiElement>();
      final ResolveResult[] psiElements = ((PsiPolyVariantReference) ref).multiResolve(false);
      for (ResolveResult resolveResult : psiElements) {
        if (resolveResult.getElement() != null) {
          result.add(resolveResult.getElement());
        }
      }
      return result;
    }
    return resolvedElement == null
        ? Collections.<PsiElement>emptyList()
        : Collections.singletonList(resolvedElement);
  }
  public static void visitRefInDocTag(
      final PsiDocTag tag,
      final JavadocManager manager,
      final PsiElement context,
      final ArrayList<ProblemDescriptor> problems,
      final InspectionManager inspectionManager,
      final boolean onTheFly) {
    final String tagName = tag.getName();
    final PsiDocTagValue value = tag.getValueElement();
    if (value == null) return;
    final JavadocTagInfo info = manager.getTagInfo(tagName);
    if (info != null && !info.isValidInContext(context)) return;
    final String message = info == null || !info.isInline() ? null : info.checkTagValue(value);
    if (message != null) {
      problems.add(createDescriptor(value, message, inspectionManager, onTheFly));
    }

    final PsiReference reference = value.getReference();
    if (reference == null) return;
    final PsiElement element = reference.resolve();
    if (element != null) return;
    final int textOffset = value.getTextOffset();
    if (textOffset == value.getTextRange().getEndOffset()) return;
    final PsiDocTagValue valueElement = tag.getValueElement();
    if (valueElement == null) return;

    final CharSequence paramName =
        value
            .getContainingFile()
            .getViewProvider()
            .getContents()
            .subSequence(textOffset, value.getTextRange().getEndOffset());
    final String params = "<code>" + paramName + "</code>";
    final List<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>();
    if (onTheFly && "param".equals(tagName)) {
      final PsiDocCommentOwner commentOwner =
          PsiTreeUtil.getParentOfType(tag, PsiDocCommentOwner.class);
      if (commentOwner instanceof PsiMethod) {
        final PsiMethod method = (PsiMethod) commentOwner;
        final PsiParameter[] parameters = method.getParameterList().getParameters();
        final PsiDocTag[] tags = tag.getContainingComment().getTags();
        final Set<String> unboundParams = new HashSet<String>();
        for (PsiParameter parameter : parameters) {
          if (!JavaDocLocalInspection.isFound(tags, parameter)) {
            unboundParams.add(parameter.getName());
          }
        }
        if (!unboundParams.isEmpty()) {
          fixes.add(new RenameReferenceQuickFix(unboundParams));
        }
      }
    }
    fixes.add(new RemoveTagFix(tagName, paramName));

    problems.add(
        inspectionManager.createProblemDescriptor(
            valueElement,
            reference.getRangeInElement(),
            cannotResolveSymbolMessage(params),
            ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
            onTheFly,
            fixes.toArray(new LocalQuickFix[fixes.size()])));
  }
示例#5
0
 public InfoSingle(final PsiReference ref, @NotNull final PsiElement targetElement) {
   super(ref.getElement(), ReferenceRange.getAbsoluteRanges(ref));
   myTargetElement = targetElement;
 }
 @Override
 public void annotate(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
   final PsiFile containingFile = element.getContainingFile();
   if (!JavaFxFileTypeFactory.isFxml(containingFile)) return;
   if (element instanceof XmlAttributeValue) {
     final String value = ((XmlAttributeValue) element).getValue();
     if (!JavaFxPsiUtil.isExpressionBinding(value)
         && !JavaFxPsiUtil.isIncorrectExpressionBinding(value)) {
       final PsiReference[] references = element.getReferences();
       for (PsiReference reference : references) {
         if (reference instanceof JavaFxColorReference) {
           attachColorIcon(element, holder, StringUtil.unquoteString(element.getText()));
           continue;
         }
         final PsiElement resolve = reference.resolve();
         if (resolve instanceof PsiMember) {
           if (!JavaFxPsiUtil.isVisibleInFxml((PsiMember) resolve)) {
             final String symbolPresentation =
                 "'" + SymbolPresentationUtil.getSymbolPresentableText(resolve) + "'";
             final Annotation annotation =
                 holder.createErrorAnnotation(
                     element,
                     symbolPresentation
                         + (resolve instanceof PsiClass
                             ? " should be public"
                             : " should be public or annotated with @FXML"));
             if (!(resolve instanceof PsiClass)) {
               annotation.registerUniversalFix(
                   new AddAnnotationFix(
                       JavaFxCommonNames.JAVAFX_FXML_ANNOTATION,
                       (PsiMember) resolve,
                       ArrayUtil.EMPTY_STRING_ARRAY),
                   null,
                   null);
             }
           }
         }
       }
     }
   } else if (element instanceof XmlAttribute) {
     final XmlAttribute attribute = (XmlAttribute) element;
     final String attributeName = attribute.getName();
     if (!FxmlConstants.FX_BUILT_IN_ATTRIBUTES.contains(attributeName)
         && !attribute.isNamespaceDeclaration()
         && JavaFxPsiUtil.isReadOnly(attributeName, attribute.getParent())) {
       holder.createErrorAnnotation(
           element.getNavigationElement(), "Property '" + attributeName + "' is read-only");
     }
     if (FxmlConstants.SOURCE.equals(attributeName)) {
       final XmlAttributeValue valueElement = attribute.getValueElement();
       if (valueElement != null) {
         final XmlTag xmlTag = attribute.getParent();
         if (xmlTag != null) {
           final XmlTag referencedTag = JavaFxBuiltInTagDescriptor.getReferencedTag(xmlTag);
           if (referencedTag != null) {
             if (referencedTag.getTextOffset() > xmlTag.getTextOffset()) {
               holder.createErrorAnnotation(
                   valueElement.getValueTextRange(), valueElement.getValue() + " not found");
             } else if (xmlTag.getParentTag() == referencedTag.getParentTag()) {
               final Annotation annotation =
                   holder.createErrorAnnotation(
                       valueElement.getValueTextRange(), "Duplicate child added");
               annotation.registerFix(
                   new JavaFxWrapWithDefineIntention(referencedTag, valueElement.getValue()));
             }
           }
         }
       }
     }
   } else if (element instanceof XmlTag) {
     if (FxmlConstants.FX_SCRIPT.equals(((XmlTag) element).getName())) {
       final XmlTagValue tagValue = ((XmlTag) element).getValue();
       if (!StringUtil.isEmptyOrSpaces(tagValue.getText())) {
         final List<String> langs =
             JavaFxPsiUtil.parseInjectedLanguages((XmlFile) element.getContainingFile());
         if (langs.isEmpty()) {
           final ASTNode openTag = element.getNode().findChildByType(XmlTokenType.XML_NAME);
           final Annotation annotation =
               holder.createErrorAnnotation(
                   openTag != null ? openTag.getPsi() : element, "Page language not specified.");
           annotation.registerFix(new JavaFxInjectPageLanguageIntention());
         }
       }
     }
   }
 }
  @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;
  }
 @Override
 public void rename(String newName) throws IncorrectOperationException {
   final PsiReference reference = getUsageInfo().getReference();
   assert reference != null : this;
   reference.handleElementRename(newName);
 }
 public static boolean isResolved(PsiReference reference) {
   if (reference instanceof PsiPolyVariantReference) {
     return ((PsiPolyVariantReference) reference).multiResolve(false).length > 0;
   }
   return reference.resolve() != null;
 }