void (@NotNull PsiMethod ctor) { if (!JavaPsiRecordUtil.isExplicitCanonicalConstructor(ctor)) return; PsiClass record = ctor.getContainingClass(); if (record == null) return; PsiCodeBlock body = ctor.getBody(); if (body == null) return; PsiRecordComponent[] components = record.getRecordComponents(); PsiParameterList parameterList = ctor.getParameterList(); PsiParameter[] parameters = parameterList.getParameters(); PsiStatement[] statements = body.getStatements(); int assignedComponents = getAssignedComponentsCount(components, parameters, statements); PsiStatement firstStatementToDelete = assignedComponents == 0 ? null : statements[statements.length - assignedComponents]; StringBuilder resultText = new StringBuilder(); for (PsiElement child : ctor.getChildren()) { if (child == parameterList) continue; if (child == body) break; resultText.append(child.getText()); } boolean skipStatements = false; CommentTracker ct = new CommentTracker(); for (PsiElement child : body.getChildren()) { if (child == firstStatementToDelete) { skipStatements = true; } if (skipStatements && child.getNextSibling() != null) { ct.grabComments(child); continue; } resultText.append(child.getText()); } PsiMethod compactCtor = JavaPsiFacade.getElementFactory(record.getProject()).createMethodFromText(resultText.toString(), ctor); PsiMethod result = (PsiMethod)ctor.replace(compactCtor); ct.insertCommentsBefore(Objects.requireNonNull(Objects.requireNonNull(result.getBody()).getRBrace())); }