com.intellij.codeInsight.intention.IntentionManager Java Examples

The following examples show how to use com.intellij.codeInsight.intention.IntentionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SymfonyLightCodeInsightFixtureTestCase.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    Set<String> items = new HashSet<>();

    for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
        if(!intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile())) {
            continue;
        }

        String text = intentionAction.getText();
        items.add(text);

        if(!text.equals(intentionText)) {
            continue;
        }

        return;
    }

    fail(String.format("Fail intention action '%s' is available in element '%s' with '%s'", intentionText, psiElement.getText(), items));
}
 
Example #2
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
        if(intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile()) && intentionAction.getText().equals(intentionText)) {
            return;
        }
    }

    fail(String.format("Fail intention action '%s' is available in element '%s'", intentionText, psiElement.getText()));
}
 
Example #3
Source File: LaravelLightCodeInsightFixtureTestCase.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
        if(intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile()) && intentionAction.getText().equals(intentionText)) {
            return;
        }
    }

    fail(String.format("Fail intention action '%s' is available in element '%s'", intentionText, psiElement.getText()));
}
 
Example #4
Source File: ShopwareLightCodeInsightFixtureTestCase.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
        if(intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile()) && intentionAction.getText().equals(intentionText)) {
            return;
        }
    }

    fail(String.format("Fail intention action '%s' is available in element '%s'", intentionText, psiElement.getText()));
}
 
Example #5
Source File: ModifierDefinitionInspection.java    From intellij-latte with MIT License 5 votes vote down vote up
@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
	if (!(file instanceof LatteFile)) {
		return null;
	}

	final List<ProblemDescriptor> problems = new ArrayList<>();
	file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
		@Override
		public void visitElement(PsiElement element) {
			if (element instanceof LatteMacroModifier) {
				String filterName = ((LatteMacroModifier) element).getModifierName();
				LatteFilterSettings latteFilter = LatteConfiguration.getInstance(element.getProject()).getFilter(filterName);
				if (latteFilter == null) {
					LocalQuickFix addModifierFix = IntentionManager.getInstance().convertToFix(new AddCustomLatteModifier(filterName));
					ProblemHighlightType type = ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
					String description = "Undefined latte filter '" + filterName + "'";
					ProblemDescriptor problem = manager.createProblemDescriptor(element, description, true, type, isOnTheFly, addModifierFix);
					problems.add(problem);
				}

			} else {
				super.visitElement(element);
			}
		}
	});

	return problems.toArray(new ProblemDescriptor[0]);
}
 
Example #6
Source File: MethodUsagesInspection.java    From intellij-latte with MIT License 5 votes vote down vote up
private void processFunction(
		LattePhpMethod element,
		@NotNull List<ProblemDescriptor> problems,
		@NotNull final InspectionManager manager,
		final boolean isOnTheFly
) {
	String name = element.getMethodName();
	if (name == null) {
		return;
	}

	LatteFunctionSettings customFunction = LatteConfiguration.getInstance(element.getProject()).getFunction(name);
	if (customFunction != null) {
		return;
	}

	Collection<Function> existing = LattePhpUtil.getFunctionByName(element.getProject(), name);
	if (existing.size() == 0) {
		LocalQuickFix addFunctionFix = IntentionManager.getInstance().convertToFix(new AddCustomLatteFunction(name));
		addProblem(manager, problems, getElementToLook(element), "Function '" + name + "' not found", isOnTheFly, addFunctionFix);

	} else {
		for (Function function : existing) {
			if (function.isDeprecated()) {
				addDeprecated(manager, problems, getElementToLook(element), "Function '" + name + "' is deprecated", isOnTheFly);
			}
			if (function.isInternal()) {
				addDeprecated(manager, problems, getElementToLook(element), "Function '" + name + "' is internal", isOnTheFly);
			}
		}
	}
}
 
Example #7
Source File: DrupalLightCodeInsightFixtureTestCase.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
        if(intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile()) && intentionAction.getText().equals(intentionText)) {
            return;
        }
    }

    fail(String.format("Fail intention action '%s' is available in element '%s'", intentionText, psiElement.getText()));
}
 
Example #8
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void assertIntentionIsAvailable(LanguageFileType languageFileType, String configureByText, String intentionText) {
    myFixture.configureByText(languageFileType, configureByText);
    PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

    for (IntentionAction intentionAction : IntentionManager.getInstance().getIntentionActions()) {
        if(intentionAction.isAvailable(getProject(), getEditor(), psiElement.getContainingFile()) && intentionAction.getText().equals(intentionText)) {
            return;
        }
    }

    fail(String.format("Fail intention action '%s' is available in element '%s'", intentionText, psiElement.getText()));
}
 
Example #9
Source File: CleanupIntentionMenuContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean appendCleanupCode(@Nonnull List<HighlightInfo.IntentionActionDescriptor> actionDescriptors, @Nonnull PsiFile file) {
  for (HighlightInfo.IntentionActionDescriptor descriptor : actionDescriptors) {
    if (descriptor.canCleanup(file)) {
      IntentionManager manager = IntentionManager.getInstance();
      actionDescriptors.add(new HighlightInfo.IntentionActionDescriptor(manager.createCleanupAllIntention(), manager.getCleanupIntentionOptions(), "Code Cleanup Options"));
      return true;
    }
  }
  return false;
}
 
Example #10
Source File: IntentionManagerSettings.java    From consulo with Apache License 2.0 5 votes vote down vote up
public IntentionManagerSettings() {
  for (IntentionActionBean bean : IntentionManager.EP_INTENTION_ACTIONS.getExtensionList()) {
    String[] categories = bean.getCategories();
    if (categories != null) {
      String descriptionDirectoryName = bean.getDescriptionDirectoryName();

      IntentionActionWrapper intentionAction = new IntentionActionWrapper(bean);
      if (descriptionDirectoryName == null) {
        descriptionDirectoryName = IntentionManagerImpl.getDescriptionDirectoryName(intentionAction);
      }

      registerIntentionMetaData(intentionAction, categories, descriptionDirectoryName);
    }
  }
}
 
Example #11
Source File: IntentionManagerSettings.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void processOptions(@Nonnull SearchableOptionProcessor processor) {
  for (IntentionActionBean bean : IntentionManager.EP_INTENTION_ACTIONS.getExtensionList()) {
    String[] categories = bean.getCategories();

    if (categories == null) {
      continue;
    }
    String descriptionDirectoryName = bean.getDescriptionDirectoryName();

    IntentionActionWrapper intentionAction = new IntentionActionWrapper(bean);
    if (descriptionDirectoryName == null) {
      descriptionDirectoryName = IntentionManagerImpl.getDescriptionDirectoryName(intentionAction);
    }

    IntentionActionMetaData data = new IntentionActionMetaData(intentionAction, getClassLoader(intentionAction), categories, descriptionDirectoryName);

    final TextDescriptor description = data.getDescription();

    try {
      String descriptionText = description.getText().toLowerCase();
      descriptionText = HTML_PATTERN.matcher(descriptionText).replaceAll(" ");
      final Set<String> words = processor.getProcessedWordsWithoutStemming(descriptionText);
      words.addAll(processor.getProcessedWords(data.getFamily()));
      for (String word : words) {
        processor.addOption(word, data.getFamily(), data.getFamily(), IntentionSettingsConfigurable.HELP_ID, CodeInsightBundle.message("intention.settings"));
      }
    }
    catch (IOException e) {
      LOG.error(e);
    }
  }
}
 
Example #12
Source File: ShowIntentionsPass.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void getActionsToShow(@Nonnull final Editor hostEditor,
                                     @Nonnull final PsiFile hostFile,
                                     @Nonnull final IntentionsInfo intentions,
                                     int passIdToShowIntentionsFor,
                                     boolean queryIntentionActions) {
  int offset = hostEditor.getCaretModel().getOffset();
  final PsiElement psiElement = hostFile.findElementAt(offset);
  if (psiElement != null) PsiUtilCore.ensureValid(psiElement);

  intentions.setOffset(offset);
  final Project project = hostFile.getProject();

  List<HighlightInfo.IntentionActionDescriptor> fixes = getAvailableFixes(hostEditor, hostFile, passIdToShowIntentionsFor, offset);
  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 {
    fillIntentionsInfoForHighlightInfo(infoAtCursor, intentions, fixes);
  }

  if (queryIntentionActions) {
    PsiFile injectedFile = InjectedLanguageUtil.findInjectedPsiNoCommit(hostFile, offset);
    for (final IntentionAction action : IntentionManager.getInstance().getAvailableIntentionActions()) {
      Pair<PsiFile, Editor> place =
              ShowIntentionActionsHandler.chooseBetweenHostAndInjected(hostFile, hostEditor, injectedFile, (psiFile, editor) -> ShowIntentionActionsHandler.availableFor(psiFile, editor, action));

      if (place != null) {
        List<IntentionAction> enableDisableIntentionAction = new ArrayList<>();
        enableDisableIntentionAction.add(new EnableDisableIntentionAction(action));
        enableDisableIntentionAction.add(new EditIntentionSettingsAction(action));
        HighlightInfo.IntentionActionDescriptor descriptor = new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null);
        if (!fixes.contains(descriptor)) {
          intentions.intentionsToShow.add(descriptor);
        }
      }
    }

    for (IntentionMenuContributor extension : IntentionMenuContributor.EP_NAME.getExtensionList()) {
      extension.collectActions(hostEditor, hostFile, intentions, passIdToShowIntentionsFor, offset);
    }
  }

  intentions.filterActions(hostFile);
}