PsiElement (@NotNull PsiElement anchor, boolean after) { ASTNode node = anchor.getNode(); // Compare by node type to distinguish between different types of comments final IElementType expectedType = node.getElementType(); ASTNode lastSeen = node; while (node != null) { final IElementType elementType = node.getElementType(); if (elementType == expectedType) { lastSeen = node; } else if (elementType == TokenType.WHITE_SPACE) { if (expectedType == JsonElementTypes.LINE_COMMENT && node.getText().indexOf('\n', 1) != -1) { break; } } else if (!JSON_COMMENTARIES.contains(elementType) || JSON_COMMENTARIES.contains(expectedType)) { break; } node = after ? node.getTreeNext() : node.getTreePrev(); } return lastSeen.getPsi(); }