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);
}
}
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations