Search in sources :

Example 26 with LogicalPosition

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

the class BackspaceHandler method getBackspaceUnindentPosition.

@Nullable
public static LogicalPosition getBackspaceUnindentPosition(final PsiFile file, final Editor editor) {
    if (editor.getSelectionModel().hasSelection())
        return null;
    final LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition();
    if (caretPos.column == 0) {
        return null;
    }
    if (!isWhitespaceBeforeCaret(editor)) {
        return null;
    }
    // Decrease column down to indentation * n
    final int indent = CodeStyleSettingsManager.getSettings(file.getProject()).getIndentOptionsByFile(file).INDENT_SIZE;
    int column = (caretPos.column - 1) / indent * indent;
    if (column < 0) {
        column = 0;
    }
    return new LogicalPosition(caretPos.line, column);
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with LogicalPosition

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

the class BackspaceHandler method isWhitespaceBeforeCaret.

public static boolean isWhitespaceBeforeCaret(Editor editor) {
    final LogicalPosition caretPos = editor.getCaretModel().getLogicalPosition();
    final CharSequence charSeq = editor.getDocument().getCharsSequence();
    // smart backspace is activated only if all characters in the check range are whitespace characters
    for (int pos = 0; pos < caretPos.column; pos++) {
        // use logicalPositionToOffset to make sure tabs are handled correctly
        final LogicalPosition checkPos = new LogicalPosition(caretPos.line, pos);
        final int offset = editor.logicalPositionToOffset(checkPos);
        if (offset < charSeq.length()) {
            final char c = charSeq.charAt(offset);
            if (c != '\t' && c != ' ' && c != '\n') {
                return false;
            }
        }
    }
    return true;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition)

Example 28 with LogicalPosition

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

the class DiffDividerDrawUtil method getVisibleInterval.

@NotNull
private static LineRange getVisibleInterval(Editor editor) {
    Rectangle area = editor.getScrollingModel().getVisibleArea();
    if (area.height < 0)
        return new LineRange(0, 0);
    LogicalPosition position1 = editor.xyToLogicalPosition(new Point(0, area.y));
    LogicalPosition position2 = editor.xyToLogicalPosition(new Point(0, area.y + area.height));
    return new LineRange(position1.line, position2.line);
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with LogicalPosition

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

the class DeclarationMover method beforeMove.

@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
    super.beforeMove(editor, info, down);
    if (myEnumToInsertSemicolonAfter != null) {
        TreeElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, null, myEnumToInsertSemicolonAfter.getManager());
        try {
            PsiElement inserted = myEnumToInsertSemicolonAfter.getParent().addAfter(semicolon.getPsi(), myEnumToInsertSemicolonAfter);
            inserted = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(inserted);
            final LogicalPosition position = editor.offsetToLogicalPosition(inserted.getTextRange().getEndOffset());
            info.toMove2 = new LineRange(position.line + 1, position.line + 1);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        } finally {
            myEnumToInsertSemicolonAfter = null;
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 30 with LogicalPosition

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

the class JavadocHelper method navigate.

/**
   * Tries to navigate caret at the given editor to the target position inserting missing white spaces if necessary.
   * 
   * @param position  target caret position
   * @param editor    target editor
   * @param project   target project
   */
@SuppressWarnings("MethodMayBeStatic")
public void navigate(@NotNull LogicalPosition position, @NotNull Editor editor, @NotNull final Project project) {
    final Document document = editor.getDocument();
    final CaretModel caretModel = editor.getCaretModel();
    final int endLineOffset = document.getLineEndOffset(position.line);
    final LogicalPosition endLinePosition = editor.offsetToLogicalPosition(endLineOffset);
    if (endLinePosition.column < position.column && !editor.getSettings().isVirtualSpace() && !editor.isViewer()) {
        final String toInsert = StringUtil.repeat(" ", position.column - endLinePosition.column);
        ApplicationManager.getApplication().runWriteAction(() -> {
            document.insertString(endLineOffset, toInsert);
            PsiDocumentManager.getInstance(project).commitDocument(document);
        });
    }
    caretModel.moveToLogicalPosition(position);
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

Aggregations

LogicalPosition (com.intellij.openapi.editor.LogicalPosition)106 Document (com.intellij.openapi.editor.Document)16 PsiElement (com.intellij.psi.PsiElement)14 Editor (com.intellij.openapi.editor.Editor)12 TextRange (com.intellij.openapi.util.TextRange)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 EditorEx (com.intellij.openapi.editor.ex.EditorEx)7 CaretModel (com.intellij.openapi.editor.CaretModel)6 NotNull (org.jetbrains.annotations.NotNull)6 RelativePoint (com.intellij.ui.awt.RelativePoint)5 JSFile (com.intellij.lang.javascript.psi.JSFile)4 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)4 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)4 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 PsiReference (com.intellij.psi.PsiReference)4 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)4 Mark (com.maddyhome.idea.vim.common.Mark)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 EditorWindow (com.intellij.injected.editor.EditorWindow)3