2626import com .intellij .java .analysis .impl .codeInspection .unusedSymbol .UnusedSymbolLocalInspectionBase ;
2727import com .intellij .java .analysis .impl .codeInspection .util .SpecialAnnotationsUtilBase ;
2828import com .intellij .java .indexing .search .searches .OverridingMethodsSearch ;
29- import com .intellij .java .language .impl .codeInsight .daemon .JavaErrorBundle ;
3029import com .intellij .java .language .impl .psi .impl .PsiClassImplUtil ;
3130import com .intellij .java .language .psi .*;
3231import com .intellij .java .language .psi .codeStyle .JavaCodeStyleManager ;
5453import consulo .language .editor .inspection .scheme .InspectionProjectProfileManager ;
5554import consulo .language .editor .intention .EmptyIntentionAction ;
5655import consulo .language .editor .intention .IntentionAction ;
57- import consulo .language .editor .intention .QuickFixAction ;
5856import consulo .language .editor .rawHighlight .HighlightDisplayKey ;
5957import consulo .language .editor .rawHighlight .HighlightInfo ;
6058import consulo .language .editor .rawHighlight .HighlightInfoHolder ;
6967import consulo .project .Project ;
7068import jakarta .annotation .Nonnull ;
7169import jakarta .annotation .Nullable ;
72- import org .jetbrains .annotations .PropertyKey ;
7370
7471import java .util .List ;
7572import java .util .Map ;
@@ -310,37 +307,29 @@ private HighlightInfo processLocalVariable(
310307
311308 if (!myRefCountHolder .isReferenced (variable )) {
312309 LocalizeValue message = JavaErrorLocalize .localVariableIsNeverUsed (identifier .getText ());
313- HighlightInfo highlightInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
314310 IntentionAction fix = variable instanceof PsiResourceVariable
315311 ? QuickFixFactory .getInstance ().createRenameToIgnoredFix (variable )
316312 : QuickFixFactory .getInstance ().createRemoveUnusedVariableFix (variable );
317- QuickFixAction .registerQuickFixAction (highlightInfo , fix , myDeadCodeKey );
318- return highlightInfo ;
313+ return UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType )
314+ .registerFix (fix , null , null , null , myDeadCodeKey )
315+ .create ();
319316 }
320317
321318 boolean referenced = myRefCountHolder .isReferencedForRead (variable );
322319 if (!referenced && !UnusedSymbolUtil .isImplicitRead (myProject , variable , progress )) {
323320 LocalizeValue message = JavaErrorLocalize .localVariableIsNotUsedForReading (identifier .getText ());
324- HighlightInfo highlightInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
325- QuickFixAction .registerQuickFixAction (
326- highlightInfo ,
327- QuickFixFactory .getInstance ().createRemoveUnusedVariableFix (variable ),
328- myDeadCodeKey
329- );
330- return highlightInfo ;
321+ return UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType )
322+ .registerFix (QuickFixFactory .getInstance ().createRemoveUnusedVariableFix (variable ), null , null , null , myDeadCodeKey )
323+ .create ();
331324 }
332325
333326 if (!variable .hasInitializer ()) {
334327 referenced = myRefCountHolder .isReferencedForWrite (variable );
335328 if (!referenced && !UnusedSymbolUtil .isImplicitWrite (myProject , variable , progress )) {
336329 LocalizeValue message = JavaErrorLocalize .localVariableIsNotAssigned (identifier .getText ());
337- HighlightInfo unusedSymbolInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
338- QuickFixAction .registerQuickFixAction (
339- unusedSymbolInfo ,
340- new EmptyIntentionAction (UnusedSymbolLocalInspectionBase .DISPLAY_NAME ),
341- myDeadCodeKey
342- );
343- return unusedSymbolInfo ;
330+ return UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType )
331+ .registerFix (new EmptyIntentionAction (UnusedSymbolLocalInspectionBase .DISPLAY_NAME ), null , null , null , myDeadCodeKey )
332+ .create ();
344333 }
345334 }
346335
@@ -364,21 +353,20 @@ private HighlightInfo processField(
364353 if (!myRefCountHolder .isReferenced (field ) && !UnusedSymbolUtil .isImplicitUsage (myProject , field , progress )) {
365354 LocalizeValue message = JavaErrorLocalize .privateFieldIsNotUsed (identifier .getText ());
366355
367- HighlightInfo highlightInfo = suggestionsToMakeFieldUsed (field , identifier , message );
356+ HighlightInfo . Builder hlBuilder = suggestionsToMakeFieldUsed (field , identifier , message );
368357 if (!field .hasInitializer () && !field .isFinal ()) {
369- QuickFixAction .registerQuickFixAction (
370- highlightInfo ,
371- HighlightMethodUtil .getFixRange (field ),
372- quickFixFactory .createCreateConstructorParameterFromFieldFix (field )
358+ hlBuilder .registerFix (
359+ quickFixFactory .createCreateConstructorParameterFromFieldFix (field ),
360+ HighlightMethodUtil .getFixRange (field )
373361 );
374362 }
375- return highlightInfo ;
363+ return hlBuilder . create () ;
376364 }
377365
378366 boolean readReferenced = myRefCountHolder .isReferencedForRead (field );
379367 if (!readReferenced && !UnusedSymbolUtil .isImplicitRead (project , field , progress )) {
380368 LocalizeValue message = JavaErrorLocalize .privateFieldIsNotUsedForReading (identifier .getText ());
381- return suggestionsToMakeFieldUsed (field , identifier , message );
369+ return suggestionsToMakeFieldUsed (field , identifier , message ). create () ;
382370 }
383371
384372 if (field .hasInitializer ()) {
@@ -387,31 +375,23 @@ private HighlightInfo processField(
387375 boolean writeReferenced = myRefCountHolder .isReferencedForWrite (field );
388376 if (!writeReferenced && !UnusedSymbolUtil .isImplicitWrite (project , field , progress )) {
389377 LocalizeValue message = JavaErrorLocalize .privateFieldIsNotAssigned (identifier .getText ());
390- HighlightInfo info = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
378+ HighlightInfo . Builder hlBuilder = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
391379
392- QuickFixAction .registerQuickFixAction (
393- info ,
394- quickFixFactory .createCreateGetterOrSetterFix (false , true , field ),
395- myDeadCodeKey
396- );
380+ hlBuilder .registerFix (quickFixFactory .createCreateGetterOrSetterFix (false , true , field ), null , null , null , myDeadCodeKey );
397381 if (!field .isFinal ()) {
398- QuickFixAction .registerQuickFixAction (
399- info ,
400- HighlightMethodUtil .getFixRange (field ),
401- quickFixFactory .createCreateConstructorParameterFromFieldFix (field )
382+ hlBuilder .registerFix (
383+ quickFixFactory .createCreateConstructorParameterFromFieldFix (field ),
384+ HighlightMethodUtil .getFixRange (field )
402385 );
403386 }
404387 SpecialAnnotationsUtilBase .createAddToSpecialAnnotationFixes (
405388 field ,
406389 annoName -> {
407- QuickFixAction .registerQuickFixAction (
408- info ,
409- quickFixFactory .createAddToImplicitlyWrittenFieldsFix (project , annoName )
410- );
390+ hlBuilder .registerFix (quickFixFactory .createAddToImplicitlyWrittenFieldsFix (project , annoName ));
411391 return true ;
412392 }
413393 );
414- return info ;
394+ return hlBuilder . create () ;
415395 }
416396 }
417397 else if (UnusedSymbolUtil .isImplicitUsage (myProject , field , progress )
@@ -421,55 +401,36 @@ else if (UnusedSymbolUtil.isImplicitUsage(myProject, field, progress)
421401 else if (UnusedSymbolUtil .isFieldUnused (myProject , myFile , field , progress , helper )) {
422402 if (UnusedSymbolUtil .isImplicitWrite (myProject , field , progress )) {
423403 LocalizeValue message = JavaErrorLocalize .privateFieldIsNotUsedForReading (identifier .getText ());
424- HighlightInfo highlightInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
425- QuickFixAction .registerQuickFixAction (
426- highlightInfo ,
427- QuickFixFactory .getInstance ().createSafeDeleteFix (field ),
428- myDeadCodeKey
429- );
430- return highlightInfo ;
404+ return UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType )
405+ .registerFix (QuickFixFactory .getInstance ().createSafeDeleteFix (field ), null , null , null , myDeadCodeKey )
406+ .create ();
431407 }
432408 return formatUnusedSymbolHighlightInfo (
433409 project ,
434- "field.is.not.used" ,
410+ JavaErrorLocalize :: fieldIsNotUsed ,
435411 field ,
436412 "fields" ,
437413 myDeadCodeKey ,
438414 myDeadCodeInfoType ,
439415 identifier
440- );
416+ ). create () ;
441417 }
442418 return null ;
443419 }
444420
421+ @ Nonnull
445422 @ RequiredReadAction
446- private HighlightInfo suggestionsToMakeFieldUsed (
423+ private HighlightInfo . Builder suggestionsToMakeFieldUsed (
447424 @ Nonnull PsiField field ,
448425 @ Nonnull PsiIdentifier identifier ,
449426 @ Nonnull LocalizeValue message
450427 ) {
451- HighlightInfo highlightInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
452- QuickFixAction .registerQuickFixAction (
453- highlightInfo ,
454- QuickFixFactory .getInstance ().createRemoveUnusedVariableFix (field ),
455- myDeadCodeKey
456- );
457- QuickFixAction .registerQuickFixAction (
458- highlightInfo ,
459- QuickFixFactory .getInstance ().createCreateGetterOrSetterFix (true , false , field ),
460- myDeadCodeKey
461- );
462- QuickFixAction .registerQuickFixAction (
463- highlightInfo ,
464- QuickFixFactory .getInstance ().createCreateGetterOrSetterFix (false , true , field ),
465- myDeadCodeKey
466- );
467- QuickFixAction .registerQuickFixAction (
468- highlightInfo ,
469- QuickFixFactory .getInstance ().createCreateGetterOrSetterFix (true , true , field ),
470- myDeadCodeKey
471- );
472- return highlightInfo ;
428+ QuickFixFactory fixFactory = QuickFixFactory .getInstance ();
429+ return UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType )
430+ .registerFix (fixFactory .createRemoveUnusedVariableFix (field ), null , null , null , myDeadCodeKey )
431+ .registerFix (fixFactory .createCreateGetterOrSetterFix (true , false , field ), null , null , null , myDeadCodeKey )
432+ .registerFix (fixFactory .createCreateGetterOrSetterFix (false , true , field ), null , null , null , myDeadCodeKey )
433+ .registerFix (fixFactory .createCreateGetterOrSetterFix (true , true , field ), null , null , null , myDeadCodeKey );
473434 }
474435
475436 private final Map <PsiMethod , Boolean > isOverriddenOrOverrides = ConcurrentFactoryMap .createMap (method -> {
@@ -502,22 +463,22 @@ private HighlightInfo processParameter(
502463 if (UnusedSymbolUtil .isInjected (project , method )) {
503464 return null ;
504465 }
505- HighlightInfo highlightInfo = checkUnusedParameter (parameter , identifier , progress );
506- if (highlightInfo != null ) {
507- QuickFixFactory .getInstance ().registerFixesForUnusedParameter (parameter , highlightInfo );
508- return highlightInfo ;
466+ HighlightInfo .Builder hlBuilder = checkUnusedParameter (parameter , identifier , progress );
467+ if (hlBuilder != null ) {
468+ HighlightInfo highlightInfo = hlBuilder .create ();
469+ if (highlightInfo != null ) {
470+ QuickFixFactory .getInstance ().registerFixesForUnusedParameter (parameter , highlightInfo );
471+ return highlightInfo ;
472+ }
509473 }
510474 }
511475 }
512476 else if (declarationScope instanceof PsiForeachStatement && !PsiUtil .isIgnoredName (parameter .getName ())) {
513- HighlightInfo highlightInfo = checkUnusedParameter (parameter , identifier , progress );
514- if (highlightInfo != null ) {
515- QuickFixAction .registerQuickFixAction (
516- highlightInfo ,
517- QuickFixFactory .getInstance ().createRenameToIgnoredFix (parameter ),
518- myDeadCodeKey
519- );
520- return highlightInfo ;
477+ HighlightInfo .Builder hlBuilder = checkUnusedParameter (parameter , identifier , progress );
478+ if (hlBuilder != null ) {
479+ return hlBuilder
480+ .registerFix (QuickFixFactory .getInstance ().createRenameToIgnoredFix (parameter ), null , null , null , myDeadCodeKey )
481+ .create ();
521482 }
522483 }
523484
@@ -526,7 +487,7 @@ else if (declarationScope instanceof PsiForeachStatement && !PsiUtil.isIgnoredNa
526487
527488 @ Nullable
528489 @ RequiredReadAction
529- private HighlightInfo checkUnusedParameter (
490+ private HighlightInfo . Builder checkUnusedParameter (
530491 @ Nonnull PsiParameter parameter ,
531492 @ Nonnull PsiIdentifier identifier ,
532493 @ Nonnull ProgressIndicator progress
@@ -559,18 +520,16 @@ private HighlightInfo processMethod(
559520 }
560521 LocalizeValue symbolName = HighlightMessageUtil .getSymbolName (method , PsiSubstitutor .EMPTY );
561522 LocalizeValue message = key .apply (symbolName );
562- HighlightInfo highlightInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
563- QuickFixAction . registerQuickFixAction ( highlightInfo , QuickFixFactory .getInstance ().createSafeDeleteFix (method ), myDeadCodeKey );
523+ HighlightInfo . Builder hlBuilder = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , myDeadCodeInfoType );
524+ hlBuilder . registerFix ( QuickFixFactory .getInstance ().createSafeDeleteFix (method ), null , null , null , myDeadCodeKey );
564525 SpecialAnnotationsUtilBase .createAddToSpecialAnnotationFixes (
565526 method ,
566527 annoName -> {
567- IntentionAction fix =
568- QuickFixFactory .getInstance ().createAddToDependencyInjectionAnnotationsFix (project , annoName , "methods" );
569- QuickFixAction .registerQuickFixAction (highlightInfo , fix );
528+ hlBuilder .registerFix (QuickFixFactory .getInstance ().createAddToDependencyInjectionAnnotationsFix (project , annoName , "methods" ));
570529 return true ;
571530 }
572531 );
573- return highlightInfo ;
532+ return hlBuilder . create () ;
574533 }
575534
576535 @ Nullable
@@ -586,51 +545,48 @@ private HighlightInfo processClass(
586545 return null ;
587546 }
588547
589- String pattern ;
548+ Function < String , LocalizeValue > pattern ;
590549 if (aClass .getContainingClass () != null && aClass .isPrivate ()) {
591- pattern = aClass .isInterface () ? "private.inner.interface.is.not.used" : "private.inner.class.is.not.used" ;
550+ pattern = aClass .isInterface ()
551+ ? JavaErrorLocalize ::privateInnerInterfaceIsNotUsed
552+ : JavaErrorLocalize ::privateInnerClassIsNotUsed ;
592553 }
593554 else if (aClass .getParent () instanceof PsiDeclarationStatement ) { // local class
594- pattern = "local.class.is.not.used" ;
555+ pattern = JavaErrorLocalize :: localClassIsNotUsed ;
595556 }
596557 else if (aClass instanceof PsiTypeParameter ) {
597- pattern = "type.parameter.is.not.used" ;
558+ pattern = JavaErrorLocalize :: typeParameterIsNotUsed ;
598559 }
599560 else {
600- pattern = "class.is.not.used" ;
561+ pattern = JavaErrorLocalize :: classIsNotUsed ;
601562 }
602- return formatUnusedSymbolHighlightInfo (myProject , pattern , aClass , "classes" , myDeadCodeKey , myDeadCodeInfoType , identifier );
563+ return formatUnusedSymbolHighlightInfo (myProject , pattern , aClass , "classes" , myDeadCodeKey , myDeadCodeInfoType , identifier )
564+ .create ();
603565 }
604566
605567 @ RequiredReadAction
606- private static HighlightInfo formatUnusedSymbolHighlightInfo (
568+ private static HighlightInfo . Builder formatUnusedSymbolHighlightInfo (
607569 @ Nonnull Project project ,
608- @ Nonnull @ PropertyKey ( resourceBundle = JavaErrorBundle . BUNDLE ) String pattern ,
570+ @ Nonnull Function < String , LocalizeValue > pattern ,
609571 @ Nonnull PsiNameIdentifierOwner aClass ,
610572 @ Nonnull String element ,
611573 HighlightDisplayKey highlightDisplayKey ,
612574 @ Nonnull HighlightInfoType highlightInfoType ,
613575 @ Nonnull PsiElement identifier
614576 ) {
615577 String symbolName = aClass .getName ();
616- String message = JavaErrorBundle .message (pattern , symbolName );
617- HighlightInfo highlightInfo = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , highlightInfoType );
618- QuickFixAction .registerQuickFixAction (
619- highlightInfo ,
620- QuickFixFactory .getInstance ().createSafeDeleteFix (aClass ),
621- highlightDisplayKey
622- );
578+ LocalizeValue message = pattern .apply (symbolName );
579+ HighlightInfo .Builder hlBuilder = UnusedSymbolUtil .createUnusedSymbolInfo (identifier , message , highlightInfoType );
580+ QuickFixFactory fixFactory = QuickFixFactory .getInstance ();
581+ hlBuilder .registerFix (fixFactory .createSafeDeleteFix (aClass ), null , null , null , highlightDisplayKey );
623582 SpecialAnnotationsUtilBase .createAddToSpecialAnnotationFixes (
624583 (PsiModifierListOwner )aClass ,
625584 annoName -> {
626- QuickFixAction .registerQuickFixAction (
627- highlightInfo ,
628- QuickFixFactory .getInstance ().createAddToDependencyInjectionAnnotationsFix (project , annoName , element )
629- );
585+ hlBuilder .registerFix (fixFactory .createAddToDependencyInjectionAnnotationsFix (project , annoName , element ));
630586 return true ;
631587 }
632588 );
633- return highlightInfo ;
589+ return hlBuilder ;
634590 }
635591
636592 @ Nullable
@@ -681,19 +637,15 @@ private HighlightInfo registerRedundantImport(
681637 @ Nonnull PsiImportStatementBase importStatement ,
682638 @ Nonnull HighlightDisplayKey unusedImportKey
683639 ) {
684- String description = InspectionLocalize .unusedImportStatement ().get ();
685640 HighlightInfo info = HighlightInfo .newHighlightInfo (JavaHighlightInfoTypes .UNUSED_IMPORT )
686641 .range (importStatement )
687- .descriptionAndTooltip (description )
642+ .descriptionAndTooltip (InspectionLocalize .unusedImportStatement ())
643+ .registerFix (QuickFixFactory .getInstance ().createOptimizeImportsFix (false ), null , null , null , unusedImportKey )
644+ .registerFix (QuickFixFactory .getInstance ().createEnableOptimizeImportsOnTheFlyFix (), null , null , null , unusedImportKey )
688645 .create ();
689646
690- QuickFixAction .registerQuickFixAction (info , QuickFixFactory .getInstance ().createOptimizeImportsFix (false ), unusedImportKey );
691- QuickFixAction .registerQuickFixAction (
692- info ,
693- QuickFixFactory .getInstance ().createEnableOptimizeImportsOnTheFlyFix (),
694- unusedImportKey
695- );
696647 myHasRedundantImports = true ;
648+
697649 return info ;
698650 }
699651}
0 commit comments