use of com.intellij.codeInsight.template.TemplateManager 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.TemplateManager 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.TemplateManager in project intellij-community by JetBrains.
the class StringBasedPostfixTemplate method expandForChooseExpression.
@Override
public final void expandForChooseExpression(@NotNull PsiElement expr, @NotNull Editor editor) {
Project project = expr.getProject();
Document document = editor.getDocument();
PsiElement elementForRemoving = getElementToRemove(expr);
document.deleteString(elementForRemoving.getTextRange().getStartOffset(), elementForRemoving.getTextRange().getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
String templateString = getTemplateString(expr);
if (templateString == null) {
PostfixTemplatesUtils.showErrorHint(expr.getProject(), editor);
return;
}
Template template = createTemplate(manager, templateString);
if (shouldAddExpressionToContext()) {
template.addVariable("expr", new TextExpression(expr.getText()), false);
}
setVariables(template, expr);
manager.startTemplate(editor, template);
}
use of com.intellij.codeInsight.template.TemplateManager 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.TemplateManager 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);
}
Aggregations