Search in sources :

Example 16 with CaretModel

use of com.intellij.openapi.editor.CaretModel 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 17 with CaretModel

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

the class ToggleColumnModeAction method setSelected.

@Override
public void setSelected(AnActionEvent e, boolean state) {
    final EditorEx editor = getEditor(e);
    final SelectionModel selectionModel = editor.getSelectionModel();
    final CaretModel caretModel = editor.getCaretModel();
    if (state) {
        caretModel.removeSecondaryCarets();
        boolean hasSelection = selectionModel.hasSelection();
        int selStart = selectionModel.getSelectionStart();
        int selEnd = selectionModel.getSelectionEnd();
        LogicalPosition blockStart, blockEnd;
        if (caretModel.supportsMultipleCarets()) {
            LogicalPosition logicalSelStart = editor.offsetToLogicalPosition(selStart);
            LogicalPosition logicalSelEnd = editor.offsetToLogicalPosition(selEnd);
            int caretOffset = caretModel.getOffset();
            blockStart = selStart == caretOffset ? logicalSelEnd : logicalSelStart;
            blockEnd = selStart == caretOffset ? logicalSelStart : logicalSelEnd;
        } else {
            blockStart = selStart == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selStart);
            blockEnd = selEnd == caretModel.getOffset() ? caretModel.getLogicalPosition() : editor.offsetToLogicalPosition(selEnd);
        }
        editor.setColumnMode(true);
        if (hasSelection) {
            selectionModel.setBlockSelection(blockStart, blockEnd);
        } else {
            selectionModel.removeSelection();
        }
    } else {
        boolean hasSelection = false;
        int selStart = 0;
        int selEnd = 0;
        if (caretModel.supportsMultipleCarets()) {
            hasSelection = true;
            List<Caret> allCarets = caretModel.getAllCarets();
            Caret fromCaret = allCarets.get(0);
            Caret toCaret = allCarets.get(allCarets.size() - 1);
            if (fromCaret == caretModel.getPrimaryCaret()) {
                Caret tmp = fromCaret;
                fromCaret = toCaret;
                toCaret = tmp;
            }
            selStart = fromCaret.getLeadSelectionOffset();
            selEnd = toCaret.getSelectionStart() == toCaret.getLeadSelectionOffset() ? toCaret.getSelectionEnd() : toCaret.getSelectionStart();
        }
        editor.setColumnMode(false);
        caretModel.removeSecondaryCarets();
        if (hasSelection) {
            selectionModel.setSelection(selStart, selEnd);
        } else {
            selectionModel.removeSelection();
        }
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Caret(com.intellij.openapi.editor.Caret)

Example 18 with CaretModel

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

the class PsiElementBaseIntentionAction method getElement.

@Nullable
private static PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
    CaretModel caretModel = editor.getCaretModel();
    int position = caretModel.getOffset();
    return file.findElementAt(position);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with CaretModel

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

the class FixDocCommentAction method generateComment.

/**
   * Generates a comment if possible.
   * <p/>
   * It's assumed that this method {@link PsiDocumentManager#commitDocument(Document) syncs} all PSI-document
   * changes during the processing.
   * 
   * @param anchor      target element for which a comment should be generated
   * @param editor      target editor
   * @param commenter   commenter to use
   * @param project     current project
   */
private static void generateComment(@NotNull PsiElement anchor, @NotNull Editor editor, @NotNull CodeDocumentationProvider documentationProvider, @NotNull CodeDocumentationAwareCommenter commenter, @NotNull Project project) {
    Document document = editor.getDocument();
    int commentStartOffset = anchor.getTextRange().getStartOffset();
    int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset));
    if (lineStartOffset > 0 && lineStartOffset < commentStartOffset) {
        // Example:
        //    void test1() {
        //    }
        //    void test2() {
        //       <offset>
        //    }
        // We want to insert the comment at the start of the line where 'test2()' is declared.
        int nonWhiteSpaceOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), commentStartOffset - 1, " \t");
        commentStartOffset = Math.max(nonWhiteSpaceOffset, lineStartOffset);
    }
    int commentBodyRelativeOffset = 0;
    int caretOffsetToSet = -1;
    StringBuilder buffer = new StringBuilder();
    String commentPrefix = commenter.getDocumentationCommentPrefix();
    if (commentPrefix != null) {
        buffer.append(commentPrefix).append("\n");
        commentBodyRelativeOffset += commentPrefix.length() + 1;
    }
    String linePrefix = commenter.getDocumentationCommentLinePrefix();
    if (linePrefix != null) {
        buffer.append(linePrefix);
        commentBodyRelativeOffset += linePrefix.length();
        caretOffsetToSet = commentStartOffset + commentBodyRelativeOffset;
    }
    buffer.append("\n");
    commentBodyRelativeOffset++;
    String commentSuffix = commenter.getDocumentationCommentSuffix();
    if (commentSuffix != null) {
        buffer.append(commentSuffix).append("\n");
    }
    if (buffer.length() <= 0) {
        return;
    }
    document.insertString(commentStartOffset, buffer);
    PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
    docManager.commitDocument(document);
    Pair<PsiElement, PsiComment> pair = documentationProvider.parseContext(anchor);
    if (pair == null || pair.second == null) {
        return;
    }
    String stub = documentationProvider.generateDocumentationContentStub(pair.second);
    CaretModel caretModel = editor.getCaretModel();
    if (stub != null) {
        int insertionOffset = commentStartOffset + commentBodyRelativeOffset;
        //if (CodeStyleSettingsManager.getSettings(project).JD_ADD_BLANK_AFTER_DESCRIPTION) {
        //  buffer.setLength(0);
        //  if (linePrefix != null) {
        //    buffer.append(linePrefix);
        //  }
        //  buffer.append("\n");
        //  buffer.append(stub);
        //  stub = buffer.toString();
        //}
        document.insertString(insertionOffset, stub);
        docManager.commitDocument(document);
        pair = documentationProvider.parseContext(anchor);
    }
    if (caretOffsetToSet >= 0) {
        caretModel.moveToOffset(caretOffsetToSet);
    }
    if (pair == null || pair.second == null) {
        return;
    }
    int start = Math.min(calcStartReformatOffset(pair.first), calcStartReformatOffset(pair.second));
    int end = pair.second.getTextRange().getEndOffset();
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    codeStyleManager.reformatText(anchor.getContainingFile(), start, end);
    int caretOffset = caretModel.getOffset();
    if (caretOffset > 0 && caretOffset <= document.getTextLength()) {
        char c = document.getCharsSequence().charAt(caretOffset - 1);
        if (!StringUtil.isWhiteSpace(c)) {
            document.insertString(caretOffset, " ");
            caretModel.moveToOffset(caretOffset + 1);
        }
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

Example 20 with CaretModel

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

the class SmartIndentingBackspaceHandler method doCharDeleted.

@Override
protected boolean doCharDeleted(char c, PsiFile file, Editor editor) {
    if (myReplacement == null) {
        return false;
    }
    Document document = editor.getDocument();
    CaretModel caretModel = editor.getCaretModel();
    int endOffset = CharArrayUtil.shiftForward(document.getImmutableCharSequence(), caretModel.getOffset(), " \t");
    document.replaceString(myStartOffset, endOffset, myReplacement);
    caretModel.moveToOffset(myStartOffset + myReplacement.length());
    return true;
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document)

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