use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.
the class CodeFoldingManagerImpl method projectOpened.
@Override
public void projectOpened() {
final EditorMouseMotionAdapter myMouseMotionListener = new EditorMouseMotionAdapter() {
LightweightHint myCurrentHint;
FoldRegion myCurrentFold;
@Override
public void mouseMoved(EditorMouseEvent e) {
if (myProject.isDisposed())
return;
HintManager hintManager = HintManager.getInstance();
if (hintManager != null && hintManager.hasShownHintsThatWillHideByOtherHint(false)) {
return;
}
if (e.getArea() != EditorMouseEventArea.FOLDING_OUTLINE_AREA)
return;
LightweightHint hint = null;
try {
Editor editor = e.getEditor();
if (PsiDocumentManager.getInstance(myProject).isUncommited(editor.getDocument()))
return;
MouseEvent mouseEvent = e.getMouseEvent();
FoldRegion fold = ((EditorEx) editor).getGutterComponentEx().findFoldingAnchorAt(mouseEvent.getX(), mouseEvent.getY());
if (fold == null || !fold.isValid())
return;
if (fold == myCurrentFold && myCurrentHint != null) {
hint = myCurrentHint;
return;
}
TextRange psiElementRange = EditorFoldingInfo.get(editor).getPsiElementRange(fold);
if (psiElementRange == null)
return;
int textOffset = psiElementRange.getStartOffset();
// There is a possible case that target PSI element's offset is less than fold region offset (e.g. complete method is
// returned as PSI element for fold region that corresponds to java method code block). We don't want to show any hint
// if start of the current fold region is displayed.
Point foldStartXY = editor.visualPositionToXY(editor.offsetToVisualPosition(Math.max(textOffset, fold.getStartOffset())));
Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
if (visibleArea.y > foldStartXY.y) {
if (myCurrentHint != null) {
myCurrentHint.hide();
myCurrentHint = null;
}
// We want to show a hint with the top fold region content that is above the current viewport position.
// However, there is a possible case that complete region has a big height and only a little bottom part
// is shown at the moment. We can't just show hint with the whole top content because it would hide actual
// editor content, hence, we show max(2; available visual lines number) instead.
// P.S. '2' is used here in assumption that many java methods have javadocs which first line is just '/**'.
// So, it's not too useful to show only it even when available vertical space is not big enough.
int availableVisualLines = 2;
JComponent editorComponent = editor.getComponent();
Container editorComponentParent = editorComponent.getParent();
if (editorComponentParent != null) {
Container contentPane = editorComponent.getRootPane().getContentPane();
if (contentPane != null) {
int y = SwingUtilities.convertPoint(editorComponentParent, editorComponent.getLocation(), contentPane).y;
int visualLines = y / editor.getLineHeight();
availableVisualLines = Math.max(availableVisualLines, visualLines);
}
}
int startVisualLine = editor.offsetToVisualPosition(textOffset).line;
int desiredEndVisualLine = Math.max(0, editor.xyToVisualPosition(new Point(0, visibleArea.y)).line - 1);
int endVisualLine = startVisualLine + availableVisualLines;
if (endVisualLine > desiredEndVisualLine) {
endVisualLine = desiredEndVisualLine;
}
// Show only the non-displayed top part of the target fold region
int endOffset = editor.logicalPositionToOffset(editor.visualToLogicalPosition(new VisualPosition(endVisualLine, 0)));
TextRange textRange = new UnfairTextRange(textOffset, endOffset);
hint = EditorFragmentComponent.showEditorFragmentHint(editor, textRange, true, true);
myCurrentFold = fold;
myCurrentHint = hint;
}
} finally {
if (hint == null) {
if (myCurrentHint != null) {
myCurrentHint.hide();
myCurrentHint = null;
}
myCurrentFold = null;
}
}
}
};
StartupManager.getInstance(myProject).registerPostStartupActivity((DumbAwareRunnable) () -> EditorFactory.getInstance().getEventMulticaster().addEditorMouseMotionListener(myMouseMotionListener, myProject));
}
use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.
the class ShowXPathAction method actionPerformed.
public void actionPerformed(AnActionEvent e) {
final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
if (editor == null) {
return;
}
final Project project = editor.getProject();
if (project == null) {
return;
}
final PsiDocumentManager docmgr = PsiDocumentManager.getInstance(project);
final Document document = editor.getDocument();
docmgr.commitDocument(document);
final PsiFile psiFile = docmgr.getPsiFile(document);
if (!(psiFile instanceof XmlFile)) {
return;
}
final PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
if (!(element instanceof XmlElement || element instanceof PsiWhiteSpace)) {
XPathAppComponent.showEditorHint("No suitable context for an XPath-expression selected.", editor);
return;
}
final PsiElement node = XPathExpressionGenerator.transformToValidShowPathNode(element);
if (node == null) {
XPathAppComponent.showEditorHint("No suitable context for an XPath-expression selected.", editor);
return;
}
final Config cfg = XPathAppComponent.getInstance().getConfig();
final RangeHighlighter h = HighlighterUtil.highlightNode(editor, node, cfg.getContextAttributes(), cfg);
final String path = XPathSupport.getInstance().getUniquePath((XmlElement) node, null);
final JTextField label = new JTextField(path);
label.setPreferredSize(new Dimension(label.getPreferredSize().width + new JLabel("M").getPreferredSize().width, label.getPreferredSize().height));
label.setOpaque(false);
label.setEditable(false);
label.setBorder(null);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
final JPanel p = new NonOpaquePanel(new BorderLayout());
final JLabel l = new JLabel("XPath:");
p.add(l, BorderLayout.WEST);
p.add(label, BorderLayout.CENTER);
InplaceButton copy = new InplaceButton(ActionsBundle.message("action.EditorCopy.text"), PlatformIcons.COPY_ICON, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CopyPasteManager.getInstance().setContents(new StringSelection(path));
}
});
p.add(copy, BorderLayout.EAST);
final LightweightHint hint = new LightweightHint(p) {
public void hide() {
super.hide();
HighlighterUtil.removeHighlighter(editor, h);
}
};
final Point point = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
point.y += editor.getLineHeight() / 2;
HintHint hintHint = new HintHint(editor, point).setAwtTooltip(true).setContentActive(true).setExplicitClose(true).setShowImmediately(true);
HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, point, HintManager.HIDE_BY_ANY_KEY, 0, false, hintHint);
}
use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.
the class XPathAppComponent method showEditorHint.
public static void showEditorHint(final String info, final Editor editor) {
final JLabel label = new JLabel(info);
label.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.WHITE, Gray._128), BorderFactory.createEmptyBorder(3, 5, 3, 5)));
label.setForeground(JBColor.foreground());
label.setBackground(HintUtil.getInformationColor());
label.setOpaque(true);
label.setFont(label.getFont().deriveFont(Font.BOLD));
final LightweightHint h = new LightweightHint(label);
final Point point = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
SwingUtilities.convertPointToScreen(point, editor.getContentComponent());
/* === HintManager API Info ===
public void showEditorHint(final LightweightHint hint,
final Editor editor,
Point p,
int flags,
int timeout,
boolean reviveOnEditorChange)
reviveOnEditorChange means hint should stay even if active editor have been changed. It's should rarely be true.
possible flags are:
public static final int HIDE_BY_ESCAPE = 0x01;
public static final int HIDE_BY_ANY_KEY = 0x02;
public static final int HIDE_BY_LOOKUP_ITEM_CHANGE = 0x04;
public static final int HIDE_BY_TEXT_CHANGE = 0x08;
public static final int HIDE_BY_OTHER_HINT = 0x10;
public static final int HIDE_BY_SCROLLING = 0x20;
public static final int HIDE_IF_OUT_OF_EDITOR = 0x40;
public static final int UPDATE_BY_SCROLLING = 0x80;
*/
final int flags = HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_SCROLLING;
HintManagerImpl.getInstanceImpl().showEditorHint(h, editor, point, flags, 0, false);
}
use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.
the class QuickFixManager method showIntentionHint.
/**
* Shows intention hint (light bulb) if it's not visible yet.
*/
final void showIntentionHint() {
if (!myComponent.isShowing() || !IJSwingUtilities.hasFocus(myComponent)) {
hideIntentionHint();
return;
}
// 1. Hide previous hint (if any)
hideIntentionHint();
// 2. Found error (if any)
final ErrorInfo[] errorInfos = getErrorInfos();
if (!haveFixes(errorInfos)) {
hideIntentionHint();
return;
}
// 3. Determine position where this hint should be shown
final Rectangle bounds = getErrorBounds();
if (bounds == null) {
return;
}
// 4. Show light bulb to fix this error
final LightBulbComponentImpl lightBulbComponent = new LightBulbComponentImpl(this, AllIcons.Actions.IntentionBulb);
myHint = new LightweightHint(lightBulbComponent);
myLastHintBounds = bounds;
myHint.show(myComponent, bounds.x - AllIcons.Actions.IntentionBulb.getIconWidth() - 4, bounds.y, myComponent, new HintHint(myComponent, bounds.getLocation()));
}
use of com.intellij.ui.LightweightHint in project intellij-community by JetBrains.
the class TrafficTooltipRendererImpl method show.
@Override
public LightweightHint show(@NotNull Editor editor, @NotNull Point p, boolean alignToRight, @NotNull TooltipGroup group, @NotNull HintHint hintHint) {
myTrafficLightRenderer = (TrafficLightRenderer) ((EditorMarkupModelImpl) editor.getMarkupModel()).getErrorStripeRenderer();
myPanel = new TrafficProgressPanel(myTrafficLightRenderer, editor, hintHint);
repaintTooltipWindow();
LineTooltipRenderer.correctLocation(editor, myPanel, p, alignToRight, true, myPanel.getMinWidth());
LightweightHint hint = new LightweightHint(myPanel);
HintManagerImpl hintManager = (HintManagerImpl) HintManager.getInstance();
hintManager.showEditorHint(hint, editor, p, HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_OTHER_HINT | HintManager.HIDE_BY_SCROLLING, 0, false, hintHint);
hint.addHintListener(new HintListener() {
@Override
public void hintHidden(EventObject event) {
//double hide?
if (myPanel == null)
return;
myPanel = null;
onHide.run();
}
});
return hint;
}
Aggregations