Java Code Examples for com.intellij.codeInsight.daemon.impl.HighlightInfo#IntentionActionDescriptor

The following examples show how to use com.intellij.codeInsight.daemon.impl.HighlightInfo#IntentionActionDescriptor . 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: Inspect.java    From neovim-intellij-complete with MIT License 6 votes vote down vote up
/**
 * Runs code analyzis and returns all problems found under cursor (row and col).
 *
 * @param path
 * @param fileContent
 * @param row
 * @param col
 * @return
 */
public static List<HighlightInfo.IntentionActionDescriptor> getFixes(
        final String path, @Nullable final String fileContent, final int row, final int col) {
    List<HighlightInfo.IntentionActionDescriptor> fixes = new ArrayList<>();
    Pair<Document, List<HighlightInfo>> problems = getProblems(path, fileContent);
    Document doc = problems.getFirst();
    for (HighlightInfo h : problems.getSecond()) {
        if (h.quickFixActionRanges == null) continue;
        for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> p : h.quickFixActionRanges) {
            int offset = EmbeditorUtil.lineAndColumnToOffset(doc, row, col);
            if (p.getSecond().contains(offset)) {
                fixes.add(p.getFirst());
            }
        }
    }
    return fixes;
}
 
Example 2
Source File: Inspect.java    From neovim-intellij-complete with MIT License 6 votes vote down vote up
/**
 * Invokes action in intentionActionDescriptor on file found in path and writes the file to disk.
 *
 * @param path
 * @param fileContent
 * @param intentionActionDescriptor
 * @return
 */
public static String doFix(String path, @Nullable String fileContent, HighlightInfo.IntentionActionDescriptor intentionActionDescriptor) {
    UIUtil.invokeAndWaitIfNeeded((Runnable)() -> {
        PsiFile psiFile = EmbeditorUtil.findTargetFile(path);
        Project project = psiFile.getProject();

        PsiFile fileCopy = fileContent != null
                ? EmbeditorUtil.createDummyPsiFile(project, fileContent, psiFile)
                : EmbeditorUtil.createDummyPsiFile(project, psiFile.getText(), psiFile);

        VirtualFile targetVirtualFile =  psiFile.getVirtualFile();
        Document document = fileCopy.getViewProvider().getDocument();

        Editor editor = EditorFactory.getInstance().createEditor(document, project, targetVirtualFile, false);

        intentionActionDescriptor.getAction().invoke(project, editor, fileCopy);

        FileDocumentManager.getInstance().saveDocument(psiFile.getViewProvider().getDocument());
    });
    return null;
}
 
Example 3
Source File: PantsScalaHighlightVisitor.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
private void tryToExtendInfo(@NotNull HighlightInfo info, @NotNull PsiFile containingFile) {
  List<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>> actionRanges = info.quickFixActionRanges;
  if (actionRanges == null) {
    return;
  }
  for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> actionAndRange : actionRanges) {
    final TextRange textRange = actionAndRange.getSecond();
    final HighlightInfo.IntentionActionDescriptor actionDescriptor = actionAndRange.getFirst();
    final IntentionAction action = actionDescriptor.getAction();
    if (action instanceof CreateTypeDefinitionQuickFix) {
      final String className = textRange.substring(containingFile.getText());
      final List<PantsQuickFix> missingDependencyFixes =
        PantsUnresolvedReferenceFixFinder.findMissingDependencies(className, containingFile);
      for (PantsQuickFix fix : missingDependencyFixes) {
        info.registerFix(fix, null, fix.getName(), textRange, null);
      }
      if (!missingDependencyFixes.isEmpty()) {
        // we should add only one fix per info
        return;
      }
    }
  }
}
 
Example 4
Source File: GlobalInspectionUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void createProblem(@Nonnull PsiElement elt,
                                 @Nonnull HighlightInfo info,
                                 TextRange range,
                                 @javax.annotation.Nullable ProblemGroup problemGroup,
                                 @Nonnull InspectionManager manager,
                                 @Nonnull ProblemDescriptionsProcessor problemDescriptionsProcessor,
                                 @Nonnull GlobalInspectionContext globalContext) {
  List<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>();
  if (info.quickFixActionRanges != null) {
    for (Pair<HighlightInfo.IntentionActionDescriptor, TextRange> actionRange : info.quickFixActionRanges) {
      final IntentionAction action = actionRange.getFirst().getAction();
      if (action instanceof LocalQuickFix) {
        fixes.add((LocalQuickFix)action);
      }
    }
  }
  ProblemDescriptor descriptor = manager.createProblemDescriptor(elt, range, createInspectionMessage(StringUtil.notNullize(info.getDescription())),
                                                                 HighlightInfo.convertType(info.type), false,
                                                                 fixes.isEmpty() ? null : fixes.toArray(new LocalQuickFix[fixes.size()]));
  descriptor.setProblemGroup(problemGroup);
  problemDescriptionsProcessor.addProblemElement(
          GlobalInspectionContextUtil.retrieveRefElement(elt, globalContext),
          descriptor
  );
}
 
Example 5
Source File: NeovimIntellijComplete.java    From neovim-intellij-complete with MIT License 5 votes vote down vote up
@NeovimHandler("IntellijProblems")
public Fix[] intellijProblems(String path, List<String> lines, final int row, final int col) {
    final String fileContent = String.join("\n", lines) ;
    List<HighlightInfo.IntentionActionDescriptor> allFixes = Inspect.getFixes(path, fileContent, row, col);
    List<Fix> fixes = new ArrayList<>();
    for (int i = 0; i < allFixes.size(); i++) {
        HighlightInfo.IntentionActionDescriptor d = allFixes.get(i);
        if (d.getAction().getText().length() == 0) continue;
        fixes.add(new Fix(d.getAction().getText(), i, d));
    }
    mCachedFixes = fixes.toArray(new Fix[fixes.size()]);
    return mCachedFixes;
}
 
Example 6
Source File: PantsHighlightingIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
protected <T> T findIntention(@NotNull HighlightInfo info, @NotNull Class<T> aClass) {
  for (Pair<HighlightInfo.IntentionActionDescriptor, RangeMarker> pair : info.quickFixActionMarkers) {
    final HighlightInfo.IntentionActionDescriptor intensionDescriptor = pair.getFirst();
    final IntentionAction action = intensionDescriptor.getAction();
    if (aClass.isInstance(action)) {
      //noinspection unchecked
      return (T)action;
    }
  }
  return null;
}
 
Example 7
Source File: CachedIntentions.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean addActionsTo(@Nonnull List<? extends HighlightInfo.IntentionActionDescriptor> newDescriptors, @Nonnull Set<? super IntentionActionWithTextCaching> cachedActions) {
  boolean changed = false;
  for (HighlightInfo.IntentionActionDescriptor descriptor : newDescriptors) {
    changed |= cachedActions.add(wrapAction(descriptor, myFile, myFile, myEditor));
  }
  return changed;
}
 
Example 8
Source File: CachedIntentions.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
IntentionActionWithTextCaching wrapAction(@Nonnull HighlightInfo.IntentionActionDescriptor descriptor,
                                          @Nonnull PsiElement element,
                                          @Nonnull PsiFile containingFile,
                                          @Nullable Editor containingEditor) {
  IntentionActionWithTextCaching cachedAction = new IntentionActionWithTextCaching(descriptor, (cached, action) -> {
    if (action instanceof QuickFixWrapper) {
      // remove only inspection fixes after invocation,
      // since intention actions might be still available
      removeActionFromCached(cached);
      markInvoked(action);
    }
  });
  final List<IntentionAction> options = descriptor.getOptions(element, containingEditor);
  if (options == null) return cachedAction;
  for (IntentionAction option : options) {
    Editor editor = ObjectUtils.chooseNotNull(myEditor, containingEditor);
    if (editor == null) continue;
    Pair<PsiFile, Editor> availableIn = ShowIntentionActionsHandler.chooseBetweenHostAndInjected(myFile, editor, containingFile, (f, e) -> ShowIntentionActionsHandler.availableFor(f, e, option));
    if (availableIn == null) continue;
    IntentionActionWithTextCaching textCaching = new IntentionActionWithTextCaching(option);
    boolean isErrorFix = myErrorFixes.contains(textCaching);
    if (isErrorFix) {
      cachedAction.addErrorFix(option);
    }
    boolean isInspectionFix = myInspectionFixes.contains(textCaching);
    if (isInspectionFix) {
      cachedAction.addInspectionFix(option);
    }
    else {
      cachedAction.addIntention(option);
    }
  }
  return cachedAction;
}
 
Example 9
Source File: ApplyIntentionAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static ApplyIntentionAction[] getAvailableIntentions(final Editor editor, final PsiFile file) {
  final ShowIntentionsPass.IntentionsInfo info = new ShowIntentionsPass.IntentionsInfo();
  ApplicationManager.getApplication().runReadAction(new Runnable() {
    @Override
    public void run() {
      ShowIntentionsPass.getActionsToShow(editor, file, info, -1);
    }
  });
  if (info.isEmpty()) return null;

  final List<HighlightInfo.IntentionActionDescriptor> actions = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
  actions.addAll(info.errorFixesToShow);
  actions.addAll(info.inspectionFixesToShow);
  actions.addAll(info.intentionsToShow);

  final ApplyIntentionAction[] result = new ApplyIntentionAction[actions.size()];
  for (int i = 0; i < result.length; i++) {
    final HighlightInfo.IntentionActionDescriptor descriptor = actions.get(i);
    final String actionText = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
      @Override
      public String compute() {
        return descriptor.getAction().getText();
      }
    });
    result[i] = new ApplyIntentionAction(descriptor, actionText, editor, file);
  }
  return result;
}
 
Example 10
Source File: NeovimIntellijComplete.java    From neovim-intellij-complete with MIT License 4 votes vote down vote up
public Fix(String description, int fixId, HighlightInfo.IntentionActionDescriptor action) {
    this.description = description;
    this.fixId = fixId;
    this.action = action;
}
 
Example 11
Source File: NeovimIntellijComplete.java    From neovim-intellij-complete with MIT License 4 votes vote down vote up
public HighlightInfo.IntentionActionDescriptor getAction() {
    return action;
}
 
Example 12
Source File: IntentionActionWithTextCaching.java    From consulo with Apache License 2.0 4 votes vote down vote up
IntentionActionWithTextCaching(@Nonnull HighlightInfo.IntentionActionDescriptor descriptor, @Nullable BiConsumer<IntentionActionWithTextCaching,IntentionAction> markInvoked){
  this(descriptor.getAction(), descriptor.getDisplayName(), descriptor.getIcon(), markInvoked);
}
 
Example 13
Source File: ApplyIntentionAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
public ApplyIntentionAction(final HighlightInfo.IntentionActionDescriptor descriptor, String text, Editor editor, PsiFile file) {
  this(descriptor.getAction(), text, editor, file);
}