Search in sources :

Example 6 with PsiComment

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

the class AugmentedAssignmentQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyAssignmentStatement && element.isWritable()) {
        final PyAssignmentStatement statement = (PyAssignmentStatement) element;
        final PyExpression target = statement.getLeftHandSideExpression();
        final PyBinaryExpression expression = (PyBinaryExpression) statement.getAssignedValue();
        if (expression == null)
            return;
        PyExpression leftExpression = expression.getLeftExpression();
        PyExpression rightExpression = expression.getRightExpression();
        if (rightExpression instanceof PyParenthesizedExpression)
            rightExpression = ((PyParenthesizedExpression) rightExpression).getContainedExpression();
        if (target != null && rightExpression != null) {
            final String targetText = target.getText();
            final String rightText = rightExpression.getText();
            if (rightText.equals(targetText)) {
                final PyExpression tmp = rightExpression;
                rightExpression = leftExpression;
                leftExpression = tmp;
            }
            final List<PsiComment> comments = PsiTreeUtil.getChildrenOfTypeAsList(statement, PsiComment.class);
            if ((leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) {
                if (leftExpression.getText().equals(targetText)) {
                    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
                    final StringBuilder stringBuilder = new StringBuilder();
                    final PsiElement psiOperator = expression.getPsiOperator();
                    if (psiOperator == null)
                        return;
                    stringBuilder.append(targetText).append(" ").append(psiOperator.getText()).append("= ").append(rightExpression.getText());
                    final PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.forElement(element), PyAugAssignmentStatementImpl.class, stringBuilder.toString());
                    for (PsiComment comment : comments) augAssignment.add(comment);
                    statement.replace(augAssignment);
                }
            }
        }
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PyAugAssignmentStatementImpl(com.jetbrains.python.psi.impl.PyAugAssignmentStatementImpl) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiComment

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

the class AddEncodingQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiFile file = element.getContainingFile();
    if (file == null)
        return;
    PsiElement firstLine = file.getFirstChild();
    if (firstLine instanceof PsiComment && firstLine.getText().startsWith("#!")) {
        firstLine = firstLine.getNextSibling();
    }
    final LanguageLevel languageLevel = LanguageLevel.forElement(file);
    final String commentText = String.format(PyEncodingUtil.ENCODING_FORMAT_PATTERN[myEncodingFormatIndex], myDefaultEncoding);
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PsiComment encodingComment = elementGenerator.createFromText(languageLevel, PsiComment.class, commentText);
    encodingComment = (PsiComment) file.addBefore(encodingComment, firstLine);
    final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(element.getContainingFile().getVirtualFile());
    if (fileEditor instanceof TextEditor) {
        if (encodingComment.getNextSibling() == null || !encodingComment.getNextSibling().textContains('\n')) {
            file.addAfter(elementGenerator.createFromText(languageLevel, PsiWhiteSpace.class, "\n"), encodingComment);
        }
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        final Document document = editor.getDocument();
        final int insertedLineNumber = document.getLineNumber(encodingComment.getTextOffset());
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(insertedLineNumber + 1, 0));
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Document(com.intellij.openapi.editor.Document) PsiComment(com.intellij.psi.PsiComment) TextEditor(com.intellij.openapi.fileEditor.TextEditor) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) PsiFile(com.intellij.psi.PsiFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 8 with PsiComment

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

the class PyFunctionImpl method getTypeCommentAnnotation.

@Nullable
@Override
public String getTypeCommentAnnotation() {
    final PyFunctionStub 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) PyFunctionStub(com.jetbrains.python.psi.stubs.PyFunctionStub) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PsiComment

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

the class PyFunctionImpl method getTypeComment.

@Nullable
@Override
public PsiComment getTypeComment() {
    final PsiComment inlineComment = PyUtil.getCommentOnHeaderLine(this);
    if (inlineComment != null && PyTypingTypeProvider.getTypeCommentValue(inlineComment.getText()) != null) {
        return inlineComment;
    }
    final PyStatementList statements = getStatementList();
    if (statements.getStatements().length != 0) {
        final PsiComment comment = as(statements.getFirstChild(), PsiComment.class);
        if (comment != null && PyTypingTypeProvider.getTypeCommentValue(comment.getText()) != null) {
            return comment;
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PsiComment

use of com.intellij.psi.PsiComment in project kotlin by JetBrains.

the class KotlinDeclarationMover method getElementSourceLineRange.

@Override
protected LineRange getElementSourceLineRange(@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) {
    PsiElement first;
    PsiElement last;
    if (element instanceof KtDeclaration) {
        first = element.getFirstChild();
        last = element.getLastChild();
        if (first == null || last == null)
            return null;
    } else {
        first = last = element;
    }
    TextRange textRange1 = first.getTextRange();
    TextRange textRange2 = last.getTextRange();
    Document doc = editor.getDocument();
    if (doc.getTextLength() < textRange2.getEndOffset())
        return null;
    int startLine = editor.offsetToLogicalPosition(textRange1.getStartOffset()).line;
    int endLine = editor.offsetToLogicalPosition(textRange2.getEndOffset()).line + 1;
    if (element instanceof PsiComment || startLine == oldRange.startLine || startLine == oldRange.endLine || endLine == oldRange.startLine || endLine == oldRange.endLine) {
        return new LineRange(startLine, endLine);
    }
    TextRange lineTextRange = new TextRange(doc.getLineStartOffset(oldRange.startLine), doc.getLineEndOffset(oldRange.endLine));
    if (element instanceof KtDeclaration) {
        for (PsiElement anchor : getDeclarationAnchors((KtDeclaration) element)) {
            TextRange suspectTextRange = anchor.getTextRange();
            if (suspectTextRange != null && lineTextRange.intersects(suspectTextRange))
                return new LineRange(startLine, endLine);
        }
    }
    return null;
}
Also used : PsiComment(com.intellij.psi.PsiComment) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiComment (com.intellij.psi.PsiComment)60 PsiElement (com.intellij.psi.PsiElement)39 Nullable (org.jetbrains.annotations.Nullable)18 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)14 ArrayList (java.util.ArrayList)10 IElementType (com.intellij.psi.tree.IElementType)8 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 Matcher (java.util.regex.Matcher)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 PsiFile (com.intellij.psi.PsiFile)5 ASTNode (com.intellij.lang.ASTNode)4 Document (com.intellij.openapi.editor.Document)4 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)4 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)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