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;
}
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;
}
};
}
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);
}
}
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();
}
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);
}
}
}
Aggregations