Search in sources :

Example 6 with Lookup

use of com.intellij.codeInsight.lookup.Lookup in project intellij-community by JetBrains.

the class DocumentationManager method showInPopup.

private void showInPopup(@NotNull final PsiElement element, boolean requestFocus, PopupUpdateProcessor updateProcessor, final PsiElement originalElement, @Nullable final Runnable closeCallback) {
    final DocumentationComponent component = myTestDocumentationComponent == null ? new DocumentationComponent(this) : myTestDocumentationComponent;
    component.setNavigateCallback(psiElement -> {
        final AbstractPopup jbPopup = (AbstractPopup) getDocInfoHint();
        if (jbPopup != null) {
            final String title = getTitle(psiElement, false);
            jbPopup.setCaption(title);
            AccessibleContextUtil.setName(component, title);
        }
    });
    Processor<JBPopup> pinCallback = popup -> {
        createToolWindow(element, originalElement);
        myToolWindow.setAutoHide(false);
        popup.cancel();
        return false;
    };
    ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            createToolWindow(element, originalElement);
            final JBPopup hint = getDocInfoHint();
            if (hint != null && hint.isVisible())
                hint.cancel();
        }
    };
    List<Pair<ActionListener, KeyStroke>> actions = ContainerUtil.newSmartList();
    AnAction quickDocAction = ActionManager.getInstance().getAction(IdeActions.ACTION_QUICK_JAVADOC);
    for (Shortcut shortcut : quickDocAction.getShortcutSet().getShortcuts()) {
        if (!(shortcut instanceof KeyboardShortcut))
            continue;
        actions.add(Pair.create(actionListener, ((KeyboardShortcut) shortcut).getFirstKeyStroke()));
    }
    boolean hasLookup = LookupManager.getActiveLookup(myEditor) != null;
    final JBPopup hint = JBPopupFactory.getInstance().createComponentPopupBuilder(component, component).setProject(element.getProject()).addListener(updateProcessor).addUserData(updateProcessor).setKeyboardActions(actions).setDimensionServiceKey(myProject, JAVADOC_LOCATION_AND_SIZE, false).setResizable(true).setMovable(true).setRequestFocus(requestFocus).setCancelOnClickOutside(// otherwise selecting lookup items by mouse would close the doc
    !hasLookup).setTitle(getTitle(element, false)).setCouldPin(pinCallback).setModalContext(false).setCancelCallback(() -> {
        myCloseOnSneeze = false;
        if (closeCallback != null) {
            closeCallback.run();
        }
        if (fromQuickSearch()) {
            ((ChooseByNameBase.JPanelProvider) myPreviouslyFocused.getParent()).unregisterHint();
        }
        Disposer.dispose(component);
        myEditor = null;
        myPreviouslyFocused = null;
        return Boolean.TRUE;
    }).setKeyEventHandler(e -> {
        if (myCloseOnSneeze) {
            closeDocHint();
        }
        if (AbstractPopup.isCloseRequest(e) && getDocInfoHint() != null) {
            closeDocHint();
            return true;
        }
        return false;
    }).createPopup();
    component.setHint(hint);
    if (myEditor == null) {
        // subsequent invocation of javadoc popup from completion will have myEditor == null because of cancel invoked, 
        // so reevaluate the editor for proper popup placement
        Lookup lookup = LookupManager.getInstance(myProject).getActiveLookup();
        myEditor = lookup != null ? lookup.getEditor() : null;
    }
    fetchDocInfo(getDefaultCollector(element, originalElement), component);
    myDocInfoHintRef = new WeakReference<>(hint);
    if (fromQuickSearch() && myPreviouslyFocused != null) {
        ((ChooseByNameBase.JPanelProvider) myPreviouslyFocused.getParent()).registerHint(hint);
    }
}
Also used : Language(com.intellij.lang.Language) com.intellij.openapi.util(com.intellij.openapi.util) AccessibleContextUtil(com.intellij.util.ui.accessibility.AccessibleContextUtil) BaseNavigateToSourceAction(com.intellij.ide.actions.BaseNavigateToSourceAction) Lookup(com.intellij.codeInsight.lookup.Lookup) VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.lang.documentation(com.intellij.lang.documentation) ModalityState(com.intellij.openapi.application.ModalityState) ProjectSettingsService(com.intellij.openapi.roots.ui.configuration.ProjectSettingsService) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) ShowQuickDocInfoAction(com.intellij.codeInsight.documentation.actions.ShowQuickDocInfoAction) Logger(com.intellij.openapi.diagnostic.Logger) Extensions(com.intellij.openapi.extensions.Extensions) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) ScrollingUtil(com.intellij.ui.ScrollingUtil) ParameterInfoController(com.intellij.codeInsight.hint.ParameterInfoController) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) LookupManager(com.intellij.codeInsight.lookup.LookupManager) PreviewManager(com.intellij.openapi.preview.PreviewManager) OrderEntry(com.intellij.openapi.roots.OrderEntry) Content(com.intellij.ui.content.Content) AbstractPopup(com.intellij.ui.popup.AbstractPopup) LookupEx(com.intellij.codeInsight.lookup.LookupEx) BrowserUtil(com.intellij.ide.BrowserUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IdeFocusManager.getGlobalInstance(com.intellij.openapi.wm.IdeFocusManager.getGlobalInstance) com.intellij.psi(com.intellij.psi) WindowManagerEx(com.intellij.openapi.wm.ex.WindowManagerEx) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) ActionListener(java.awt.event.ActionListener) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) ToolWindowId(com.intellij.openapi.wm.ToolWindowId) Project(com.intellij.openapi.project.Project) PopupPositionManager(com.intellij.ui.popup.PopupPositionManager) WeakReference(java.lang.ref.WeakReference) DataManager(com.intellij.ide.DataManager) LibraryUtil(com.intellij.openapi.roots.libraries.LibraryUtil) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ChooseByNameBase(com.intellij.ide.util.gotoByName.ChooseByNameBase) Editor(com.intellij.openapi.editor.Editor) ActionEvent(java.awt.event.ActionEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) TestOnly(org.jetbrains.annotations.TestOnly) java.awt(java.awt) LanguageDocumentation(com.intellij.lang.LanguageDocumentation) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) CommandProcessor(com.intellij.openapi.command.CommandProcessor) SymbolPresentationUtil(com.intellij.psi.presentation.java.SymbolPresentationUtil) HintManagerImpl(com.intellij.codeInsight.hint.HintManagerImpl) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) AbstractPopup(com.intellij.ui.popup.AbstractPopup) ActionEvent(java.awt.event.ActionEvent) ActionListener(java.awt.event.ActionListener) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) Lookup(com.intellij.codeInsight.lookup.Lookup) JBPopup(com.intellij.openapi.ui.popup.JBPopup)

Example 7 with Lookup

use of com.intellij.codeInsight.lookup.Lookup in project intellij-community by JetBrains.

the class DocumentationManager method getElementFromLookup.

@Nullable
public PsiElement getElementFromLookup(final Editor editor, @Nullable final PsiFile file) {
    final Lookup activeLookup = LookupManager.getInstance(myProject).getActiveLookup();
    if (activeLookup != null) {
        LookupElement item = activeLookup.getCurrentItem();
        if (item != null) {
            int offset = editor.getCaretModel().getOffset();
            if (offset > 0 && offset == editor.getDocument().getTextLength())
                offset--;
            PsiReference ref = TargetElementUtil.findReference(editor, offset);
            PsiElement contextElement = file == null ? null : file.findElementAt(offset);
            PsiElement targetElement = ref != null ? ref.getElement() : contextElement;
            if (targetElement != null) {
                PsiUtilCore.ensureValid(targetElement);
            }
            DocumentationProvider documentationProvider = getProviderFromElement(file);
            PsiManager psiManager = PsiManager.getInstance(myProject);
            return documentationProvider.getDocumentationElementForLookupItem(psiManager, item.getObject(), targetElement);
        }
    }
    return null;
}
Also used : Lookup(com.intellij.codeInsight.lookup.Lookup) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with Lookup

use of com.intellij.codeInsight.lookup.Lookup in project intellij-community by JetBrains.

the class ShowParameterInfoHandler method invoke.

public static void invoke(final Project project, final Editor editor, PsiFile file, int lbraceOffset, PsiElement highlightedElement, boolean requestFocus) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement psiElement = findAnyElementAt(file, offset);
    if (psiElement == null)
        return;
    final ShowParameterInfoContext context = new ShowParameterInfoContext(editor, project, file, offset, lbraceOffset, requestFocus);
    context.setHighlightedElement(highlightedElement);
    context.setRequestFocus(requestFocus);
    final Language language = psiElement.getLanguage();
    ParameterInfoHandler[] handlers = getHandlers(project, language, file.getViewProvider().getBaseLanguage());
    if (handlers == null)
        handlers = new ParameterInfoHandler[0];
    Lookup lookup = LookupManager.getInstance(project).getActiveLookup();
    if (lookup != null) {
        LookupElement item = lookup.getCurrentItem();
        if (item != null) {
            for (ParameterInfoHandler handler : handlers) {
                if (handler.couldShowInLookup()) {
                    final Object[] items = handler.getParametersForLookup(item, context);
                    if (items != null && items.length > 0) {
                        showLookupEditorHint(items, editor, project, handler, requestFocus);
                    }
                    return;
                }
            }
        }
        return;
    }
    DumbService.getInstance(project).setAlternativeResolveEnabled(true);
    try {
        for (ParameterInfoHandler<Object, ?> handler : handlers) {
            Object element = handler.findElementForParameterInfo(context);
            if (element != null) {
                handler.showParameterInfo(element, context);
            }
        }
    } finally {
        DumbService.getInstance(project).setAlternativeResolveEnabled(false);
    }
}
Also used : Language(com.intellij.lang.Language) Lookup(com.intellij.codeInsight.lookup.Lookup) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ParameterInfoHandler(com.intellij.lang.parameterInfo.ParameterInfoHandler) LightweightHint(com.intellij.ui.LightweightHint) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Lookup (com.intellij.codeInsight.lookup.Lookup)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Language (com.intellij.lang.Language)2 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1 ShowQuickDocInfoAction (com.intellij.codeInsight.documentation.actions.ShowQuickDocInfoAction)1 HintManagerImpl (com.intellij.codeInsight.hint.HintManagerImpl)1 ParameterInfoController (com.intellij.codeInsight.hint.ParameterInfoController)1 LookupEx (com.intellij.codeInsight.lookup.LookupEx)1 LookupManager (com.intellij.codeInsight.lookup.LookupManager)1 LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)1 LookupManagerImpl (com.intellij.codeInsight.lookup.impl.LookupManagerImpl)1 BrowserUtil (com.intellij.ide.BrowserUtil)1 DataManager (com.intellij.ide.DataManager)1 BaseNavigateToSourceAction (com.intellij.ide.actions.BaseNavigateToSourceAction)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 ChooseByNameBase (com.intellij.ide.util.gotoByName.ChooseByNameBase)1 LanguageDocumentation (com.intellij.lang.LanguageDocumentation)1