Search in sources :

Example 6 with VisualPosition

use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.

the class PopupFactoryImpl method getVisibleBestPopupLocation.

@Nullable
private static Point getVisibleBestPopupLocation(@NotNull 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());
        }
    }
    Point p = editor.visualPositionToXY(new VisualPosition(visualPosition.line + 1, visualPosition.column));
    final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
    return !visibleArea.contains(p) && !visibleArea.contains(p.x, p.y - editor.getLineHeight()) ? null : p;
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) VisualPosition(com.intellij.openapi.editor.VisualPosition) RelativePoint(com.intellij.ui.awt.RelativePoint) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with VisualPosition

use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.

the class DeleteToLineStartAndEndActionsTest method testMoveCaretFromVirtualSpaceToRealOffset.

public void testMoveCaretFromVirtualSpaceToRealOffset() throws IOException {
    boolean virtualSpacesBefore = EditorSettingsExternalizable.getInstance().isVirtualSpace();
    EditorSettingsExternalizable.getInstance().setVirtualSpace(true);
    try {
        doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(1, 10), "\n<caret>\n b b \n", new VisualPosition(1, 0));
        doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(2, 10), "\n a a \n<caret>\n", new VisualPosition(2, 0));
        doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(3, 10), "\n a a \n b b \n<caret>", new VisualPosition(3, 0));
        doTestDeleteToStart("\n a a \n b b \n", new VisualPosition(20, 10), "\n a a \n b b \n<caret>", new VisualPosition(3, 0));
        doTestDeleteToEnd("\n a a \n b b \n", new VisualPosition(1, 10), "\n a a <caret> b b \n", new VisualPosition(1, 5));
        // keep old behavior - not sure if it was intentional or not
        doTestDeleteToEnd("\n a a \n b b \n", new VisualPosition(3, 10), "\n a a \n b b \n<caret>", new VisualPosition(3, 10));
    } finally {
        EditorSettingsExternalizable.getInstance().setVirtualSpace(virtualSpacesBefore);
    }
}
Also used : VisualPosition(com.intellij.openapi.editor.VisualPosition)

Example 8 with VisualPosition

use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.

the class NextPrevWordHandler method doExecute.

@Override
protected void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) {
    assert caret != null;
    if (EditorUtil.isPasswordEditor(editor)) {
        int selectionStartOffset = caret.getLeadSelectionOffset();
        caret.moveToOffset(myNext ? editor.getDocument().getTextLength() : 0);
        if (myWithSelection)
            caret.setSelection(selectionStartOffset, caret.getOffset());
    } else {
        VisualPosition currentPosition = caret.getVisualPosition();
        if (caret.isAtBidiRunBoundary() && (myNext ^ currentPosition.leansRight)) {
            int selectionStartOffset = caret.getLeadSelectionOffset();
            VisualPosition selectionStartPosition = caret.getLeadSelectionPosition();
            caret.moveToVisualPosition(currentPosition.leanRight(!currentPosition.leansRight));
            if (myWithSelection) {
                caret.setSelection(selectionStartPosition, selectionStartOffset, caret.getVisualPosition(), caret.getOffset());
            }
        } else {
            if (myNext ^ caret.isAtRtlLocation()) {
                EditorActionUtil.moveCaretToNextWord(editor, myWithSelection, myInDifferentHumpsMode ^ editor.getSettings().isCamelWords());
            } else {
                EditorActionUtil.moveCaretToPreviousWord(editor, myWithSelection, myInDifferentHumpsMode ^ editor.getSettings().isCamelWords());
            }
        }
    }
}
Also used : VisualPosition(com.intellij.openapi.editor.VisualPosition)

Example 9 with VisualPosition

use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.

the class IntentionHintComponent method getHintPosition.

@NotNull
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 {
        // try to place bulb on the same line
        int yShift = -(NORMAL_BORDER_SIZE + AllIcons.Actions.RealIntentionBulb.getIconHeight());
        if (canPlaceBulbOnTheSameLine(editor)) {
            final int borderHeight = NORMAL_BORDER_SIZE;
            yShift = -(borderHeight + (AllIcons.Actions.RealIntentionBulb.getIconHeight() - editor.getLineHeight()) / 2 + 3);
        }
        final int xShift = AllIcons.Actions.RealIntentionBulb.getIconWidth();
        Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();
        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);
}
Also used : RelativePoint(com.intellij.ui.awt.RelativePoint) VisualPosition(com.intellij.openapi.editor.VisualPosition) com.intellij.codeInsight.hint(com.intellij.codeInsight.hint) LightweightHint(com.intellij.ui.LightweightHint) RelativePoint(com.intellij.ui.awt.RelativePoint) HintHint(com.intellij.ui.HintHint) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with VisualPosition

use of com.intellij.openapi.editor.VisualPosition in project intellij-community by JetBrains.

the class NamedElementDuplicateHandler method executeWriteAction.

@Override
public void executeWriteAction(Editor editor, DataContext dataContext) {
    Project project = editor.getProject();
    if (project != null && !editor.getSelectionModel().hasSelection()) {
        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file != null) {
            VisualPosition caret = editor.getCaretModel().getVisualPosition();
            Pair<LogicalPosition, LogicalPosition> lines = EditorUtil.calcSurroundingRange(editor, caret, caret);
            TextRange toDuplicate = new TextRange(editor.logicalPositionToOffset(lines.first), editor.logicalPositionToOffset(lines.second));
            PsiElement name = findNameIdentifier(editor, file, toDuplicate);
            if (name != null && !name.getTextRange().containsOffset(editor.getCaretModel().getOffset())) {
                editor.getCaretModel().moveToOffset(name.getTextOffset());
            }
        }
    }
    myOriginal.execute(editor, dataContext);
}
Also used : Project(com.intellij.openapi.project.Project) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) VisualPosition(com.intellij.openapi.editor.VisualPosition) PsiElement(com.intellij.psi.PsiElement)

Aggregations

VisualPosition (com.intellij.openapi.editor.VisualPosition)20 RelativePoint (com.intellij.ui.awt.RelativePoint)5 HintHint (com.intellij.ui.HintHint)3 LightweightHint (com.intellij.ui.LightweightHint)3 com.intellij.codeInsight.hint (com.intellij.codeInsight.hint)2 Editor (com.intellij.openapi.editor.Editor)2 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Caret (com.intellij.openapi.editor.Caret)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Inlay (com.intellij.openapi.editor.Inlay)1 SoftWrap (com.intellij.openapi.editor.SoftWrap)1 SoftWrapModel (com.intellij.openapi.editor.SoftWrapModel)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1