use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class CreateFieldFix method doFix.
protected void doFix(@NotNull Project project, @NotNull @GrModifier.ModifierConstant String[] modifiers, @NotNull @NonNls String fieldName, @NotNull TypeConstraint[] typeConstraints, @NotNull PsiElement context) throws IncorrectOperationException {
JVMElementFactory factory = JVMElementFactories.getFactory(myTargetClass.getLanguage(), project);
if (factory == null)
return;
PsiField field = factory.createField(fieldName, PsiType.INT);
if (myTargetClass instanceof GroovyScriptClass) {
field.getModifierList().addAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD);
}
for (@GrModifier.ModifierConstant String modifier : modifiers) {
PsiUtil.setModifierProperty(field, modifier, true);
}
field = CreateFieldFromUsageHelper.insertField(myTargetClass, field, context);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(field.getParent());
Editor newEditor = IntentionUtils.positionCursor(project, myTargetClass.getContainingFile(), field);
Template template = CreateFieldFromUsageHelper.setupTemplate(field, typeConstraints, myTargetClass, newEditor, context, false);
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(newEditor, template);
}
use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class AddDtdDeclarationFix method applyFix.
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiFile containingFile = element.getContainingFile();
@NonNls String prefixToInsert = "";
@NonNls String suffixToInsert = "";
final int UNDEFINED_OFFSET = -1;
int anchorOffset = UNDEFINED_OFFSET;
PsiElement anchor = PsiTreeUtil.getParentOfType(element, XmlElementDecl.class, XmlAttlistDecl.class, XmlEntityDecl.class, XmlConditionalSection.class);
if (anchor != null)
anchorOffset = anchor.getTextRange().getStartOffset();
if (anchorOffset == UNDEFINED_OFFSET && containingFile.getLanguage() == XMLLanguage.INSTANCE) {
XmlFile file = (XmlFile) containingFile;
final XmlProlog prolog = file.getDocument().getProlog();
assert prolog != null;
final XmlDoctype doctype = prolog.getDoctype();
final XmlMarkupDecl markupDecl;
if (doctype != null) {
markupDecl = doctype.getMarkupDecl();
} else {
markupDecl = null;
}
if (doctype == null) {
final XmlTag rootTag = file.getDocument().getRootTag();
prefixToInsert = "<!DOCTYPE " + ((rootTag != null) ? rootTag.getName() : "null");
suffixToInsert = ">\n";
}
if (markupDecl == null) {
prefixToInsert += " [\n";
suffixToInsert = "]" + suffixToInsert;
if (doctype != null) {
// just before last '>'
anchorOffset = doctype.getTextRange().getEndOffset() - 1;
} else {
anchorOffset = prolog.getTextRange().getEndOffset();
}
}
}
if (anchorOffset == UNDEFINED_OFFSET)
anchorOffset = element.getTextRange().getStartOffset();
OpenFileDescriptor openDescriptor = new OpenFileDescriptor(project, containingFile.getVirtualFile(), anchorOffset);
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(openDescriptor, true);
final TemplateManager templateManager = TemplateManager.getInstance(project);
final Template t = templateManager.createTemplate("", "");
if (!prefixToInsert.isEmpty())
t.addTextSegment(prefixToInsert);
t.addTextSegment("<!" + myElementDeclarationName + " " + myReference + " ");
t.addEndVariable();
t.addTextSegment(">\n");
if (!suffixToInsert.isEmpty())
t.addTextSegment(suffixToInsert);
templateManager.startTemplate(editor, t);
}
use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class JsonLiveTemplateTest method createJsonTemplate.
@NotNull
private Template createJsonTemplate(@NotNull String name, @NotNull String group, @NotNull String text) {
final TemplateManager templateManager = TemplateManager.getInstance(getProject());
final Template template = templateManager.createTemplate(name, group, text);
final TemplateContextType context = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JsonContextType.class);
assertNotNull(context);
((TemplateImpl) template).getTemplateContext().setEnabled(context, true);
CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable());
return template;
}
use of com.intellij.codeInsight.template.Template in project intellij-community by JetBrains.
the class HighlightUtils method showRenameTemplate.
public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element, PsiReference... references) {
context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
final Project project = context.getProject();
final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
final Editor editor = fileEditorManager.getSelectedTextEditor();
if (editor == null) {
return;
}
final TemplateBuilderImpl builder = new TemplateBuilderImpl(context);
final Expression macroCallNode = new MacroCallNode(new SuggestVariableNameMacro());
final PsiElement identifier = element.getNameIdentifier();
builder.replaceElement(identifier, "PATTERN", macroCallNode, true);
for (PsiReference reference : references) {
builder.replaceElement(reference, "PATTERN", "PATTERN", false);
}
final Template template = builder.buildInlineTemplate();
final TextRange textRange = context.getTextRange();
final int startOffset = textRange.getStartOffset();
editor.getCaretModel().moveToOffset(startOffset);
final TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template);
}
use of com.intellij.codeInsight.template.Template 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);
}
}
}
}
Aggregations