Java Code Examples for com.intellij.openapi.editor.Editor#logicalPositionToXY()

The following examples show how to use com.intellij.openapi.editor.Editor#logicalPositionToXY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractToggleUseSoftWrapsAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void toggleSoftWraps(@Nonnull Editor editor, @Nullable SoftWrapAppliancePlaces places, boolean state) {
  Point point = editor.getScrollingModel().getVisibleArea().getLocation();
  LogicalPosition anchorPosition = editor.xyToLogicalPosition(point);
  int intraLineShift = point.y - editor.logicalPositionToXY(anchorPosition).y;

  if (places != null) {
    EditorSettingsExternalizable.getInstance().setUseSoftWraps(state, places);
    EditorFactory.getInstance().refreshAllEditors();
  }
  if (editor.getSettings().isUseSoftWraps() != state) {
    editor.getSettings().setUseSoftWraps(state);
  }

  editor.getScrollingModel().disableAnimation();
  editor.getScrollingModel().scrollVertically(editor.logicalPositionToXY(anchorPosition).y + intraLineShift);
  editor.getScrollingModel().enableAnimation();
}
 
Example 2
Source File: EditorEventManager.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
/**
 * Immediately requests the server for documentation at the current editor position
 *
 * @param editor The editor
 */
public void quickDoc(Editor editor) {
    if (editor == this.editor) {
        LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition();
        Point pointPos = editor.logicalPositionToXY(caretPos);
        long currentTime = System.nanoTime();
        pool(() -> requestAndShowDoc(caretPos, pointPos));
        predTime = currentTime;
    } else {
        LOG.warn("Not same editor!");
    }
}
 
Example 3
Source File: PropertyEditorPanel.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static boolean insideVisibleArea(Editor e, TextRange r) {
  final int textLength = e.getDocument().getTextLength();
  if (r.getStartOffset() > textLength) return false;
  if (r.getEndOffset() > textLength) return false;
  final Rectangle visibleArea = e.getScrollingModel().getVisibleArea();
  final Point point = e.logicalPositionToXY(e.offsetToLogicalPosition(r.getStartOffset()));

  return visibleArea.contains(point);
}
 
Example 4
Source File: PropertyEditorPanel.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static boolean insideVisibleArea(Editor e, TextRange r) {
  final int textLength = e.getDocument().getTextLength();
  if (r.getStartOffset() > textLength) return false;
  if (r.getEndOffset() > textLength) return false;
  final Rectangle visibleArea = e.getScrollingModel().getVisibleArea();
  final Point point = e.logicalPositionToXY(e.offsetToLogicalPosition(r.getStartOffset()));

  return visibleArea.contains(point);
}
 
Example 5
Source File: DiffDrawUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static int lineToY(@Nonnull Editor editor, int line) {
  Document document = editor.getDocument();
  if (line >= getLineCount(document)) {
    int y = lineToY(editor, getLineCount(document) - 1);
    return y + editor.getLineHeight() * (line - getLineCount(document) + 1);
  }
  return editor.logicalPositionToXY(editor.offsetToLogicalPosition(document.getLineStartOffset(line))).y;
}
 
Example 6
Source File: DiffEmptyHighlighterRenderer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void paint(@Nonnull Editor editor, @Nonnull RangeHighlighter highlighter, @Nonnull Graphics g) {
  g.setColor(myDiffType.getColor(editor));
  Point point = editor.logicalPositionToXY(editor.offsetToLogicalPosition(highlighter.getStartOffset()));
  int endy = point.y + editor.getLineHeight() - 1;
  g.drawLine(point.x, point.y, point.x, endy);
  g.drawLine(point.x - 1, point.y, point.x - 1, endy);
}
 
Example 7
Source File: DiffDividerDrawUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Transformation getTransformation(@Nonnull final Editor editor) {
  return new Transformation() {
    @Override
    public int transform(int line) {
      int yOffset = editor.logicalPositionToXY(new LogicalPosition(line, 0)).y;

      final JComponent header = editor.getHeaderComponent();
      int headerOffset = header == null ? 0 : header.getHeight();

      return yOffset - editor.getScrollingModel().getVerticalScrollOffset() + headerOffset;
    }
  };
}
 
Example 8
Source File: EditorFragmentComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static LightweightHint showEditorFragmentHint(Editor editor, TextRange range, boolean showFolding, boolean hideByAnyKey) {
  if (!(editor instanceof EditorEx)) return null;
  JRootPane rootPane = editor.getComponent().getRootPane();
  if (rootPane == null) return null;
  JLayeredPane layeredPane = rootPane.getLayeredPane();
  int lineHeight = editor.getLineHeight();
  int overhang = editor.getScrollingModel().getVisibleArea().y - editor.logicalPositionToXY(editor.offsetToLogicalPosition(range.getEndOffset())).y;
  int yRelative = overhang > 0 && overhang < lineHeight ? lineHeight - overhang + JBUIScale.scale(LINE_BORDER_THICKNESS + EMPTY_BORDER_THICKNESS) : 0;
  Point point = SwingUtilities.convertPoint(((EditorEx)editor).getScrollPane().getViewport(), -2, yRelative, layeredPane);
  return showEditorFragmentHintAt(editor, range, point.y, true, showFolding, hideByAnyKey, true, false);
}
 
Example 9
Source File: SyncScrollSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void syncVerticalScroll(@Nonnull ScrollingContext context,
                                       @Nonnull Rectangle newRectangle,
                                       @Nonnull Rectangle oldRectangle) {
  if (newRectangle.y == oldRectangle.y) return;
  EditingSides sidesContainer = context.getSidesContainer();
  FragmentSide masterSide = context.getMasterSide();
  FragmentSide masterDiffSide = context.getMasterDiffSide();

  Editor master = sidesContainer.getEditor(masterSide);
  Editor slave = sidesContainer.getEditor(masterSide.otherSide());

  if (master == null || slave == null) return;

  int masterVerticalScrollOffset = master.getScrollingModel().getVerticalScrollOffset();
  int slaveVerticalScrollOffset = slave.getScrollingModel().getVerticalScrollOffset();

  Rectangle viewRect = master.getScrollingModel().getVisibleArea();
  int middleY = viewRect.height / 3;

  if (master.getDocument().getTextLength() == 0) return;

  LogicalPosition masterPos = master.xyToLogicalPosition(new Point(viewRect.x, masterVerticalScrollOffset + middleY));
  int masterCenterLine = masterPos.line;
  int scrollToLine = sidesContainer.getLineBlocks().transform(masterDiffSide, masterCenterLine);

  int offset;
  if (scrollToLine < 0) {
    offset = slaveVerticalScrollOffset + newRectangle.y - oldRectangle.y;
  }
  else {
    int correction = (masterVerticalScrollOffset + middleY) % master.getLineHeight();
    Point point = slave.logicalPositionToXY(new LogicalPosition(scrollToLine, masterPos.column));
    offset = point.y - middleY + correction;
  }

  int deltaHeaderOffset = getHeaderOffset(slave) - getHeaderOffset(master);
  doScrollVertically(slave.getScrollingModel(), offset + deltaHeaderOffset);
}
 
Example 10
Source File: DebuggerUIUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RelativePoint calcPopupLocation(@Nonnull Editor editor, final int line) {
  Point p = editor.logicalPositionToXY(new LogicalPosition(line + 1, 0));

  final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
  if (!visibleArea.contains(p)) {
    p = new Point((visibleArea.x + visibleArea.width) / 2, (visibleArea.y + visibleArea.height) / 2);
  }
  return new RelativePoint(editor.getContentComponent(), p);
}
 
Example 11
Source File: ShowContainerInfoHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isDeclarationVisible(PsiElement container, Editor editor) {
  Rectangle viewRect = editor.getScrollingModel().getVisibleArea();
  final TextRange range = DeclarationRangeUtil.getPossibleDeclarationAtRange(container);
  if (range == null) {
    return false;
  }

  LogicalPosition pos = editor.offsetToLogicalPosition(range.getStartOffset());
  Point loc = editor.logicalPositionToXY(pos);
  return loc.y >= viewRect.y;
}
 
Example 12
Source File: IntentionsUIImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void update(@Nonnull CachedIntentions cachedIntentions, boolean actionsChanged) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  Editor editor = cachedIntentions.getEditor();
  if (editor == null) return;
  if (!ApplicationManager.getApplication().isUnitTestMode() && !editor.getContentComponent().hasFocus()) return;
  if (!actionsChanged) return;

  //IntentionHintComponent hint = myLastIntentionHint;
  //if (hint != null && hint.getPopupUpdateResult(actionsChanged) == IntentionHintComponent.PopupUpdateResult.CHANGED_INVISIBLE) {
  //  hint.recreate();
  //  return;
  //}

  Project project = cachedIntentions.getProject();
  LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition();
  Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
  Point xy = editor.logicalPositionToXY(caretPos);

  hide();
  if (!HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(false) &&
      visibleArea.contains(xy) &&
      editor.getSettings().isShowIntentionBulb() &&
      editor.getCaretModel().getCaretCount() == 1 &&
      cachedIntentions.showBulb()) {
    myLastIntentionHint = IntentionHintComponent.showIntentionHint(project, cachedIntentions.getFile(), editor, false, cachedIntentions);
  }
}
 
Example 13
Source File: DaemonTooltipUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void showInfoTooltip(@Nonnull final HighlightInfo info, @Nonnull Editor editor, final int defaultOffset, final int currentWidth, final boolean requestFocus, final boolean showImmediately) {
  if (Registry.is("editor.new.mouse.hover.popups")) {
    EditorMouseHoverPopupManager.getInstance().showInfoTooltip(editor, info, defaultOffset, requestFocus, showImmediately);
    return;
  }
  String text = info.getToolTip();
  if (text == null) return;
  Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();

  Point point = editor.logicalPositionToXY(editor.offsetToLogicalPosition(defaultOffset));
  Point highlightEndPoint = editor.logicalPositionToXY(editor.offsetToLogicalPosition(info.endOffset));
  if (highlightEndPoint.y > point.y) {
    if (highlightEndPoint.x > point.x) {
      point = new Point(point.x, highlightEndPoint.y);
    }
    else if (highlightEndPoint.y > point.y + editor.getLineHeight()) {
      point = new Point(point.x, highlightEndPoint.y - editor.getLineHeight());
    }
  }

  Point bestPoint = new Point(point);
  bestPoint.y += editor.getLineHeight() / 2;
  if (!visibleArea.contains(bestPoint)) bestPoint = point;

  Point p = SwingUtilities.convertPoint(editor.getContentComponent(), bestPoint, editor.getComponent().getRootPane().getLayeredPane());

  HintHint hintHint = new HintHint(editor, bestPoint).setAwtTooltip(true).setHighlighterType(true).setRequestFocus(requestFocus).setCalloutShift(editor.getLineHeight() / 2 - 1)
          .setShowImmediately(showImmediately);

  TooltipAction action = TooltipActionProvider.calcTooltipAction(info, editor);
  ErrorStripTooltipRendererProvider provider = ((EditorMarkupModel)editor.getMarkupModel()).getErrorStripTooltipRendererProvider();
  TooltipRenderer tooltipRenderer = provider.calcTooltipRenderer(text, action, currentWidth);

  TooltipController.getInstance().showTooltip(editor, p, tooltipRenderer, false, DAEMON_INFO_GROUP, hintHint);
}
 
Example 14
Source File: SearchResults.java    From consulo with Apache License 2.0 5 votes vote down vote up
static boolean insideVisibleArea(Editor e, TextRange r) {
  int startOffset = r.getStartOffset();
  if (startOffset > e.getDocument().getTextLength()) return false;
  Rectangle visibleArea = e.getScrollingModel().getVisibleArea();
  Point point = e.logicalPositionToXY(e.offsetToLogicalPosition(startOffset));

  return visibleArea.contains(point);
}
 
Example 15
Source File: DebuggerUIUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static RelativePoint getPositionForPopup(@Nonnull Editor editor, int line) {
  Point p = editor.logicalPositionToXY(new LogicalPosition(line + 1, 0));
  return editor.getScrollingModel().getVisibleArea().contains(p) ? new RelativePoint(editor.getContentComponent(), p) : null;
}
 
Example 16
Source File: LookupUi.java    From consulo with Apache License 2.0 4 votes vote down vote up
Rectangle calculatePosition() {
  final JComponent lookupComponent = myLookup.getComponent();
  Dimension dim = lookupComponent.getPreferredSize();
  int lookupStart = myLookup.getLookupStart();
  Editor editor = myLookup.getTopLevelEditor();
  if (lookupStart < 0 || lookupStart > editor.getDocument().getTextLength()) {
    LOG.error(lookupStart + "; offset=" + editor.getCaretModel().getOffset() + "; element=" + myLookup.getPsiElement());
  }

  LogicalPosition pos = editor.offsetToLogicalPosition(lookupStart);
  Point location = editor.logicalPositionToXY(pos);
  location.y += editor.getLineHeight();
  location.x -= myLookup.myCellRenderer.getTextIndent();
  // extra check for other borders
  final Window window = UIUtil.getWindow(lookupComponent);
  if (window != null) {
    final Point point = SwingUtilities.convertPoint(lookupComponent, 0, 0, window);
    location.x -= point.x;
  }

  SwingUtilities.convertPointToScreen(location, editor.getContentComponent());
  final Rectangle screenRectangle = ScreenUtil.getScreenRectangle(editor.getContentComponent());

  if (!isPositionedAboveCaret()) {
    int shiftLow = screenRectangle.y + screenRectangle.height - (location.y + dim.height);
    myPositionedAbove = shiftLow < 0 && shiftLow < location.y - dim.height && location.y >= dim.height;
  }
  if (isPositionedAboveCaret()) {
    location.y -= dim.height + editor.getLineHeight();
    if (pos.line == 0) {
      location.y += 1;
      //otherwise the lookup won't intersect with the editor and every editor's resize (e.g. after typing in console) will close the lookup
    }
  }

  if (!screenRectangle.contains(location)) {
    location = ScreenUtil.findNearestPointOnBorder(screenRectangle, location);
  }

  Rectangle candidate = new Rectangle(location, dim);
  ScreenUtil.cropRectangleToFitTheScreen(candidate);

  JRootPane rootPane = editor.getComponent().getRootPane();
  if (rootPane != null) {
    SwingUtilities.convertPointFromScreen(location, rootPane.getLayeredPane());
  }
  else {
    LOG.error("editor.disposed=" + editor.isDisposed() + "; lookup.disposed=" + myLookup.isLookupDisposed() + "; editorShowing=" + editor.getContentComponent().isShowing());
  }

  myMaximumHeight = candidate.height;
  return new Rectangle(location.x, location.y, dim.width, candidate.height);
}