Search in sources :

Example 86 with Document

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

the class PyUtil method findNextAtOffset.

@Nullable
public static PsiElement findNextAtOffset(@NotNull final PsiFile psiFile, int caretOffset, Class... toSkip) {
    PsiElement element = psiFile.findElementAt(caretOffset);
    if (element == null) {
        return null;
    }
    final Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile);
    int lineEndOffset = 0;
    if (document != null) {
        int lineNumber = document.getLineNumber(caretOffset);
        lineEndOffset = document.getLineEndOffset(lineNumber);
    }
    while (caretOffset < lineEndOffset && instanceOf(element, toSkip)) {
        caretOffset++;
        element = psiFile.findElementAt(caretOffset);
    }
    return instanceOf(element, toSkip) ? null : element;
}
Also used : Document(com.intellij.openapi.editor.Document) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 87 with Document

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

the class PyUtil method addElementToStatementList.

/**
   * Inserts specified element into the statement list either at the beginning or at its end. If new element is going to be
   * inserted at the beginning, any preceding docstrings and/or calls to super methods will be skipped.
   * Moreover if statement list previously didn't contain any statements, explicit new line and indentation will be inserted in
   * front of it.
   *
   * @param element        element to insert
   * @param statementList  statement list
   * @param toTheBeginning whether to insert element at the beginning or at the end of the statement list
   * @return actually inserted element as for {@link PsiElement#add(PsiElement)}
   */
@NotNull
public static PsiElement addElementToStatementList(@NotNull PsiElement element, @NotNull PyStatementList statementList, boolean toTheBeginning) {
    final PsiElement prevElem = PyPsiUtils.getPrevNonWhitespaceSibling(statementList);
    // If statement list is on the same line as previous element (supposedly colon), move its only statement on the next line
    if (prevElem != null && onSameLine(statementList, prevElem)) {
        final PsiDocumentManager manager = PsiDocumentManager.getInstance(statementList.getProject());
        final Document document = manager.getDocument(statementList.getContainingFile());
        if (document != null) {
            final PyStatementListContainer container = (PyStatementListContainer) statementList.getParent();
            manager.doPostponedOperationsAndUnblockDocument(document);
            final String indentation = "\n" + PyIndentUtil.getElementIndent(statementList);
            // If statement list was empty initially, we need to add some anchor statement ("pass"), so that preceding new line was not
            // parsed as following entire StatementListContainer (e.g. function). It's going to be replaced anyway.
            final String text = statementList.getStatements().length == 0 ? indentation + PyNames.PASS : indentation;
            document.insertString(statementList.getTextRange().getStartOffset(), text);
            manager.commitDocument(document);
            statementList = container.getStatementList();
        }
    }
    final PsiElement firstChild = statementList.getFirstChild();
    if (firstChild == statementList.getLastChild() && firstChild instanceof PyPassStatement) {
        element = firstChild.replace(element);
    } else {
        final PyStatement[] statements = statementList.getStatements();
        if (toTheBeginning && statements.length > 0) {
            final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(statementList, PyDocStringOwner.class);
            PyStatement anchor = statements[0];
            if (docStringOwner != null && anchor instanceof PyExpressionStatement && ((PyExpressionStatement) anchor).getExpression() == docStringOwner.getDocStringExpression()) {
                final PyStatement next = PsiTreeUtil.getNextSiblingOfType(anchor, PyStatement.class);
                if (next == null) {
                    return statementList.addAfter(element, anchor);
                }
                anchor = next;
            }
            while (anchor instanceof PyExpressionStatement) {
                final PyExpression expression = ((PyExpressionStatement) anchor).getExpression();
                if (expression instanceof PyCallExpression) {
                    final PyExpression callee = ((PyCallExpression) expression).getCallee();
                    if ((isSuperCall((PyCallExpression) expression) || (callee != null && PyNames.INIT.equals(callee.getName())))) {
                        final PyStatement next = PsiTreeUtil.getNextSiblingOfType(anchor, PyStatement.class);
                        if (next == null) {
                            return statementList.addAfter(element, anchor);
                        }
                        anchor = next;
                        continue;
                    }
                }
                break;
            }
            element = statementList.addBefore(element, anchor);
        } else {
            element = statementList.add(element);
        }
    }
    return element;
}
Also used : Document(com.intellij.openapi.editor.Document)

Example 88 with Document

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

the class PyPackageUtil method addRequirementToTxtOrSetupPy.

public static void addRequirementToTxtOrSetupPy(@NotNull Module module, @NotNull String requirementName, @NotNull LanguageLevel languageLevel) {
    final VirtualFile requirementsTxt = findRequirementsTxt(module);
    if (requirementsTxt != null && requirementsTxt.isWritable()) {
        final Document document = FileDocumentManager.getInstance().getDocument(requirementsTxt);
        if (document != null) {
            document.insertString(0, requirementName + "\n");
        }
        return;
    }
    final PyFile setupPy = findSetupPy(module);
    if (setupPy == null) {
        return;
    }
    final PyCallExpression setupCall = findSetupCall(setupPy);
    final PyListLiteralExpression installRequires = findSetupPyInstallRequires(module, setupCall);
    final PyElementGenerator generator = PyElementGenerator.getInstance(module.getProject());
    if (installRequires != null && installRequires.isWritable()) {
        final String text = String.format("'%s'", requirementName);
        final PyExpression generated = generator.createExpressionFromText(languageLevel, text);
        installRequires.add(generated);
        return;
    }
    if (setupCall != null) {
        final PyArgumentList argumentList = setupCall.getArgumentList();
        final PyKeywordArgument requiresArg = generateRequiresKwarg(setupPy, requirementName, languageLevel, generator);
        if (argumentList != null && requiresArg != null) {
            argumentList.addArgument(requiresArg);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document)

Example 89 with Document

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

the class PyUtil method findPrevAtOffset.

@Nullable
public static PsiElement findPrevAtOffset(PsiFile psiFile, int caretOffset, Class... toSkip) {
    PsiElement element;
    if (caretOffset < 0) {
        return null;
    }
    int lineStartOffset = 0;
    final Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile);
    if (document != null) {
        int lineNumber = document.getLineNumber(caretOffset);
        lineStartOffset = document.getLineStartOffset(lineNumber);
    }
    do {
        caretOffset--;
        element = psiFile.findElementAt(caretOffset);
    } while (caretOffset >= lineStartOffset && instanceOf(element, toSkip));
    return instanceOf(element, toSkip) ? null : element;
}
Also used : Document(com.intellij.openapi.editor.Document) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 90 with Document

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

the class PythonEditorHighlighter method beforeDocumentChange.

@Override
public void beforeDocumentChange(DocumentEvent e) {
    final Document document = e.getDocument();
    hadUnicodeImport = document.getUserData(KEY);
}
Also used : 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