List<String> (@NotNull KtFile file, CommentType commentType, boolean assertMustExist) { PsiElement lastChild = file.getLastChild(); if (lastChild != null && lastChild.getNode().getElementType().equals(KtTokens.WHITE_SPACE)) { lastChild = lastChild.getPrevSibling(); } assert lastChild != null; List<String> comments = new ArrayList<>(); while (true) { if (lastChild.getNode().getElementType().equals(KtTokens.BLOCK_COMMENT)) { if (commentType == CommentType.ALL || commentType == CommentType.BLOCK_COMMENT) { String lastChildText = lastChild.getText(); comments.add(lastChildText.substring(2, lastChildText.length() - 2).trim()); } } else if (lastChild.getNode().getElementType().equals(KtTokens.EOL_COMMENT)) { if (commentType == CommentType.ALL || commentType == CommentType.LINE_COMMENT) { comments.add(lastChild.getText().substring(2).trim()); } } else { break; } lastChild = lastChild.getPrevSibling(); } if (comments.isEmpty() && assertMustExist) { throw new AssertionError(String.format( "Test file '%s' should end in a comment of type %s; last node was: %s", file.getName(), commentType, lastChild)); } return comments; }