Search in sources :

Example 26 with LightweightHint

use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.

the class CtrlMouseHandler method getHintBounds.

@Nullable
private Rectangle getHintBounds() {
    LightweightHint hint = myHint;
    if (hint == null) {
        return null;
    }
    JComponent hintComponent = hint.getComponent();
    if (!hintComponent.isShowing()) {
        return null;
    }
    return new Rectangle(hintComponent.getLocationOnScreen(), hintComponent.getSize());
}
Also used : LightweightHint(com.intellij.ui.LightweightHint) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with LightweightHint

use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.

the class CtrlMouseHandler method fulfillDocInfo.

private void fulfillDocInfo(@NotNull final String header, @NotNull final DocumentationProvider provider, @NotNull final PsiElement originalElement, @NotNull final PsiElement anchorElement, @NotNull final Consumer<String> newTextConsumer, @NotNull final LightweightHint hint) {
    myDocAlarm.cancelAllRequests();
    myDocAlarm.addRequest(() -> {
        final Ref<String> fullTextRef = new Ref<>();
        final Ref<String> qualifiedNameRef = new Ref<>();
        ApplicationManager.getApplication().runReadAction(() -> {
            if (anchorElement.isValid() && originalElement.isValid()) {
                try {
                    fullTextRef.set(provider.generateDoc(anchorElement, originalElement));
                } catch (IndexNotReadyException e) {
                    fullTextRef.set("Documentation is not available while indexing is in progress");
                }
                if (anchorElement instanceof PsiQualifiedNamedElement) {
                    qualifiedNameRef.set(((PsiQualifiedNamedElement) anchorElement).getQualifiedName());
                }
            }
        });
        String fullText = fullTextRef.get();
        if (fullText == null) {
            return;
        }
        final String updatedText = DocPreviewUtil.buildPreview(header, qualifiedNameRef.get(), fullText);
        final String newHtml = HintUtil.prepareHintText(updatedText, HintUtil.getInformationHint());
        UIUtil.invokeLaterIfNeeded(() -> {
            // There is a possible case that quick doc control width is changed, e.g. it contained text
            // like 'public final class String implements java.io.Serializable, java.lang.Comparable<java.lang.String>' and
            // new text replaces fully-qualified class names by hyperlinks with short name.
            // That's why we might need to update the control size. We assume that the hint component is located at the
            // layered pane, so, the algorithm is to find an ancestor layered pane and apply new size for the target component.
            JComponent component = hint.getComponent();
            Dimension oldSize = component.getPreferredSize();
            newTextConsumer.consume(newHtml);
            final int widthIncrease;
            if (component instanceof QuickDocInfoPane) {
                int buttonWidth = ((QuickDocInfoPane) component).getButtonWidth();
                widthIncrease = calculateWidthIncrease(buttonWidth, updatedText);
            } else {
                widthIncrease = 0;
            }
            if (oldSize == null) {
                return;
            }
            Dimension newSize = component.getPreferredSize();
            if (newSize.width + widthIncrease == oldSize.width) {
                return;
            }
            component.setPreferredSize(new Dimension(newSize.width + widthIncrease, newSize.height));
            // We're assuming here that there are two possible hint representation modes: popup and layered pane.
            if (hint.isRealPopup()) {
                TooltipProvider tooltipProvider = myTooltipProvider;
                if (tooltipProvider != null) {
                    // There is a possible case that 'raw' control was rather wide but the 'rich' one is narrower. That's why we try to
                    // re-show the hint here. Benefits: there is a possible case that we'll be able to show nice layered pane-based balloon;
                    // the popup will be re-positioned according to the new width.
                    hint.hide();
                    tooltipProvider.showHint(new LightweightHint(component));
                } else {
                    component.setPreferredSize(new Dimension(newSize.width + widthIncrease, oldSize.height));
                    hint.pack();
                }
                return;
            }
            Container topLevelLayeredPaneChild = null;
            boolean adjustBounds = false;
            for (Container current = component.getParent(); current != null; current = current.getParent()) {
                if (current instanceof JLayeredPane) {
                    adjustBounds = true;
                    break;
                } else {
                    topLevelLayeredPaneChild = current;
                }
            }
            if (adjustBounds && topLevelLayeredPaneChild != null) {
                Rectangle bounds = topLevelLayeredPaneChild.getBounds();
                topLevelLayeredPaneChild.setBounds(bounds.x, bounds.y, bounds.width + newSize.width + widthIncrease - oldSize.width, bounds.height);
            }
        });
    }, 0);
}
Also used : LightweightHint(com.intellij.ui.LightweightHint) LightweightHint(com.intellij.ui.LightweightHint) Ref(com.intellij.openapi.util.Ref) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException)

Example 28 with LightweightHint

use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.

the class ElementPreviewHintProvider method show.

@Override
public void show(@NotNull PsiElement element, @NotNull Editor editor, @NotNull Point point, boolean keyTriggered) {
    LightweightHint newHint = getHint(element);
    hideCurrentHintIfAny();
    if (newHint == null) {
        return;
    }
    hint = newHint;
    HintManagerImpl.getInstanceImpl().showEditorHint(newHint, editor, getHintPosition(newHint, editor, editor.xyToLogicalPosition(point), HintManager.RIGHT_UNDER), HINT_HIDE_FLAGS, 0, false);
}
Also used : LightweightHint(com.intellij.ui.LightweightHint)

Example 29 with LightweightHint

use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testLightBulbIsHiddenWhenFixRangeIsCollapsed.

public void testLightBulbIsHiddenWhenFixRangeIsCollapsed() {
    configureByText(StdFileTypes.JAVA, "class S { void foo() { boolean var; if (<selection>va<caret>r</selection>) {}} }");
    ((EditorImpl) myEditor).getScrollPane().getViewport().setSize(1000, 1000);
    final Set<LightweightHint> visibleHints = ContainerUtil.newIdentityTroveSet();
    getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(EditorHintListener.TOPIC, new EditorHintListener() {

        @Override
        public void hintShown(final Project project, final LightweightHint hint, final int flags) {
            visibleHints.add(hint);
            hint.addHintListener(new HintListener() {

                @Override
                public void hintHidden(EventObject event) {
                    visibleHints.remove(hint);
                    hint.removeHintListener(this);
                }
            });
        }
    });
    highlightErrors();
    IntentionHintComponent lastHintBeforeDeletion = myDaemonCodeAnalyzer.getLastIntentionHint();
    assertNotNull(lastHintBeforeDeletion);
    delete(myEditor);
    highlightErrors();
    IntentionHintComponent lastHintAfterDeletion = myDaemonCodeAnalyzer.getLastIntentionHint();
    assertSame(lastHintBeforeDeletion, lastHintAfterDeletion);
    assertEmpty(visibleHints);
}
Also used : EditorHintListener(com.intellij.codeInsight.hint.EditorHintListener) HintListener(com.intellij.ui.HintListener) Project(com.intellij.openapi.project.Project) IntentionHintComponent(com.intellij.codeInsight.intention.impl.IntentionHintComponent) EditorHintListener(com.intellij.codeInsight.hint.EditorHintListener) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) LightweightHint(com.intellij.ui.LightweightHint) LightweightHint(com.intellij.ui.LightweightHint)

Example 30 with LightweightHint

use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testBulbAppearsAfterType.

public void testBulbAppearsAfterType() throws Throwable {
    String text = "class S { ArrayList<caret>XXX x;}";
    configureByText(StdFileTypes.JAVA, text);
    ((EditorImpl) myEditor).getScrollPane().getViewport().setSize(1000, 1000);
    DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
    final Set<LightweightHint> shown = ContainerUtil.newIdentityTroveSet();
    getProject().getMessageBus().connect().subscribe(EditorHintListener.TOPIC, (project, hint, flags) -> {
        shown.add(hint);
        hint.addHintListener(event -> shown.remove(hint));
    });
    DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(getProject());
    highlightErrors();
    IntentionHintComponent hintComponent = codeAnalyzer.getLastIntentionHint();
    assertNotNull(hintComponent);
    assertFalse(hintComponent.isDisposed());
    assertNotNull(hintComponent.getComponentHint());
    assertTrue(shown.contains(hintComponent.getComponentHint()));
    type("x");
    highlightErrors();
    hintComponent = codeAnalyzer.getLastIntentionHint();
    assertNotNull(hintComponent);
    assertFalse(hintComponent.isDisposed());
    assertNotNull(hintComponent.getComponentHint());
    assertTrue(shown.contains(hintComponent.getComponentHint()));
}
Also used : IntentionHintComponent(com.intellij.codeInsight.intention.impl.IntentionHintComponent) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) LightweightHint(com.intellij.ui.LightweightHint)

Aggregations

LightweightHint (com.intellij.ui.LightweightHint)38 HintHint (com.intellij.ui.HintHint)11 Document (com.intellij.openapi.editor.Document)5 Project (com.intellij.openapi.project.Project)5 TextRange (com.intellij.openapi.util.TextRange)4 Nullable (org.jetbrains.annotations.Nullable)4 EditorHintListener (com.intellij.codeInsight.hint.EditorHintListener)3 Editor (com.intellij.openapi.editor.Editor)3 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)3 PsiFile (com.intellij.psi.PsiFile)3 HintListener (com.intellij.ui.HintListener)3 HintManager (com.intellij.codeInsight.hint.HintManager)2 IntentionHintComponent (com.intellij.codeInsight.intention.impl.IntentionHintComponent)2 Disposable (com.intellij.openapi.Disposable)2 AnAction (com.intellij.openapi.actionSystem.AnAction)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)2 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)2 PsiElement (com.intellij.psi.PsiElement)2 EventObject (java.util.EventObject)2