TextRange (final PsiElement element, final TextRange originalRange) { final Ref<TextRange> rangeRef = new Ref<>(originalRange); element.accept(new JavaRecursiveElementVisitor() { @Override public void visitExpressionStatement(final @NotNull PsiExpressionStatement statement) { final TextRange stRange = statement.getTextRange(); if (originalRange.intersects(stRange)) { final TextRange currentRange = rangeRef.get(); final int start = Math.min(currentRange.getStartOffset(), stRange.getStartOffset()); final int end = Math.max(currentRange.getEndOffset(), stRange.getEndOffset()); rangeRef.set(new TextRange(start, end)); } } }); return rangeRef.get(); }