Java Code Examples for com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#getInstance()

The following examples show how to use com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#getInstance() . 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: StatusBarUpdater.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void updateStatus() {
  if (myProject.isDisposed()) {
    return;
  }

  Editor editor = FileEditorManager.getInstance(myProject).getSelectedTextEditor();
  if (editor == null || !editor.getContentComponent().hasFocus()) {
    return;
  }

  final Document document = editor.getDocument();
  if (document instanceof DocumentEx && ((DocumentEx)document).isInBulkUpdate()) return;

  int offset = editor.getCaretModel().getOffset();
  DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(myProject);
  HighlightInfo info = ((DaemonCodeAnalyzerImpl)codeAnalyzer).findHighlightByOffset(document, offset, false, HighlightSeverity.WARNING);
  String text = info != null && info.getDescription() != null ? info.getDescription() : "";

  StatusBar statusBar = WindowManager.getInstance().getStatusBar(editor.getContentComponent(), myProject);
  if (statusBar instanceof StatusBarEx) {
    StatusBarEx barEx = (StatusBarEx)statusBar;
    if (!text.equals(barEx.getInfo())) {
      statusBar.setInfo(text, "updater");
    }
  }
}
 
Example 2
Source File: CodeSmellDetectorImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private List<CodeSmellInfo> findCodeSmells(@Nonnull final VirtualFile file, @Nonnull final ProgressIndicator progress) {
  final List<CodeSmellInfo> result = Collections.synchronizedList(new ArrayList<CodeSmellInfo>());

  final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(myProject);
  final ProgressIndicator daemonIndicator = new DaemonProgressIndicator();
  ((ProgressIndicatorEx)progress).addStateDelegate(new AbstractProgressIndicatorExBase() {
    @Override
    public void cancel() {
      super.cancel();
      daemonIndicator.cancel();
    }
  });
  ProgressManager.getInstance().runProcess(new Runnable() {
    @Override
    public void run() {
      DumbService.getInstance(myProject).runReadActionInSmartMode(new Runnable() {
        @Override
        public void run() {
          final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
          final Document document = FileDocumentManager.getInstance().getDocument(file);
          if (psiFile == null || document == null) {
            return;
          }
          List<HighlightInfo> infos = codeAnalyzer.runMainPasses(psiFile, document, daemonIndicator);
          convertErrorsAndWarnings(infos, result, document);
        }
      });
    }
  }, daemonIndicator);

  return result;
}
 
Example 3
Source File: GotoNextErrorHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void showMessageWhenNoHighlights(Project project, PsiFile file, Editor editor) {
  DaemonCodeAnalyzerImpl codeHighlighter = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(project);
  String message = codeHighlighter.isErrorAnalyzingFinished(file)
                   ? InspectionsBundle.message("no.errors.found.in.this.file")
                   : InspectionsBundle.message("error.analysis.is.in.progress");
  HintManager.getInstance().showInformationHint(editor, message);
}
 
Example 4
Source File: ShowErrorDescriptionHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file) {
  int offset = editor.getCaretModel().getOffset();
  DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
  HighlightInfo info = ((DaemonCodeAnalyzerImpl)codeAnalyzer).findHighlightByOffset(editor.getDocument(), offset, false);
  if (info != null) {
    DaemonTooltipUtil.showInfoTooltip(info, editor, editor.getCaretModel().getOffset(), myWidth, myRequestFocus, true);
  }
}
 
Example 5
Source File: TogglePopupHintsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(@Nonnull AnActionEvent e) {
  PsiFile psiFile = getTargetFile(e.getDataContext());
  LOG.assertTrue(psiFile != null);
  Project project = e.getProject();
  LOG.assertTrue(project != null);
  DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
  codeAnalyzer.setImportHintsEnabled(psiFile, !codeAnalyzer.isImportHintsEnabled(psiFile));
}
 
Example 6
Source File: ShowIntentionActionsHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void invoke(@Nonnull final Project project, @Nonnull Editor editor, @Nonnull PsiFile file, boolean showFeedbackOnEmptyMenu) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();
  if (editor instanceof EditorWindow) {
    editor = ((EditorWindow)editor).getDelegate();
    file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
  }

  final LookupEx lookup = LookupManager.getActiveLookup(editor);
  if (lookup != null) {
    lookup.showElementActions(null);
    return;
  }

  final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(project);
  letAutoImportComplete(editor, file, codeAnalyzer);

  ShowIntentionsPass.IntentionsInfo intentions = ShowIntentionsPass.getActionsToShow(editor, file, true);
  IntentionsUI.getInstance(project).hide();

  if (HintManagerImpl.getInstanceImpl().performCurrentQuestionAction()) return;

  //intentions check isWritable before modification: if (!file.isWritable()) return;

  TemplateState state = TemplateManagerImpl.getTemplateState(editor);
  if (state != null && !state.isFinished()) {
    return;
  }

  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  Editor finalEditor = editor;
  PsiFile finalFile = file;
  showIntentionHint(project, finalEditor, finalFile, intentions, showFeedbackOnEmptyMenu);
}
 
Example 7
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);
}
 
Example 8
Source File: ShowErrorDescriptionAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean isEnabledForFile(Project project, Editor editor, PsiFile file) {
  DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
  HighlightInfo info =
    ((DaemonCodeAnalyzerImpl)codeAnalyzer).findHighlightByOffset(editor.getDocument(), editor.getCaretModel().getOffset(), false);
  return info != null && info.getDescription() != null;
}