Search in sources :

Example 6 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder 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 7 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class AddMethodQuickFix method showTemplateBuilder.

private static void showTemplateBuilder(@NotNull PyFunction method) {
    method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
    final PsiFile file = method.getContainingFile();
    if (file == null)
        return;
    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(method);
    ParamHelper.walkDownParamArray(method.getParameterList().getParameters(), new ParamHelper.ParamVisitor() {

        public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
            builder.replaceElement(param, param.getName());
        }
    });
    final PyStatementList statementList = method.getStatementList();
    builder.replaceElement(statementList, PyNames.PASS);
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null)
        return;
    final Editor editor = FileEditorManager.getInstance(file.getProject()).openTextEditor(new OpenFileDescriptor(file.getProject(), virtualFile), true);
    if (editor == null)
        return;
    builder.run(editor, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ParamHelper(com.jetbrains.python.psi.impl.ParamHelper) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 8 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class GenerateXmlTagAction method generateTag.

public static void generateTag(@NotNull XmlTag newTag, Editor editor) {
    generateRaw(newTag);
    final XmlTag restored = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newTag);
    if (restored == null) {
        LOG.error("Could not restore tag: " + newTag.getText());
    }
    TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(restored);
    replaceElements(restored, builder);
    builder.run(editor, false);
}
Also used : TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder)

Example 9 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class UnresolvedReferenceAddParameterQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    final PyNamedParameter parameter = elementGenerator.createParameter(element.getText() + "=None");
    final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class);
    if (function != null) {
        final PyParameterList parameterList = function.getParameterList();
        parameterList.addParameter(parameter);
        CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parameterList);
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter);
        builder.replaceRange(TextRange.create(parameter.getTextLength() - 4, parameter.getTextLength()), "None");
        final VirtualFile virtualFile = function.getContainingFile().getVirtualFile();
        if (virtualFile == null)
            return;
        final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
        if (editor == null)
            return;
        builder.run(editor, false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFunction(com.jetbrains.python.psi.PyFunction) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) PyParameterList(com.jetbrains.python.psi.PyParameterList) PsiElement(com.intellij.psi.PsiElement)

Example 10 with TemplateBuilder

use of com.intellij.codeInsight.template.TemplateBuilder in project intellij-community by JetBrains.

the class UnresolvedRefCreateFunctionQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PyCallExpression callExpr = myCallExpr.getElement();
    final PyReferenceExpression referenceExpr = myReferenceExpr.getElement();
    if (callExpr == null || !callExpr.isValid() || referenceExpr == null || !referenceExpr.isValid()) {
        return;
    }
    final PyFunctionBuilder functionBuilder = new PyFunctionBuilder(referenceExpr.getText(), callExpr);
    // if function is actually an argument of a call, don't use other arguments of the call to create parameter list of new function
    final PyArgumentList argumentList = callExpr.getArgumentList();
    if (argumentList != null && !PsiTreeUtil.isAncestor(argumentList, referenceExpr, false)) {
        for (PyExpression param : argumentList.getArguments()) {
            if (param instanceof PyKeywordArgument) {
                functionBuilder.parameter(((PyKeywordArgument) param).getKeyword());
            } else if (param instanceof PyReferenceExpression) {
                PyReferenceExpression refex = (PyReferenceExpression) param;
                functionBuilder.parameter(refex.getReferencedName());
            } else {
                functionBuilder.parameter("param");
            }
        }
    } else {
        functionBuilder.parameter("args");
    }
    PyFunction function = functionBuilder.buildFunction(project, LanguageLevel.getDefault());
    final InjectedLanguageManager instance = InjectedLanguageManager.getInstance(project);
    final PsiLanguageInjectionHost host = instance.getInjectionHost(callExpr);
    final PsiElement insertAnchor = host != null ? host : callExpr;
    final PyFunction parentFunction = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyFunction.class);
    if (parentFunction != null) {
        final PyClass parentClass = PsiTreeUtil.getTopmostParentOfType(parentFunction, PyClass.class);
        if (parentClass != null) {
            final PsiElement parent = parentClass.getParent();
            function = (PyFunction) parent.addBefore(function, parentClass);
        } else {
            final PsiElement parent = parentFunction.getParent();
            function = (PyFunction) parent.addBefore(function, parentFunction);
        }
    } else {
        final PyStatement statement = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyStatement.class);
        if (statement != null) {
            final PsiElement parent = statement.getParent();
            if (parent != null) {
                function = (PyFunction) parent.addBefore(function, statement);
            }
        }
    }
    function = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(function);
    ParamHelper.walkDownParamArray(function.getParameterList().getParameters(), new ParamHelper.ParamVisitor() {

        public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
            builder.replaceElement(param, param.getName());
        }
    });
    builder.replaceElement(function.getStatementList(), PyNames.PASS);
    final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(insertAnchor.getContainingFile().getVirtualFile());
    if (!(editor instanceof TextEditor)) {
        return;
    }
    builder.run(((TextEditor) editor).getEditor(), false);
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) ParamHelper(com.jetbrains.python.psi.impl.ParamHelper) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement)

Aggregations

TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)11 Editor (com.intellij.openapi.editor.Editor)5 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 PsiElement (com.intellij.psi.PsiElement)5 ParamHelper (com.jetbrains.python.psi.impl.ParamHelper)3 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 PyClass (com.jetbrains.python.psi.PyClass)2 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PyArgumentList (com.jetbrains.python.psi.PyArgumentList)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyFile (com.jetbrains.python.psi.PyFile)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)1