use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.
the class ShowIntentionsPass method getActionsToShow.
public static void getActionsToShow(@NotNull final Editor hostEditor, @NotNull final PsiFile hostFile, @NotNull final IntentionsInfo intentions, int passIdToShowIntentionsFor) {
final PsiElement psiElement = hostFile.findElementAt(hostEditor.getCaretModel().getOffset());
LOG.assertTrue(psiElement == null || psiElement.isValid(), psiElement);
int offset = hostEditor.getCaretModel().getOffset();
final Project project = hostFile.getProject();
List<HighlightInfo.IntentionActionDescriptor> fixes = getAvailableFixes(hostEditor, hostFile, passIdToShowIntentionsFor);
final DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
final Document hostDocument = hostEditor.getDocument();
HighlightInfo infoAtCursor = ((DaemonCodeAnalyzerImpl) codeAnalyzer).findHighlightByOffset(hostDocument, offset, true);
if (infoAtCursor == null) {
intentions.errorFixesToShow.addAll(fixes);
} else {
final boolean isError = infoAtCursor.getSeverity() == HighlightSeverity.ERROR;
for (HighlightInfo.IntentionActionDescriptor fix : fixes) {
if (fix.isError() && isError) {
intentions.errorFixesToShow.add(fix);
} else {
intentions.inspectionFixesToShow.add(fix);
}
}
}
for (final IntentionAction action : IntentionManager.getInstance().getAvailableIntentionActions()) {
Pair<PsiFile, Editor> place = ShowIntentionActionsHandler.chooseBetweenHostAndInjected(hostFile, hostEditor, (psiFile, editor) -> ShowIntentionActionsHandler.availableFor(psiFile, editor, action));
if (place != null) {
List<IntentionAction> enableDisableIntentionAction = new ArrayList<>();
enableDisableIntentionAction.add(new IntentionHintComponent.EnableDisableIntentionAction(action));
enableDisableIntentionAction.add(new IntentionHintComponent.EditIntentionSettingsAction(action));
HighlightInfo.IntentionActionDescriptor descriptor = new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null);
if (!fixes.contains(descriptor)) {
intentions.intentionsToShow.add(descriptor);
}
}
}
if (HighlightingLevelManager.getInstance(project).shouldInspect(hostFile)) {
PsiElement intentionElement = psiElement;
int intentionOffset = offset;
if (psiElement instanceof PsiWhiteSpace && offset == psiElement.getTextRange().getStartOffset() && offset > 0) {
final PsiElement prev = hostFile.findElementAt(offset - 1);
if (prev != null && prev.isValid()) {
intentionElement = prev;
intentionOffset = offset - 1;
}
}
if (intentionElement != null && intentionElement.getManager().isInProject(intentionElement)) {
collectIntentionsFromDoNotShowLeveledInspections(project, hostFile, intentionElement, intentionOffset, intentions);
}
}
final int line = hostDocument.getLineNumber(offset);
MarkupModelEx model = (MarkupModelEx) DocumentMarkupModel.forDocument(hostDocument, project, true);
List<RangeHighlighterEx> result = new ArrayList<>();
Processor<RangeHighlighterEx> processor = Processors.cancelableCollectProcessor(result);
model.processRangeHighlightersOverlappingWith(hostDocument.getLineStartOffset(line), hostDocument.getLineEndOffset(line), processor);
GutterIntentionAction.addActions(hostEditor, intentions, project, result);
boolean cleanup = appendCleanupCode(intentions.inspectionFixesToShow, hostFile);
if (!cleanup) {
appendCleanupCode(intentions.errorFixesToShow, hostFile);
}
EditorNotificationActions.collectDescriptorsForEditor(hostEditor, intentions.notificationActionsToShow);
intentions.filterActions(hostFile);
}
use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.
the class UnusedSymbolUtil method createUnusedSymbolInfo.
@Nullable
public static HighlightInfo createUnusedSymbolInfo(@NotNull PsiElement element, @NotNull String message, @NotNull final HighlightInfoType highlightInfoType) {
HighlightInfo info = HighlightInfo.newHighlightInfo(highlightInfoType).range(element).descriptionAndTooltip(message).create();
if (info == null) {
//filtered out
return null;
}
UnusedDeclarationFixProvider[] fixProviders = Extensions.getExtensions(UnusedDeclarationFixProvider.EP_NAME);
for (UnusedDeclarationFixProvider provider : fixProviders) {
IntentionAction[] fixes = provider.getQuickFixes(element);
for (IntentionAction fix : fixes) {
QuickFixAction.registerQuickFixAction(info, fix);
}
}
return info;
}
use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.
the class HighlightMethodUtil method registerMethodCallIntentions.
private static void registerMethodCallIntentions(@Nullable HighlightInfo highlightInfo, PsiMethodCallExpression methodCall, PsiExpressionList list, PsiResolveHelper resolveHelper) {
TextRange fixRange = getFixRange(methodCall);
final PsiExpression qualifierExpression = methodCall.getMethodExpression().getQualifierExpression();
if (qualifierExpression instanceof PsiReferenceExpression) {
final PsiElement resolve = ((PsiReferenceExpression) qualifierExpression).resolve();
if (resolve instanceof PsiClass && ((PsiClass) resolve).getContainingClass() != null && !((PsiClass) resolve).hasModifierProperty(PsiModifier.STATIC)) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix((PsiClass) resolve, PsiModifier.STATIC, true, false));
}
}
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateMethodFromUsageFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateAbstractMethodFromUsageFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateConstructorFromSuperFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateConstructorFromThisFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreatePropertyFromUsageFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createCreateGetterSetterPropertyFromUsageFix(methodCall));
CandidateInfo[] methodCandidates = resolveHelper.getReferencedMethodCandidates(methodCall, false);
CastMethodArgumentFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
PermuteArgumentsFix.registerFix(highlightInfo, methodCall, methodCandidates, fixRange);
AddTypeArgumentsFix.REGISTRAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
WrapStringWithFileFix.REGISTAR.registerCastActions(methodCandidates, methodCall, highlightInfo, fixRange);
registerMethodAccessLevelIntentions(methodCandidates, methodCall, list, highlightInfo);
registerChangeMethodSignatureFromUsageIntentions(methodCandidates, list, highlightInfo, fixRange);
RemoveRedundantArgumentsFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
ConvertDoubleToFloatFix.registerIntentions(methodCandidates, list, highlightInfo, fixRange);
WrapExpressionFix.registerWrapAction(methodCandidates, list.getExpressions(), highlightInfo);
registerChangeParameterClassFix(methodCall, list, highlightInfo);
if (methodCandidates.length == 0) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createStaticImportMethodFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.addMethodQualifierFix(methodCall));
}
for (IntentionAction action : QUICK_FIX_FACTORY.getVariableTypeFromCallFixes(methodCall, list)) {
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, action);
}
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createReplaceAddAllArrayToCollectionFix(methodCall));
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QUICK_FIX_FACTORY.createSurroundWithArrayFix(methodCall, null));
QualifyThisArgumentFix.registerQuickFixAction(methodCandidates, methodCall, highlightInfo, fixRange);
CandidateInfo[] candidates = resolveHelper.getReferencedMethodCandidates(methodCall, true);
ChangeStringLiteralToCharInMethodCallFix.registerFixes(candidates, methodCall, highlightInfo);
}
use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.
the class HighlightMethodUtil method checkMethodCanHaveBody.
@Nullable
static HighlightInfo checkMethodCanHaveBody(@NotNull PsiMethod method, @NotNull LanguageLevel languageLevel) {
PsiClass aClass = method.getContainingClass();
boolean hasNoBody = method.getBody() == null;
boolean isInterface = aClass != null && aClass.isInterface();
boolean isExtension = method.hasModifierProperty(PsiModifier.DEFAULT);
boolean isStatic = method.hasModifierProperty(PsiModifier.STATIC);
boolean isPrivate = method.hasModifierProperty(PsiModifier.PRIVATE);
final List<IntentionAction> additionalFixes = new ArrayList<>();
String description = null;
if (hasNoBody) {
if (isExtension) {
description = JavaErrorMessages.message("extension.method.should.have.a.body");
additionalFixes.add(QUICK_FIX_FACTORY.createAddMethodBodyFix(method));
} else if (isInterface) {
if (isStatic && languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
description = "Static methods in interfaces should have a body";
} else if (isPrivate && languageLevel.isAtLeast(LanguageLevel.JDK_1_9)) {
description = "Private methods in interfaces should have a body";
}
}
} else if (isInterface) {
if (!isExtension && !isStatic && !isPrivate) {
description = JavaErrorMessages.message("interface.methods.cannot.have.body");
if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
additionalFixes.add(QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.DEFAULT, true, false));
additionalFixes.add(QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.STATIC, true, false));
}
}
} else if (isExtension) {
description = JavaErrorMessages.message("extension.method.in.class");
} else if (method.hasModifierProperty(PsiModifier.ABSTRACT)) {
description = JavaErrorMessages.message("abstract.methods.cannot.have.a.body");
} else if (method.hasModifierProperty(PsiModifier.NATIVE)) {
description = JavaErrorMessages.message("native.methods.cannot.have.a.body");
}
if (description == null)
return null;
TextRange textRange = HighlightNamesUtil.getMethodDeclarationTextRange(method);
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create();
if (!hasNoBody) {
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createDeleteMethodBodyFix(method));
}
if (method.hasModifierProperty(PsiModifier.ABSTRACT) && !isInterface) {
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(method, PsiModifier.ABSTRACT, false, false));
}
for (IntentionAction intentionAction : additionalFixes) {
QuickFixAction.registerQuickFixAction(info, intentionAction);
}
return info;
}
use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.
the class MoveInitializerToConstructorAction method createConstructor.
@NotNull
private static Collection<PsiMethod> createConstructor(@NotNull Project project, @NotNull Editor editor, PsiFile file, @NotNull PsiClass aClass) {
final IntentionAction addDefaultConstructorFix = QuickFixFactory.getInstance().createAddDefaultConstructorFix(aClass);
final int offset = editor.getCaretModel().getOffset();
addDefaultConstructorFix.invoke(project, editor, file);
//restore caret
editor.getCaretModel().moveToOffset(offset);
return Arrays.asList(aClass.getConstructors());
}
Aggregations