Search in sources :

Example 41 with CaretModel

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

the class JavadocFixer method moveCaretToTheLineEndIfPossible.

private static void moveCaretToTheLineEndIfPossible(@NotNull Editor editor, int line) {
    final Document document = editor.getDocument();
    final CaretModel caretModel = editor.getCaretModel();
    int offset;
    if (line >= document.getLineCount()) {
        offset = document.getTextLength();
    } else {
        offset = document.getLineEndOffset(line);
    }
    caretModel.moveToOffset(offset);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

Example 42 with CaretModel

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

the class JavadocFixer method process.

/**
   * Checks if caret of the given editor is located inside javadoc and tries to perform smart completion there in case of the positive
   * answer.
   * 
   * @param editor   target editor
   * @param psiFile  PSI file for the document exposed via the given editor
   * @return  {@code true} if smart completion was performed; {@code false} otherwise
   */
public boolean process(@NotNull Editor editor, @NotNull PsiFile psiFile) {
    // Check parameter description completion.
    final CaretModel caretModel = editor.getCaretModel();
    final Pair<JavadocHelper.JavadocParameterInfo, List<JavadocHelper.JavadocParameterInfo>> pair = myHelper.parse(psiFile, editor, caretModel.getOffset());
    if (pair.first == null) {
        return false;
    }
    final JavadocHelper.JavadocParameterInfo next = findNext(pair.second, pair.first);
    if (next == null) {
        final int line = pair.first.lastLine + 1;
        final Document document = editor.getDocument();
        if (line < document.getLineCount()) {
            StringBuilder indent = new StringBuilder();
            boolean insertIndent = true;
            final CharSequence text = document.getCharsSequence();
            for (int i = document.getLineStartOffset(line), max = document.getLineEndOffset(line); i < max; i++) {
                final char c = text.charAt(i);
                if (c == ' ' || c == '\t') {
                    indent.append(c);
                    continue;
                } else if (c == '*') {
                    indent.append("* ");
                    if (i < max - 1 && text.charAt(i + 1) != '/') {
                        insertIndent = false;
                    }
                }
                indent.append("\n");
                break;
            }
            if (insertIndent) {
                document.insertString(document.getLineStartOffset(line), indent);
            }
        }
        moveCaretToTheLineEndIfPossible(editor, line);
        return true;
    }
    if (next.parameterDescriptionStartPosition != null) {
        myHelper.navigate(next.parameterDescriptionStartPosition, editor, psiFile.getProject());
    } else {
        final LogicalPosition position = myHelper.calculateDescriptionStartPosition(psiFile, pair.second, next);
        myHelper.navigate(position, editor, psiFile.getProject());
    }
    return true;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document) JavadocHelper(com.intellij.javadoc.JavadocHelper) List(java.util.List)

Example 43 with CaretModel

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

the class EnterInJavadocParamDescriptionHandler method postProcessEnter.

@Override
public Result postProcessEnter(@NotNull final PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
    if (!(file instanceof PsiJavaFile) || !CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER || !CodeStyleSettingsManager.getSettings(file.getProject()).JD_ALIGN_PARAM_COMMENTS) {
        return Result.Continue;
    }
    final CaretModel caretModel = editor.getCaretModel();
    final int caretOffset = caretModel.getOffset();
    if (!isInJavaDoc(editor, caretOffset)) {
        return Result.Continue;
    }
    final Pair<JavadocHelper.JavadocParameterInfo, List<JavadocHelper.JavadocParameterInfo>> pair = myHelper.parse(file, editor, caretOffset);
    if (pair.first == null || pair.first.parameterDescriptionStartPosition == null) {
        return Result.Continue;
    }
    final LogicalPosition caretPosition = caretModel.getLogicalPosition();
    final LogicalPosition nameEndPosition = pair.first.parameterNameEndPosition;
    if (nameEndPosition.line == caretPosition.line && caretPosition.column <= nameEndPosition.column) {
        return Result.Continue;
    }
    final int descriptionStartColumn = pair.first.parameterDescriptionStartPosition.column;
    final LogicalPosition desiredPosition = new LogicalPosition(caretPosition.line, descriptionStartColumn);
    final Document document = editor.getDocument();
    final CharSequence text = document.getCharsSequence();
    final int offsetAfterLastWs = CharArrayUtil.shiftForward(text, caretOffset, " \t");
    if (editor.offsetToLogicalPosition(offsetAfterLastWs).column < desiredPosition.column) {
        final int lineStartOffset = document.getLineStartOffset(desiredPosition.line);
        final String toInsert = StringUtil.repeat(" ", desiredPosition.column - (offsetAfterLastWs - lineStartOffset));
        ApplicationManager.getApplication().runWriteAction(() -> {
            document.insertString(caretOffset, toInsert);
            PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
        });
    }
    myHelper.navigate(desiredPosition, editor, file.getProject());
    return Result.Stop;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) CaretModel(com.intellij.openapi.editor.CaretModel) PsiJavaFile(com.intellij.psi.PsiJavaFile) List(java.util.List) Document(com.intellij.openapi.editor.Document)

Example 44 with CaretModel

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

the class PyStatementMover method restoreCaretAndSelection.

private static void restoreCaretAndSelection(@NotNull final PsiFile file, @NotNull final Editor editor, boolean selectionStartAtCaret, boolean hasSelection, @NotNull final SelectionContainer selectionContainer, int shift, int offset, @NotNull final MyLineRange toMove) {
    final Document document = editor.getDocument();
    final SelectionModel selectionModel = editor.getSelectionModel();
    final CaretModel caretModel = editor.getCaretModel();
    Integer selectionLen = selectionContainer.myLen;
    final PsiElement at = file.findElementAt(offset);
    if (at != null) {
        final PsiElement added = getCommentOrStatement(document, at);
        int size = toMove.size;
        if (size > 1) {
            PsiElement tmp = added.getNextSibling();
            while (size > 1 && tmp != null) {
                if (tmp instanceof PsiWhiteSpace) {
                    if (!selectionStartAtCaret)
                        shift += tmp.getTextLength();
                    selectionLen += tmp.getTextLength();
                }
                tmp = tmp.getNextSibling();
                size -= 1;
            }
        }
        if (shift < 0)
            shift = 0;
        final int column = editor.offsetToLogicalPosition(added.getTextRange().getStartOffset()).column;
        if (selectionContainer.myAtTheBeginning || column < selectionContainer.myAdditional) {
            selectionLen += column;
        } else {
            selectionLen += selectionContainer.myAdditional;
        }
        if (selectionContainer.myAtTheBeginning && selectionStartAtCaret)
            shift = -column;
    }
    final int documentLength = document.getTextLength();
    int newCaretOffset = offset + shift;
    if (newCaretOffset >= documentLength)
        newCaretOffset = documentLength;
    caretModel.moveToOffset(newCaretOffset);
    if (hasSelection) {
        if (selectionStartAtCaret) {
            int newSelectionEnd = newCaretOffset + selectionLen;
            selectionModel.setSelection(newCaretOffset, newSelectionEnd);
        } else {
            int newSelectionStart = newCaretOffset - selectionLen;
            selectionModel.setSelection(newSelectionStart, newCaretOffset);
        }
    }
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Document(com.intellij.openapi.editor.Document)

Example 45 with CaretModel

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

the class PyPlainEnterProcessor method getStatementList.

@Nullable
private static PyStatementList getStatementList(PsiElement psiElement, Editor editor) {
    if (psiElement instanceof PyStatementListContainer) {
        return ((PyStatementListContainer) psiElement).getStatementList();
    } else {
        final CaretModel caretModel = editor.getCaretModel();
        final PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset());
        final PyStatementPart statementPart = PsiTreeUtil.getParentOfType(atCaret, PyStatementPart.class);
        if (statementPart != null) {
            return statementPart.getStatementList();
        }
    }
    return null;
}
Also used : PyStatementListContainer(com.jetbrains.python.psi.PyStatementListContainer) CaretModel(com.intellij.openapi.editor.CaretModel) PyStatementPart(com.jetbrains.python.psi.PyStatementPart) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CaretModel (com.intellij.openapi.editor.CaretModel)57 Document (com.intellij.openapi.editor.Document)23 PsiElement (com.intellij.psi.PsiElement)14 SelectionModel (com.intellij.openapi.editor.SelectionModel)10 TextRange (com.intellij.openapi.util.TextRange)9 Editor (com.intellij.openapi.editor.Editor)8 Project (com.intellij.openapi.project.Project)7 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)6 Nullable (org.jetbrains.annotations.Nullable)6 EditorEx (com.intellij.openapi.editor.ex.EditorEx)5 IElementType (com.intellij.psi.tree.IElementType)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)3 PsiFile (com.intellij.psi.PsiFile)3 List (java.util.List)3 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 Caret (com.intellij.openapi.editor.Caret)2 ScrollingModel (com.intellij.openapi.editor.ScrollingModel)2