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);
}
}
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;
}
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);
}
}
Aggregations