LineRange ( @NotNull PsiElement firstElement, @NotNull PsiElement lastElement, @NotNull Editor editor, LineRange oldRange ) { PsiElement parent = PsiTreeUtil.findCommonParent(firstElement, lastElement); int topExtension = 0; int bottomExtension = 0; if (parent instanceof KtFunctionLiteral) { KtBlockExpression block = ((KtFunctionLiteral) parent).getBodyExpression(); if (block != null) { PsiElement comment = null; boolean extendDown = false; if (checkCommentAtBlockBound(firstElement, lastElement, block)) { comment = lastElement; extendDown = true; lastElement = block.getLastChild(); } else if (checkCommentAtBlockBound(lastElement, firstElement, block)) { comment = firstElement; firstElement = block.getFirstChild(); } if (comment != null) { int extension = PsiLinesUtilsKt.getLineCount(comment); if (extendDown) { bottomExtension = extension; } else { topExtension = extension; } } parent = PsiTreeUtil.findCommonParent(firstElement, lastElement); } } if (parent == null) return null; Pair<PsiElement, PsiElement> originalRange = getElementRange(parent, firstElement, lastElement); if (!checkSourceElement(originalRange.first) || !checkSourceElement(originalRange.second)) return null; LineRange lineRange1 = getElementSourceLineRange(originalRange.first, editor, oldRange); if (lineRange1 == null) return null; LineRange lineRange2 = getElementSourceLineRange(originalRange.second, editor, oldRange); if (lineRange2 == null) return null; LineRange parentLineRange = getElementSourceLineRange(parent, editor, oldRange); LineRange sourceRange = new LineRange(lineRange1.startLine - topExtension, lineRange2.endLine + bottomExtension); if (parentLineRange != null && sourceRange.startLine == parentLineRange.startLine && sourceRange.endLine == parentLineRange.endLine ) { sourceRange.firstElement = sourceRange.lastElement = parent; } else { sourceRange.firstElement = originalRange.first; sourceRange.lastElement = originalRange.second; } return sourceRange; }