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