Java Code Examples for com.intellij.codeInsight.lookup.LookupManager#getActiveLookup()

The following examples show how to use com.intellij.codeInsight.lookup.LookupManager#getActiveLookup() . 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: EscapeHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext) {
  final SelectionModel selectionModel = editor.getSelectionModel();
  if (selectionModel.hasSelection()) {
    final TemplateState state = TemplateManagerImpl.getTemplateState(editor);
    if (state != null && editor.getUserData(InplaceRefactoring.INPLACE_RENAMER) != null) {
      final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
      if (lookup != null) {
        selectionModel.removeSelection();
        lookup.hide();
        return;
      }
    }
  }

  myOriginalHandler.execute(editor, dataContext);
}
 
Example 2
Source File: CompletionAutoPopupHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public Result checkAutoPopup(char charTyped, @Nonnull final Project project, @Nonnull final Editor editor, @Nonnull final PsiFile file) {
  LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);

  if (LOG.isDebugEnabled()) {
    LOG.debug("checkAutoPopup: character=" + charTyped + ";");
    LOG.debug("phase=" + CompletionServiceImpl.getCompletionPhase());
    LOG.debug("lookup=" + lookup);
    LOG.debug("currentCompletion=" + CompletionServiceImpl.getCompletionService().getCurrentCompletion());
  }

  if (lookup != null) {
    if (editor.getSelectionModel().hasSelection()) {
      lookup.performGuardedChange(() -> EditorModificationUtil.deleteSelectedText(editor));
    }
    return Result.STOP;
  }

  if (Character.isLetterOrDigit(charTyped) || charTyped == '_') {
    AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
    return Result.STOP;
  }

  return Result.CONTINUE;
}
 
Example 3
Source File: EscapeHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext) {
  TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
  if (templateState != null && !templateState.isFinished()) {
    SelectionModel selectionModel = editor.getSelectionModel();
    LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);

    // the idea behind lookup checking is that if there is a preselected value in lookup
    // then user might want just to close lookup but not finish a template.
    // E.g. user wants to move to the next template segment by Tab without completion invocation.
    // If there is no selected value in completion that user definitely wants to finish template
    boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
    if (!selectionModel.hasSelection() && lookupIsEmpty) {
      CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
      templateState.gotoEnd(true);
      return;
    }
  }

  if (myOriginalHandler.isEnabled(editor, dataContext)) {
    myOriginalHandler.execute(editor, dataContext);
  }
}
 
Example 4
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void restoreOldCaretPositionAndSelection(final int offset) {
  //move to old offset
  Runnable runnable = new Runnable() {
    @Override
    public void run() {
      myEditor.getCaretModel().moveToOffset(restoreCaretOffset(offset));
      restoreSelection();
    }
  };

  final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(myEditor);
  if (lookup != null && lookup.getLookupStart() <= (restoreCaretOffset(offset))) {
    lookup.setFocusDegree(LookupImpl.FocusDegree.UNFOCUSED);
    lookup.performGuardedChange(runnable);
  }
  else {
    runnable.run();
  }
}
 
Example 5
Source File: VariableInplaceRenameHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
  PsiElement element = PsiElementRenameHandler.getElement(dataContext);
  if (element == null) {
    if (LookupManager.getActiveLookup(editor) != null) {
      final PsiElement elementUnderCaret = file.findElementAt(editor.getCaretModel().getOffset());
      if (elementUnderCaret != null) {
        final PsiElement parent = elementUnderCaret.getParent();
        if (parent instanceof PsiReference) {
          element = ((PsiReference)parent).resolve();
        } else {
          element = PsiTreeUtil.getParentOfType(elementUnderCaret, PsiNamedElement.class);
        }
      }
      if (element == null) return;
    } else {
      return;
    }
  }
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  if (checkAvailable(element, editor, dataContext)) {
    doRename(element, editor, dataContext);
  }
}
 
Example 6
Source File: PopupPositionManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void positionPopupInBestPosition(final JBPopup hint, @Nullable final Editor editor, @Nullable DataContext dataContext, @Nonnull Position... relationToExistingPopup) {
  final LookupEx lookup = LookupManager.getActiveLookup(editor);
  if (lookup != null && lookup.getCurrentItem() != null && lookup.getComponent().isShowing()) {
    new PositionAdjuster(lookup.getComponent()).adjust(hint, relationToExistingPopup);
    lookup.addLookupListener(new LookupListener() {
      @Override
      public void lookupCanceled(@Nonnull LookupEvent event) {
        if (hint.isVisible()) {
          hint.cancel();
        }
      }
    });
    return;
  }

  final PositionAdjuster positionAdjuster = createPositionAdjuster(hint);
  if (positionAdjuster != null) {
    positionAdjuster.adjust(hint, relationToExistingPopup);
    return;
  }

  if (editor != null && editor.getComponent().isShowing() && editor instanceof EditorEx) {
    dataContext = ((EditorEx)editor).getDataContext();
  }

  if (dataContext != null) {
    if (hint.canShow()) {
      hint.showInBestPositionFor(dataContext);
    }
    else {
      hint.setLocation(hint.getBestPositionFor(dataContext));
    }
  }
}
 
Example 7
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean canMoveInEditor(final boolean next) {
  final Editor consoleEditor = myConsole.getCurrentEditor();
  final Document document = consoleEditor.getDocument();
  final CaretModel caretModel = consoleEditor.getCaretModel();

  if (LookupManager.getActiveLookup(consoleEditor) != null) return false;

  if (next) {
    return document.getLineNumber(caretModel.getOffset()) == 0;
  }
  else {
    final int lineCount = document.getLineCount();
    return (lineCount == 0 || document.getLineNumber(caretModel.getOffset()) == lineCount - 1) && StringUtil.isEmptyOrSpaces(document.getText().substring(caretModel.getOffset()));
  }
}
 
Example 8
Source File: UpDownHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void update(AnActionEvent e) {
  final LookupEx lookup;
  if (myInput instanceof EditorTextField) {
    lookup = LookupManager.getActiveLookup(((EditorTextField)myInput).getEditor());
  } else if (myInput instanceof EditorComponentImpl) {
    lookup = LookupManager.getActiveLookup(((EditorComponentImpl)myInput).getEditor());
  } else {
    lookup = null;
  }

  e.getPresentation().setEnabled(lookup == null);
}
 
Example 9
Source File: LangIndentSelectionAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isEnabled(Editor editor, DataContext dataContext) {
  if (!originalIsEnabled(editor, wantSelection())) return false;
  if (LookupManager.getActiveLookup(editor) != null) return false;

  PsiFile psiFile = dataContext.getData(CommonDataKeys.PSI_FILE);
  if (psiFile != null && NextPrevParameterAction.hasSutablePolicy(editor, psiFile)) return false;

  return true;
}
 
Example 10
Source File: ConsoleExecuteAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public final void update(@Nonnull AnActionEvent e) {
  EditorEx editor = myConsoleView.getConsoleEditor();
  boolean enabled = !editor.isRendererMode() && isEnabled() &&
                    (myExecuteActionHandler.isEmptyCommandExecutionAllowed() || !StringUtil.isEmptyOrSpaces(editor.getDocument().getCharsSequence()));
  if (enabled) {
    Lookup lookup = LookupManager.getActiveLookup(editor);
    // we should check getCurrentItem() also - fast typing could produce outdated lookup, such lookup reports isCompletion() true
    enabled = lookup == null || !lookup.isCompletion() || lookup.getCurrentItem() == null ||
              (lookup instanceof LookupImpl && ((LookupImpl)lookup).getFocusDegree() == LookupImpl.FocusDegree.UNFOCUSED);
  }

  e.getPresentation().setEnabled(enabled);
}
 
Example 11
Source File: HomeHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void doExecute(Editor editor, Caret caret, DataContext dataContext){
  LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
  if (lookup == null || !lookup.isFocused()) {
    myOriginalHandler.execute(editor, caret, dataContext);
    return;
  }

  lookup.markSelectionTouched();
  ScrollingUtil.moveHome(lookup.getList());
}
 
Example 12
Source File: ChooseItemAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnabled(Editor editor, DataContext dataContext) {
  LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
  if (lookup == null) return false;
  if (!lookup.isAvailableToUser()) return false;
  if (focusedOnly && lookup.getFocusDegree() == LookupImpl.FocusDegree.UNFOCUSED) return false;
  if (finishingChar == Lookup.REPLACE_SELECT_CHAR) {
    return !lookup.getItems().isEmpty();
  }

  return true;
}
 
Example 13
Source File: ChooseItemAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(@Nonnull final Editor editor, final DataContext dataContext) {
  final LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
  if (lookup == null) {
    throw new AssertionError("The last lookup disposed at: " + LookupImpl.getLastLookupDisposeTrace() + "\n-----------------------\n");
  }

  if ((finishingChar == Lookup.NORMAL_SELECT_CHAR || finishingChar == Lookup.REPLACE_SELECT_CHAR) &&
      hasTemplatePrefix(lookup, finishingChar)) {
    lookup.hideLookup(true);

    ExpandLiveTemplateCustomAction.createExpandTemplateHandler(finishingChar).execute(editor, null, dataContext);

    return;
  }

  if (finishingChar == Lookup.NORMAL_SELECT_CHAR) {
    if (!lookup.isFocused()) {
      FeatureUsageTracker.getInstance().triggerFeatureUsed(CodeCompletionFeatures.EDITING_COMPLETION_CONTROL_ENTER);
    }
  } else if (finishingChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
    FeatureUsageTracker.getInstance().triggerFeatureUsed(CodeCompletionFeatures.EDITING_COMPLETION_FINISH_BY_SMART_ENTER);
  } else if (finishingChar == Lookup.REPLACE_SELECT_CHAR) {
    FeatureUsageTracker.getInstance().triggerFeatureUsed(CodeCompletionFeatures.EDITING_COMPLETION_REPLACE);
  } else if (finishingChar == '.')  {
    FeatureUsageTracker.getInstance().triggerFeatureUsed(CodeCompletionFeatures.EDITING_COMPLETION_FINISH_BY_CONTROL_DOT);
  }

  lookup.finishLookup(finishingChar);
}
 
Example 14
Source File: EndHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void doExecute(Editor editor, Caret caret, DataContext dataContext){
  LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
  if (lookup == null || !lookup.isFocused()) {
    myOriginalHandler.execute(editor, caret, dataContext);
    return;
  }

  lookup.markSelectionTouched();
  ScrollingUtil.moveEnd(lookup.getList());
  lookup.refreshUi(false, true);
}
 
Example 15
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
private Component getPopupAnchor() {
  LookupEx lookup = myManager == null ? null : LookupManager.getActiveLookup(myManager.getEditor());

  if (lookup != null && lookup.getCurrentItem() != null && lookup.getComponent().isShowing()) {
    return lookup.getComponent();
  }
  Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
  JBPopup popup = PopupUtil.getPopupContainerFor(focusOwner);
  if (popup != null && popup != myHint && !popup.isDisposed()) {
    return popup.getContent();
  }
  return null;
}
 
Example 16
Source File: CSharpMemberInplaceRenameHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredReadAction
protected boolean isAvailable(PsiElement element, Editor editor, PsiFile file)
{
	final PsiElement nameSuggestionContext = file.findElementAt(editor.getCaretModel().getOffset());
	if(element == null && LookupManager.getActiveLookup(editor) != null)
	{
		element = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiNamedElement.class);
	}
	final RefactoringSupportProvider supportProvider = element != null ? LanguageRefactoringSupport.INSTANCE.forLanguage(element.getLanguage()) : null;
	return editor.getSettings().isVariableInplaceRenameEnabled() && supportProvider != null && supportProvider.isMemberInplaceRenameAvailable(element, nameSuggestionContext) && element
			instanceof CSharpNamedElement;
}
 
Example 17
Source File: LookupActionHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isEnabledForCaret(@Nonnull Editor editor, @Nonnull Caret caret, DataContext dataContext) {
  return LookupManager.getActiveLookup(editor) != null;
}
 
Example 18
Source File: LookupActionHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnabledForCaret(@Nonnull Editor editor, @Nonnull Caret caret, DataContext dataContext) {
  LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(editor);
  return lookup != null || myOriginalHandler.isEnabled(editor, caret, dataContext);
}
 
Example 19
Source File: LookupActionHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean executeInCommand(@Nonnull Editor editor, DataContext dataContext) {
  return LookupManager.getActiveLookup(editor) == null;
}
 
Example 20
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public LookupImpl getLookup() {
  return (LookupImpl)LookupManager.getActiveLookup(myEditor);
}