Search in sources :

Example 66 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class AntCreateTargetFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    Navigatable result = null;
    if (containingFile instanceof XmlFile) {
        final XmlFile xmlFile = (XmlFile) containingFile;
        final XmlTag rootTag = xmlFile.getRootTag();
        if (rootTag != null) {
            final XmlTag propTag = rootTag.createChildTag(TAG_NAME, rootTag.getNamespace(), "", false);
            propTag.setAttribute(NAME_ATTR, myCanonicalText);
            final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
            PsiElement generated;
            if (contextElement == null) {
                generated = rootTag.addSubTag(propTag, true);
            } else {
                final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
                final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
                final XmlTag tag = anchor.getXmlTag();
                if (!rootTag.equals(tag)) {
                    generated = tag.getParent().addBefore(propTag, tag);
                } else {
                    generated = rootTag.addSubTag(propTag, true);
                }
            }
            if (generated instanceof XmlTag) {
                result = new OpenFileDescriptor(project, containingFile.getVirtualFile(), ((XmlTag) generated).getValue().getTextRange().getEndOffset());
            }
            if (result == null && generated instanceof Navigatable) {
                result = (Navigatable) generated;
            }
        }
    }
    if (result != null) {
        result.navigate(true);
    }
}
Also used : DomElement(com.intellij.util.xml.DomElement) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) PsiElement(com.intellij.psi.PsiElement) Navigatable(com.intellij.pom.Navigatable) XmlTag(com.intellij.psi.xml.XmlTag)

Example 67 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class MavenDependenciesCompletionProvider method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement xmlText = parameters.getPosition().getParent();
    if (!(xmlText instanceof XmlText))
        return;
    PsiElement eDependencyTag = xmlText.getParent();
    if (!(eDependencyTag instanceof XmlTag))
        return;
    XmlTag dependencyTag = (XmlTag) eDependencyTag;
    if (!"dependency".equals(dependencyTag.getName()))
        return;
    if (!PsiImplUtil.isLeafElementOfType(xmlText.getPrevSibling(), XmlTokenType.XML_TAG_END) || !PsiImplUtil.isLeafElementOfType(xmlText.getNextSibling(), XmlTokenType.XML_END_TAG_START)) {
        return;
    }
    Project project = dependencyTag.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(dependencyTag);
    if (!(domElement instanceof MavenDomDependency)) {
        return;
    }
    MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
    for (String groupId : indicesManager.getGroupIds()) {
        for (String artifactId : indicesManager.getArtifactIds(groupId)) {
            LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId).withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
            result.addElement(builder);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager) DomElement(com.intellij.util.xml.DomElement) XmlText(com.intellij.psi.xml.XmlText) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 68 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class MavenPomXmlCompletionTagListenerContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    if (TemplateManager.getInstance(parameters.getOriginalFile().getProject()).getActiveTemplate(parameters.getEditor()) != null) {
        // Don't brake the template.
        return;
    }
    PsiFile psiFile = parameters.getOriginalFile();
    if (!(psiFile instanceof XmlFile))
        return;
    if (!MavenDomUtil.isProjectFile(psiFile))
        return;
    DomFileDescription<?> description = DomManager.getDomManager(psiFile.getProject()).getDomFileDescription((XmlFile) psiFile);
    if (!(description instanceof MavenDomProjectModelDescription))
        return;
    result.runRemainingContributors(parameters, r -> {
        final LookupElement lookupElement = r.getLookupElement();
        if (myHandledTags.contains(lookupElement.getLookupString())) {
            LookupElement decorator = LookupElementDecorator.withInsertHandler(lookupElement, new InsertHandler<LookupElementDecorator<LookupElement>>() {

                @Override
                public void handleInsert(final InsertionContext context, LookupElementDecorator<LookupElement> item) {
                    lookupElement.handleInsert(context);
                    Object object = lookupElement.getObject();
                    if ("dependency".equals(lookupElement.getLookupString()) && object instanceof XmlTag && "maven-4.0.0.xsd".equals(((XmlTag) object).getContainingFile().getName())) {
                        context.commitDocument();
                        CaretModel caretModel = context.getEditor().getCaretModel();
                        PsiElement psiElement = context.getFile().findElementAt(caretModel.getOffset());
                        XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
                        if (xmlTag != null) {
                            DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement(xmlTag);
                            if (domElement instanceof MavenDomDependency) {
                                String s = "\n<groupId></groupId>\n<artifactId></artifactId>\n";
                                context.getDocument().insertString(caretModel.getOffset(), s);
                                caretModel.moveToOffset(caretModel.getOffset() + s.length() - "</artifactId>\n".length());
                                context.commitDocument();
                                new ReformatCodeProcessor(context.getProject(), context.getFile(), xmlTag.getTextRange(), false).run();
                                MavenDependencyCompletionUtil.invokeCompletion(context, CompletionType.BASIC);
                            }
                        }
                    }
                }
            });
            r = r.withLookupElement(decorator);
        }
        result.passResult(r);
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) XmlFile(com.intellij.psi.xml.XmlFile) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) DomElement(com.intellij.util.xml.DomElement) MavenDomProjectModelDescription(org.jetbrains.idea.maven.dom.MavenDomProjectModelDescription) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 69 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class IntroducePropertyDialog method getSuggestions.

private String[] getSuggestions(int level) {
    Collection<String> result = new THashSet<>();
    String value = mySelectedString.trim();
    boolean addUnqualifiedForm = true;
    XmlTag parent = PsiTreeUtil.getParentOfType(myContext, XmlTag.class, false);
    DomElement domParent = DomUtil.getDomElement(parent);
    if (domParent != null) {
        DomElement domSuperParent = domParent.getParent();
        DomFileElement<DomElement> domFile = DomUtil.getFileElement(domParent);
        if (domSuperParent != null && domFile != null && domFile.getRootElement() == domSuperParent) {
            value = domSuperParent.getXmlElementName();
            addUnqualifiedForm = false;
        } else {
            MavenDomShortArtifactCoordinates coordinates = DomUtil.getParentOfType(domParent, MavenDomShortArtifactCoordinates.class, false);
            if (coordinates != null && !(coordinates instanceof MavenDomProjectModel) && domParent != coordinates.getArtifactId()) {
                String artifactId = coordinates.getArtifactId().getStringValue();
                if (!StringUtil.isEmptyOrSpaces(artifactId)) {
                    value = artifactId;
                    addUnqualifiedForm = false;
                }
            }
        }
    }
    while (true) {
        String newValue = value.replaceAll("  ", " ");
        if (newValue.equals(value))
            break;
        value = newValue;
    }
    value = value.replaceAll(" ", ".");
    List<String> parts = StringUtil.split(value, ".");
    String shortValue = parts.get(parts.size() - 1);
    if (addUnqualifiedForm) {
        result.add(value);
        result.add(shortValue);
    }
    String suffix = "";
    while (parent != null && level != 0) {
        suffix = parent.getName() + suffix;
        result.add(suffix);
        result.add(value + "." + suffix);
        result.add(shortValue + "." + suffix);
        suffix = "." + suffix;
        parent = parent.getParentTag();
        level--;
    }
    result = new ArrayList<>(result);
    Collections.sort((List) result, CodeStyleSettingsManager.getSettings(myProject).PREFER_LONGER_NAMES ? StringLenComparator.getDescendingInstance() : StringLenComparator.getInstance());
    return ArrayUtil.toStringArray(result);
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) DomElement(com.intellij.util.xml.DomElement) MavenDomShortArtifactCoordinates(org.jetbrains.idea.maven.dom.model.MavenDomShortArtifactCoordinates) THashSet(gnu.trove.THashSet) XmlTag(com.intellij.psi.xml.XmlTag)

Example 70 with DomElement

use of com.intellij.util.xml.DomElement in project intellij-community by JetBrains.

the class MavenPluginConfigurationDomExtender method registerPluginParameter.

private static void registerPluginParameter(boolean isInPluginManagement, DomExtensionsRegistrar r, final ParameterData data, final String parameterName) {
    DomExtension e = r.registerFixedNumberChildExtension(new XmlName(parameterName), MavenDomConfigurationParameter.class);
    if (isCollection(data.parameter)) {
        e.addExtender(new DomExtender() {

            public void registerExtensions(@NotNull DomElement domElement, @NotNull DomExtensionsRegistrar registrar) {
                for (String each : collectPossibleNameForCollectionParameter(parameterName)) {
                    DomExtension inner = registrar.registerCollectionChildrenExtension(new XmlName(each), MavenDomConfigurationParameter.class);
                    inner.setDeclaringElement(data.parameter);
                }
            }
        });
    } else {
        addValueConverter(e, data.parameter);
        if (!isInPluginManagement) {
            addRequiredAnnotation(e, data);
        }
    }
    e.setDeclaringElement(data.parameter);
    data.parameter.getXmlElement().putUserData(PLUGIN_PARAMETER_KEY, data);
}
Also used : DomExtension(com.intellij.util.xml.reflect.DomExtension) DomElement(com.intellij.util.xml.DomElement) DomExtender(com.intellij.util.xml.reflect.DomExtender) DomExtensionsRegistrar(com.intellij.util.xml.reflect.DomExtensionsRegistrar) XmlName(com.intellij.util.xml.XmlName) MavenDomConfigurationParameter(org.jetbrains.idea.maven.dom.model.MavenDomConfigurationParameter)

Aggregations

DomElement (com.intellij.util.xml.DomElement)97 XmlTag (com.intellij.psi.xml.XmlTag)34 PsiElement (com.intellij.psi.PsiElement)19 Nullable (org.jetbrains.annotations.Nullable)19 NotNull (org.jetbrains.annotations.NotNull)15 Project (com.intellij.openapi.project.Project)11 PsiFile (com.intellij.psi.PsiFile)11 XmlFile (com.intellij.psi.xml.XmlFile)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)9 ArrayList (java.util.ArrayList)8 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)7 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)6 DomManager (com.intellij.util.xml.DomManager)5 Style (org.jetbrains.android.dom.resources.Style)5 AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)4 XmlElement (com.intellij.psi.xml.XmlElement)4 DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 TextRange (com.intellij.openapi.util.TextRange)3