use of com.intellij.codeInsight.template.TemplateEditingAdapter in project intellij-community by JetBrains.
the class TestIntegrationUtils method runTestMethodTemplate.
public static void runTestMethodTemplate(final Editor editor, final PsiClass targetClass, final PsiMethod method, boolean automatic, final Template template) {
final int startOffset = method.getModifierList().getTextRange().getStartOffset();
final TextRange range = new TextRange(startOffset, method.getTextRange().getEndOffset());
editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), "");
editor.getCaretModel().moveToOffset(range.getStartOffset());
final Project project = targetClass.getProject();
TemplateEditingAdapter adapter = null;
if (!automatic) {
adapter = new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiFile psi = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
PsiElement el = PsiTreeUtil.findElementOfClassAtOffset(psi, editor.getCaretModel().getOffset() - 1, PsiMethod.class, false);
if (el != null) {
PsiMethod method1 = PsiTreeUtil.getParentOfType(el, PsiMethod.class, false);
if (method1 != null) {
if (method1.findDeepestSuperMethods().length > 0) {
GenerateMembersUtil.setupGeneratedMethod(method1);
}
CreateFromUsageUtils.setupEditor(method1, editor);
}
}
});
}
};
}
TemplateManager.getInstance(project).startTemplate(editor, template, adapter);
}
use of com.intellij.codeInsight.template.TemplateEditingAdapter in project intellij-community by JetBrains.
the class CreateFieldFromUsageFix method createFieldFromUsageTemplate.
public static void createFieldFromUsageTemplate(final PsiClass targetClass, final Project project, final ExpectedTypeInfo[] expectedTypes, final PsiField field, final boolean createConstantField, final PsiElement context) {
final PsiFile targetFile = targetClass.getContainingFile();
final Editor newEditor = positionCursor(project, targetFile, field);
if (newEditor == null)
return;
Template template = CreateFieldFromUsageHelper.setupTemplate(field, expectedTypes, targetClass, newEditor, context, createConstantField);
startTemplate(newEditor, template, project, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
final int offset = newEditor.getCaretModel().getOffset();
final PsiField psiField = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset, PsiField.class, false);
if (psiField != null) {
ApplicationManager.getApplication().runWriteAction(() -> {
CodeStyleManager.getInstance(project).reformat(psiField);
});
newEditor.getCaretModel().moveToOffset(psiField.getTextRange().getEndOffset() - 1);
}
}
});
}
use of com.intellij.codeInsight.template.TemplateEditingAdapter 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.TemplateEditingAdapter in project intellij-community by JetBrains.
the class GenerateMembersHandlerBase method runTemplates.
private static void runTemplates(final Project myProject, final Editor editor, final List<TemplateGenerationInfo> templates, final int index) {
TemplateGenerationInfo info = templates.get(index);
final Template template = info.getTemplate();
final PsiElement element = info.getPsiMember();
final TextRange range = element.getTextRange();
WriteAction.run(() -> editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset()));
int offset = range.getStartOffset();
editor.getCaretModel().moveToOffset(offset);
editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
TemplateManager.getInstance(myProject).startTemplate(editor, template, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (index + 1 < templates.size()) {
ApplicationManager.getApplication().invokeLater(() -> new WriteCommandAction(myProject) {
@Override
protected void run(@NotNull Result result) throws Throwable {
runTemplates(myProject, editor, templates, index + 1);
}
}.execute());
}
}
});
}
use of com.intellij.codeInsight.template.TemplateEditingAdapter in project intellij-community by JetBrains.
the class CreateSubclassAction method startTemplate.
private static void startTemplate(PsiTypeParameterList oldTypeParameterList, final Project project, final PsiClass psiClass, final PsiClass targetClass, final boolean includeClassName) {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiJavaCodeReferenceElement ref = elementFactory.createClassReferenceElement(psiClass);
try {
if (psiClass.isInterface()) {
ref = (PsiJavaCodeReferenceElement) targetClass.getImplementsList().add(ref);
} else {
ref = (PsiJavaCodeReferenceElement) targetClass.getExtendsList().add(ref);
}
if (psiClass.hasTypeParameters() || includeClassName) {
final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.getContainingFile(), targetClass);
final TemplateBuilderImpl templateBuilder = editor != null ? (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass) : null;
if (includeClassName && templateBuilder != null) {
templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName());
}
if (oldTypeParameterList != null) {
for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) {
final PsiElement param = ref.getParameterList().add(elementFactory.createTypeElement(elementFactory.createType(parameter)));
if (templateBuilder != null) {
templateBuilder.replaceElement(param, param.getText());
}
}
}
replaceTypeParamsList(targetClass, oldTypeParameterList);
if (templateBuilder != null) {
templateBuilder.setEndVariableBefore(ref);
final Template template = templateBuilder.buildTemplate();
template.addEndVariable();
final PsiFile containingFile = targetClass.getContainingFile();
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
final TextRange textRange = targetClass.getTextRange();
final RangeMarker startClassOffset = editor.getDocument().createRangeMarker(textRange.getStartOffset(), textRange.getEndOffset());
startClassOffset.setGreedyToLeft(true);
startClassOffset.setGreedyToRight(true);
editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
CreateFromUsageBaseFix.startTemplate(editor, template, project, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
try {
LOG.assertTrue(startClassOffset.isValid(), startClassOffset);
final PsiElement psiElement = containingFile.findElementAt(startClassOffset.getStartOffset());
final PsiClass aTargetClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class);
LOG.assertTrue(aTargetClass != null, psiElement);
if (!brokenOff) {
TransactionGuard.getInstance().submitTransactionAndWait(() -> chooseAndImplement(psiClass, project, aTargetClass, editor));
}
} finally {
startClassOffset.dispose();
}
}
}, getTitle(psiClass));
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
Aggregations