1818import com .intellij .java .analysis .codeInsight .daemon .JavaImplicitUsageProvider ;
1919import com .intellij .java .analysis .codeInsight .intention .QuickFixFactory ;
2020import com .intellij .java .analysis .impl .codeInsight .daemon .impl .GlobalUsageHelper ;
21- import com .intellij .java .analysis .impl .codeInsight .daemon .impl .JavaHighlightInfoTypes ;
2221import com .intellij .java .analysis .impl .codeInsight .daemon .impl .UnusedSymbolUtil ;
2322import com .intellij .java .analysis .impl .codeInspection .deadCode .UnusedDeclarationInspectionBase ;
2423import com .intellij .java .analysis .impl .codeInspection .deadCode .UnusedDeclarationInspectionState ;
3635import consulo .annotation .access .RequiredReadAction ;
3736import consulo .application .progress .ProgressIndicator ;
3837import consulo .application .util .ConcurrentFactoryMap ;
38+ import consulo .codeEditor .CodeInsightColors ;
3939import consulo .component .ProcessCanceledException ;
4040import consulo .disposer .Disposable ;
4141import consulo .disposer .Disposer ;
4747import consulo .language .editor .ImplicitUsageProvider ;
4848import consulo .language .editor .annotation .HighlightSeverity ;
4949import consulo .language .editor .highlight .HighlightingLevelManager ;
50+ import consulo .language .editor .inspection .HighlightInfoTypeSeverityByKey ;
5051import consulo .language .editor .inspection .SuppressionUtil ;
5152import consulo .language .editor .inspection .localize .InspectionLocalize ;
5253import consulo .language .editor .inspection .scheme .InspectionProfile ;
5354import consulo .language .editor .inspection .scheme .InspectionProjectProfileManager ;
55+ import consulo .language .editor .inspection .scheme .InspectionToolWrapper ;
5456import consulo .language .editor .intention .EmptyIntentionAction ;
5557import consulo .language .editor .intention .IntentionAction ;
56- import consulo .language .editor .rawHighlight .HighlightDisplayKey ;
57- import consulo .language .editor .rawHighlight .HighlightInfo ;
58- import consulo .language .editor .rawHighlight .HighlightInfoHolder ;
59- import consulo .language .editor .rawHighlight .HighlightInfoType ;
58+ import consulo .language .editor .rawHighlight .*;
6059import consulo .language .editor .util .CollectHighlightsUtil ;
6160import consulo .language .file .FileViewProvider ;
6261import consulo .language .pom .PomNamedTarget ;
7069
7170import java .util .List ;
7271import java .util .Map ;
72+ import java .util .Objects ;
7373import java .util .Set ;
7474import java .util .function .Function ;
7575
@@ -91,6 +91,8 @@ public class PostHighlightingVisitor {
9191 private final UnusedDeclarationInspectionBase myDeadCodeInspection ;
9292 private final UnusedDeclarationInspectionState myDeadCodeState ;
9393
94+ private final HighlightInfoType myUnusedImportHighlightType ;
95+
9496 private void optimizeImportsOnTheFlyLater (@ Nonnull ProgressIndicator progress ) {
9597 if ((myHasRedundantImports || myHasMissortedImports ) && !progress .isCanceled ()) {
9698 // schedule optimise action at the time of session disposal, which is after all applyInformation() calls
@@ -107,14 +109,14 @@ private void optimizeImportsOnTheFlyLater(@Nonnull ProgressIndicator progress) {
107109 });
108110 };
109111 try {
110- Disposer .register ((Disposable )progress , invokeFixLater );
112+ Disposer .register ((Disposable ) progress , invokeFixLater );
111113 }
112114 catch (Exception ignored ) {
113115 // suppress "parent already has been disposed" exception here
114116 }
115117 if (progress .isCanceled ()) {
116118 Disposer .dispose (invokeFixLater );
117- Disposer .dispose ((Disposable )progress );
119+ Disposer .dispose ((Disposable ) progress );
118120 progress .checkCanceled ();
119121 }
120122 }
@@ -138,22 +140,24 @@ public PostHighlightingVisitor(
138140
139141 InspectionProfile profile = InspectionProjectProfileManager .getInstance (myProject ).getInspectionProfile ();
140142
141- myDeadCodeKey = HighlightDisplayKey .find (UnusedDeclarationInspectionBase .SHORT_NAME );
143+ InspectionToolWrapper tool = Objects .requireNonNull (profile .getToolById (UnusedDeclarationInspectionBase .SHORT_NAME , myFile ));
144+
145+ myDeadCodeKey = tool .getHighlightDisplayKey ();
146+
147+ myDeadCodeInspection = (UnusedDeclarationInspectionBase ) tool .getTool ();
142148
143- myDeadCodeInspection =
144- (UnusedDeclarationInspectionBase )profile .getUnwrappedTool (UnusedDeclarationInspectionBase .SHORT_NAME , myFile );
145149 myDeadCodeState = profile .getToolState (UnusedDeclarationInspectionBase .SHORT_NAME , myFile );
146150
147- LOG .assertTrue (myDeadCodeInspection != null );
151+ myUnusedSymbolInspection = myDeadCodeInspection .getSharedLocalInspectionTool ();
152+
153+ myDeadCodeInfoType = new HighlightInfoTypeImpl (
154+ profile .getErrorLevel (myDeadCodeKey , myFile ).getSeverity (),
155+ HighlightInfoType .RAW_UNUSED_SYMBOL .getAttributesKey ()
156+ );
148157
149- myUnusedSymbolInspection = myDeadCodeInspection != null ? myDeadCodeInspection . getSharedLocalInspectionTool () : null ;
158+ InspectionToolWrapper unusedImportTool = Objects . requireNonNull ( profile . getToolById ( UnusedImportLocalInspection . SHORT_NAME , myFile )) ;
150159
151- myDeadCodeInfoType = myDeadCodeKey == null
152- ? HighlightInfoType .UNUSED_SYMBOL
153- : new HighlightInfoType .HighlightInfoTypeImpl (
154- profile .getErrorLevel (myDeadCodeKey , myFile ).getSeverity (),
155- HighlightInfoType .UNUSED_SYMBOL .getAttributesKey ()
156- );
160+ myUnusedImportHighlightType = new HighlightInfoTypeSeverityByKey (unusedImportTool .getHighlightDisplayKey (), CodeInsightColors .NOT_USED_ELEMENT_ATTRIBUTES );
157161 }
158162
159163 @ RequiredReadAction
@@ -192,7 +196,7 @@ public void collectHighlights(@Nonnull HighlightInfoHolder result, @Nonnull Prog
192196
193197 HighlightDisplayKey unusedImportKey = HighlightDisplayKey .find (UnusedImportLocalInspection .SHORT_NAME );
194198 if (isUnusedImportEnabled (unusedImportKey )) {
195- PsiImportList importList = ((PsiJavaFile )myFile ).getImportList ();
199+ PsiImportList importList = ((PsiJavaFile ) myFile ).getImportList ();
196200 if (importList != null ) {
197201 PsiImportStatementBase [] imports = importList .getAllImportStatements ();
198202 for (PsiImportStatementBase statement : imports ) {
@@ -580,7 +584,7 @@ private static HighlightInfo.Builder formatUnusedSymbolHighlightInfo(
580584 QuickFixFactory fixFactory = QuickFixFactory .getInstance ();
581585 hlBuilder .registerFix (fixFactory .createSafeDeleteFix (aClass ), null , LocalizeValue .of (), null , highlightDisplayKey );
582586 SpecialAnnotationsUtilBase .createAddToSpecialAnnotationFixes (
583- (PsiModifierListOwner )aClass ,
587+ (PsiModifierListOwner ) aClass ,
584588 annoName -> {
585589 hlBuilder .registerFix (fixFactory .createAddToDependencyInjectionAnnotationsFix (project , annoName , element ));
586590 return true ;
@@ -604,7 +608,7 @@ private HighlightInfo processImport(@Nonnull PsiImportStatementBase importStatem
604608 boolean isRedundant = myRefCountHolder .isRedundant (importStatement );
605609 if (!isRedundant && !(importStatement instanceof PsiImportStaticStatement )) {
606610 //check import from same package
607- String packageName = ((PsiClassOwner )importStatement .getContainingFile ()).getPackageName ();
611+ String packageName = ((PsiClassOwner ) importStatement .getContainingFile ()).getPackageName ();
608612 PsiJavaCodeReferenceElement reference = importStatement .getImportReference ();
609613 PsiElement resolved = reference == null ? null : reference .resolve ();
610614 if (resolved instanceof PsiPackage psiPackage ) {
@@ -613,7 +617,7 @@ private HighlightInfo processImport(@Nonnull PsiImportStatementBase importStatem
613617 else if (resolved instanceof PsiClass psiClass && !importStatement .isOnDemand ()) {
614618 String qName = psiClass .getQualifiedName ();
615619 if (qName != null ) {
616- String name = ((PomNamedTarget )resolved ).getName ();
620+ String name = ((PomNamedTarget ) resolved ).getName ();
617621 isRedundant = qName .equals (packageName + '.' + name );
618622 }
619623 }
@@ -637,7 +641,7 @@ private HighlightInfo registerRedundantImport(
637641 @ Nonnull PsiImportStatementBase importStatement ,
638642 @ Nonnull HighlightDisplayKey unusedImportKey
639643 ) {
640- HighlightInfo info = HighlightInfo .newHighlightInfo (JavaHighlightInfoTypes . UNUSED_IMPORT )
644+ HighlightInfo info = HighlightInfo .newHighlightInfo (myUnusedImportHighlightType )
641645 .range (importStatement )
642646 .descriptionAndTooltip (InspectionLocalize .unusedImportStatement ())
643647 .registerFix (QuickFixFactory .getInstance ().createOptimizeImportsFix (false ), null , LocalizeValue .of (), null , unusedImportKey )
0 commit comments