LineRange (@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) { PsiElement first; PsiElement last; if (element instanceof KtDeclaration) { first = element.getFirstChild(); last = element.getLastChild(); if (first == null || last == null) return null; } else { first = last = element; } TextRange textRange1 = first.getTextRange(); TextRange textRange2 = last.getTextRange(); Document doc = editor.getDocument(); if (doc.getTextLength() < textRange2.getEndOffset()) return null; int startLine = editor.offsetToLogicalPosition(textRange1.getStartOffset()).line; int endLine = editor.offsetToLogicalPosition(textRange2.getEndOffset()).line + 1; if (element instanceof PsiComment || startLine == oldRange.startLine || startLine == oldRange.endLine || endLine == oldRange.startLine || endLine == oldRange.endLine) { return new LineRange(startLine, endLine); } int lineCount = doc.getLineCount(); if (oldRange.startLine >= lineCount || oldRange.endLine >= lineCount) { return null; } TextRange lineTextRange = new TextRange(doc.getLineStartOffset(oldRange.startLine), doc.getLineEndOffset(oldRange.endLine)); if (element instanceof KtDeclaration) { for (PsiElement anchor : getDeclarationAnchors((KtDeclaration) element)) { TextRange suspectTextRange = anchor.getTextRange(); if (suspectTextRange != null && lineTextRange.intersects(suspectTextRange)) return new LineRange(startLine, endLine); } } return null; }