Search in sources :

Example 1 with BeanPropertyPathReferenceSet

use of com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet in project intellij-plugins by JetBrains.

the class InterceptorRefInStackParamNameCustomConverter method getCustomReferences.

@NotNull
@Override
public PsiReference[] getCustomReferences(final XmlAttributeValue nameAttributeValue, final DomElement paramsElement) {
    if (!(paramsElement instanceof InterceptorRef)) {
        return PsiReference.EMPTY_ARRAY;
    }
    InterceptorRef interceptorRef = (InterceptorRef) paramsElement;
    final InterceptorOrStackBase value = interceptorRef.getName().getValue();
    if (!(value instanceof InterceptorStack)) {
        return PsiReference.EMPTY_ARRAY;
    }
    final String text = nameAttributeValue.getValue();
    final boolean hasDot = StringUtil.containsChar(text, '.');
    final int idx = hasDot ? text.indexOf('.') : text.length();
    final String refName = text.substring(0, idx);
    final InterceptorStack stack = (InterceptorStack) value;
    final InterceptorRef resolvedInterceptorRef = ContainerUtil.find(stack.getInterceptorRefs(), ref -> Comparing.strEqual(refName, ref.getName().getStringValue()));
    final List<PsiReference> customReferences = new ArrayList<>(2);
    customReferences.add(new InterceptorRefPsiReference(nameAttributeValue, TextRange.from(1, idx), resolvedInterceptorRef, stack));
    if (resolvedInterceptorRef == null) {
        return ArrayUtil.toObjectArray(customReferences, PsiReference.class);
    }
    final String propertyText = text.substring(idx + 1, text.length());
    final PsiClass paramsClass = resolvedInterceptorRef.getParamsClass();
    final BeanPropertyPathReferenceSet beanPropertyPathReferenceSet = new BeanPropertyPathReferenceSet(propertyText, nameAttributeValue, idx + 2, '.', paramsClass, false);
    Collections.addAll(customReferences, beanPropertyPathReferenceSet.getPsiReferences());
    return ArrayUtil.toObjectArray(customReferences, PsiReference.class);
}
Also used : InterceptorOrStackBase(com.intellij.struts2.dom.struts.strutspackage.InterceptorOrStackBase) InterceptorStack(com.intellij.struts2.dom.struts.strutspackage.InterceptorStack) ArrayList(java.util.ArrayList) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) InterceptorRef(com.intellij.struts2.dom.struts.strutspackage.InterceptorRef) BeanPropertyPathReferenceSet(com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with BeanPropertyPathReferenceSet

use of com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet in project intellij-plugins by JetBrains.

the class ResultActionPropertyReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull final PsiElement psiElement, @NotNull final ProcessingContext processingContext) {
    final Result result = (Result) DomUtil.getDomElement(psiElement);
    assert result != null : psiElement.getText();
    final Action action = result.getParentOfType(Action.class, true);
    assert action != null : psiElement.getText();
    final PsiClass actionClass = action.searchActionClass();
    if (actionClass == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final int tagValueStartOffset = ElementManipulators.getOffsetInElement(result.getXmlTag());
    PsiReference[] references = new PsiReference[1];
    final String stringValue = result.getStringValue();
    if (!StringUtil.isNotEmpty(stringValue)) {
        return PsiReference.EMPTY_ARRAY;
    }
    final String resultText = StringUtil.replace(stringValue, "&", "&amp;");
    final int lastExpressionEnd = // missing '}'
    Math.max(// missing '}'
    resultText.length(), resultText.lastIndexOf(EXPRESSION_START));
    int startOffset = 0;
    while (startOffset < lastExpressionEnd) {
        startOffset = resultText.indexOf(EXPRESSION_START, startOffset);
        if (startOffset == -1) {
            break;
        }
        startOffset += EXPRESSION_START.length();
        final int closingBraceIdx = resultText.indexOf(EXPRESSION_END, startOffset);
        final int length = (closingBraceIdx != -1 ? closingBraceIdx : resultText.length()) - startOffset;
        final String expressionString = resultText.substring(startOffset, startOffset + length);
        // we only "fake" OGNL here, skip method call expressions for now
        if (StringUtil.containsChar(expressionString, '(')) {
            continue;
        }
        final BeanPropertyPathReferenceSet propertyPathReferenceSet = new BeanPropertyPathReferenceSet(expressionString, psiElement, startOffset, '.', actionClass, true) {

            // CTOR creates references eagerly, so we have to subclass here
            @Override
            public boolean isSoft() {
                return false;
            }

            @NotNull
            @Override
            protected BeanPropertyPathReference createReference(final TextRange range, final int index) {
                final TextRange shift = TextRange.from(range.getStartOffset() + tagValueStartOffset, // shift range to XmlTag value range
                range.getLength());
                return createBeanPropertyPathReference(shift, index);
            }
        };
        references = ArrayUtil.mergeArrays(references, propertyPathReferenceSet.getPsiReferences());
    }
    return references;
}
Also used : Action(com.intellij.struts2.dom.struts.action.Action) TextRange(com.intellij.openapi.util.TextRange) BeanPropertyPathReferenceSet(com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet) Result(com.intellij.struts2.dom.struts.action.Result) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with BeanPropertyPathReferenceSet

use of com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet in project intellij-plugins by JetBrains.

the class ParamNameConverterImpl method createReferences.

@NotNull
public PsiReference[] createReferences(final GenericDomValue<List<BeanProperty>> listGenericDomValue, final PsiElement psiElement, final ConvertContext convertContext) {
    final DomElement paramsElement = findEnclosingTag(convertContext);
    if (paramsElement == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    for (ParamNameCustomConverter customConverter : Extensions.getExtensions(EP_NAME)) {
        final PsiReference[] customReferences = customConverter.getCustomReferences((XmlAttributeValue) psiElement, paramsElement);
        if (customReferences.length > 0) {
            return customReferences;
        }
    }
    final PsiClass rootPsiClass = findBeanPropertyClass(paramsElement);
    return new BeanPropertyPathReferenceSet(psiElement, rootPsiClass, false).getPsiReferences();
}
Also used : DomElement(com.intellij.util.xml.DomElement) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) BeanPropertyPathReferenceSet(com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with BeanPropertyPathReferenceSet

use of com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet in project intellij-plugins by JetBrains.

the class ActionPropertyReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull final PsiElement psiElement, @NotNull final ProcessingContext processingContext) {
    if (TaglibUtil.isDynamicExpression(((XmlAttributeValue) psiElement).getValue())) {
        return PsiReference.EMPTY_ARRAY;
    }
    final XmlTag tag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
    assert tag != null;
    final XmlTag actionTag = findEnclosingTag(tag, tag.getNamespacePrefix());
    if (actionTag == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final String actionName = Comparing.equal(actionTag.getLocalName(), "action") ? actionTag.getAttributeValue("name") : actionTag.getAttributeValue("action");
    if (actionName == null || TaglibUtil.isDynamicExpression(actionName)) {
        return PsiReference.EMPTY_ARRAY;
    }
    final StrutsManager strutsManager = StrutsManager.getInstance(psiElement.getProject());
    final StrutsModel strutsModel = strutsManager.getCombinedModel(psiElement);
    if (strutsModel == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final List<Action> actions = strutsModel.findActionsByName(actionName, actionTag.getAttributeValue("namespace"));
    if (actions.size() != 1) {
        return PsiReference.EMPTY_ARRAY;
    }
    final Action action = actions.get(0);
    return new BeanPropertyPathReferenceSet(psiElement, action.searchActionClass(), supportsReadOnlyProperties) {

        // TODO CTOR creates references eagerly, so we have to subclass here
        @Override
        public boolean isSoft() {
            return false;
        }
    }.getPsiReferences();
}
Also used : Action(com.intellij.struts2.dom.struts.action.Action) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) BeanPropertyPathReferenceSet(com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BeanPropertyPathReferenceSet (com.intellij.struts2.reference.common.BeanPropertyPathReferenceSet)4 NotNull (org.jetbrains.annotations.NotNull)4 PsiClass (com.intellij.psi.PsiClass)2 PsiReference (com.intellij.psi.PsiReference)2 Action (com.intellij.struts2.dom.struts.action.Action)2 TextRange (com.intellij.openapi.util.TextRange)1 XmlTag (com.intellij.psi.xml.XmlTag)1 Result (com.intellij.struts2.dom.struts.action.Result)1 StrutsManager (com.intellij.struts2.dom.struts.model.StrutsManager)1 StrutsModel (com.intellij.struts2.dom.struts.model.StrutsModel)1 InterceptorOrStackBase (com.intellij.struts2.dom.struts.strutspackage.InterceptorOrStackBase)1 InterceptorRef (com.intellij.struts2.dom.struts.strutspackage.InterceptorRef)1 InterceptorStack (com.intellij.struts2.dom.struts.strutspackage.InterceptorStack)1 DomElement (com.intellij.util.xml.DomElement)1 ArrayList (java.util.ArrayList)1