Search in sources :

Example 6 with PsiNamedElement

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

the class XmlMover method checkAvailable.

//private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.editor.actions.moveUpDown.XmlMover");
@Override
public boolean checkAvailable(@NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final MoveInfo info, final boolean down) {
    if (!(file instanceof XmlFile)) {
        return false;
    }
    if (!super.checkAvailable(editor, file, info, down))
        return false;
    // updated moved range end to cover multiline tag start
    final Document document = editor.getDocument();
    int movedLineStart = document.getLineStartOffset(info.toMove.startLine);
    final int movedLineEnd = document.getLineEndOffset(info.toMove.endLine - 1);
    PsiElement movedEndElement = file.findElementAt(movedLineEnd);
    if (movedEndElement instanceof PsiWhiteSpace)
        movedEndElement = PsiTreeUtil.prevLeaf(movedEndElement);
    PsiElement movedStartElement = file.findElementAt(movedLineStart);
    if (movedStartElement instanceof PsiWhiteSpace)
        movedStartElement = PsiTreeUtil.nextLeaf(movedStartElement);
    if (movedEndElement == null || movedStartElement == null)
        return false;
    final PsiNamedElement namedParentAtEnd = PsiTreeUtil.getParentOfType(movedEndElement, PsiNamedElement.class);
    final PsiNamedElement namedParentAtStart = PsiTreeUtil.getParentOfType(movedStartElement, PsiNamedElement.class);
    if (checkInjections(movedEndElement, movedStartElement))
        return false;
    PsiNamedElement movedParent = null;
    if (namedParentAtEnd == namedParentAtStart)
        movedParent = namedParentAtEnd;
    else if (namedParentAtEnd instanceof XmlAttribute && namedParentAtStart instanceof XmlTag && namedParentAtEnd.getParent() == namedParentAtStart) {
        movedParent = namedParentAtStart;
    } else if (namedParentAtStart instanceof XmlAttribute && namedParentAtEnd instanceof XmlTag && namedParentAtStart.getParent() == namedParentAtEnd) {
        movedParent = namedParentAtEnd;
    }
    if (movedParent == null) {
        return false;
    }
    final TextRange textRange = movedParent.getTextRange();
    if (movedParent instanceof XmlTag) {
        final XmlTag tag = (XmlTag) movedParent;
        PsiElement parent = tag.getParent();
        if (!(parent instanceof XmlTag) && PsiTreeUtil.getChildrenOfType(parent, XmlTag.class).length < 2) {
            // the only top-level tag
            return info.prohibitMove();
        }
        final TextRange valueRange = tag.getValue().getTextRange();
        final int valueStart = valueRange.getStartOffset();
        if (HtmlUtil.isHtmlTag(tag) && (HtmlUtil.isScriptTag(tag) || HtmlUtil.STYLE_TAG_NAME.equals(tag.getName()))) {
            info.toMove = new LineRange(tag);
            int nextLine = down ? info.toMove.endLine : info.toMove.startLine - 1;
            info.toMove2 = new LineRange(nextLine, nextLine + 1);
        }
        if (movedLineStart < valueStart && valueStart + 1 < document.getTextLength()) {
            movedLineStart = updateMovedRegionEnd(document, movedLineStart, valueStart + 1, info, down);
        }
        if (movedLineStart < valueStart) {
            movedLineStart = updateMovedRegionStart(document, movedLineStart, tag.getTextRange().getStartOffset(), info, down);
        }
    } else if (movedParent instanceof XmlAttribute) {
        final int endOffset = textRange.getEndOffset() + 1;
        if (endOffset < document.getTextLength())
            movedLineStart = updateMovedRegionEnd(document, movedLineStart, endOffset, info, down);
        movedLineStart = updateMovedRegionStart(document, movedLineStart, textRange.getStartOffset(), info, down);
    }
    final TextRange moveDestinationRange = new UnfairTextRange(document.getLineStartOffset(info.toMove2.startLine), document.getLineEndOffset(info.toMove2.endLine - 1));
    if (movedParent instanceof XmlAttribute) {
        final XmlTag parent = ((XmlAttribute) movedParent).getParent();
        if (parent != null) {
            final TextRange valueRange = parent.getValue().getTextRange();
            // Do not move attributes out of tags
            if ((down && moveDestinationRange.getEndOffset() >= valueRange.getStartOffset()) || (!down && moveDestinationRange.getStartOffset() <= parent.getTextRange().getStartOffset())) {
                return info.prohibitMove();
            }
        }
    }
    if (down) {
        PsiElement updatedElement = file.findElementAt(moveDestinationRange.getEndOffset());
        if (updatedElement instanceof PsiWhiteSpace)
            updatedElement = PsiTreeUtil.prevLeaf(updatedElement);
        if (updatedElement != null) {
            final PsiNamedElement targetParent = PsiTreeUtil.getParentOfType(updatedElement, movedParent.getClass());
            if (targetParent instanceof XmlTag) {
                if (targetParent == movedParent)
                    return false;
                if (moveTags(info, (XmlTag) movedParent, (XmlTag) targetParent, down))
                    return true;
                final XmlTag tag = (XmlTag) targetParent;
                final int offset = tag.isEmpty() ? tag.getTextRange().getStartOffset() : tag.getValue().getTextRange().getStartOffset();
                updatedMovedIntoEnd(document, info, offset);
                if (tag.isEmpty()) {
                    info.toMove2 = new LineRange(targetParent);
                }
            } else if (targetParent instanceof XmlAttribute) {
                updatedMovedIntoEnd(document, info, targetParent.getTextRange().getEndOffset());
            }
        }
    } else {
        PsiElement updatedElement = file.findElementAt(moveDestinationRange.getStartOffset());
        if (updatedElement instanceof PsiWhiteSpace)
            updatedElement = PsiTreeUtil.nextLeaf(updatedElement);
        if (updatedElement != null) {
            final PsiNamedElement targetParent = PsiTreeUtil.getParentOfType(updatedElement, movedParent.getClass());
            if (targetParent instanceof XmlTag) {
                final XmlTag tag = (XmlTag) targetParent;
                final TextRange tagValueRange = tag.getValue().getTextRange();
                // We need to update destination range to jump over tag start
                final XmlTag[] subtags = tag.getSubTags();
                if ((tagValueRange.contains(movedLineStart) && subtags.length > 0 && subtags[0] == movedParent) || (tagValueRange.getLength() == 0 && tag.getTextRange().intersects(moveDestinationRange))) {
                    final int line = document.getLineNumber(tag.getTextRange().getStartOffset());
                    final LineRange toMove2 = info.toMove2;
                    info.toMove2 = new LineRange(Math.min(line, toMove2.startLine), toMove2.endLine);
                }
                if (targetParent == movedParent)
                    return false;
                if (moveTags(info, (XmlTag) movedParent, (XmlTag) targetParent, down))
                    return true;
            } else if (targetParent instanceof XmlAttribute) {
                final int line = document.getLineNumber(targetParent.getTextRange().getStartOffset());
                final LineRange toMove2 = info.toMove2;
                info.toMove2 = new LineRange(Math.min(line, toMove2.startLine), toMove2.endLine);
            }
        }
    }
    if (movedParent instanceof XmlTag) {
        // it's quite simple after all...
        info.toMove = new LineRange(movedParent);
    }
    return true;
}
Also used : UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) PsiNamedElement(com.intellij.psi.PsiNamedElement) TextRange(com.intellij.openapi.util.TextRange) UnfairTextRange(com.intellij.openapi.util.UnfairTextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 7 with PsiNamedElement

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

the class GroovyIntroduceParameterObjectDelegate method createMergedParameterInfo.

@Override
public GrParameterInfo createMergedParameterInfo(GroovyIntroduceObjectClassDescriptor descriptor, GrMethod method, List<GrParameterInfo> oldMethodParameters) {
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(method.getProject());
    PsiType classType = elementFactory.createTypeByFQClassName(StringUtil.getQualifiedName(descriptor.getPackageName(), descriptor.getClassName()));
    return new GrParameterInfo(descriptor.getClassName(), null, null, classType, -1, false) {

        @Nullable
        @Override
        public PsiElement getActualValue(PsiElement callExpression, Object substitutor) {
            final IntroduceParameterObjectDelegate<PsiNamedElement, ParameterInfo, IntroduceParameterObjectClassDescriptor<PsiNamedElement, ParameterInfo>> delegate = findDelegate(callExpression);
            return delegate != null ? delegate.createNewParameterInitializerAtCallSite(callExpression, descriptor, oldMethodParameters, substitutor) : null;
        }
    };
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) PsiNamedElement(com.intellij.psi.PsiNamedElement) IntroduceParameterObjectClassDescriptor(com.intellij.refactoring.introduceParameterObject.IntroduceParameterObjectClassDescriptor) ParameterInfo(com.intellij.refactoring.changeSignature.ParameterInfo) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Example 8 with PsiNamedElement

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

the class GrVariableInplaceRenamer method renameSynthetic.

@Override
protected void renameSynthetic(String newName) {
    PsiNamedElement elementToRename = getVariable();
    if (elementToRename instanceof ClosureSyntheticParameter && !"it".equals(newName)) {
        final GrClosableBlock closure = ((ClosureSyntheticParameter) elementToRename).getClosure();
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
        final PsiType type = ((ClosureSyntheticParameter) elementToRename).getTypeGroovy();
        final GrParameter newParam = factory.createParameter(newName, TypesUtil.unboxPrimitiveTypeWrapper(type));
        final GrParameter added = closure.addParameter(newParam);
        JavaCodeStyleManager.getInstance(added.getProject()).shortenClassReferences(added);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiNamedElement(com.intellij.psi.PsiNamedElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) PsiType(com.intellij.psi.PsiType)

Example 9 with PsiNamedElement

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

the class XmlFindUsagesProvider method getNodeText.

@Override
@NotNull
public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
    if (element instanceof XmlTag) {
        final XmlTag xmlTag = (XmlTag) element;
        final PsiMetaData metaData = xmlTag.getMetaData();
        final String name = metaData != null ? DescriptiveNameUtil.getMetaDataName(metaData) : xmlTag.getName();
        String presentableName = metaData == null ? "<" + name + ">" : name;
        return presentableName + " of file " + xmlTag.getContainingFile().getName();
    }
    if (element instanceof XmlAttributeValue) {
        return ((XmlAttributeValue) element).getValue();
    }
    if (element instanceof PsiNamedElement) {
        return ((PsiNamedElement) element).getName();
    }
    return element.getText();
}
Also used : PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiNamedElement(com.intellij.psi.PsiNamedElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PsiNamedElement

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

the class PyMakeFunctionFromMethodQuickFix method updateUsage.

private static void updateUsage(@NotNull final PsiElement finalElement, @NotNull final PyReferenceExpression element, @NotNull final PsiFile usageFile, boolean addImport) {
    final PyExpression qualifier = element.getQualifier();
    if (qualifier == null)
        return;
    if (qualifier.getText().equals(PyNames.CANONICAL_SELF)) {
        PyUtil.removeQualifier(element);
        return;
    }
    if (qualifier instanceof PyCallExpression) {
        // remove qualifier A().m()
        if (addImport)
            AddImportHelper.addImport((PsiNamedElement) finalElement, usageFile, element);
        PyUtil.removeQualifier(element);
        removeFormerImport(usageFile, addImport);
    } else {
        final PsiReference reference = qualifier.getReference();
        if (reference == null)
            return;
        final PsiElement resolved = reference.resolve();
        if (resolved instanceof PyTargetExpression) {
            // qualifier came from assignment  a = A(); a.m()
            updateAssignment(element, resolved);
        } else if (resolved instanceof PyClass) {
            //call with first instance argument A.m(A())
            PyUtil.removeQualifier(element);
            updateArgumentList(element);
        }
    }
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiNamedElement (com.intellij.psi.PsiNamedElement)65 PsiElement (com.intellij.psi.PsiElement)29 NotNull (org.jetbrains.annotations.NotNull)16 ArrayList (java.util.ArrayList)14 PsiFile (com.intellij.psi.PsiFile)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)7 Pair (com.intellij.openapi.util.Pair)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Project (com.intellij.openapi.project.Project)4 UsageInfo (com.intellij.usageView.UsageInfo)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PsiReference (com.intellij.psi.PsiReference)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2