Search in sources :

Example 1 with PyArgumentList

use of com.jetbrains.python.psi.PyArgumentList in project intellij-community by JetBrains.

the class PyClassFixer method doApply.

public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyClass pyClass) throws IncorrectOperationException {
    final PsiElement colon = PyPsiUtils.getFirstChildOfType(pyClass, PyTokenTypes.COLON);
    if (colon == null) {
        final PyArgumentList argList = PsiTreeUtil.getChildOfType(pyClass, PyArgumentList.class);
        final int colonOffset = sure(argList).getTextRange().getEndOffset();
        String textToInsert = ":";
        if (pyClass.getNameNode() == null) {
            int newCaretOffset = argList.getTextOffset();
            if (argList.getTextLength() == 0) {
                newCaretOffset += 1;
                textToInsert = " :";
            }
            processor.registerUnresolvedError(newCaretOffset);
        }
        editor.getDocument().insertString(colonOffset, textToInsert);
    }
}
Also used : PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyArgumentList

use of com.jetbrains.python.psi.PyArgumentList in project intellij-community by JetBrains.

the class PyChangeBaseClassQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    assert pyClass != null;
    final PyArgumentList expressionList = pyClass.getSuperClassExpressionList();
    if (expressionList != null && expressionList.getArguments().length != 0) {
        final PyExpression argument = expressionList.getArguments()[0];
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(argument);
        builder.replaceElement(argument, argument.getText());
        final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
        if (virtualFile != null) {
            final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
            assert editor != null;
            builder.run(editor, false);
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyArgumentList(com.jetbrains.python.psi.PyArgumentList) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) PyExpression(com.jetbrains.python.psi.PyExpression) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PyArgumentList

use of com.jetbrains.python.psi.PyArgumentList in project intellij-community by JetBrains.

the class PyRemoveCallQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement call = descriptor.getPsiElement();
    assert call instanceof PyCallExpression;
    if (call instanceof PyDecorator) {
        call.delete();
    } else {
        final PyArgumentList argumentList = ((PyCallExpression) call).getArgumentList();
        assert argumentList != null;
        argumentList.delete();
    }
}
Also used : PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) PyDecorator(com.jetbrains.python.psi.PyDecorator) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyArgumentList

use of com.jetbrains.python.psi.PyArgumentList in project intellij-community by JetBrains.

the class PyDeclarationRangeHandler method getDeclarationRange.

@NotNull
@Override
public TextRange getDeclarationRange(@NotNull PsiElement container) {
    int start = container.getTextRange().getStartOffset();
    if (container instanceof PyFunction) {
        PyParameterList parameterList = ((PyFunction) container).getParameterList();
        return new TextRange(start, parameterList.getTextRange().getEndOffset());
    }
    if (container instanceof PyClass) {
        PyArgumentList argumentList = ((PyClass) container).getSuperClassExpressionList();
        if (argumentList != null) {
            return new TextRange(start, argumentList.getTextRange().getEndOffset());
        }
        ASTNode nameNode = ((PyClass) container).getNameNode();
        if (nameNode != null) {
            return new TextRange(start, nameNode.getStartOffset() + nameNode.getTextLength());
        }
    }
    return container.getTextRange();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PyFunction(com.jetbrains.python.psi.PyFunction) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PyParameterList(com.jetbrains.python.psi.PyParameterList) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PyArgumentList

use of com.jetbrains.python.psi.PyArgumentList in project intellij-community by JetBrains.

the class PyDecoratorTest method testDecoParamCall.

public void testDecoParamCall() throws Exception {
    PsiElement targetElement = find().getParent();
    assertTrue(targetElement instanceof PyDecorator);
    PyDecorator deco = (PyDecorator) targetElement;
    PyFunction decofun = deco.getTarget();
    assertNotNull(decofun);
    assertEquals("foo", decofun.getName());
    assertFalse(deco.isBuiltin());
    assertTrue(deco.hasArgumentList());
    PyArgumentList arglist = deco.getArgumentList();
    assertNotNull(arglist);
    PyExpression[] args = arglist.getArguments();
    assertEquals("argument count", 1, args.length);
    assertEquals("argument value", "1", args[0].getText());
}
Also used : PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PyFunction(com.jetbrains.python.psi.PyFunction) PyExpression(com.jetbrains.python.psi.PyExpression) PyDecorator(com.jetbrains.python.psi.PyDecorator) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PyArgumentList (com.jetbrains.python.psi.PyArgumentList)5 PsiElement (com.intellij.psi.PsiElement)4 PyClass (com.jetbrains.python.psi.PyClass)2 PyDecorator (com.jetbrains.python.psi.PyDecorator)2 PyExpression (com.jetbrains.python.psi.PyExpression)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 ASTNode (com.intellij.lang.ASTNode)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PyCallExpression (com.jetbrains.python.psi.PyCallExpression)1 PyParameterList (com.jetbrains.python.psi.PyParameterList)1 NotNull (org.jetbrains.annotations.NotNull)1