com.intellij.psi.xml.XmlAttributeValue Java Examples

The following examples show how to use com.intellij.psi.xml.XmlAttributeValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DoctrineMetadataLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNull Collection<LineMarkerInfo> results) {
    // we need project element; so get it from first item
    if(psiElements.size() == 0) {
        return;
    }

    Project project = psiElements.get(0).getProject();
    if(!Symfony2ProjectComponent.isEnabled(project)) {
        return;
    }

    for(PsiElement psiElement: psiElements) {
        if(psiElement.getNode().getElementType() != XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) {
            continue;
        }

        PsiElement xmlAttributeValue = psiElement.getParent();
        if(xmlAttributeValue instanceof XmlAttributeValue && (DoctrineMetadataPattern.getXmlTargetDocumentClass().accepts(xmlAttributeValue) || DoctrineMetadataPattern.getXmlTargetEntityClass().accepts(xmlAttributeValue) || DoctrineMetadataPattern.getEmbeddableNameClassPattern().accepts(xmlAttributeValue))) {
            attachXmlRelationMarker(psiElement, (XmlAttributeValue) xmlAttributeValue, results);
        }
    }
}
 
Example #2
Source File: BeanUtils.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * If element at this location specifies a reference to a bean (e.g. a BeanInject annotation, bean property reference,
 * etc.), returns the expected type of that bean
 */
public PsiType findExpectedBeanTypeAt(PsiElement location) {
    PsiAnnotation annotation = PsiTreeUtil.getParentOfType(location, PsiAnnotation.class);
    if (annotation != null) {
        return IdeaUtils.getService().findAnnotatedElementType(annotation);
    }
    return Optional.ofNullable(PsiTreeUtil.getParentOfType(location, XmlAttributeValue.class, false))
            .filter(e -> Arrays.stream(e.getReferences()).anyMatch(ref -> ref instanceof BeanReference))
            .map(e -> PsiTreeUtil.getParentOfType(e, XmlTag.class))
            .map(this::findPropertyNameReference)
            .map(PropertyNameReference::resolve)
            .map(target -> {
                if (target instanceof PsiField) {
                    return ((PsiField) target).getType();
                } else if (target instanceof PsiMethod) {
                    return ((PsiMethod) target).getParameterList().getParameters()[0].getType();
                } else {
                    return null;
                }
            })
            .orElse(null);
}
 
Example #3
Source File: CamelJSonPathAnnotator.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust the text range according to the type of ${@link PsiElement}
 * @return a new text range
 */
private TextRange getAdjustedTextRange(@NotNull PsiElement element, TextRange range, String text, LanguageValidationResult result) {
    if (element instanceof XmlAttributeValue) {
        // we can use the xml range as-is
        range = ((XmlAttributeValue) element).getValueTextRange();
    } else if (getIdeaUtils().isJavaLanguage(element)) {
        // all the programming languages need to have the offset adjusted by 1
        range = TextRange.create(range.getStartOffset() + 1, range.getEndOffset());
    }
    //we need to calculate the correct start and end position to be sure we highlight the correct word
    int startIdx = result.getIndex();
    //test if the simple expression is closed correctly
    int endIdx = text.indexOf("}", startIdx);
    if (endIdx == -1) {
        //the expression is not closed, test for first " " to see if can stop text range here
        endIdx = text.indexOf(" ", startIdx) - 1;
    }
    //calc the end index for highlighted word
    endIdx = endIdx < 0 ? (range.getEndOffset() - 1) : (range.getStartOffset() + endIdx) + 1;

    if (endIdx <= startIdx) {
        endIdx = range.getEndOffset();
    }
    range = TextRange.create(range.getStartOffset() + result.getIndex(), endIdx);
    return range;
}
 
Example #4
Source File: CamelSimpleAnnotator.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust the text range according to the type of ${@link PsiElement}
 * @return a new text range
 */
private TextRange getAdjustedTextRange(@NotNull PsiElement element, TextRange range, String text, SimpleValidationResult result) {
    if (element instanceof XmlAttributeValue) {
        // we can use the xml range as-is
        range = ((XmlAttributeValue) element).getValueTextRange();
    } else if (getIdeaUtils().isJavaLanguage(element)) {
        // all the programming languages need to have the offset adjusted by 1
        range = TextRange.create(range.getStartOffset() + 1, range.getEndOffset());
    }
    //we need to calculate the correct start and end position to be sure we highlight the correct word
    int startIdx = result.getIndex();
    //test if the simple expression is closed correctly
    int endIdx = text.indexOf("}", startIdx);
    if (endIdx == -1) {
        //the expression is not closed, test for first " " to see if can stop text range here
        endIdx = text.indexOf(" ", startIdx) - 1;
    }
    //calc the end index for highlighted word
    endIdx = endIdx < 0 ? range.getEndOffset() : (range.getStartOffset() + endIdx) + 1;

    if (endIdx < startIdx) {
        endIdx = range.getEndOffset();
    }
    range = TextRange.create(range.getStartOffset() + result.getIndex(), endIdx);
    return range;
}
 
Example #5
Source File: XmlIdeaUtils.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<String> extractTextFromElement(PsiElement element, boolean concatString, boolean stripWhitespace) {
    // maybe its xml then try that
    if (element instanceof XmlAttributeValue) {
        return Optional.ofNullable(((XmlAttributeValue) element).getValue());
    } else if (element instanceof XmlText) {
        return Optional.ofNullable(((XmlText) element).getValue());
    } else if (element instanceof XmlToken) {
        // it may be a token which is a part of an combined attribute
        if (concatString) {
            XmlAttributeValue xml = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class);
            if (xml != null) {
                return Optional.ofNullable(getInnerText(xml.getValue()));
            }
        } else {
            String returnText = element.getText();
            final PsiElement prevSibling = element.getPrevSibling();
            if (prevSibling != null && prevSibling.getText().equalsIgnoreCase("&amp;")) {
                returnText = prevSibling.getText() + returnText;
            }
            return Optional.ofNullable(getInnerText(returnText));
        }
    }
    return Optional.empty();
}
 
Example #6
Source File: XmlCamelIdeaUtils.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPlaceForEndpointUri(PsiElement location) {
    XmlFile file = PsiTreeUtil.getParentOfType(location, XmlFile.class);
    if (file == null || file.getRootTag() == null || !isAcceptedNamespace(file.getRootTag().getNamespace())) {
        return false;
    }
    XmlAttributeValue value = PsiTreeUtil.getParentOfType(location, XmlAttributeValue.class, false);
    if (value == null) {
        return false;
    }
    XmlAttribute attr = PsiTreeUtil.getParentOfType(location, XmlAttribute.class);
    if (attr == null) {
        return false;
    }
    return attr.getLocalName().equals("uri") && isInsideCamelRoute(location, false);
}
 
Example #7
Source File: XmlCamelIdeaUtils.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
private List<PsiElement> findEndpoints(Module module, Predicate<String> uriCondition, Predicate<XmlTag> tagCondition) {
    Predicate<XmlAttributeValue> endpointMatcher =
        ((Predicate<XmlAttributeValue>)this::isEndpointUriValue)
        .and(e -> parentTagMatches(e, tagCondition))
        .and(e -> uriCondition.test(e.getValue()));

    List<PsiElement> endpointDeclarations = new ArrayList<>();
    IdeaUtils.getService().iterateXmlDocumentRoots(module, root -> {
        if (isAcceptedNamespace(root.getNamespace())) {
            IdeaUtils.getService().iterateXmlNodes(root, XmlAttributeValue.class, value -> {
                if (endpointMatcher.test(value)) {
                    endpointDeclarations.add(value);
                }
                return true;
            });
        }
    });
    return endpointDeclarations;
}
 
Example #8
Source File: MustacheVarReference.java    From weex-language-support with MIT License 6 votes vote down vote up
private PsiElement findDeclaration(PsiElement element, String type) {

        if (!(element instanceof XmlAttributeValue)) {
            return null;
        }

        JSObjectLiteralExpression exports = WeexFileUtil.getExportsStatement(element);

        if (exports == null) {
            return null;
        } else {
            String valueName = CodeUtil.getVarNameFromMustache(((XmlAttributeValue) element).getValue());
            if ("function".equals(type)) {
                return WeexFileUtil.getFunctionDeclaration(value, valueName);
            } else {
                return WeexFileUtil.getVarDeclaration(value, valueName);
            }
        }
    }
 
Example #9
Source File: DoctrineMetadataLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void attachXmlRelationMarker(@NotNull PsiElement target, @NotNull XmlAttributeValue psiElement, @NotNull Collection<LineMarkerInfo> results) {
    String value = psiElement.getValue();
    if(StringUtils.isBlank(value)) {
        return;
    }

    Collection<PhpClass> classesInterface = DoctrineMetadataUtil.getClassInsideScope(psiElement, value);
    if(classesInterface.size() == 0) {
        return;
    }

    NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.DOCTRINE_LINE_MARKER).
        setTargets(classesInterface).
        setTooltipText("Navigate to class");

    results.add(builder.createLineMarkerInfo(target));
}
 
Example #10
Source File: FlowRefPsiReference.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
public boolean isReferenceTo(PsiElement element) {
    if (element == null)
        return false;

    PsiElement parent = PsiTreeUtil.getParentOfType(element, XmlTag.class);

    if (parent != null && parent instanceof XmlTag &&
            (MuleConfigConstants.FLOW_TAG_NAME.equals(((XmlTag)parent).getName()) ||
             MuleConfigConstants.SUB_FLOW_TAG_NAME.equals(((XmlTag)parent).getName()))) { //It's a <flow> tag or <sub-flow> tag
        if (element instanceof XmlAttributeValue && ((XmlAttributeValue)element).getValue().equals(getFlowName())) {
            return true;
        }
    }

    return false;
}
 
Example #11
Source File: TranslationReferenceTest.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public void testReferenceCanResolveDefinition() {
    PsiFile file = myFixture.configureByText(PhpFileType.INSTANCE, "<?php \n" +
            "\"LLL:EXT:foo/sample.xlf:sys_<caret>language.language_isocode.ab\";");

    PsiElement elementAtCaret = file.findElementAt(myFixture.getCaretOffset()).getParent();
    PsiReference[] references = elementAtCaret.getReferences();
    for (PsiReference reference : references) {
        if (reference instanceof TranslationReference) {
            ResolveResult[] resolveResults = ((TranslationReference) reference).multiResolve(false);
            for (ResolveResult resolveResult : resolveResults) {
                assertInstanceOf(resolveResult.getElement(), XmlAttributeValue.class);

                return;
            }
        }
    }

    fail("TranslationReference could not be resolved");
}
 
Example #12
Source File: FlowInPlaceRenamer.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
private static Template buildTemplate(@NotNull XmlAttribute attr, List<XmlTag> refs) {
        //XmlFile containingFile = (XmlFile)attr.getContainingFile();
        PsiElement commonParent = PsiTreeUtil.findCommonParent(refs);
        TemplateBuilderImpl builder = new TemplateBuilderImpl(attr);

        XmlAttributeValue attrValue = attr.getValueElement();
        PsiElement valuePsi = attrValue.getFirstChild().getNextSibling();

        String flowNameValue = new String(attrValue.getValue());
        builder.replaceElement(valuePsi,"PrimaryVariable", new TextExpression(flowNameValue), true);
/*

        for (XmlTag ref : refs) {
            if (ref.getContainingFile().equals(attr.getContainingFile())) {
                XmlAttribute nextAttr = ref.getAttribute(MuleConfigConstants.NAME_ATTRIBUTE);
                XmlAttributeValue nextValue = nextAttr.getValueElement();
                PsiElement nextValuePsi = nextValue.getFirstChild().getNextSibling();
                builder.replaceElement(nextValuePsi, "OtherVariable", "PrimaryVariable",false);
            }
        }
*/

        return builder.buildInlineTemplate();
    }
 
Example #13
Source File: FluidUtil.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static PsiElement retrieveFluidElementAtPosition(PsiElement psiElement) {
    FileViewProvider viewProvider = psiElement.getContainingFile().getViewProvider();
    if (!viewProvider.getLanguages().contains(FluidLanguage.INSTANCE)) {
        return null;
    }

    int textOffset = psiElement.getTextOffset();
    FluidFile psi = (FluidFile) viewProvider.getPsi(FluidLanguage.INSTANCE);

    if (psiElement instanceof XmlAttributeValue) {
        textOffset += 2;
    }

    PsiElement elementAt = psi.findElementAt(textOffset);
    if (elementAt == null) {
        return null;
    }

    if (elementAt.getNode().getElementType().equals(FluidTypes.IDENTIFIER)) {
        return elementAt.getParent();
    }

    return null;
}
 
Example #14
Source File: XmlHelperTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see XmlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgumentParameter() {
    myFixture.configureByText(XmlFileType.INSTANCE, "" +
        "<service class=\"Foo\\Bar\">\n" +
        "   <call method=\"setBar\">\n" +
        "      <argument/>\n" +
        "      <argument type=\"service\" id=\"ma<caret>iler\" />\n" +
        "   </call>\n" +
        "</service>"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    PsiElement parent = psiElement.getParent();

    Collection<Parameter> results = new ArrayList<>();

    XmlHelper.visitServiceCallArgumentMethodIndex((XmlAttributeValue) parent, results::add);

    assertNotNull(
        ContainerUtil.find(results, parameter -> "arg2".equals(parameter.getName()))
    );
}
 
Example #15
Source File: RouteSettingDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private void registerAttributeRequirementProblem(@NotNull ProblemsHolder holder, @NotNull XmlAttributeValue xmlAttributeValue, @NotNull final String requirementAttribute) {
    if(!xmlAttributeValue.getValue().equals(requirementAttribute)) {
        return;
    }

    XmlAttribute xmlAttributeKey = PsiElementAssertUtil.getParentOfTypeWithNameOrNull(xmlAttributeValue, XmlAttribute.class, "key");
    if(xmlAttributeKey != null) {
        XmlTag xmlTagDefault = PsiElementAssertUtil.getParentOfTypeWithNameOrNull(xmlAttributeKey, XmlTag.class, "requirement");
        if(xmlTagDefault != null) {
            XmlTag xmlTagRoute = PsiElementAssertUtil.getParentOfTypeWithNameOrNull(xmlTagDefault, XmlTag.class, "route");
            if(xmlTagRoute != null) {
                // attach to attribute token only we dont want " or ' char included
                PsiElement target = findAttributeValueToken(xmlAttributeValue, requirementAttribute);

                holder.registerProblem(target != null ? target : xmlAttributeValue, String.format("The '%s' requirement is deprecated", requirementAttribute), ProblemHighlightType.LIKE_DEPRECATED);
            }
        }
    }
}
 
Example #16
Source File: XmlHelperTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see XmlHelper#visitServiceCallArgumentMethodIndex
 */
public void testVisitServiceCallArgument() {
    myFixture.configureByText(XmlFileType.INSTANCE, "" +
        "<service class=\"Foo\\Bar\">\n" +
        "   <call method=\"setBar\">\n" +
        "      <argument/>\n" +
        "      <argument type=\"service\" id=\"ma<caret>iler\" />\n" +
        "   </call>\n" +
        "</service>"
    );

    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    PsiElement parent = psiElement.getParent();

    Collection<String> results = new ArrayList<>();

    XmlHelper.visitServiceCallArgument((XmlAttributeValue) parent, visitor ->
        results.add(visitor.getClassName() + ":" + visitor.getMethod() + ":" + visitor.getParameterIndex())
    );

    assertContainsElements(results, "Foo\\Bar:setBar:1");
}
 
Example #17
Source File: RouteSettingDeprecatedInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if(element instanceof XmlAttributeValue) {
                registerAttributeRequirementProblem(holder, (XmlAttributeValue) element, "_method");
                registerAttributeRequirementProblem(holder, (XmlAttributeValue) element, "_scheme");
            } else if(element instanceof XmlAttribute) {
                registerRoutePatternProblem(holder, (XmlAttribute) element);
            } else if(element instanceof YAMLKeyValue) {
                registerYmlRoutePatternProblem(holder, (YAMLKeyValue) element);
            }

            super.visitElement(element);
        }
    };
}
 
Example #18
Source File: XmlGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<LookupElement> getLookupElements() {
    PsiElement parent = getElement().getParent();
    if(!(parent instanceof XmlAttributeValue)) {
        return Collections.emptyList();
    }

    Collection<PhpClass> phpClasses = new ArrayList<>();

    ContainerUtil.addIfNotNull(phpClasses, XmlHelper.getPhpClassForClassFactory((XmlAttributeValue) parent));
    ContainerUtil.addIfNotNull(phpClasses, XmlHelper.getPhpClassForServiceFactory((XmlAttributeValue) parent));

    Collection<LookupElement> lookupElements = new ArrayList<>();

    for (PhpClass phpClass : phpClasses) {
        lookupElements.addAll(PhpElementsUtil.getClassPublicMethod(phpClass).stream()
            .map(PhpLookupElement::new)
            .collect(Collectors.toList())
        );
    }

    return lookupElements;
}
 
Example #19
Source File: BeanReferenceProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected PsiReference[] getAttributeReferences(@NotNull XmlAttribute attribute, @NotNull XmlAttributeValue value,
                                                ProcessingContext context) {
    String beanId = attribute.getValue();
    if (beanId != null && beanId.length() > 0) {
        if ("ref".equals(attribute.getLocalName())) {
            return new PsiReference[] {new BeanReference(value, beanId)};
        } else if ("id".equals(attribute.getLocalName())) {
            return new PsiReference[] {new BeanSelfReference(value, beanId)};
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
 
Example #20
Source File: BlueprintJavaClassReferenceProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected PsiReference[] getAttributeReferences(@NotNull XmlAttribute attribute, @NotNull XmlAttributeValue value,
                                                ProcessingContext context) {
    if (isBeanClassAttribute(attribute) || isTypeAttributeInRoute(attribute)) {
        return javaClassReferenceProvider.getReferencesByElement(value, context);
    } else {
        return PsiReference.EMPTY_ARRAY;
    }
}
 
Example #21
Source File: BlueprintAttributeValueReferenceProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected PsiReference[] getCamelReferencesByElement(PsiElement element, ProcessingContext context) {
    if (element instanceof XmlAttributeValue) {
        XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class);
        if (attribute != null) {
            XmlTag tag = attribute.getParent();
            XmlAttributeValue value = attribute.getValueElement();
            if (tag != null && value != null && BeanUtils.getService().isPartOfBeanContainer(tag)) {
                return getAttributeReferences(attribute, value, context);
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
 
Example #22
Source File: BlueprintPropertyNameReferenceProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected PsiReference[] getAttributeReferences(@NotNull XmlAttribute attribute, @NotNull XmlAttributeValue value,
                                                ProcessingContext context) {
    if (isPropertyName(attribute, value)) {
        PsiClass beanClass = BeanUtils.getService().getPropertyBeanClass(attribute.getParent());
        if (beanClass != null) {
            return new PsiReference[] {new PropertyNameReference(value, value.getValue(), beanClass)};
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
 
Example #23
Source File: XmlGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void getLookupElements(@NotNull GotoCompletionProviderLookupArguments arguments) {
    // find class name of service tag
    PsiElement xmlToken = this.getElement();
    if(xmlToken instanceof XmlToken) {
        PsiElement xmlAttrValue = xmlToken.getParent();
        if(xmlAttrValue instanceof XmlAttributeValue) {
            PsiElement xmlAttribute = xmlAttrValue.getParent();
            if(xmlAttribute instanceof XmlAttribute) {
                PsiElement xmlTag = xmlAttribute.getParent();
                if(xmlTag instanceof XmlTag) {
                    String aClass = ((XmlTag) xmlTag).getAttributeValue("class");
                    if(aClass == null) {
                        // <service id="Foo\Bar"/>

                        PhpClassCompletionProvider.addClassCompletion(
                            arguments.getParameters(),
                            arguments.getResultSet(),
                            getElement(),
                            false
                        );
                    } else if(StringUtils.isNotBlank(aClass)) {
                        // <service id="foo.bar" class="Foo\Bar"/>

                        LookupElementBuilder lookupElement = LookupElementBuilder
                            .create(ServiceUtil.getServiceNameForClass(getProject(), aClass))
                            .withIcon(Symfony2Icons.SERVICE);

                        LookupElementBuilder lookupElementWithClassName = LookupElementBuilder
                                .create(aClass)
                                .withIcon(Symfony2Icons.SERVICE);

                        arguments.getResultSet().addElement(lookupElement);
                        arguments.getResultSet().addElement(lookupElementWithClassName);
                    }
                }
            }
        }
    }
}
 
Example #24
Source File: CamelDirectEndpointReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public void testXmlMultipleReferences() {
    myFixture.configureByText("route-with-references.xml", XML_ROUTE_WITH_MULTIPLE_REFERENCES);
    PsiElement element = TestReferenceUtil.getParentElementAtCaret(myFixture);
    List<XmlAttributeValue> values = TestReferenceUtil.resolveReference(element, XmlAttributeValue.class);
    assertEquals(2, values.size());
    for (XmlAttributeValue value : values) {
        assertEquals("direct:abc", value.getValue());
    }
}
 
Example #25
Source File: GotoCompletionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static String getTextValueForElement(@NotNull PsiElement psiElement) {
    PsiElement parent = psiElement.getParent();

    String value = null;
    if(parent instanceof StringLiteralExpression) {
        value = ((StringLiteralExpression) parent).getContents();
    } else if(parent instanceof XmlAttributeValue) {
        // <foo attr="FOO"/>
        value = ((XmlAttributeValue) parent).getValue();
    } else if(parent instanceof XmlText) {
        // <foo>FOO</foo>
        value = ((XmlText) parent).getValue();
    } else if(parent instanceof YAMLScalar) {
        // foo: foo, foo: 'foo', foo: "foo"
        value = ((YAMLScalar) parent).getTextValue();
    } else if(psiElement.getNode().getElementType() == TwigTokenTypes.STRING_TEXT) {
        // twig: 'foobar'
        value = psiElement.getText();
    }

    if(StringUtils.isBlank(value)) {
        return null;
    }

    return value;
}
 
Example #26
Source File: GotoCompletionUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * <foo attr="FOO"/>
 */
@Nullable
public static String getXmlAttributeValue(@NotNull PsiElement psiElement) {
    PsiElement parent = psiElement.getParent();
    if(!(parent instanceof XmlAttributeValue)) {
        return null;
    }

    final String value = ((XmlAttributeValue) parent).getValue();
    if(StringUtils.isBlank(value)) {
        return null;
    }

    return value;
}
 
Example #27
Source File: TranslationUtilTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testCanFindDefinitionElements() {
    assertSize(2, TranslationUtil.findDefinitionElements(myFixture.getProject(), "LLL:EXT:foo/sample.xlf:sys_language.language_isocode.ab"));
    assertSize(1, TranslationUtil.findDefinitionElements(myFixture.getProject(), "LLL:EXT:foo/de.sample.xlf:sys_language.language_isocode.ab"));

    PsiElement[] definitionElements = TranslationUtil.findDefinitionElements(myFixture.getProject(), "LLL:EXT:foo/sample.xlf:sys_language.language_isocode.ab");
    for (PsiElement definitionElement : definitionElements) {
        assertInstanceOf(definitionElement, XmlAttributeValue.class);
    }
}
 
Example #28
Source File: DoctrineXmlGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement element) {
    PsiElement parent = element.getParent();
    if(!(parent instanceof XmlAttributeValue)) {
        return Collections.emptyList();
    }

    String value = ((XmlAttributeValue) parent).getValue();
    if(StringUtils.isBlank(value)) {
        return Collections.emptyList();
    }

    return new ArrayList<>(DoctrineMetadataUtil.getClassInsideScope(element, value));
}
 
Example #29
Source File: CaseSensitivityServiceInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void xmlVisitor(final @NotNull ProblemsHolder holder, @NotNull PsiFile psiFile) {
    psiFile.acceptChildren(new PsiRecursiveElementVisitor() {
        @Override
        public void visitElement(PsiElement psiElement) {
            if(psiElement instanceof XmlAttributeValue && (XmlHelper.getArgumentServiceIdPattern().accepts(psiElement) || XmlHelper.getServiceIdAttributePattern().accepts(psiElement))) {
                String serviceName = ((XmlAttributeValue) psiElement).getValue();
                if(StringUtils.isNotBlank(serviceName) && !serviceName.equals(serviceName.toLowerCase()) && !YamlHelper.isClassServiceId(serviceName)) {
                    holder.registerProblem(psiElement, SYMFONY_LOWERCASE_LETTERS_FOR_SERVICE, ProblemHighlightType.WEAK_WARNING);
                }
            }

            super.visitElement(psiElement);
        }
    });
}
 
Example #30
Source File: WeexCompletionContributor.java    From weex-language-support with MIT License 5 votes vote down vote up
private void performInsert(XmlAttributeValue value, InsertionContext insertionContext, LookupElement lookupElement) {
    if (value.getText().startsWith("\"")) {
        insertionContext.getDocument().replaceString(
                value.getTextOffset(),
                value.getTextOffset() + getTailLength(value) + lookupElement.getLookupString().length(),
                lookupElement.getLookupString());
    } else {
        insertionContext.getDocument().replaceString(
                value.getTextOffset() - 1,
                value.getTextOffset() + getTailLength(value) + lookupElement.getLookupString().length() - 1,
                "\"" + lookupElement.getLookupString() + "\"");
    }
}