Search in sources :

Example 56 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.

the class PyTargetExpressionImpl method getTypeCommentAnnotation.

@Nullable
@Override
public String getTypeCommentAnnotation() {
    final PyTargetExpressionStub stub = getStub();
    if (stub != null) {
        return stub.getTypeComment();
    }
    final PsiComment comment = getTypeComment();
    if (comment != null) {
        return PyTypingTypeProvider.getTypeCommentValue(comment.getText());
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PyTargetExpressionStub(com.jetbrains.python.psi.stubs.PyTargetExpressionStub) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.

the class PyTargetExpressionImpl method getTypeComment.

@Nullable
@Override
public PsiComment getTypeComment() {
    PsiComment comment = null;
    final PyAssignmentStatement assignment = PsiTreeUtil.getParentOfType(this, PyAssignmentStatement.class);
    if (assignment != null) {
        final PyExpression assignedValue = assignment.getAssignedValue();
        if (assignedValue != null) {
            comment = as(PyPsiUtils.getNextNonWhitespaceSiblingOnSameLine(assignedValue), PsiComment.class);
        }
    } else {
        final PyStatementListContainer forOrWith = PsiTreeUtil.getParentOfType(this, PyForPart.class, PyWithStatement.class);
        if (forOrWith != null) {
            comment = PyUtil.getCommentOnHeaderLine(forOrWith);
        }
    }
    return comment != null && PyTypingTypeProvider.getTypeCommentValue(comment.getText()) != null ? comment : null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.

the class PyExtractMethodHandler method getStatementsRange.

@Nullable
private static Couple<PsiElement> getStatementsRange(final PsiElement element1, final PsiElement element2) {
    final PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
    if (parent == null) {
        return null;
    }
    final PyElement statementList = PyPsiUtils.getStatementList(parent);
    if (statementList == null) {
        return null;
    }
    final PsiElement statement1 = PyPsiUtils.getParentRightBefore(element1, statementList);
    final PsiElement statement2 = PyPsiUtils.getParentRightBefore(element2, statementList);
    if (statement1 == null || statement2 == null) {
        return null;
    }
    // return elements if they are really first and last elements of statements
    if (element1 == PsiTreeUtil.getDeepestFirst(statement1) && element2 == PyPsiUtils.getPrevSignificantLeaf(PsiTreeUtil.getDeepestLast(statement2), !(element2 instanceof PsiComment))) {
        return Couple.of(statement1, statement2);
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) PyElement(com.jetbrains.python.psi.PyElement) Nullable(org.jetbrains.annotations.Nullable)

Example 59 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.

the class PyJoinIfIntention method doInvoke.

public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PyIfStatement expression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
    PyIfStatement ifStatement = getIfStatement(expression);
    PyStatement firstStatement = getFirstStatement(ifStatement);
    if (ifStatement == null)
        return;
    if (firstStatement != null && firstStatement instanceof PyIfStatement) {
        PyExpression condition = ((PyIfStatement) firstStatement).getIfPart().getCondition();
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        PyExpression ifCondition = ifStatement.getIfPart().getCondition();
        if (ifCondition == null || condition == null)
            return;
        StringBuilder replacementText = new StringBuilder(ifCondition.getText() + " and ");
        if (condition instanceof PyBinaryExpression && ((PyBinaryExpression) condition).getOperator() == PyTokenTypes.OR_KEYWORD) {
            replacementText.append("(").append(condition.getText()).append(")");
        } else
            replacementText.append(condition.getText());
        PyExpression newCondition = elementGenerator.createExpressionFromText(replacementText.toString());
        ifCondition.replace(newCondition);
        PyStatementList stList = ((PyIfStatement) firstStatement).getIfPart().getStatementList();
        PyStatementList ifStatementList = ifStatement.getIfPart().getStatementList();
        if (ifStatementList == null || stList == null)
            return;
        List<PsiComment> comments = PsiTreeUtil.getChildrenOfTypeAsList(ifStatement.getIfPart(), PsiComment.class);
        comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(((PyIfStatement) firstStatement).getIfPart(), PsiComment.class));
        comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(ifStatementList, PsiComment.class));
        comments.addAll(PsiTreeUtil.getChildrenOfTypeAsList(stList, PsiComment.class));
        for (PsiElement comm : comments) {
            ifStatement.getIfPart().addBefore(comm, ifStatementList);
            comm.delete();
        }
        ifStatementList.replace(stList);
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement)

Example 60 with PsiComment

use of com.intellij.psi.PsiComment in project intellij-community by JetBrains.

the class DocStringUtil method getAttributeDocComment.

@Nullable
public static String getAttributeDocComment(@NotNull PyTargetExpression attr) {
    if (attr.getParent() instanceof PyAssignmentStatement) {
        final PyAssignmentStatement assignment = (PyAssignmentStatement) attr.getParent();
        final PsiElement prevSibling = PyPsiUtils.getPrevNonWhitespaceSibling(assignment);
        if (prevSibling instanceof PsiComment && prevSibling.getText().startsWith("#:")) {
            return prevSibling.getText().substring(2);
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiComment (com.intellij.psi.PsiComment)68 PsiElement (com.intellij.psi.PsiElement)47 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)21 Nullable (org.jetbrains.annotations.Nullable)19 IElementType (com.intellij.psi.tree.IElementType)11 ArrayList (java.util.ArrayList)11 TextRange (com.intellij.openapi.util.TextRange)9 PsiFile (com.intellij.psi.PsiFile)8 NotNull (org.jetbrains.annotations.NotNull)8 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)7 ASTNode (com.intellij.lang.ASTNode)6 Matcher (java.util.regex.Matcher)6 Document (com.intellij.openapi.editor.Document)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)3 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)3 XmlTag (com.intellij.psi.xml.XmlTag)3 Commenter (com.intellij.lang.Commenter)2 Language (com.intellij.lang.Language)2 Editor (com.intellij.openapi.editor.Editor)2 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)2