Search in sources :

Example 91 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class AntCreatePropertyFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    final FileModificationService modificationService = FileModificationService.getInstance();
    Navigatable result = null;
    if (myPropFile != null) {
        final VirtualFile vFile = myPropFile.getVirtualFile();
        boolean canModify = true;
        if (myPropFile instanceof PsiFile) {
            canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
        } else if (vFile != null) {
            canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
        }
        if (canModify) {
            final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
            result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
        }
    } else {
        if (containingFile instanceof XmlFile) {
            final XmlFile xmlFile = (XmlFile) containingFile;
            final XmlTag rootTag = xmlFile.getRootTag();
            if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
                final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
                propTag.setAttribute(NAME_ATTR, myCanonicalText);
                propTag.setAttribute(VALUE_ATTR, "");
                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) {
                    final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
                    if (valueAttrib != null) {
                        final XmlAttributeValue valueElement = valueAttrib.getValueElement();
                        if (valueElement instanceof Navigatable) {
                            result = (Navigatable) valueElement;
                        }
                    }
                }
                if (result == null && generated instanceof Navigatable) {
                    result = (Navigatable) generated;
                }
            }
        }
    }
    if (result != null) {
        result.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) Navigatable(com.intellij.pom.Navigatable) DomElement(com.intellij.util.xml.DomElement) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) FileModificationService(com.intellij.codeInsight.FileModificationService) XmlTag(com.intellij.psi.xml.XmlTag)

Example 92 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class AntRenameHandler method getElements.

@Nullable
private static PsiElement[] getElements(DataContext dataContext) {
    final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (!(psiFile instanceof XmlFile && AntDomFileDescription.isAntFile((XmlFile) psiFile))) {
        return null;
    }
    final Editor editor = LangDataKeys.EDITOR.getData(dataContext);
    if (editor == null) {
        return null;
    }
    return getPsiElementsIn(editor, psiFile);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 93 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class AntHectorConfigurable method applyItem.

private void applyItem(final String contextStr) {
    XmlFile context = null;
    if (!NONE.equals(contextStr)) {
        context = myPathToFileMap.get(contextStr);
        assert context != null;
    }
    AntConfigurationBase.getInstance(myProject).setContextFile(myFile, context);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile)

Example 94 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class AntHectorConfigurable method createComponent.

public JComponent createComponent() {
    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(IdeBorderFactory.createTitledBorder("File Context", false));
    myCombo = new ComboBox();
    myCombo.putClientProperty(CONTEXTS_COMBO_KEY, Boolean.TRUE);
    panel.add(new JLabel("Included into:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, JBUI.insets(5, 0), 0, 0));
    panel.add(myCombo, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, JBUI.insets(5, 5, 5, 0), 0, 0));
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    final Collection<VirtualFile> antFiles = fbi.getContainingFiles(AntImportsIndex.INDEX_NAME, AntImportsIndex.ANT_FILES_WITH_IMPORTS_KEY, myFileFilter);
    for (VirtualFile file : antFiles) {
        final PsiFile psiFile = psiManager.findFile(file);
        if (!(psiFile instanceof XmlFile)) {
            continue;
        }
        final XmlFile xmlFile = (XmlFile) psiFile;
        if (!xmlFile.equals(myFile) && AntDomFileDescription.isAntFile(xmlFile)) {
            final String path = PathUtil.getLocalPath(file);
            final XmlFile previous = myPathToFileMap.put(path, xmlFile);
            assert previous == null;
        }
    }
    final List<String> paths = new ArrayList<>(myPathToFileMap.keySet());
    Collections.sort(paths, (o1, o2) -> o1.compareTo(o2));
    myCombo.addItem(NONE);
    for (String path : paths) {
        myCombo.addItem(path);
    }
    final AntConfigurationBase antConfig = AntConfigurationBase.getInstance(myProject);
    final XmlFile currentContext = antConfig.getContextFile(myFile);
    if (currentContext != null) {
        final VirtualFile vFile = currentContext.getVirtualFile();
        assert vFile != null;
        final String path = PathUtil.getLocalPath(vFile);
        if (!FileUtil.pathsEqual(path, myLocalPath)) {
            myOriginalContext = path;
        }
    }
    myCombo.setSelectedItem(myOriginalContext);
    return panel;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) ComboBox(com.intellij.openapi.ui.ComboBox) PsiManager(com.intellij.psi.PsiManager) AntConfigurationBase(com.intellij.lang.ant.config.AntConfigurationBase) PsiFile(com.intellij.psi.PsiFile) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex)

Example 95 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class XmlSurroundWithRangeAdjuster method adjustSurroundWithRange.

@Override
public TextRange adjustSurroundWithRange(final PsiFile file, final TextRange selectedRange) {
    if (!(file instanceof XmlFile))
        return selectedRange;
    int startOffset = selectedRange.getStartOffset();
    int endOffset = selectedRange.getEndOffset();
    PsiElement element1 = file.findElementAt(startOffset);
    PsiElement element2 = file.findElementAt(endOffset - 1);
    Language lang1 = getLanguage(element1);
    Language lang2 = getLanguage(element2);
    if (element1 instanceof PsiWhiteSpace && isLanguageWithWSSignificant(lang1)) {
        startOffset = element1.getTextRange().getEndOffset();
        element1 = file.findElementAt(startOffset);
    }
    if (element2 instanceof PsiWhiteSpace && isLanguageWithWSSignificant(lang2)) {
        endOffset = element2.getTextRange().getStartOffset();
        element2 = file.findElementAt(endOffset);
    }
    lang1 = getLanguage(element1);
    lang2 = getLanguage(element2);
    if (lang1 != lang2)
        return null;
    TextRange.assertProperRange(startOffset, endOffset, "Wrong offsets for " + selectedRange.substring(file.getText()));
    return new TextRange(startOffset, endOffset);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) Language(com.intellij.lang.Language) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

XmlFile (com.intellij.psi.xml.XmlFile)409 XmlTag (com.intellij.psi.xml.XmlTag)155 PsiFile (com.intellij.psi.PsiFile)121 VirtualFile (com.intellij.openapi.vfs.VirtualFile)102 Nullable (org.jetbrains.annotations.Nullable)74 Project (com.intellij.openapi.project.Project)69 NotNull (org.jetbrains.annotations.NotNull)66 PsiElement (com.intellij.psi.PsiElement)64 XmlAttribute (com.intellij.psi.xml.XmlAttribute)39 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 Module (com.intellij.openapi.module.Module)34 XmlDocument (com.intellij.psi.xml.XmlDocument)32 Result (com.intellij.openapi.application.Result)28 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)23 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)22 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)21 ArrayList (java.util.ArrayList)20 Document (com.intellij.openapi.editor.Document)19 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)18 Editor (com.intellij.openapi.editor.Editor)15