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

The following examples show how to use com.intellij.openapi.editor.Editor#visualPositionToXY() . 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: PopupFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static Point getVisibleBestPopupLocation(@Nonnull Editor editor) {
  VisualPosition visualPosition = editor.getUserData(ANCHOR_POPUP_POSITION);

  if (visualPosition == null) {
    CaretModel caretModel = editor.getCaretModel();
    if (caretModel.isUpToDate()) {
      visualPosition = caretModel.getVisualPosition();
    }
    else {
      visualPosition = editor.offsetToVisualPosition(caretModel.getOffset());
    }
  }

  final int lineHeight = editor.getLineHeight();
  Point p = editor.visualPositionToXY(visualPosition);
  p.y += lineHeight;

  final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
  return !visibleArea.contains(p) && !visibleArea.contains(p.x, p.y - lineHeight) ? null : p;
}
 
Example 2
Source File: ConsoleLogConsole.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
public RelativePoint getRangeHighlighterLocation(RangeHighlighter range) {
    Editor editor = myLogEditor.getValue();
    Project project = editor.getProject();
    Window window = NotificationsManagerImpl.findWindowForBalloon(project);
    if (range != null && window != null) {
        Point point = editor.visualPositionToXY(editor.offsetToVisualPosition(range.getStartOffset()));
        return new RelativePoint(window, SwingUtilities.convertPoint(editor.getContentComponent(), point, window));
    }
    return null;
}
 
Example 3
Source File: ParticleContainerManager.java    From power-mode-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void updateInUI(Editor editor) {
    VisualPosition visualPosition = editor.getCaretModel().getVisualPosition();
    Point point = editor.visualPositionToXY(visualPosition);
    ScrollingModel scrollingModel = editor.getScrollingModel();
    point.x = point.x - scrollingModel.getHorizontalScrollOffset();
    point.y = point.y - scrollingModel.getVerticalScrollOffset();
    final ParticleContainer particleContainer = particleContainers.get(editor);
    if (particleContainer != null) {
        particleContainer.update(point);
    }
}
 
Example 4
Source File: CaretVisualPositionKeeper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private CaretVisualPositionKeeper(Editor[] editors) {
  for (Editor editor : editors) {
    Rectangle visibleArea = editor.getScrollingModel().getVisibleAreaOnScrollingFinished();
    Point pos = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    int relativePosition = pos.y - visibleArea.y;
    myCaretRelativeVerticalPositions.put(editor, relativePosition);
  }
}
 
Example 5
Source File: CaretVisualPositionKeeper.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void restoreOriginalLocation() {
  for (Map.Entry<Editor, Integer> e : myCaretRelativeVerticalPositions.entrySet()) {
    Editor editor = e.getKey();
    int relativePosition = e.getValue();
    Point caretLocation = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    int scrollOffset = caretLocation.y - relativePosition;
    ScrollingModel scrollingModel = editor.getScrollingModel();
    Rectangle targetArea = scrollingModel.getVisibleAreaOnScrollingFinished();
    // when animated scrolling is in progress, we'll not stop it immediately
    boolean useAnimation = !targetArea.equals(scrollingModel.getVisibleArea());
    if (!useAnimation) scrollingModel.disableAnimation();
    scrollingModel.scroll(targetArea.x, scrollOffset);
    if (!useAnimation) scrollingModel.enableAnimation();
  }
}
 
Example 6
Source File: ReformatCodeProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private CaretVisualPositionKeeper(@Nullable Document document) {
  if (document == null) return;

  Editor[] editors = EditorFactory.getInstance().getEditors(document);
  for (Editor editor : editors) {
    Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
    Point pos = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    int relativePosition = pos.y - visibleArea.y;
    myCaretRelativeVerticalPositions.put(editor, relativePosition);
  }
}
 
Example 7
Source File: ReformatCodeProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void restoreOriginalLocation() {
  for (Map.Entry<Editor, Integer> e : myCaretRelativeVerticalPositions.entrySet()) {
    Editor editor = e.getKey();
    int relativePosition = e.getValue();
    Point caretLocation = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    int scrollOffset = caretLocation.y - relativePosition;
    editor.getScrollingModel().disableAnimation();
    editor.getScrollingModel().scrollVertically(scrollOffset);
    editor.getScrollingModel().enableAnimation();
  }
}
 
Example 8
Source File: IntentionHintComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean canPlaceBulbOnTheSameLine(Editor editor) {
  if (ApplicationManager.getApplication().isUnitTestMode() || editor.isOneLineMode()) return false;
  if (Registry.is("always.show.intention.above.current.line", false)) return false;
  final int offset = editor.getCaretModel().getOffset();
  final VisualPosition pos = editor.offsetToVisualPosition(offset);
  int line = pos.line;

  final int firstNonSpaceColumnOnTheLine = EditorActionUtil.findFirstNonSpaceColumnOnTheLine(editor, line);
  if (firstNonSpaceColumnOnTheLine == -1) return false;
  final Point point = editor.visualPositionToXY(new VisualPosition(line, firstNonSpaceColumnOnTheLine));
  return point.x > AllIcons.Actions.RealIntentionBulb.getIconWidth() + (editor.isOneLineMode() ? SMALL_BORDER_SIZE : NORMAL_BORDER_SIZE) * 2;
}
 
Example 9
Source File: EditorUtils.java    From KJump with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public static Point offsetToXYCompat(@NotNull Editor editor, int offset, boolean leanForward, boolean beforeSoftWrap) {
    VisualPosition visualPosition = editor.offsetToVisualPosition(offset, leanForward, beforeSoftWrap);
    return editor.visualPositionToXY(visualPosition);
}
 
Example 10
Source File: IntentionHintComponent.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private static Point getHintPosition(Editor editor) {
  if (ApplicationManager.getApplication().isUnitTestMode()) return new Point();
  final int offset = editor.getCaretModel().getOffset();
  final VisualPosition pos = editor.offsetToVisualPosition(offset);
  int line = pos.line;

  final Point position = editor.visualPositionToXY(new VisualPosition(line, 0));
  LOG.assertTrue(editor.getComponent().isDisplayable());

  JComponent convertComponent = editor.getContentComponent();

  Point realPoint;
  final boolean oneLineEditor = editor.isOneLineMode();
  if (oneLineEditor) {
    // place bulb at the corner of the surrounding component
    final JComponent contentComponent = editor.getContentComponent();
    Container ancestorOfClass = SwingUtilities.getAncestorOfClass(JComboBox.class, contentComponent);

    if (ancestorOfClass != null) {
      convertComponent = (JComponent)ancestorOfClass;
    }
    else {
      ancestorOfClass = SwingUtilities.getAncestorOfClass(JTextField.class, contentComponent);
      if (ancestorOfClass != null) {
        convertComponent = (JComponent)ancestorOfClass;
      }
    }

    realPoint = new Point(-(AllIcons.Actions.RealIntentionBulb.getIconWidth() / 2) - 4, -(AllIcons.Actions.RealIntentionBulb.getIconHeight() / 2));
  }
  else {
    Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
    if (position.y < visibleArea.y || position.y >= visibleArea.y + visibleArea.height) return null;

    // try to place bulb on the same line
    int yShift = -(NORMAL_BORDER_SIZE + AllIcons.Actions.RealIntentionBulb.getIconHeight());
    if (canPlaceBulbOnTheSameLine(editor)) {
      yShift = -(NORMAL_BORDER_SIZE + (AllIcons.Actions.RealIntentionBulb.getIconHeight() - editor.getLineHeight()) / 2 + 3);
    }
    else if (position.y < visibleArea.y + editor.getLineHeight()) {
      yShift = editor.getLineHeight() - NORMAL_BORDER_SIZE;
    }

    final int xShift = AllIcons.Actions.RealIntentionBulb.getIconWidth();

    realPoint = new Point(Math.max(0, visibleArea.x - xShift), position.y + yShift);
  }

  Point location = SwingUtilities.convertPoint(convertComponent, realPoint, editor.getComponent().getRootPane().getLayeredPane());
  return new Point(location.x, location.y);
}