use of com.intellij.codeInsight.template.TemplateBuilderImpl in project intellij-community by JetBrains.
the class PyRenameArgumentQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiNamedElement))
return;
final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
if (virtualFile != null) {
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
final TemplateBuilderImpl builder = new TemplateBuilderImpl(element);
final String name = ((PsiNamedElement) element).getName();
assert name != null;
assert editor != null;
builder.replaceElement(element, TextRange.create(0, name.length()), name);
builder.run(editor, false);
}
}
use of com.intellij.codeInsight.template.TemplateBuilderImpl in project intellij-community by JetBrains.
the class CreateEnumConstantFromUsageFix method invokeImpl.
@Override
protected void invokeImpl(final PsiClass targetClass) {
LOG.assertTrue(targetClass.isEnum());
final String name = myReferenceExpression.getReferenceName();
LOG.assertTrue(name != null);
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(myReferenceExpression.getProject()).getElementFactory();
PsiEnumConstant enumConstant = elementFactory.createEnumConstantFromText(name, null);
enumConstant = (PsiEnumConstant) targetClass.add(enumConstant);
final PsiMethod[] constructors = targetClass.getConstructors();
if (constructors.length > 0) {
final PsiMethod constructor = constructors[0];
final PsiParameter[] parameters = constructor.getParameterList().getParameters();
if (parameters.length > 0) {
final String params = StringUtil.join(parameters, psiParameter -> psiParameter.getName(), ",");
enumConstant = (PsiEnumConstant) enumConstant.replace(elementFactory.createEnumConstantFromText(name + "(" + params + ")", null));
final TemplateBuilderImpl builder = new TemplateBuilderImpl(enumConstant);
final PsiExpressionList argumentList = enumConstant.getArgumentList();
LOG.assertTrue(argumentList != null);
for (PsiExpression expression : argumentList.getExpressions()) {
builder.replaceElement(expression, new EmptyExpression());
}
enumConstant = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(enumConstant);
final Template template = builder.buildTemplate();
final Project project = targetClass.getProject();
final Editor newEditor = positionCursor(project, targetClass.getContainingFile(), enumConstant);
if (newEditor != null) {
final TextRange range = enumConstant.getTextRange();
newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
startTemplate(newEditor, template, project);
}
}
}
}
use of com.intellij.codeInsight.template.TemplateBuilderImpl in project intellij-community by JetBrains.
the class CreateMethodFromUsageFix method doCreate.
public static void doCreate(PsiClass targetClass, PsiMethod method, boolean shouldBeAbstract, List<Pair<PsiExpression, PsiType>> arguments, PsiSubstitutor substitutor, ExpectedTypeInfo[] expectedTypes, @Nullable final PsiElement context) {
method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
if (method == null) {
return;
}
final Project project = targetClass.getProject();
final PsiFile targetFile = targetClass.getContainingFile();
Document document = PsiDocumentManager.getInstance(project).getDocument(targetFile);
if (document == null)
return;
TemplateBuilderImpl builder = new TemplateBuilderImpl(method);
CreateFromUsageUtils.setupMethodParameters(method, builder, context, substitutor, arguments);
final PsiTypeElement returnTypeElement = method.getReturnTypeElement();
if (returnTypeElement != null) {
new GuessTypeParameters(JavaPsiFacade.getInstance(project).getElementFactory()).setupTypeElement(returnTypeElement, expectedTypes, substitutor, builder, context, targetClass);
}
PsiCodeBlock body = method.getBody();
builder.setEndVariableAfter(shouldBeAbstract || body == null ? method : body.getLBrace());
method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
if (method == null)
return;
RangeMarker rangeMarker = document.createRangeMarker(method.getTextRange());
final Editor newEditor = positionCursor(project, targetFile, method);
if (newEditor == null)
return;
Template template = builder.buildTemplate();
newEditor.getCaretModel().moveToOffset(rangeMarker.getStartOffset());
newEditor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
rangeMarker.dispose();
if (!shouldBeAbstract) {
startTemplate(newEditor, template, project, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (brokenOff)
return;
WriteCommandAction.runWriteCommandAction(project, () -> {
PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
final int offset = newEditor.getCaretModel().getOffset();
PsiMethod method1 = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false);
if (method1 != null) {
try {
CreateFromUsageUtils.setupMethodBody(method1);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
CreateFromUsageUtils.setupEditor(method1, newEditor);
}
});
}
});
} else {
startTemplate(newEditor, template, project);
}
}
use of com.intellij.codeInsight.template.TemplateBuilderImpl in project intellij-community by JetBrains.
the class RenameWrongRefFix method invoke.
@Override
public void invoke(@NotNull Project project, final Editor editor, PsiFile file) {
PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(myRefExpr, PsiMember.class, PsiFile.class);
PsiElement element = PsiTreeUtil.getParentOfType(myRefExpr, PsiMember.class, PsiFile.class);
LookupElement[] items = collectItems();
ReferenceNameExpression refExpr = new ReferenceNameExpression(items, myRefExpr.getReferenceName());
TemplateBuilderImpl builder = new TemplateBuilderImpl(element);
for (PsiReferenceExpression expr : refs) {
if (!expr.equals(myRefExpr)) {
builder.replaceElement(expr.getReferenceNameElement(), OTHER_VARIABLE_NAME, INPUT_VARIABLE_NAME, false);
} else {
builder.replaceElement(expr.getReferenceNameElement(), INPUT_VARIABLE_NAME, refExpr, true);
}
}
final float proportion = EditorUtil.calcVerticalScrollProportion(editor);
editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset());
/*for (int i = refs.length - 1; i >= 0; i--) {
TextRange range = refs[i].getReferenceNameElement().getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
}
*/
Template template = builder.buildInlineTemplate();
editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset());
TemplateManager.getInstance(project).startTemplate(editor, template);
EditorUtil.setVerticalScrollProportion(editor, proportion);
}
use of com.intellij.codeInsight.template.TemplateBuilderImpl in project intellij-community by JetBrains.
the class AddMissingRequiredAnnotationParametersFix method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final PsiNameValuePair[] addedParameters = myAnnotation.getParameterList().getAttributes();
final TObjectIntHashMap<String> annotationsOrderMap = getAnnotationsOrderMap();
final SortedSet<Pair<String, PsiAnnotationMemberValue>> newParameters = new TreeSet<>(Comparator.comparingInt(o -> annotationsOrderMap.get(o.getFirst())));
final boolean order = isAlreadyAddedOrdered(annotationsOrderMap, addedParameters);
if (order) {
if (addedParameters.length != 0) {
final PsiAnnotationParameterList parameterList = myAnnotation.getParameterList();
parameterList.deleteChildRange(addedParameters[0], addedParameters[addedParameters.length - 1]);
for (final PsiNameValuePair addedParameter : addedParameters) {
final String name = addedParameter.getName();
final PsiAnnotationMemberValue value = addedParameter.getValue();
if (name == null || value == null) {
LOG.error(String.format("Invalid annotation parameter name = %s, value = %s", name, value));
continue;
}
newParameters.add(Pair.create(name, value));
}
}
}
final PsiExpression nullValue = JavaPsiFacade.getElementFactory(project).createExpressionFromText(PsiKeyword.NULL, null);
for (final String misssedParameter : myMissedElements) {
newParameters.add(Pair.create(misssedParameter, nullValue));
}
TemplateBuilderImpl builder = null;
for (final Pair<String, PsiAnnotationMemberValue> newParameter : newParameters) {
final PsiAnnotationMemberValue value = myAnnotation.setDeclaredAttributeValue(newParameter.getFirst(), newParameter.getSecond());
if (myMissedElements.contains(newParameter.getFirst())) {
if (builder == null) {
builder = new TemplateBuilderImpl(myAnnotation.getParameterList());
}
builder.replaceElement(value, new EmptyExpression(), true);
}
}
editor.getCaretModel().moveToOffset(myAnnotation.getParameterList().getTextRange().getStartOffset());
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(file);
if (document == null) {
throw new IllegalStateException();
}
documentManager.doPostponedOperationsAndUnblockDocument(document);
TemplateManager.getInstance(project).startTemplate(editor, builder.buildInlineTemplate(), null);
}
Aggregations