[java-inspections] Extract method recommender: do not include initial comment into the length

Fixes IDEA-354492 "Method can be extracted" includes comments as minimum code length

GitOrigin-RevId: 3d4e34e6726f7917fe3b0224653b89f07cd2ae42
This commit is contained in:
Tagir Valeev
2024-06-21 12:17:42 +02:00
committed by intellij-monorepo-bot
parent 3b45af56a7
commit e573732e7c
3 changed files with 30 additions and 3 deletions

View File

@@ -85,7 +85,8 @@ public final class ExtractMethodRecommenderInspection extends AbstractBaseJavaLo
PsiStatement[] range = Arrays.copyOfRange(statements, from, to);
if (ContainerUtil.exists(range, e -> e instanceof PsiSwitchLabelStatementBase)) continue;
TextRange textRange = getRange(range);
if (textRange.getLength() < minLength || textRange.getLength() > maxLength) continue;
int length = range[range.length - 1].getTextRange().getEndOffset() - range[0].getTextRange().getStartOffset();
if (length < minLength || textRange.getLength() > maxLength) continue;
try {
ControlFlowWrapper wrapper = new ControlFlowWrapper(fragment, range);
Collection<PsiStatement> exitStatements = wrapper.prepareExitStatements(range);
@@ -118,11 +119,11 @@ public final class ExtractMethodRecommenderInspection extends AbstractBaseJavaLo
JavaAnalysisBundle.message("inspection.extract.method.dont.suggest.parameters", inputVariables.size()),
inputVariables.size() - 1)));
}
if (textRange.getLength() < 10_000) {
if (length < 10_000) {
fixes.add(LocalQuickFix.from(new UpdateInspectionOptionFix(
ExtractMethodRecommenderInspection.this, "minLength",
JavaAnalysisBundle.message("inspection.extract.method.dont.suggest.length"),
textRange.getLength() + 1)));
length + 1)));
}
int firstLineBreak = textRange.substring(block.getText()).indexOf('\n');
PsiElement anchor = block;

View File

@@ -0,0 +1,18 @@
import java.util.*;
public class ExtractMethodRecommenderComment {
void testLongCommentBefore() {
// We are creating a new Date object here and set it's to 12 hours
// We are creating a new Date object here and set it's to 12 hours
// We are creating a new Date object here and set it's to 12 hours
Date d = new Date();
d.setHours(12);
System.out.println(d);
System.out.println(d);
System.out.println(d);
System.out.println(d);
System.out.println(d);
System.out.println(d);
System.out.println(d);
}
}

View File

@@ -33,6 +33,14 @@ public class ExtractMethodRecommenderInspectionTest extends LightJavaCodeInsight
myFixture.checkHighlighting();
}
public void testExtractMethodRecommenderComment() {
ExtractMethodRecommenderInspection inspection = new ExtractMethodRecommenderInspection();
inspection.minLength = 100;
myFixture.enableInspections(inspection);
myFixture.configureByFile(getTestName(false) + ".java");
myFixture.checkHighlighting();
}
public void testImplicitClass() {
ExtractMethodRecommenderInspection inspection = new ExtractMethodRecommenderInspection();
myFixture.enableInspections(inspection);