@Nullable public static PsiElement searchNonSpaceNonCommentBack(PsiElement element) { if (element == null || element.getNode() == null) return null; ASTNode leftNeibour = TreeUtil.prevLeaf(element.getNode()); while (leftNeibour != null && (leftNeibour.getElementType() == TokenType.WHITE_SPACE || leftNeibour.getPsi() instanceof PsiComment)) { leftNeibour = TreeUtil.prevLeaf(leftNeibour); } return leftNeibour != null ? leftNeibour.getPsi() : null; }
@Override protected String replaceWithPsiInLeaf( final TextRange textRange, String whiteSpace, ASTNode leafElement) { if (!myCanModifyAllWhiteSpaces) { if (leafElement.getElementType() == TokenType.WHITE_SPACE) return null; LOG.assertTrue(leafElement.getPsi().isValid()); ASTNode prevNode = TreeUtil.prevLeaf(leafElement); if (prevNode != null) { IElementType type = prevNode.getElementType(); if (type == TokenType.WHITE_SPACE) { final String text = prevNode.getText(); final @NonNls String cdataStartMarker = "<![CDATA["; final int cdataPos = text.indexOf(cdataStartMarker); if (cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) { whiteSpace = mergeWsWithCdataMarker(whiteSpace, text, cdataPos); if (whiteSpace == null) return null; } prevNode = TreeUtil.prevLeaf(prevNode); type = prevNode != null ? prevNode.getElementType() : null; } final @NonNls String cdataEndMarker = "]]>"; if (type == XmlTokenType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) { final ASTNode at = findElementAt(prevNode.getStartOffset()); if (at != null && at.getPsi() instanceof PsiWhiteSpace) { final String s = at.getText(); final int cdataEndPos = s.indexOf(cdataEndMarker); whiteSpace = mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos); leafElement = at; } else { whiteSpace = null; } if (whiteSpace == null) return null; } } } FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange); return whiteSpace; }