int (@NotNull final Document document, @NotNull final PsiFile psiFile, final int start, final int end) { PsiJavaToken elementAtStartLineEnd = tryCast(psiFile.findElementAt(start), PsiJavaToken.class); if (elementAtStartLineEnd == null || !elementAtStartLineEnd.getTokenType().equals(JavaTokenType.SEMICOLON)) return CANNOT_JOIN; PsiExpressionStatement secondStatement = PsiTreeUtil.getParentOfType(psiFile.findElementAt(end), PsiExpressionStatement.class); if (secondStatement == null) return CANNOT_JOIN; PsiMethodCallExpression secondCall = tryCast(secondStatement.getExpression(), PsiMethodCallExpression.class); if (secondCall == null) return CANNOT_JOIN; PsiElement firstStatement = elementAtStartLineEnd.getParent(); boolean result = false; if (firstStatement instanceof PsiExpressionStatement) { if (firstStatement.getParent() != secondStatement.getParent()) return CANNOT_JOIN; PsiExpression firstExpression = ((PsiExpressionStatement)firstStatement).getExpression(); if (firstExpression instanceof PsiMethodCallExpression) { result = joinTwoCalls((PsiMethodCallExpression)firstExpression, secondCall); } else if (firstExpression instanceof PsiAssignmentExpression) { result = joinAssignmentAndCall((PsiAssignmentExpression)firstExpression, secondCall); } } else if (firstStatement instanceof PsiLocalVariable var) { result = joinExpressionAndCall(var, var.getInitializer(), secondCall); } if (!result) return CANNOT_JOIN; secondStatement.delete(); return firstStatement.getTextRange().getEndOffset(); }