use of com.intellij.psi.PsiWhiteSpace 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.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyIndentUtil method getElementIndent.
@NotNull
public static String getElementIndent(@NotNull PsiElement anchor) {
if (anchor instanceof PsiFile) {
return "";
}
final PyStatementList statementList = getAnchorStatementList(anchor);
if (statementList == null) {
return "";
}
final PsiElement prevSibling = statementList.getPrevSibling();
final String whitespace = prevSibling instanceof PsiWhiteSpace ? prevSibling.getText() : "";
final int i = whitespace.lastIndexOf("\n");
if (i >= 0 && statementList.getStatements().length != 0) {
return whitespace.substring(i + 1);
} else {
return getExpectedBlockIndent(statementList);
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class DefaultGenerateElementProvider method generate.
@SuppressWarnings({ "unchecked" })
@Nullable
public T generate(@Nullable final DomElement parent, final Editor editor) {
if (parent == null) {
return null;
}
final List<? extends DomCollectionChildDescription> list = parent.getGenericInfo().getCollectionChildrenDescriptions();
for (DomCollectionChildDescription childDescription : list) {
if (ReflectionUtil.getRawType(childDescription.getType()).isAssignableFrom(myChildElementClass)) {
XmlTag parentTag = parent.getXmlTag();
if (editor != null && parentTag != null) {
int offset = editor.getCaretModel().getOffset();
PsiFile file = parentTag.getContainingFile();
PsiElement psiElement = file.findElementAt(offset);
if (psiElement instanceof PsiWhiteSpace && PsiTreeUtil.isAncestor(parentTag, psiElement, false)) {
Document document = editor.getDocument();
XmlTag childTag = parentTag.createChildTag(childDescription.getXmlElementName(), null, null, true);
String text = childTag.getText();
document.insertString(offset, text);
Project project = editor.getProject();
assert project != null;
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
PsiElement element = file.findElementAt(offset + 1);
T domElement = DomUtil.findDomElement(element, myChildElementClass);
if (domElement != null)
return domElement;
document.deleteString(offset, offset + text.length());
documentManager.commitDocument(document);
}
}
int index = getCollectionIndex(parent, childDescription, editor);
return index < 0 ? (T) childDescription.addValue(parent, myChildElementClass) : (T) childDescription.addValue(parent, myChildElementClass, index);
}
}
return null;
}
use of com.intellij.psi.PsiWhiteSpace in project kotlin by JetBrains.
the class UpdateKotlinCopyright method scanFile.
@Override
protected void scanFile() {
PsiElement first = getFile().getFirstChild();
PsiElement last = first;
PsiElement next = first;
while (next != null) {
if (next instanceof PsiComment || next instanceof PsiWhiteSpace) {
next = getNextSibling(next);
} else {
break;
}
last = next;
}
if (first != null) {
checkComments(first, last, true);
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class ParenthesesInsertHandler method findNextToken.
@Nullable
protected PsiElement findNextToken(@NotNull InsertionContext context) {
final PsiFile file = context.getFile();
PsiElement element = file.findElementAt(context.getTailOffset());
if (element instanceof PsiWhiteSpace) {
if (!myAllowParametersOnNextLine && element.getText().contains("\n")) {
return null;
}
element = file.findElementAt(element.getTextRange().getEndOffset());
}
return element;
}
Aggregations