Search in sources :

Example 1 with VisualPosition

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

the class XmlSyncTagCommunityTest method testMultiCaretAdding.

public void testMultiCaretAdding() {
    doTest("<div<caret>></div>\n" + "<div></div>\n", "\b\b\biii", "<iii></iii>\n" + "<div></div>\n");
    myFixture.getEditor().getCaretModel().addCaret(new VisualPosition(1, 4));
    type("\b");
    myFixture.checkResult("<ii></ii>\n" + "<di></di>\n");
}
Also used : VisualPosition(com.intellij.openapi.editor.VisualPosition)

Example 2 with VisualPosition

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

the class SearchSupport method getPopupLocation.

protected Point getPopupLocation() {
    VisualPosition visualPosition = getEditor().offsetToVisualPosition(getEditor().getCaretModel().getOffset());
    Point point = getEditor().visualPositionToXY(visualPosition);
    SwingUtilities.convertPointToScreen(point, getEditor().getComponent());
    point.y += 2;
    return point;
}
Also used : VisualPosition(com.intellij.openapi.editor.VisualPosition)

Example 3 with VisualPosition

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

the class MotionActionTest method testUpDownMove.

// |j| |k|
public void testUpDownMove() {
    final Editor editor = typeTextInFile(parseKeys("2j", "k"), "one\n" + "tw<caret>o\n" + "three\n" + "four\n");
    final VisualPosition position = editor.getCaretModel().getVisualPosition();
    assertEquals(new VisualPosition(2, 2), position);
}
Also used : VisualPosition(com.intellij.openapi.editor.VisualPosition) Editor(com.intellij.openapi.editor.Editor)

Example 4 with VisualPosition

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

the class SoftWrapHelper method isCaretAfterSoftWrap.

/**
   * Every soft wrap implies that multiple visual positions correspond to the same document offset. We can classify
   * such positions by the following criteria:
   * <pre>
   * <ul>
   *   <li>positions from visual line with soft wrap start;</li>
   *   <li>positions from visual line with soft wrap end;</li>
   * </ul>
   * </pre>
   * <p/>
   * This method allows to answer if caret offset of the given editor points to soft wrap and visual caret position
   * belongs to the visual line where soft wrap end is located.
   *
   * @return          <code>true</code> if caret offset of the given editor points to visual position that belongs to
   *                  visual line where soft wrap end is located
   */
public static boolean isCaretAfterSoftWrap(CaretImpl caret) {
    if (!caret.isUpToDate()) {
        return false;
    }
    EditorImpl editor = caret.getEditor();
    SoftWrapModel softWrapModel = editor.getSoftWrapModel();
    int offset = caret.getOffset();
    SoftWrap softWrap = softWrapModel.getSoftWrap(offset);
    if (softWrap == null) {
        return false;
    }
    VisualPosition afterWrapPosition = editor.offsetToVisualPosition(offset, false, false);
    VisualPosition caretPosition = caret.getVisualPosition();
    return caretPosition.line == afterWrapPosition.line && caretPosition.column <= afterWrapPosition.column;
}
Also used : EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) SoftWrapModel(com.intellij.openapi.editor.SoftWrapModel) SoftWrap(com.intellij.openapi.editor.SoftWrap) VisualPosition(com.intellij.openapi.editor.VisualPosition)

Example 5 with VisualPosition

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

the class ReassignVariableUtil method reassign.

@VisibleForTesting
public static boolean reassign(final Editor editor) {
    final SmartPsiElementPointer<PsiDeclarationStatement> pointer = editor.getUserData(DECLARATION_KEY);
    final PsiDeclarationStatement declaration = pointer != null ? pointer.getElement() : null;
    final PsiType type = getVariableType(declaration);
    if (type != null) {
        VariablesProcessor proc = findVariablesOfType(declaration, type);
        if (proc.size() > 0) {
            List<PsiVariable> vars = new ArrayList<>();
            for (int i = 0; i < proc.size(); i++) {
                final PsiVariable variable = proc.getResult(i);
                PsiElement outerCodeBlock = PsiUtil.getVariableCodeBlock(variable, null);
                if (outerCodeBlock == null)
                    continue;
                if (ReferencesSearch.search(variable, new LocalSearchScope(outerCodeBlock)).forEach(reference -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        return HighlightControlFlowUtil.getInnerClassVariableReferencedFrom(variable, element) == null;
                    }
                    return true;
                })) {
                    vars.add(variable);
                }
            }
            if (vars.isEmpty()) {
                return true;
            }
            if (vars.size() == 1) {
                replaceWithAssignment(declaration, proc.getResult(0), editor);
                return true;
            }
            final DefaultListModel<PsiVariable> model = new DefaultListModel<>();
            for (PsiVariable var : vars) {
                model.addElement(var);
            }
            final JBList list = new JBList(model);
            list.setCellRenderer(new ListCellRendererWrapper<PsiVariable>() {

                @Override
                public void customize(JList list, PsiVariable value, int index, boolean selected, boolean hasFocus) {
                    if (value != null) {
                        setText(value.getName());
                        setIcon(value.getIcon(0));
                    }
                }
            });
            final VisualPosition visualPosition = editor.getCaretModel().getVisualPosition();
            final Point point = editor.visualPositionToXY(new VisualPosition(visualPosition.line + 1, visualPosition.column));
            JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose variable to reassign").setRequestFocus(true).setItemChoosenCallback(() -> replaceWithAssignment(declaration, (PsiVariable) list.getSelectedValue(), editor)).createPopup().show(new RelativePoint(editor.getContentComponent(), point));
        }
        return true;
    }
    return false;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) VariablesProcessor(com.intellij.psi.scope.processor.VariablesProcessor) ArrayList(java.util.ArrayList) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) JBList(com.intellij.ui.components.JBList) VisualPosition(com.intellij.openapi.editor.VisualPosition) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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