Search in sources :

Example 6 with GenericAttributeValue

use of com.intellij.util.xml.GenericAttributeValue in project android by JetBrains.

the class AndroidApplicationPackageRenameProcessor method renameElement.

@Override
public void renameElement(PsiElement element, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    if (element instanceof PsiPackage) {
        final Map<GenericAttributeValue, String> newAttrValues = new HashMap<GenericAttributeValue, String>();
        final Project project = element.getProject();
        final String oldPackageQName = ((PsiPackage) element).getQualifiedName();
        final String newPackageQName = PsiUtilCore.getQualifiedNameAfterRename(oldPackageQName, newName);
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            final AndroidFacet facet = AndroidFacet.getInstance(module);
            final Manifest manifest = facet != null ? facet.getManifest() : null;
            if (manifest != null) {
                final XmlElement manifestElement = manifest.getXmlElement();
                final PsiFile manifestPsiFile = manifestElement != null ? manifestElement.getContainingFile() : null;
                if (manifestPsiFile instanceof XmlFile) {
                    final String basePackage = manifest.getPackage().getValue();
                    if (basePackage == null) {
                        continue;
                    }
                    processAllAttributesToUpdate((XmlFile) manifestPsiFile, basePackage, oldPackageQName, newPackageQName, new Processor<Pair<GenericAttributeValue, String>>() {

                        @Override
                        public boolean process(Pair<GenericAttributeValue, String> pair) {
                            newAttrValues.put(pair.getFirst(), pair.getSecond());
                            return true;
                        }
                    });
                }
            }
        }
        new RenamePsiPackageProcessor().renameElement(element, newName, usages, listener);
        for (Map.Entry<GenericAttributeValue, String> e : newAttrValues.entrySet()) {
            //noinspection unchecked
            e.getKey().setStringValue(e.getValue());
        }
        return;
    }
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return;
    }
    final Map<GenericAttributeValue, PsiClass> attr2class = buildAttr2ClassMap((XmlFile) file);
    new RenameXmlAttributeProcessor().renameElement(element, newName, usages, listener);
    for (Map.Entry<GenericAttributeValue, PsiClass> e : attr2class.entrySet()) {
        //noinspection unchecked
        e.getKey().setValue(e.getValue());
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) HashMap(com.intellij.util.containers.HashMap) RenamePsiPackageProcessor(com.intellij.refactoring.rename.RenamePsiPackageProcessor) Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) RenameXmlAttributeProcessor(com.intellij.refactoring.rename.RenameXmlAttributeProcessor) XmlElement(com.intellij.psi.xml.XmlElement) Module(com.intellij.openapi.module.Module) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue) Pair(com.intellij.openapi.util.Pair)

Example 7 with GenericAttributeValue

use of com.intellij.util.xml.GenericAttributeValue in project android by JetBrains.

the class DbLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectionPlacesRegistrar) {
    if (!(host instanceof XmlAttributeValue)) {
        return;
    }
    String valueText = ((XmlAttributeValue) host).getValue();
    if (!DataBindingUtil.isBindingExpression(valueText)) {
        return;
    }
    String prefix = valueText.startsWith(PREFIX_TWOWAY_BINDING_EXPR) ? PREFIX_TWOWAY_BINDING_EXPR : PREFIX_BINDING_EXPR;
    PsiElement parent = host.getParent();
    if (!(parent instanceof XmlAttribute))
        return;
    GenericAttributeValue element = DomManager.getDomManager(host.getProject()).getDomElement((XmlAttribute) parent);
    if (element == null || !(element.getParent() instanceof AndroidDomElement))
        return;
    // Parser only parses the expression, not the prefix '@{' or the suffix '}'. Extract the start/end index of the expression.
    String unescapedValue = host.getText();
    int startIndex = unescapedValue.indexOf(prefix.charAt(0)) + prefix.length();
    int endIndex;
    if (valueText.endsWith("}")) {
        endIndex = unescapedValue.lastIndexOf('}');
    } else {
        if (host.getNode().getLastChildNode().getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            endIndex = host.getLastChild().getStartOffsetInParent();
        } else {
            endIndex = unescapedValue.length();
        }
    }
    if (endIndex == startIndex) {
        // No expression found.
        return;
    }
    injectionPlacesRegistrar.addPlace(DbLanguage.INSTANCE, TextRange.from(startIndex, endIndex - startIndex), null, null);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidDomElement(org.jetbrains.android.dom.AndroidDomElement) PsiElement(com.intellij.psi.PsiElement) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Example 8 with GenericAttributeValue

use of com.intellij.util.xml.GenericAttributeValue in project intellij-plugins by JetBrains.

the class StrutsPackageImpl method searchDefaultResultType.

@Nullable
public ResultType searchDefaultResultType() {
    if (myCachedDefaultResultType == null) {
        final PsiFile containingFile = getContainingFile();
        if (containingFile == null) {
            return null;
        }
        myCachedDefaultResultType = CachedValuesManager.getManager(containingFile.getProject()).createCachedValue(() -> {
            final Ref<ResultType> result = new Ref<>();
            final StrutsPackageHierarchyWalker walker = new StrutsPackageHierarchyWalker(this, strutsPackage -> {
                final List<ResultType> resultTypes = strutsPackage.getResultTypes();
                for (final ResultType resultType : resultTypes) {
                    final GenericAttributeValue<Boolean> defaultAttribute = resultType.getDefault();
                    if (DomUtil.hasXml(defaultAttribute) && defaultAttribute.getValue() == Boolean.TRUE) {
                        result.set(resultType);
                        return false;
                    }
                }
                return true;
            });
            walker.walkUp();
            return CachedValueProvider.Result.createSingleDependency(result.get(), PsiModificationTracker.MODIFICATION_COUNT);
        }, false);
    }
    return myCachedDefaultResultType.getValue();
}
Also used : StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) PsiModificationTracker(com.intellij.psi.util.PsiModificationTracker) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) BaseImpl(com.intellij.jam.model.common.BaseImpl) CachedValue(com.intellij.psi.util.CachedValue) ResultType(com.intellij.struts2.dom.struts.strutspackage.ResultType) DefaultClassRef(com.intellij.struts2.dom.struts.strutspackage.DefaultClassRef) Processor(com.intellij.util.Processor) PsiFile(com.intellij.psi.PsiFile) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) DomUtil(com.intellij.util.xml.DomUtil) DefaultClassRef(com.intellij.struts2.dom.struts.strutspackage.DefaultClassRef) Ref(com.intellij.openapi.util.Ref) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) ResultType(com.intellij.struts2.dom.struts.strutspackage.ResultType) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GenericAttributeValue (com.intellij.util.xml.GenericAttributeValue)8 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)2 OnClickConverter (org.jetbrains.android.dom.converters.OnClickConverter)2 Manifest (org.jetbrains.android.dom.manifest.Manifest)2 ClassFilter (com.intellij.ide.util.ClassFilter)1 TreeClassChooser (com.intellij.ide.util.TreeClassChooser)1 BaseImpl (com.intellij.jam.model.common.BaseImpl)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 Ref (com.intellij.openapi.util.Ref)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 CachedValue (com.intellij.psi.util.CachedValue)1 CachedValueProvider (com.intellij.psi.util.CachedValueProvider)1