Search in sources :

Example 61 with Document

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

the class PyConditionalStatementPartFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyConditionalStatementPart statementPart) throws IncorrectOperationException {
    final PyExpression condition = statementPart.getCondition();
    final Document document = editor.getDocument();
    final PsiElement colon = PyPsiUtils.getFirstChildOfType(statementPart, PyTokenTypes.COLON);
    if (colon == null) {
        if (condition != null) {
            final PsiElement firstNonComment = PyPsiUtils.getNextNonCommentSibling(condition.getNextSibling(), false);
            if (firstNonComment != null && !":".equals(firstNonComment.getNode().getText())) {
                document.insertString(firstNonComment.getTextRange().getEndOffset(), ":");
            }
        } else {
            final TokenSet keywords = TokenSet.create(PyTokenTypes.IF_KEYWORD, PyTokenTypes.ELIF_KEYWORD, PyTokenTypes.WHILE_KEYWORD);
            final PsiElement keywordToken = PyPsiUtils.getChildByFilter(statementPart, keywords, 0);
            final int offset = sure(keywordToken).getTextRange().getEndOffset();
            document.insertString(offset, " :");
            processor.registerUnresolvedError(offset + 1);
        }
    } else if (condition == null) {
        processor.registerUnresolvedError(colon.getTextRange().getStartOffset());
    }
}
Also used : TokenSet(com.intellij.psi.tree.TokenSet) PyExpression(com.jetbrains.python.psi.PyExpression) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 62 with Document

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

the class PyForPartFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyForPart forPart) {
    final PsiElement colon = PyPsiUtils.getFirstChildOfType(forPart, PyTokenTypes.COLON);
    final Document document = editor.getDocument();
    final PsiElement forToken = PyPsiUtils.getFirstChildOfType(forPart, PyTokenTypes.FOR_KEYWORD);
    if (colon == null) {
        String textToInsert = ":";
        PsiElement sourceOrTarget = forPart.getSource();
        PsiElement positionToInsert = sourceOrTarget;
        if (sourceOrTarget == null) {
            sourceOrTarget = forPart.getTarget();
            final PsiElement inToken = PyPsiUtils.getFirstChildOfType(forPart, PyTokenTypes.IN_KEYWORD);
            if (inToken == null) {
                if (sourceOrTarget == null) {
                    positionToInsert = sure(forToken);
                    textToInsert = "  in :";
                    processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
                } else {
                    positionToInsert = sourceOrTarget;
                    textToInsert = " in :";
                    processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 4);
                }
            } else {
                positionToInsert = inToken;
                textToInsert = " :";
                processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
            }
        }
        document.insertString(positionToInsert.getTextRange().getEndOffset(), textToInsert);
    }
}
Also used : Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 63 with Document

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

the class PyParameterListFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParameterList parameters) throws IncorrectOperationException {
    final PsiElement lBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.OPEN_BRACES, 0);
    final PsiElement rBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.CLOSE_BRACES, 0);
    final PyFunction pyFunction = as(parameters.getParent(), PyFunction.class);
    if (pyFunction != null && !PyFunctionFixer.isFakeFunction(pyFunction) && (lBrace == null || rBrace == null)) {
        final Document document = editor.getDocument();
        if (lBrace == null) {
            final String textToInsert = pyFunction.getNameNode() == null ? " (" : "(";
            document.insertString(parameters.getTextOffset(), textToInsert);
        } else if (parameters.getParameters().length == 0) {
            document.insertString(lBrace.getTextRange().getEndOffset(), ")");
        } else {
            document.insertString(parameters.getTextRange().getEndOffset(), ")");
        }
    }
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 64 with Document

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

the class OverwriteEqualsInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    if (context.getCompletionChar() != Lookup.REPLACE_SELECT_CHAR) {
        return;
    }
    Document doc = context.getDocument();
    int tailOffset = context.getTailOffset();
    if (tailOffset < doc.getCharsSequence().length() && doc.getCharsSequence().charAt(tailOffset) == '=') {
        doc.deleteString(tailOffset, tailOffset + 1);
    }
}
Also used : Document(com.intellij.openapi.editor.Document)

Example 65 with Document

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

the class PyClassInsertHandler method handleInsert.

public void handleInsert(InsertionContext context, LookupElement item) {
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    if (context.getCompletionChar() == '(') {
        context.setAddCompletionChar(false);
        final int offset = context.getTailOffset();
        document.insertString(offset, "()");
        PyClass pyClass = PyUtil.as(item.getPsiElement(), PyClass.class);
        PyFunction init = pyClass != null ? pyClass.findInitOrNew(true, null) : null;
        if (init != null && PyFunctionInsertHandler.hasParams(context, init)) {
            editor.getCaretModel().moveToOffset(offset + 1);
            AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(context.getEditor(), init);
        } else {
            editor.getCaretModel().moveToOffset(offset + 2);
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Aggregations

Document (com.intellij.openapi.editor.Document)1075 VirtualFile (com.intellij.openapi.vfs.VirtualFile)246 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)164 NotNull (org.jetbrains.annotations.NotNull)138 Nullable (org.jetbrains.annotations.Nullable)129 TextRange (com.intellij.openapi.util.TextRange)122 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)117 PsiElement (com.intellij.psi.PsiElement)107 Editor (com.intellij.openapi.editor.Editor)104 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)56 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)45 IncorrectOperationException (com.intellij.util.IncorrectOperationException)43 IOException (java.io.IOException)42 RangeMarker (com.intellij.openapi.editor.RangeMarker)35 MockVirtualFile (com.intellij.mock.MockVirtualFile)33 File (java.io.File)32 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 List (java.util.List)29